Class: Sirens::AbstractView

Inherits:
Object
  • Object
show all
Defined in:
lib/views/abstract_view.rb

Overview

A View is the library binding to a GUI interface handle. It is not a Component but is wrapped by a Component. a View takes care of handling the internals of the GUI objects such as handles, events, default initialization, etc.

By separating the View from the Component that wraps it makes the Component responsibilities more consistent with regular Components and it makes it easier to switch between GUI libraries (say, from Gtk to Qt).

Direct Known Subclasses

ComponentView, WidgetView

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAbstractView

Initializes this View handles



39
40
41
42
43
44
# File 'lib/views/abstract_view.rb', line 39

def initialize()
    super()

    @child_views = []
    @attributes = Hash[]
end

Class Method Details

.accepted_stylesObject

Answer the styles accepted by this view.



22
23
24
# File 'lib/views/abstract_view.rb', line 22

def accepted_styles()
    @accepted_styles ||= Set.new(view_accepted_styles)
end

.view_accepted_stylesObject

Answer the styles accepted by this view.



29
30
31
# File 'lib/views/abstract_view.rb', line 29

def view_accepted_styles()
    [ :width, :height, :background_color, :foreground_color ].freeze
end

Instance Method Details

#accepted_stylesObject

Answer the styles accepted by this view.



70
71
72
# File 'lib/views/abstract_view.rb', line 70

def accepted_styles()
    self.class.accepted_styles
end

#add_view(child_view) ⇒ Object

Adds a child_view.



142
143
144
# File 'lib/views/abstract_view.rb', line 142

def add_view(child_view)
    @child_views << child_view
end

#attribute_at(key) ⇒ Object

Accessing



48
49
50
# File 'lib/views/abstract_view.rb', line 48

def attribute_at(key)
    @attributes[key]
end

#background_color=(value) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/views/abstract_view.rb', line 92

def background_color=(value)
    state_colors_from(value).each_pair do |state, value|
        next if value.nil?

        main_handle.override_background_color( state, Gdk::RGBA.parse(value) )
    end
end

#foreground_color=(value, state: :normal) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/views/abstract_view.rb', line 100

def foreground_color=(value, state: :normal)
    state_colors_from(value).each_pair do |state, value|
        next if value.nil?

        main_handle.override_color( state, Gdk::RGBA.parse(value) )
    end
end

#heightObject



88
89
90
# File 'lib/views/abstract_view.rb', line 88

def height()
    main_handle.height_request
end

#height=(value) ⇒ Object



84
85
86
# File 'lib/views/abstract_view.rb', line 84

def height=(value)
    main_handle.height_request = value
end

#main_handleObject

Returns the main handle of this View. The main handle is the one that this View parent add as its child. Also, it is the handle that receives the style props and events by default.

Raises:

  • (RuntimeError)


61
62
63
# File 'lib/views/abstract_view.rb', line 61

def main_handle()
    raise RuntimeError.new("Subclass #{self.class.name} must implement the method ::initialize_handles().")
end

#populate_popup_menu_block=(populate_popup_menu_block) ⇒ Object

Popup menu



157
158
159
# File 'lib/views/abstract_view.rb', line 157

def populate_popup_menu_block=(populate_popup_menu_block)
    @populate_popup_menu_block = populate_popup_menu_block
end

#remove_view(child_view) ⇒ Object

Removes a child view.



149
150
151
152
153
# File 'lib/views/abstract_view.rb', line 149

def remove_view(child_view)
    @child_views.delete(child_view)

    main_handle.remove(child_view.main_handle)
end

#set_attribute(key, value) ⇒ Object



52
53
54
# File 'lib/views/abstract_view.rb', line 52

def set_attribute(key, value)
    @attributes[key] = value
end

#showObject

Makes this component visible.



133
134
135
# File 'lib/views/abstract_view.rb', line 133

def show()
    main_handle.show_all
end

#show_popup_menu(props) ⇒ Object

Create and show a popup menu



164
165
166
167
168
169
170
# File 'lib/views/abstract_view.rb', line 164

def show_popup_menu(props)
    menu = MenuView.new

    @populate_popup_menu_block.call(menu: menu)

    menu.open(props) unless menu.empty?
end

#state_colors_from(value) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/views/abstract_view.rb', line 108

def state_colors_from(value)
    colors = Hash[
        normal: nil,
        active: nil,
        prelight: nil,
        selected: nil,
        insensitive: nil,
    ]

    if value.kind_of?(Hash)
        value.each_pair do |state, value|
            colors[state] = value
        end
    else
        colors[:normal] = value
    end

    colors
end

#widthObject



80
81
82
# File 'lib/views/abstract_view.rb', line 80

def width()
    main_handle.width_request
end

#width=(value) ⇒ Object

Styles



76
77
78
# File 'lib/views/abstract_view.rb', line 76

def width=(value)
    main_handle.width_request = value
end