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
AreaProxy, ButtonColumnProxy, ButtonProxy, CheckboxColumnProxy, CheckboxProxy, CheckboxTextColumnProxy, ColorButtonProxy, ComboboxProxy, DateTimePickerProxy, EditableComboboxProxy, FontButtonProxy, FormProxy, GridProxy, GroupProxy, HorizontalBoxProxy, ImageColumnProxy, ImagePartProxy, ImageProxy, ImageTextColumnProxy, LabelProxy, MenuItemProxy, MenuProxy, MultilineEntryProxy, PathProxy, ProgressBarColumnProxy, RadioButtonsProxy, TabItemProxy, TableProxy, TextColumnProxy, 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.
-
#block ⇒ 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
- .boolean_to_integer(bool, allow_nil: true) ⇒ Object
-
.control_proxies ⇒ Object
autosave all controls in this array to avoid garbage collection.
- .create(keyword, parent, args, &block) ⇒ Object
- .exists?(keyword) ⇒ Boolean
- .image_proxies ⇒ Object
- .integer_to_boolean(int, allow_nil: true) ⇒ 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
- #content(&block) ⇒ Object
- #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.
97 98 99 100 101 102 103 104 105 |
# File 'lib/glimmer/libui/control_proxy.rb', line 97 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
158 159 160 161 162 163 164 165 166 167 |
# File 'lib/glimmer/libui/control_proxy.rb', line 158 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
95 96 97 |
# File 'lib/glimmer/libui/control_proxy.rb', line 95 def args @args end |
#block ⇒ Object (readonly)
libui returns the contained LibUI object
95 96 97 |
# File 'lib/glimmer/libui/control_proxy.rb', line 95 def block @block end |
#keyword ⇒ Object (readonly)
libui returns the contained LibUI object
95 96 97 |
# File 'lib/glimmer/libui/control_proxy.rb', line 95 def keyword @keyword end |
#libui ⇒ Object (readonly)
libui returns the contained LibUI object
95 96 97 |
# File 'lib/glimmer/libui/control_proxy.rb', line 95 def libui @libui end |
#parent_proxy ⇒ Object (readonly)
libui returns the contained LibUI object
95 96 97 |
# File 'lib/glimmer/libui/control_proxy.rb', line 95 def parent_proxy @parent_proxy end |
Class Method Details
.boolean_to_integer(bool, allow_nil: true) ⇒ Object
62 63 64 |
# File 'lib/glimmer/libui/control_proxy.rb', line 62 def boolean_to_integer(bool, allow_nil: true) bool.nil? ? (allow_nil ? nil : 0) : (bool ? 1 : 0) end |
.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 control_proxies @@control_proxies = [] unless defined?(@@control_proxies) @@control_proxies 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| control_proxies << c} end |
.exists?(keyword) ⇒ Boolean
29 30 31 32 33 |
# File 'lib/glimmer/libui/control_proxy.rb', line 29 def exists?(keyword) ::LibUI.respond_to?("new_#{keyword}") || ::LibUI.respond_to?(keyword) || Glimmer::LibUI.constants.include?("#{keyword.camelcase(:upper)}Proxy".to_sym) end |
.image_proxies ⇒ Object
70 71 72 |
# File 'lib/glimmer/libui/control_proxy.rb', line 70 def image_proxies control_proxies.select {|c| c.keyword == 'image' } end |
.integer_to_boolean(int, allow_nil: true) ⇒ Object
58 59 60 |
# File 'lib/glimmer/libui/control_proxy.rb', line 58 def integer_to_boolean(int, allow_nil: true) int.nil? ? (allow_nil ? nil : false) : int == 1 end |
.main_window_proxy ⇒ Object
54 55 56 |
# File 'lib/glimmer/libui/control_proxy.rb', line 54 def main_window_proxy 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 control_proxies.select {|c| c.keyword == 'menu' } end |
.new_control(keyword, args) ⇒ Object
74 75 76 |
# File 'lib/glimmer/libui/control_proxy.rb', line 74 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
193 194 195 |
# File 'lib/glimmer/libui/control_proxy.rb', line 193 def append_properties @parent_proxy&.class&.constants&.include?(:APPEND_PROPERTIES) ? @parent_proxy.class::APPEND_PROPERTIES : [] end |
#append_property(property, value = nil) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/glimmer/libui/control_proxy.rb', line 197 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
125 126 127 128 |
# File 'lib/glimmer/libui/control_proxy.rb', line 125 def can_handle_listener?(listener_name) ::LibUI.respond_to?("#{libui_api_keyword}_#{listener_name}") || ::LibUI.respond_to?("control_#{listener_name}") end |
#content(&block) ⇒ Object
261 262 263 |
# File 'lib/glimmer/libui/control_proxy.rb', line 261 def content(&block) Glimmer::DSL::Engine.add_content(self, Glimmer::DSL::Libui::ControlExpression.new, @keyword, &block) end |
#default_destroy ⇒ Object
225 226 227 228 |
# File 'lib/glimmer/libui/control_proxy.rb', line 225 def default_destroy send_to_libui('destroy') ControlProxy.control_proxies.delete(self) end |
#destroy ⇒ Object
213 214 215 216 217 218 219 |
# File 'lib/glimmer/libui/control_proxy.rb', line 213 def destroy if parent_proxy.nil? default_destroy else parent_proxy.destroy_child(self) end end |
#destroy_child(child) ⇒ Object
221 222 223 |
# File 'lib/glimmer/libui/control_proxy.rb', line 221 def destroy_child(child) child.default_destroy end |
#enabled(value = nil) ⇒ Object Also known as: enabled?, set_enabled, enabled=
230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/glimmer/libui/control_proxy.rb', line 230 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
130 131 132 133 134 135 136 137 |
# File 'lib/glimmer/libui/control_proxy.rb', line 130 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
209 210 211 |
# File 'lib/glimmer/libui/control_proxy.rb', line 209 def libui_api_keyword @keyword end |
#post_add_content ⇒ Object
Subclasses may override to perform post add_content work (normally must call super)
108 109 110 |
# File 'lib/glimmer/libui/control_proxy.rb', line 108 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
113 114 115 |
# File 'lib/glimmer/libui/control_proxy.rb', line 113 def post_initialize_child(child) # No Op by default end |
#respond_to?(method_name, *args, &block) ⇒ Boolean
139 140 141 142 143 144 145 146 147 |
# File 'lib/glimmer/libui/control_proxy.rb', line 139 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
149 150 151 152 153 154 155 156 |
# File 'lib/glimmer/libui/control_proxy.rb', line 149 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
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/glimmer/libui/control_proxy.rb', line 169 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=
245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/glimmer/libui/control_proxy.rb', line 245 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
117 118 119 120 121 122 123 |
# File 'lib/glimmer/libui/control_proxy.rb', line 117 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 |