class Sirens::PrimitiveComponent

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.

Public Class Methods

new(props = Hash[]) click to toggle source

Initializes this component

Calls superclass method Sirens::AbstractComponent::new
# File lib/components/primitive_component.rb, line 14
def initialize(props = Hash[])
    super(props)

    apply_props

    sync_ui_from_model
end

Public Instance Methods

apply_props() click to toggle source

Applies the props to the view.

# 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) click to toggle source

Adds the child_component to this component.

# 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:) click to toggle source

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.

# 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) click to toggle source

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.

# File lib/components/primitive_component.rb, line 81
def on_value_changed(announcement)
    sync_ui_from_model
end
populate_popup_menu(menu:) click to toggle source

Populate the popup menu.

# 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) click to toggle source

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

Calls superclass method Sirens::AbstractComponent#set_props
# File lib/components/primitive_component.rb, line 39
def set_props(props)
    super(props)

    apply_props
end
subscribe_to_model_events() click to toggle source

Subscribes this component to the model events

# 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_model() click to toggle source

Synchronizes the view to the models current state.

# File lib/components/primitive_component.rb, line 99
def sync_ui_from_model()
end