Class: Sirens::PrimitiveComponent

Inherits:
AbstractComponent show all
Defined in:
lib/components/primitive_component.rb

Overview

PrimitiveComponent is a component wrapping a PrimitiveView. A PrimitiveView implements the actual GUI binding to a Widget (a Gtk widget, for instance). Besides acting as a regular Component, PrimitiveComponent also takes care of handling the PrimitiveView.

Instance Method Summary collapse

Methods inherited from AbstractComponent

#add_all_components, #add_component, #child_components, #create_view, #default_model, #model, open, #props, #remove_component_at, #remove_last_component, #set_model, #view

Constructor Details

#initialize(props = Hash[]) ⇒ PrimitiveComponent

Initializes this component



14
15
16
17
18
19
20
# File 'lib/components/primitive_component.rb', line 14

def initialize(props = Hash[])
    super(props)

    apply_props

    sync_ui_from_model
end

Instance Method Details

#apply_propsObject

Applies the props to the view.



25
26
27
28
29
30
31
# File 'lib/components/primitive_component.rb', line 25

def apply_props()
    @view.apply_props(props)

    @view.populate_popup_menu_block = proc { |menu:| populate_popup_menu(menu: menu) }

    set_model( props.key?(:model) ? props[:model] : default_model )
end

#on_component_added(child_component) ⇒ Object

Adds the child_component to this component.



50
51
52
# File 'lib/components/primitive_component.rb', line 50

def on_component_added(child_component)
    @view.add_view(child_component.view)
end

#on_model_changed(old_model:, new_model:) ⇒ Object

Method called when this component model changes. Unsubscribes this component from the current model events, subscribes this component to the new model events and syncs the widget with the new model.



61
62
63
64
65
66
67
# File 'lib/components/primitive_component.rb', line 61

def on_model_changed(old_model:, new_model:)
    old_model.delete_observer(self) if old_model.respond_to?(:delete_observer)

    subscribe_to_model_events

    sync_ui_from_model
end

#on_value_changed(announcement) ⇒ Object

Hook method called when the model value changes. Note that the model remains the same, what changed is the value the model holds. Subclasses may use this method to update other aspects of its model or perform actions.



81
82
83
# File 'lib/components/primitive_component.rb', line 81

def on_value_changed(announcement)
    sync_ui_from_model
end

#populate_popup_menu(menu:) ⇒ Object

Populate the popup menu.



88
89
90
91
92
# File 'lib/components/primitive_component.rb', line 88

def populate_popup_menu(menu:)
    return if props[:popup_menu].nil?

    props[:popup_menu].call(menu: menu, menu_owner: self)
end

#set_props(props) ⇒ Object

Applies the given props to the current component.props. The component.props that are not included in given props remain untouched.



39
40
41
42
43
# File 'lib/components/primitive_component.rb', line 39

def set_props(props)
    super(props)

    apply_props
end

#subscribe_to_model_eventsObject

Subscribes this component to the model events



72
73
74
# File 'lib/components/primitive_component.rb', line 72

def subscribe_to_model_events()
    model.add_observer(self, :on_value_changed) if model.respond_to?(:add_observer)
end

#sync_ui_from_modelObject

Synchronizes the view to the models current state.



99
100
# File 'lib/components/primitive_component.rb', line 99

def sync_ui_from_model()
end