Class: Glimmer::LibUI::ControlProxy
- Inherits:
-
Object
- Object
- Glimmer::LibUI::ControlProxy
- Defined in:
- lib/glimmer/libui/control_proxy.rb
Overview
Proxy for LibUI control objects
Follows the Proxy Design Pattern
Direct Known Subclasses
ButtonProxy, CheckboxProxy, ColorButtonProxy, ComboboxProxy, DateTimePickerProxy, EditableComboboxProxy, FontButtonProxy, FormProxy, GridProxy, GroupProxy, HorizontalBoxProxy, LabelProxy, MenuItemProxy, MenuProxy, MultilineEntryProxy, RadioButtonsProxy, TabItemProxy, VerticalBoxProxy, WindowProxy
Constant Summary collapse
- BOOLEAN_PROPERTIES =
%w[ padded checked enabled toplevel visible read_only margined borderless fullscreen stretchy ]- STRING_PROPERTIES =
%w[ text title ]
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
libui returns the contained LibUI object.
-
#keyword ⇒ Object
readonly
libui returns the contained LibUI object.
-
#libui ⇒ Object
readonly
libui returns the contained LibUI object.
-
#parent_proxy ⇒ Object
readonly
libui returns the contained LibUI object.
Class Method Summary collapse
-
.all_control_proxies ⇒ Object
autosave all controls in this array to avoid garbage collection.
- .boolean_to_integer(bool) ⇒ Object
- .control_exists?(keyword) ⇒ Boolean
- .create(keyword, parent, args, &block) ⇒ Object
- .integer_to_boolean(int) ⇒ Object
- .main_window_proxy ⇒ Object
- .menu_proxies ⇒ Object
- .new_control(keyword, args) ⇒ Object
- .widget_proxy_class(keyword) ⇒ Object
Instance Method Summary collapse
- #append_properties ⇒ Object
- #append_property(property, value = nil) ⇒ Object
- #can_handle_listener?(listener_name) ⇒ Boolean
- #default_destroy ⇒ Object
- #destroy ⇒ Object
- #destroy_child(child) ⇒ Object
- #enabled(value = nil) ⇒ Object (also: #enabled?, #set_enabled, #enabled=)
- #handle_listener(listener_name, &listener) ⇒ Object
-
#initialize(keyword, parent, args, &block) ⇒ ControlProxy
constructor
A new instance of ControlProxy.
- #libui_api_keyword ⇒ Object
- #method_missing(method_name, *args, &block) ⇒ Object
-
#post_add_content ⇒ Object
Subclasses may override to perform post add_content work (normally must call super).
-
#post_initialize_child(child) ⇒ Object
Subclasses may override to perform post initialization work on an added child.
- #respond_to?(method_name, *args, &block) ⇒ Boolean
- #respond_to_libui?(method_name, *args, &block) ⇒ Boolean
- #send_to_libui(method_name, *args, &block) ⇒ Object
- #visible(value = nil) ⇒ Object (also: #visible?, #set_visible, #visible=)
- #window_proxy ⇒ Object
Constructor Details
#initialize(keyword, parent, args, &block) ⇒ ControlProxy
Returns a new instance of ControlProxy.
93 94 95 96 97 98 99 100 101 |
# File 'lib/glimmer/libui/control_proxy.rb', line 93 def initialize(keyword, parent, args, &block) @keyword = keyword @parent_proxy = parent @args = args @block = block @enabled = true build_control post_add_content if @block.nil? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
154 155 156 157 158 159 160 161 162 163 |
# File 'lib/glimmer/libui/control_proxy.rb', line 154 def method_missing(method_name, *args, &block) if respond_to_libui?(method_name, *args, &block) send_to_libui(method_name, *args, &block) elsif append_properties.include?(method_name.to_s) || append_properties.include?(method_name.to_s.sub(/(=|\?)$/, '')) append_property(method_name, *args) else super end end |
Instance Attribute Details
#args ⇒ Object (readonly)
libui returns the contained LibUI object
91 92 93 |
# File 'lib/glimmer/libui/control_proxy.rb', line 91 def args @args end |
#keyword ⇒ Object (readonly)
libui returns the contained LibUI object
91 92 93 |
# File 'lib/glimmer/libui/control_proxy.rb', line 91 def keyword @keyword end |
#libui ⇒ Object (readonly)
libui returns the contained LibUI object
91 92 93 |
# File 'lib/glimmer/libui/control_proxy.rb', line 91 def libui @libui end |
#parent_proxy ⇒ Object (readonly)
libui returns the contained LibUI object
91 92 93 |
# File 'lib/glimmer/libui/control_proxy.rb', line 91 def parent_proxy @parent_proxy end |
Class Method Details
.all_control_proxies ⇒ Object
autosave all controls in this array to avoid garbage collection
49 50 51 52 |
# File 'lib/glimmer/libui/control_proxy.rb', line 49 def all_control_proxies @@all_control_proxies = [] unless defined?(@@all_control_proxies) @@all_control_proxies end |
.boolean_to_integer(bool) ⇒ Object
62 63 64 |
# File 'lib/glimmer/libui/control_proxy.rb', line 62 def boolean_to_integer(bool) bool.nil? ? nil : (bool ? 1 : 0) end |
.control_exists?(keyword) ⇒ Boolean
29 30 31 32 33 |
# File 'lib/glimmer/libui/control_proxy.rb', line 29 def control_exists?(keyword) ::LibUI.respond_to?("new_#{keyword}") || ::LibUI.respond_to?(keyword) || Glimmer::LibUI.constants.include?("#{keyword.camelcase(:upper)}Proxy".to_sym) end |
.create(keyword, parent, args, &block) ⇒ Object
35 36 37 |
# File 'lib/glimmer/libui/control_proxy.rb', line 35 def create(keyword, parent, args, &block) (keyword).new(keyword, parent, args, &block).tap {|c| all_control_proxies << c} end |
.integer_to_boolean(int) ⇒ Object
58 59 60 |
# File 'lib/glimmer/libui/control_proxy.rb', line 58 def integer_to_boolean(int) int.nil? ? nil : int == 1 end |
.main_window_proxy ⇒ Object
54 55 56 |
# File 'lib/glimmer/libui/control_proxy.rb', line 54 def main_window_proxy all_control_proxies.find {|c| c.is_a?(Glimmer::LibUI::WindowProxy)} end |
.menu_proxies ⇒ Object
66 67 68 |
# File 'lib/glimmer/libui/control_proxy.rb', line 66 def all_control_proxies.select {|c| c.keyword == 'menu' } end |
.new_control(keyword, args) ⇒ Object
70 71 72 |
# File 'lib/glimmer/libui/control_proxy.rb', line 70 def new_control(keyword, args) ::LibUI.send("new_#{keyword}", *args) end |
.widget_proxy_class(keyword) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/glimmer/libui/control_proxy.rb', line 39 def (keyword) begin class_name = "#{keyword.camelcase(:upper)}Proxy".to_sym Glimmer::LibUI.const_get(class_name) rescue Glimmer::LibUI::ControlProxy end end |
Instance Method Details
#append_properties ⇒ Object
189 190 191 |
# File 'lib/glimmer/libui/control_proxy.rb', line 189 def append_properties @parent_proxy&.class&.constants&.include?(:APPEND_PROPERTIES) ? @parent_proxy.class::APPEND_PROPERTIES : [] end |
#append_property(property, value = nil) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/glimmer/libui/control_proxy.rb', line 193 def append_property(property, value = nil) property = property.to_s.sub(/(=|\?)$/, '') @append_property_hash ||= {} if value.nil? value = @append_property_hash[property] handle_string_property(property, handle_boolean_property(property, value)) else value = ControlProxy.boolean_to_integer(value) if BOOLEAN_PROPERTIES.include?(property) && (value.is_a?(TrueClass) || value.is_a?(FalseClass)) @append_property_hash[property] = value end end |
#can_handle_listener?(listener_name) ⇒ Boolean
121 122 123 124 |
# File 'lib/glimmer/libui/control_proxy.rb', line 121 def can_handle_listener?(listener_name) ::LibUI.respond_to?("#{libui_api_keyword}_#{listener_name}") || ::LibUI.respond_to?("control_#{listener_name}") end |
#default_destroy ⇒ Object
221 222 223 224 |
# File 'lib/glimmer/libui/control_proxy.rb', line 221 def default_destroy send_to_libui('destroy') ControlProxy.all_control_proxies.delete(self) end |
#destroy ⇒ Object
209 210 211 212 213 214 215 |
# File 'lib/glimmer/libui/control_proxy.rb', line 209 def destroy if parent_proxy.nil? default_destroy else parent_proxy.destroy_child(self) end end |
#destroy_child(child) ⇒ Object
217 218 219 |
# File 'lib/glimmer/libui/control_proxy.rb', line 217 def destroy_child(child) child.default_destroy end |
#enabled(value = nil) ⇒ Object Also known as: enabled?, set_enabled, enabled=
226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/glimmer/libui/control_proxy.rb', line 226 def enabled(value = nil) if value.nil? @enabled elsif value != @enabled if value == 1 || value send_to_libui('enable') else send_to_libui('disable') end end end |
#handle_listener(listener_name, &listener) ⇒ Object
126 127 128 129 130 131 132 133 |
# File 'lib/glimmer/libui/control_proxy.rb', line 126 def handle_listener(listener_name, &listener) safe_listener = Proc.new { listener.call(self) } if ::LibUI.respond_to?("#{libui_api_keyword}_#{listener_name}") ::LibUI.send("#{libui_api_keyword}_#{listener_name}", @libui, &safe_listener) elsif ::LibUI.respond_to?("control_#{listener_name}") ::LibUI.send("control_#{listener_name}", @libui, &safe_listener) end end |
#libui_api_keyword ⇒ Object
205 206 207 |
# File 'lib/glimmer/libui/control_proxy.rb', line 205 def libui_api_keyword @keyword end |
#post_add_content ⇒ Object
Subclasses may override to perform post add_content work (normally must call super)
104 105 106 |
# File 'lib/glimmer/libui/control_proxy.rb', line 104 def post_add_content @parent_proxy&.post_initialize_child(self) end |
#post_initialize_child(child) ⇒ Object
Subclasses may override to perform post initialization work on an added child
109 110 111 |
# File 'lib/glimmer/libui/control_proxy.rb', line 109 def post_initialize_child(child) # No Op by default end |
#respond_to?(method_name, *args, &block) ⇒ Boolean
135 136 137 138 139 140 141 142 143 |
# File 'lib/glimmer/libui/control_proxy.rb', line 135 def respond_to?(method_name, *args, &block) respond_to_libui?(method_name, *args, &block) || ( append_properties.include?(method_name.to_s) || (append_properties.include?(method_name.to_s.sub(/\?$/, '')) && BOOLEAN_PROPERTIES.include?(method_name.to_s.sub(/\?$/, ''))) || append_properties.include?(method_name.to_s.sub(/=$/, '')) ) || super(method_name, true) end |
#respond_to_libui?(method_name, *args, &block) ⇒ Boolean
145 146 147 148 149 150 151 152 |
# File 'lib/glimmer/libui/control_proxy.rb', line 145 def respond_to_libui?(method_name, *args, &block) ::LibUI.respond_to?("control_#{method_name}") || (::LibUI.respond_to?("control_#{method_name.to_s.sub(/\?$/, '')}") && BOOLEAN_PROPERTIES.include?(method_name.to_s.sub(/\?$/, '')) ) || ::LibUI.respond_to?("control_set_#{method_name.to_s.sub(/=$/, '')}") || ::LibUI.respond_to?("#{libui_api_keyword}_#{method_name}") || (::LibUI.respond_to?("#{libui_api_keyword}_#{method_name.to_s.sub(/\?$/, '')}") && BOOLEAN_PROPERTIES.include?(method_name.to_s.sub(/\?$/, '')) ) || ::LibUI.respond_to?("#{libui_api_keyword}_set_#{method_name.to_s.sub(/=$/, '')}") end |
#send_to_libui(method_name, *args, &block) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/glimmer/libui/control_proxy.rb', line 165 def send_to_libui(method_name, *args, &block) if ::LibUI.respond_to?("#{libui_api_keyword}_#{method_name.to_s.sub(/\?$/, '')}") && args.empty? property = method_name.to_s.sub(/\?$/, '') value = ::LibUI.send("#{libui_api_keyword}_#{property}", @libui, *args) handle_string_property(property, handle_boolean_property(property, value)) elsif ::LibUI.respond_to?("#{libui_api_keyword}_set_#{method_name.to_s.sub(/=$/, '')}") && !args.empty? property = method_name.to_s.sub(/=$/, '') args[0] = ControlProxy.boolean_to_integer(args.first) if BOOLEAN_PROPERTIES.include?(property) && (args.first.is_a?(TrueClass) || args.first.is_a?(FalseClass)) ::LibUI.send("#{libui_api_keyword}_set_#{property}", @libui, *args) elsif ::LibUI.respond_to?("#{libui_api_keyword}_#{method_name}") && !args.empty? ::LibUI.send("#{libui_api_keyword}_#{method_name}", @libui, *args) elsif ::LibUI.respond_to?("control_#{method_name.to_s.sub(/\?$/, '')}") && args.empty? property = method_name.to_s.sub(/\?$/, '') value = ::LibUI.send("control_#{property}", @libui, *args) handle_string_property(property, handle_boolean_property(property, value)) elsif ::LibUI.respond_to?("control_set_#{method_name.to_s.sub(/=$/, '')}") property = method_name.to_s.sub(/=$/, '') args[0] = ControlProxy.boolean_to_integer(args.first) if BOOLEAN_PROPERTIES.include?(property) && (args.first.is_a?(TrueClass) || args.first.is_a?(FalseClass)) ::LibUI.send("control_set_#{method_name.to_s.sub(/=$/, '')}", @libui, *args) elsif ::LibUI.respond_to?("control_#{method_name}") && !args.empty? ::LibUI.send("control_#{method_name}", @libui, *args) end end |
#visible(value = nil) ⇒ Object Also known as: visible?, set_visible, visible=
241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/glimmer/libui/control_proxy.rb', line 241 def visible(value = nil) current_value = send_to_libui('visible') if value.nil? current_value elsif value != current_value if value == 1 || value send_to_libui('show') else send_to_libui('hide') end end end |
#window_proxy ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/glimmer/libui/control_proxy.rb', line 113 def window_proxy found_proxy = self until found_proxy.nil? || found_proxy.is_a?(WindowProxy) found_proxy = found_proxy.parent_proxy end found_proxy end |