Module: Glimmer::UI::CustomWidget
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#add_observer(observer, attribute_name) ⇒ Object
-
#async_exec(&block) ⇒ Object
TODO see if it is worth it to eliminate duplication of async_exec/sync_exec delegation to DisplayProxy, via a module.
-
#attribute_setter(attribute_name) ⇒ Object
-
#can_add_observer?(attribute_name) ⇒ Boolean
-
#can_handle_observation_request?(observation_request) ⇒ Boolean
-
#children_owner ⇒ Object
-
#content(&block) ⇒ Object
Returns content block if used as an attribute reader (no args) Otherwise, if a block is passed, it adds it as content to this custom widget.
-
#disposed? ⇒ Boolean
-
#get_attribute(attribute_name) ⇒ Object
-
#handle_observation_request(observation_request, &block) ⇒ Object
-
#has_attribute?(attribute_name, *args) ⇒ Boolean
-
#has_instance_method?(method_name) ⇒ Boolean
This method ensures it has an instance method not coming from Glimmer DSL.
-
#has_style?(style) ⇒ Boolean
-
#initialize(parent, *swt_style_symbols, options, &content) ⇒ Object
-
#local_respond_to? ⇒ Object
-
#method_missing(method, *args, &block) ⇒ Object
-
#observer_registrations ⇒ Object
-
#post_add_content ⇒ Object
-
#post_initialize_child(child) ⇒ Object
Calls post_initialize_child on children_owner by default Subclasses may override to perform post initialization work on an added child.
-
#respond_to?(method, *args, &block) ⇒ Boolean
-
#set_attribute(attribute_name, *args) ⇒ Object
-
#shell_proxy ⇒ Object
-
#swt_style ⇒ Object
returns calculated SWT style integer value from swt_style_symbols keep in mind that it would not work when using glimmer-only custom swt_style_symbols like :editable for table In that case, just reference swt_style_symbols array instead.
-
#swt_widget ⇒ Object
-
#sync_exec(&block) ⇒ Object
-
#timer_exec(delay_in_millis, &block) ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
347
348
349
350
351
352
353
354
355
356
357
|
# File 'lib/glimmer/ui/custom_widget.rb', line 347
def method_missing(method, *args, &block)
if block && can_handle_observation_request?(method)
handle_observation_request(method, &block)
elsif children_owner != @body_root && children_owner.respond_to?(method, *args, &block)
children_owner.send(method, *args, &block)
else
body_root.send(method, *args, &block)
end
end
|
Instance Attribute Details
#body_root ⇒ Object
Returns the value of attribute body_root.
164
165
166
|
# File 'lib/glimmer/ui/custom_widget.rb', line 164
def body_root
@body_root
end
|
#options ⇒ Object
Returns the value of attribute options.
164
165
166
|
# File 'lib/glimmer/ui/custom_widget.rb', line 164
def options
@options
end
|
#parent ⇒ Object
Returns the value of attribute parent.
164
165
166
|
# File 'lib/glimmer/ui/custom_widget.rb', line 164
def parent
@parent
end
|
#parent_proxy ⇒ Object
Returns the value of attribute parent_proxy.
164
165
166
|
# File 'lib/glimmer/ui/custom_widget.rb', line 164
def parent_proxy
@parent_proxy
end
|
#swt_style_symbols ⇒ Object
Returns the value of attribute swt_style_symbols.
164
165
166
|
# File 'lib/glimmer/ui/custom_widget.rb', line 164
def swt_style_symbols
@swt_style_symbols
end
|
Class Method Details
92
93
94
95
96
|
# File 'lib/glimmer/ui/custom_widget.rb', line 92
def add_custom_widget_namespaces_for(klass)
Glimmer::UI::CustomWidget.namespaces_for_class(klass).drop(1).each do |namespace|
Glimmer::UI::CustomWidget.custom_widget_namespaces << namespace
end
end
|
.after_body(&block) ⇒ Object
159
160
161
|
# File 'lib/glimmer/ui/custom_widget.rb', line 159
def after_body(&block)
@after_body_block = block
end
|
.before_body(&block) ⇒ Object
151
152
153
|
# File 'lib/glimmer/ui/custom_widget.rb', line 151
def before_body(&block)
@before_body_block = block
end
|
.body(&block) ⇒ Object
155
156
157
|
# File 'lib/glimmer/ui/custom_widget.rb', line 155
def body(&block)
@body_block = block
end
|
106
107
108
|
# File 'lib/glimmer/ui/custom_widget.rb', line 106
def custom_widget_namespaces
@custom_widget_namespaces ||= reset_custom_widget_namespaces
end
|
.def_option_attr_accessors(new_options) ⇒ Object
138
139
140
141
142
143
144
145
146
147
148
149
|
# File 'lib/glimmer/ui/custom_widget.rb', line 138
def def_option_attr_accessors(new_options)
new_options.each do |option, default|
class_eval "def \#{option}\noptions[:\#{option}]\nend\ndef \#{option}=(option_value)\nself.options[:\#{option}] = option_value\nend\n", __FILE__, __LINE__
end
end
|
Flyweight Design Pattern memoization cache. Can be cleared if memory is needed.
78
79
80
|
# File 'lib/glimmer/ui/custom_widget.rb', line 78
def flyweight_custom_widget_classes
@flyweight_custom_widget_classes ||= {}
end
|
.for(underscored_custom_widget_name) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/glimmer/ui/custom_widget.rb', line 44
def for(underscored_custom_widget_name)
unless flyweight_custom_widget_classes.keys.include?(underscored_custom_widget_name)
begin
= underscored_custom_widget_name.
to_s.
split(/__/).map do |namespace|
namespace.camelcase(:upper)
end
custom_widget_namespaces.each do |base|
.reduce(base) do |result, namespace|
if !result.constants.include?(namespace)
namespace = result.constants.detect {|c| c.to_s.upcase == namespace.to_s.upcase } || namespace
end
begin
flyweight_custom_widget_classes[underscored_custom_widget_name] = constant = result.const_get(namespace)
return constant if constant.ancestors.include?(Glimmer::UI::CustomWidget)
flyweight_custom_widget_classes[underscored_custom_widget_name] = constant
rescue => e
flyweight_custom_widget_classes[underscored_custom_widget_name] = result
end
end
end
raise "#{underscored_custom_widget_name} has no custom widget class!"
rescue => e
Glimmer::Config.logger.debug {e.message}
Glimmer::Config.logger.debug {"#{e.message}\n#{e.backtrace.join("\n")}"}
flyweight_custom_widget_classes[underscored_custom_widget_name] = nil
end
end
flyweight_custom_widget_classes[underscored_custom_widget_name]
end
|
.keyword ⇒ Object
Returns keyword to use for this custom widget
83
84
85
|
# File 'lib/glimmer/ui/custom_widget.rb', line 83
def keyword
self.name.underscore.gsub('::', '__')
end
|
.namespaces_for_class(m) ⇒ Object
98
99
100
101
102
103
104
|
# File 'lib/glimmer/ui/custom_widget.rb', line 98
def namespaces_for_class(m)
return [m] if m.name.nil?
namespace_constants = m.name.split(/::/).map(&:to_sym)
namespace_constants.reduce([Object]) do |output, namespace_constant|
output += [output.last.const_get(namespace_constant)]
end[1..-1].uniq.reverse
end
|
.option(new_option, default: nil) ⇒ Object
131
132
133
134
135
136
|
# File 'lib/glimmer/ui/custom_widget.rb', line 131
def option(new_option, default: nil)
new_option = new_option.to_s.to_sym
new_options = {new_option => default}
@options = options.merge(new_options)
def_option_attr_accessors(new_options)
end
|
.options(*new_options) ⇒ Object
Allows defining convenience option accessors for an array of option names Example: ‘options :color1, :color2` defines `#color1` and `#color2` where they return the instance values `options` and `options` respectively. Can be called multiple times to set more options additively. When passed no arguments, it returns list of all option names captured so far
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/glimmer/ui/custom_widget.rb', line 120
def options(*new_options)
new_options = new_options.compact.map(&:to_s).map(&:to_sym)
if new_options.empty?
@options ||= {}
else
new_options = new_options.reduce({}) {|new_options_hash, new_option| new_options_hash.merge(new_option => nil)}
@options = options.merge(new_options)
def_option_attr_accessors(new_options)
end
end
|
110
111
112
|
# File 'lib/glimmer/ui/custom_widget.rb', line 110
def reset_custom_widget_namespaces
@custom_widget_namespaces = Set[Object, Glimmer::UI]
end
|
.shortcut_keyword ⇒ Object
Returns shortcut keyword to use for this custom widget (keyword minus namespace)
88
89
90
|
# File 'lib/glimmer/ui/custom_widget.rb', line 88
def shortcut_keyword
self.name.underscore.gsub('::', '__').split('__').last
end
|
Instance Method Details
#add_observer(observer, attribute_name) ⇒ Object
257
258
259
260
261
262
263
264
265
|
# File 'lib/glimmer/ui/custom_widget.rb', line 257
def add_observer(observer, attribute_name)
if has_instance_method?(attribute_name)
super
elsif children_owner != @body_root && children_owner.can_add_observer?(attribute_name)
children_owner.add_observer(observer, attribute_name)
else
@body_root.add_observer(observer, attribute_name)
end
end
|
#async_exec(&block) ⇒ Object
TODO see if it is worth it to eliminate duplication of async_exec/sync_exec delegation to DisplayProxy, via a module
325
326
327
|
# File 'lib/glimmer/ui/custom_widget.rb', line 325
def async_exec(&block)
SWT::DisplayProxy.instance.async_exec(&block)
end
|
#attribute_setter(attribute_name) ⇒ Object
302
303
304
|
# File 'lib/glimmer/ui/custom_widget.rb', line 302
def attribute_setter(attribute_name)
"#{attribute_name}="
end
|
#can_add_observer?(attribute_name) ⇒ Boolean
250
251
252
253
254
255
|
# File 'lib/glimmer/ui/custom_widget.rb', line 250
def can_add_observer?(attribute_name)
has_instance_method?(attribute_name) ||
has_instance_method?("#{attribute_name}?") ||
(children_owner != @body_root && children_owner.can_add_observer?(attribute_name)) ||
@body_root.can_add_observer?(attribute_name)
end
|
#can_handle_observation_request?(observation_request) ⇒ Boolean
226
227
228
229
230
231
232
233
234
235
236
|
# File 'lib/glimmer/ui/custom_widget.rb', line 226
def can_handle_observation_request?(observation_request)
observation_request = observation_request.to_s
result = false
if observation_request.start_with?('on_updated_')
property = observation_request.sub(/^on_updated_/, '')
result = can_add_observer?(property)
end
result ||
(children_owner != body_root && children_owner&.can_handle_observation_request?(observation_request)) ||
body_root&.can_handle_observation_request?(observation_request)
end
|
#children_owner ⇒ Object
310
311
312
|
# File 'lib/glimmer/ui/custom_widget.rb', line 310
def children_owner
@children_owner || @body_root
end
|
#content(&block) ⇒ Object
Returns content block if used as an attribute reader (no args) Otherwise, if a block is passed, it adds it as content to this custom widget
339
340
341
342
343
344
345
|
# File 'lib/glimmer/ui/custom_widget.rb', line 339
def content(&block)
if block_given?
Glimmer::DSL::Engine.add_content(self, Glimmer::DSL::SWT::CustomWidgetExpression.new, self.class.keyword, &block)
else
@content
end
end
|
#disposed? ⇒ Boolean
314
315
316
|
# File 'lib/glimmer/ui/custom_widget.rb', line 314
def disposed?
swt_widget.isDisposed
end
|
#get_attribute(attribute_name) ⇒ Object
292
293
294
295
296
297
298
299
300
|
# File 'lib/glimmer/ui/custom_widget.rb', line 292
def get_attribute(attribute_name)
if has_instance_method?(attribute_name)
send(attribute_name)
elsif children_owner != @body_root && children_owner.has_attribute?(attribute_name)
children_owner.get_attribute(attribute_name)
else
@body_root.get_attribute(attribute_name)
end
end
|
#handle_observation_request(observation_request, &block) ⇒ Object
238
239
240
241
242
243
244
245
246
247
248
|
# File 'lib/glimmer/ui/custom_widget.rb', line 238
def handle_observation_request(observation_request, &block)
observation_request = observation_request.to_s
if observation_request.start_with?('on_updated_')
property = observation_request.sub(/^on_updated_/, '')
add_observer(DataBinding::Observer.proc(&block), property) if can_add_observer?(property)
elsif children_owner != body_root && children_owner&.can_handle_observation_request?(observation_request)
children_owner.handle_observation_request(observation_request, &block)
else
body_root.handle_observation_request(observation_request, &block)
end
end
|
#has_attribute?(attribute_name, *args) ⇒ Boolean
267
268
269
270
271
|
# File 'lib/glimmer/ui/custom_widget.rb', line 267
def has_attribute?(attribute_name, *args)
has_instance_method?(attribute_setter(attribute_name)) ||
(children_owner != @body_root && children_owner.has_attribute?(attribute_name, *args)) ||
@body_root.has_attribute?(attribute_name, *args)
end
|
#has_instance_method?(method_name) ⇒ Boolean
This method ensures it has an instance method not coming from Glimmer DSL
284
285
286
287
288
289
290
|
# File 'lib/glimmer/ui/custom_widget.rb', line 284
def has_instance_method?(method_name)
respond_to?(method_name) and
!swt_widget&.respond_to?(method_name) and
(method(method_name) rescue nil) and
!method(method_name)&.source_location&.first&.include?('glimmer/dsl/engine.rb') and
!method(method_name)&.source_location&.first&.include?('glimmer/swt/widget_proxy.rb')
end
|
#has_style?(style) ⇒ Boolean
318
319
320
|
# File 'lib/glimmer/ui/custom_widget.rb', line 318
def has_style?(style)
(swt_style & SWT::SWTProxy[style]) == SWT::SWTProxy[style]
end
|
#initialize(parent, *swt_style_symbols, options, &content) ⇒ Object
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
# File 'lib/glimmer/ui/custom_widget.rb', line 166
def initialize(parent, *swt_style_symbols, options, &content)
SWT::DisplayProxy.current_custom_widgets_and_shapes << self
@parent_proxy = @parent = parent
@parent_proxy = @parent&.get_data('proxy') if @parent.respond_to?(:get_data) && @parent.get_data('proxy')
@swt_style_symbols = swt_style_symbols
options ||= {}
@options = self.class.options.merge(options)
@content = Util::ProcTracker.new(content) if content
auto_exec { execute_hook('before_body') }
body_block = self.class.instance_variable_get("@body_block")
raise Glimmer::Error, 'Invalid custom widget for having no body! Please define body block!' if body_block.nil?
@body_root = auto_exec { instance_exec(&body_block) }
raise Glimmer::Error, 'Invalid custom widget for having an empty body! Please fill body block!' if @body_root.nil?
auto_exec do
@body_root.set_data('custom_widget', self)
end
auto_exec { execute_hook('after_body') }
auto_exec do
@dispose_listener_registration = @body_root.on_widget_disposed do
unless @body_root.shell_proxy.last_shell_closing?
observer_registrations.compact.each(&:deregister)
observer_registrations.clear
end
end
end
post_add_content if content.nil?
end
|
#local_respond_to? ⇒ Object
359
|
# File 'lib/glimmer/ui/custom_widget.rb', line 359
alias local_respond_to? respond_to?
|
#observer_registrations ⇒ Object
222
223
224
|
# File 'lib/glimmer/ui/custom_widget.rb', line 222
def observer_registrations
@observer_registrations ||= []
end
|
#post_add_content ⇒ Object
200
201
202
|
# File 'lib/glimmer/ui/custom_widget.rb', line 200
def post_add_content
SWT::DisplayProxy.current_custom_widgets_and_shapes.delete(self)
end
|
#post_initialize_child(child) ⇒ Object
Calls post_initialize_child on children_owner by default Subclasses may override to perform post initialization work on an added child
196
197
198
|
# File 'lib/glimmer/ui/custom_widget.rb', line 196
def post_initialize_child(child)
children_owner.post_initialize_child(child)
end
|
#respond_to?(method, *args, &block) ⇒ Boolean
360
361
362
363
364
365
|
# File 'lib/glimmer/ui/custom_widget.rb', line 360
def respond_to?(method, *args, &block)
super or
can_handle_observation_request?(method) or
children_owner.respond_to?(method, *args, &block) or
body_root.respond_to?(method, *args, &block)
end
|
#set_attribute(attribute_name, *args) ⇒ Object
273
274
275
276
277
278
279
280
281
|
# File 'lib/glimmer/ui/custom_widget.rb', line 273
def set_attribute(attribute_name, *args)
if has_instance_method?(attribute_setter(attribute_name))
send(attribute_setter(attribute_name), *args)
elsif (children_owner != @body_root && children_owner.has_attribute?(attribute_name, *args))
children_owner.set_attribute(attribute_name, *args)
else
@body_root.set_attribute(attribute_name, *args)
end
end
|
#shell_proxy ⇒ Object
306
307
308
|
# File 'lib/glimmer/ui/custom_widget.rb', line 306
def shell_proxy
@body_root.shell_proxy
end
|
#swt_style ⇒ Object
returns calculated SWT style integer value from swt_style_symbols keep in mind that it would not work when using glimmer-only custom swt_style_symbols like :editable for table In that case, just reference swt_style_symbols array instead
218
219
220
|
# File 'lib/glimmer/ui/custom_widget.rb', line 218
def swt_style
@swt_style ||= SWT::SWTProxy[*@swt_style_symbols]
end
|
204
205
206
207
208
209
210
211
212
|
# File 'lib/glimmer/ui/custom_widget.rb', line 204
def swt_widget
if @swt_widget.nil? && children_owner
@swt_widget = children_owner.swt_widget
auto_exec do
@swt_widget.set_data('custom_widget', self)
end
end
@swt_widget
end
|
#sync_exec(&block) ⇒ Object
329
330
331
|
# File 'lib/glimmer/ui/custom_widget.rb', line 329
def sync_exec(&block)
SWT::DisplayProxy.instance.sync_exec(&block)
end
|
#timer_exec(delay_in_millis, &block) ⇒ Object
333
334
335
|
# File 'lib/glimmer/ui/custom_widget.rb', line 333
def timer_exec(delay_in_millis, &block)
SWT::DisplayProxy.instance.timer_exec(delay_in_millis, &block)
end
|