glimmer-dsl-swt 4.18.4.4 → 4.18.4.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,315 +1,332 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
2
- #
3
- # Permission is hereby granted, free of charge, to any person obtaining
4
- # a copy of this software and associated documentation files (the
5
- # "Software"), to deal in the Software without restriction, including
6
- # without limitation the rights to use, copy, modify, merge, publish,
7
- # distribute, sublicense, and/or sell copies of the Software, and to
8
- # permit persons to whom the Software is furnished to do so, subject to
9
- # the following conditions:
10
- #
11
- # The above copyright notice and this permission notice shall be
12
- # included in all copies or substantial portions of the Software.
13
- #
14
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
-
22
- require 'glimmer'
23
- require 'glimmer/error'
24
- require 'glimmer/swt/swt_proxy'
25
- require 'glimmer/swt/display_proxy'
26
- require 'glimmer/util/proc_tracker'
27
- require 'glimmer/data_binding/observer'
28
- require 'glimmer/data_binding/observable_model'
29
-
30
- module Glimmer
31
- module UI
32
- module CustomWidget
33
- include SuperModule
34
- include DataBinding::ObservableModel
35
-
36
- super_module_included do |klass|
37
- # TODO clear memoization of WidgetProxy.swt_widget_class_for for a keyword if a custom widget was defined with that keyword
38
- klass.include(Glimmer) unless klass.name.include?('Glimmer::UI::CustomShell')
39
- Glimmer::UI::CustomWidget.add_custom_widget_namespaces_for(klass) unless klass.name.include?('Glimmer::UI::CustomShell')
40
- end
41
-
42
- class << self
43
- def for(underscored_custom_widget_name)
44
- unless flyweight_custom_widget_classes.keys.include?(underscored_custom_widget_name)
45
- begin
46
- extracted_namespaces = underscored_custom_widget_name.
47
- to_s.
48
- split(/__/).map do |namespace|
49
- namespace.camelcase(:upper)
50
- end
51
- custom_widget_namespaces.each do |base|
52
- extracted_namespaces.reduce(base) do |result, namespace|
53
- if !result.constants.include?(namespace)
54
- namespace = result.constants.detect {|c| c.to_s.upcase == namespace.to_s.upcase } || namespace
55
- end
56
- begin
57
- flyweight_custom_widget_classes[underscored_custom_widget_name] = constant = result.const_get(namespace)
58
- return constant if constant.ancestors.include?(Glimmer::UI::CustomWidget)
59
- flyweight_custom_widget_classes[underscored_custom_widget_name] = constant
60
- rescue => e
61
- # Glimmer::Config.logger.debug {"#{e.message}\n#{e.backtrace.join("\n")}"}
62
- flyweight_custom_widget_classes[underscored_custom_widget_name] = result
63
- end
64
- end
65
- end
66
- raise "#{underscored_custom_widget_name} has no custom widget class!"
67
- rescue => e
68
- Glimmer::Config.logger.debug {e.message}
69
- Glimmer::Config.logger.debug {"#{e.message}\n#{e.backtrace.join("\n")}"}
70
- flyweight_custom_widget_classes[underscored_custom_widget_name] = nil
71
- end
72
- end
73
- flyweight_custom_widget_classes[underscored_custom_widget_name]
74
- end
75
-
76
- # Flyweight Design Pattern memoization cache. Can be cleared if memory is needed.
77
- def flyweight_custom_widget_classes
78
- @flyweight_custom_widget_classes ||= {}
79
- end
80
-
81
- # Returns keyword to use for this custom widget
82
- def keyword
83
- self.name.underscore.gsub('::', '__')
84
- end
85
-
86
- # Returns shortcut keyword to use for this custom widget (keyword minus namespace)
87
- def shortcut_keyword
88
- self.name.underscore.gsub('::', '__').split('__').last
89
- end
90
-
91
- def add_custom_widget_namespaces_for(klass)
92
- Glimmer::UI::CustomWidget.namespaces_for_class(klass).drop(1).each do |namespace|
93
- Glimmer::UI::CustomWidget.custom_widget_namespaces << namespace
94
- end
95
- end
96
-
97
- def namespaces_for_class(m)
98
- return [m] if m.name.nil?
99
- namespace_constants = m.name.split(/::/).map(&:to_sym)
100
- namespace_constants.reduce([Object]) do |output, namespace_constant|
101
- output += [output.last.const_get(namespace_constant)]
102
- end[1..-1].uniq.reverse
103
- end
104
-
105
- def custom_widget_namespaces
106
- @custom_widget_namespaces ||= reset_custom_widget_namespaces
107
- end
108
-
109
- def reset_custom_widget_namespaces
110
- @custom_widget_namespaces = Set[Object, Glimmer::UI]
111
- end
112
-
113
- # Allows defining convenience option accessors for an array of option names
114
- # Example: `options :color1, :color2` defines `#color1` and `#color2`
115
- # where they return the instance values `options[:color1]` and `options[:color2]`
116
- # respectively.
117
- # Can be called multiple times to set more options additively.
118
- # When passed no arguments, it returns list of all option names captured so far
119
- def options(*new_options)
120
- new_options = new_options.compact.map(&:to_s).map(&:to_sym)
121
- if new_options.empty?
122
- @options ||= {} # maps options to defaults
123
- else
124
- new_options = new_options.reduce({}) {|new_options_hash, new_option| new_options_hash.merge(new_option => nil)}
125
- @options = options.merge(new_options)
126
- def_option_attr_accessors(new_options)
127
- end
128
- end
129
-
130
- def option(new_option, default: nil)
131
- new_option = new_option.to_s.to_sym
132
- new_options = {new_option => default}
133
- @options = options.merge(new_options)
134
- def_option_attr_accessors(new_options)
135
- end
136
-
137
- def def_option_attr_accessors(new_options)
138
- new_options.each do |option, default|
139
- class_eval <<-end_eval, __FILE__, __LINE__
140
- def #{option}
141
- options[:#{option}]
142
- end
143
- def #{option}=(option_value)
144
- self.options[:#{option}] = option_value
145
- end
146
- end_eval
147
- end
148
- end
149
-
150
- def before_body(&block)
151
- @before_body_block = block
152
- end
153
-
154
- def body(&block)
155
- @body_block = block
156
- end
157
-
158
- def after_body(&block)
159
- @after_body_block = block
160
- end
161
- end
162
-
163
- attr_reader :body_root, :swt_widget, :parent, :parent_proxy, :swt_style, :options
164
-
165
- def initialize(parent, *swt_constants, options, &content)
166
- @parent_proxy = @parent = parent
167
- @parent_proxy = @parent&.get_data('proxy') if @parent.respond_to?(:get_data) && @parent.get_data('proxy')
168
- @swt_style = SWT::SWTProxy[*swt_constants]
169
- options ||= {}
170
- @options = self.class.options.merge(options)
171
- @content = Util::ProcTracker.new(content) if content
172
- execute_hook('before_body')
173
- body_block = self.class.instance_variable_get("@body_block")
174
- raise Glimmer::Error, 'Invalid custom widget for having no body! Please define body block!' if body_block.nil?
175
- @body_root = instance_exec(&body_block)
176
- raise Glimmer::Error, 'Invalid custom widget for having an empty body! Please fill body block!' if @body_root.nil?
177
- @swt_widget = @body_root.swt_widget
178
- @swt_widget.set_data('custom_widget', self)
179
- execute_hook('after_body')
180
- end
181
-
182
- # Subclasses may override to perform post initialization work on an added child
183
- def post_initialize_child(child)
184
- # No Op by default
185
- end
186
-
187
- def can_handle_observation_request?(observation_request)
188
- observation_request = observation_request.to_s
189
- result = false
190
- if observation_request.start_with?('on_updated_')
191
- property = observation_request.sub(/^on_updated_/, '')
192
- result = can_add_observer?(property)
193
- end
194
- result || body_root&.can_handle_observation_request?(observation_request)
195
- end
196
-
197
- def handle_observation_request(observation_request, &block)
198
- observation_request = observation_request.to_s
199
- if observation_request.start_with?('on_updated_')
200
- property = observation_request.sub(/^on_updated_/, '') # TODO look into eliminating duplication from above
201
- add_observer(DataBinding::Observer.proc(&block), property) if can_add_observer?(property)
202
- else
203
- body_root.handle_observation_request(observation_request, &block)
204
- end
205
- end
206
-
207
- def can_add_observer?(attribute_name)
208
- has_instance_method?(attribute_name) || has_instance_method?("#{attribute_name}?") || @body_root.can_add_observer?(attribute_name)
209
- end
210
-
211
- def add_observer(observer, attribute_name)
212
- if has_instance_method?(attribute_name)
213
- super
214
- else
215
- @body_root.add_observer(observer, attribute_name)
216
- end
217
- end
218
-
219
- def has_attribute?(attribute_name, *args)
220
- has_instance_method?(attribute_setter(attribute_name)) ||
221
- @body_root.has_attribute?(attribute_name, *args)
222
- end
223
-
224
- def set_attribute(attribute_name, *args)
225
- if has_instance_method?(attribute_setter(attribute_name))
226
- send(attribute_setter(attribute_name), *args)
227
- else
228
- @body_root.set_attribute(attribute_name, *args)
229
- end
230
- end
231
-
232
- # This method ensures it has an instance method not coming from Glimmer DSL
233
- def has_instance_method?(method_name)
234
- respond_to?(method_name) and
235
- !swt_widget&.respond_to?(method_name) and
236
- (method(method_name) rescue nil) and
237
- !method(method_name)&.source_location&.first&.include?('glimmer/dsl/engine.rb') and
238
- !method(method_name)&.source_location&.first&.include?('glimmer/swt/widget_proxy.rb')
239
- end
240
-
241
- def get_attribute(attribute_name)
242
- if has_instance_method?(attribute_name)
243
- send(attribute_name)
244
- else
245
- @body_root.get_attribute(attribute_name)
246
- end
247
- end
248
-
249
- def attribute_setter(attribute_name)
250
- "#{attribute_name}="
251
- end
252
-
253
- def disposed?
254
- swt_widget.isDisposed
255
- end
256
-
257
- def has_style?(style)
258
- (swt_style & SWT::SWTProxy[style]) == SWT::SWTProxy[style]
259
- end
260
-
261
- def pack(*args)
262
- body_root.pack(*args)
263
- end
264
-
265
- # TODO see if it is worth it to eliminate duplication of async_exec/sync_exec
266
- # delegation to DisplayProxy, via a module
267
-
268
- def async_exec(&block)
269
- SWT::DisplayProxy.instance.async_exec(&block)
270
- end
271
-
272
- def sync_exec(&block)
273
- SWT::DisplayProxy.instance.sync_exec(&block)
274
- end
275
-
276
- # Returns content block if used as an attribute reader (no args)
277
- # Otherwise, if a block is passed, it adds it as content to this custom widget
278
- def content(&block)
279
- if block_given?
280
- body_root.content(&block)
281
- else
282
- @content
283
- end
284
- end
285
-
286
- def method_missing(method, *args, &block)
287
- # TODO Consider supporting a glimmer error silencing option for methods defined here
288
- # but fail the glimmer DSL for the right reason to avoid seeing noise in the log output
289
- if can_handle_observation_request?(method)
290
- handle_observation_request(method, &block)
291
- else
292
- body_root.send(method, *args, &block)
293
- end
294
- end
295
-
296
- alias local_respond_to? respond_to?
297
- def respond_to?(method, *args, &block)
298
- super or
299
- can_handle_observation_request?(method) or
300
- body_root.respond_to?(method, *args, &block)
301
- end
302
-
303
- private
304
-
305
- def execute_hook(hook_name)
306
- hook_block = self.class.instance_variable_get("@#{hook_name}_block")
307
- return if hook_block.nil?
308
- temp_method_name = "#{hook_name}_block_#{hook_block.hash.abs}_#{(Time.now.to_f * 1_000_000).to_i}"
309
- singleton_class.define_method(temp_method_name, &hook_block)
310
- send(temp_method_name)
311
- singleton_class.send(:remove_method, temp_method_name)
312
- end
313
- end
314
- end
315
- end
1
+ # Copyright (c) 2007-2021 Andy Maleh
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ require 'glimmer'
23
+ require 'glimmer/error'
24
+ require 'glimmer/swt/swt_proxy'
25
+ require 'glimmer/swt/display_proxy'
26
+ require 'glimmer/util/proc_tracker'
27
+ require 'glimmer/data_binding/observer'
28
+ require 'glimmer/data_binding/observable_model'
29
+
30
+ module Glimmer
31
+ module UI
32
+ module CustomWidget
33
+ include SuperModule
34
+ include DataBinding::ObservableModel
35
+
36
+ super_module_included do |klass|
37
+ # TODO clear memoization of WidgetProxy.swt_widget_class_for for a keyword if a custom widget was defined with that keyword
38
+ klass.include(Glimmer) unless klass.name.include?('Glimmer::UI::CustomShell')
39
+ Glimmer::UI::CustomWidget.add_custom_widget_namespaces_for(klass) unless klass.name.include?('Glimmer::UI::CustomShell')
40
+ end
41
+
42
+ class << self
43
+ def for(underscored_custom_widget_name)
44
+ unless flyweight_custom_widget_classes.keys.include?(underscored_custom_widget_name)
45
+ begin
46
+ extracted_namespaces = underscored_custom_widget_name.
47
+ to_s.
48
+ split(/__/).map do |namespace|
49
+ namespace.camelcase(:upper)
50
+ end
51
+ custom_widget_namespaces.each do |base|
52
+ extracted_namespaces.reduce(base) do |result, namespace|
53
+ if !result.constants.include?(namespace)
54
+ namespace = result.constants.detect {|c| c.to_s.upcase == namespace.to_s.upcase } || namespace
55
+ end
56
+ begin
57
+ flyweight_custom_widget_classes[underscored_custom_widget_name] = constant = result.const_get(namespace)
58
+ return constant if constant.ancestors.include?(Glimmer::UI::CustomWidget)
59
+ flyweight_custom_widget_classes[underscored_custom_widget_name] = constant
60
+ rescue => e
61
+ # Glimmer::Config.logger.debug {"#{e.message}\n#{e.backtrace.join("\n")}"}
62
+ flyweight_custom_widget_classes[underscored_custom_widget_name] = result
63
+ end
64
+ end
65
+ end
66
+ raise "#{underscored_custom_widget_name} has no custom widget class!"
67
+ rescue => e
68
+ Glimmer::Config.logger.debug {e.message}
69
+ Glimmer::Config.logger.debug {"#{e.message}\n#{e.backtrace.join("\n")}"}
70
+ flyweight_custom_widget_classes[underscored_custom_widget_name] = nil
71
+ end
72
+ end
73
+ flyweight_custom_widget_classes[underscored_custom_widget_name]
74
+ end
75
+
76
+ # Flyweight Design Pattern memoization cache. Can be cleared if memory is needed.
77
+ def flyweight_custom_widget_classes
78
+ @flyweight_custom_widget_classes ||= {}
79
+ end
80
+
81
+ # Returns keyword to use for this custom widget
82
+ def keyword
83
+ self.name.underscore.gsub('::', '__')
84
+ end
85
+
86
+ # Returns shortcut keyword to use for this custom widget (keyword minus namespace)
87
+ def shortcut_keyword
88
+ self.name.underscore.gsub('::', '__').split('__').last
89
+ end
90
+
91
+ def add_custom_widget_namespaces_for(klass)
92
+ Glimmer::UI::CustomWidget.namespaces_for_class(klass).drop(1).each do |namespace|
93
+ Glimmer::UI::CustomWidget.custom_widget_namespaces << namespace
94
+ end
95
+ end
96
+
97
+ def namespaces_for_class(m)
98
+ return [m] if m.name.nil?
99
+ namespace_constants = m.name.split(/::/).map(&:to_sym)
100
+ namespace_constants.reduce([Object]) do |output, namespace_constant|
101
+ output += [output.last.const_get(namespace_constant)]
102
+ end[1..-1].uniq.reverse
103
+ end
104
+
105
+ def custom_widget_namespaces
106
+ @custom_widget_namespaces ||= reset_custom_widget_namespaces
107
+ end
108
+
109
+ def reset_custom_widget_namespaces
110
+ @custom_widget_namespaces = Set[Object, Glimmer::UI]
111
+ end
112
+
113
+ # Allows defining convenience option accessors for an array of option names
114
+ # Example: `options :color1, :color2` defines `#color1` and `#color2`
115
+ # where they return the instance values `options[:color1]` and `options[:color2]`
116
+ # respectively.
117
+ # Can be called multiple times to set more options additively.
118
+ # When passed no arguments, it returns list of all option names captured so far
119
+ def options(*new_options)
120
+ new_options = new_options.compact.map(&:to_s).map(&:to_sym)
121
+ if new_options.empty?
122
+ @options ||= {} # maps options to defaults
123
+ else
124
+ new_options = new_options.reduce({}) {|new_options_hash, new_option| new_options_hash.merge(new_option => nil)}
125
+ @options = options.merge(new_options)
126
+ def_option_attr_accessors(new_options)
127
+ end
128
+ end
129
+
130
+ def option(new_option, default: nil)
131
+ new_option = new_option.to_s.to_sym
132
+ new_options = {new_option => default}
133
+ @options = options.merge(new_options)
134
+ def_option_attr_accessors(new_options)
135
+ end
136
+
137
+ def def_option_attr_accessors(new_options)
138
+ new_options.each do |option, default|
139
+ class_eval <<-end_eval, __FILE__, __LINE__
140
+ def #{option}
141
+ options[:#{option}]
142
+ end
143
+ def #{option}=(option_value)
144
+ self.options[:#{option}] = option_value
145
+ end
146
+ end_eval
147
+ end
148
+ end
149
+
150
+ def before_body(&block)
151
+ @before_body_block = block
152
+ end
153
+
154
+ def body(&block)
155
+ @body_block = block
156
+ end
157
+
158
+ def after_body(&block)
159
+ @after_body_block = block
160
+ end
161
+
162
+ # Current custom widgets being rendered. Useful to yoke all observers evaluated during rendering of their custom widgets for automatical disposal on_widget_disposed
163
+ def current_custom_widgets
164
+ @current_custom_widgets ||= []
165
+ end
166
+ end
167
+
168
+ attr_reader :body_root, :swt_widget, :parent, :parent_proxy, :swt_style, :options
169
+
170
+ def initialize(parent, *swt_constants, options, &content)
171
+ Glimmer::UI::CustomWidget.current_custom_widgets << self
172
+ @parent_proxy = @parent = parent
173
+ @parent_proxy = @parent&.get_data('proxy') if @parent.respond_to?(:get_data) && @parent.get_data('proxy')
174
+ @swt_style = SWT::SWTProxy[*swt_constants]
175
+ options ||= {}
176
+ @options = self.class.options.merge(options)
177
+ @content = Util::ProcTracker.new(content) if content
178
+ execute_hook('before_body')
179
+ body_block = self.class.instance_variable_get("@body_block")
180
+ raise Glimmer::Error, 'Invalid custom widget for having no body! Please define body block!' if body_block.nil?
181
+ @body_root = instance_exec(&body_block)
182
+ raise Glimmer::Error, 'Invalid custom widget for having an empty body! Please fill body block!' if @body_root.nil?
183
+ @swt_widget = @body_root.swt_widget
184
+ @swt_widget.set_data('custom_widget', self)
185
+ execute_hook('after_body')
186
+ @dispose_listener_registration = @body_root.on_widget_disposed do
187
+ observer_registrations.each(&:deregister)
188
+ end
189
+ end
190
+
191
+ # Subclasses may override to perform post initialization work on an added child
192
+ def post_initialize_child(child)
193
+ # No Op by default
194
+ end
195
+
196
+ def observer_registrations
197
+ @observer_registrations ||= []
198
+ end
199
+
200
+ def can_handle_observation_request?(observation_request)
201
+ observation_request = observation_request.to_s
202
+ result = false
203
+ if observation_request.start_with?('on_updated_')
204
+ property = observation_request.sub(/^on_updated_/, '')
205
+ result = can_add_observer?(property)
206
+ end
207
+ result || body_root&.can_handle_observation_request?(observation_request)
208
+ end
209
+
210
+ def handle_observation_request(observation_request, &block)
211
+ observation_request = observation_request.to_s
212
+ if observation_request.start_with?('on_updated_')
213
+ property = observation_request.sub(/^on_updated_/, '') # TODO look into eliminating duplication from above
214
+ add_observer(DataBinding::Observer.proc(&block), property) if can_add_observer?(property)
215
+ else
216
+ body_root.handle_observation_request(observation_request, &block)
217
+ end
218
+ end
219
+
220
+ def can_add_observer?(attribute_name)
221
+ has_instance_method?(attribute_name) || has_instance_method?("#{attribute_name}?") || @body_root.can_add_observer?(attribute_name)
222
+ end
223
+
224
+ def add_observer(observer, attribute_name)
225
+ if has_instance_method?(attribute_name)
226
+ super
227
+ else
228
+ @body_root.add_observer(observer, attribute_name)
229
+ end
230
+ end
231
+
232
+ def has_attribute?(attribute_name, *args)
233
+ has_instance_method?(attribute_setter(attribute_name)) ||
234
+ @body_root.has_attribute?(attribute_name, *args)
235
+ end
236
+
237
+ def set_attribute(attribute_name, *args)
238
+ if has_instance_method?(attribute_setter(attribute_name))
239
+ send(attribute_setter(attribute_name), *args)
240
+ else
241
+ @body_root.set_attribute(attribute_name, *args)
242
+ end
243
+ end
244
+
245
+ # This method ensures it has an instance method not coming from Glimmer DSL
246
+ def has_instance_method?(method_name)
247
+ respond_to?(method_name) and
248
+ !swt_widget&.respond_to?(method_name) and
249
+ (method(method_name) rescue nil) and
250
+ !method(method_name)&.source_location&.first&.include?('glimmer/dsl/engine.rb') and
251
+ !method(method_name)&.source_location&.first&.include?('glimmer/swt/widget_proxy.rb')
252
+ end
253
+
254
+ def get_attribute(attribute_name)
255
+ if has_instance_method?(attribute_name)
256
+ send(attribute_name)
257
+ else
258
+ @body_root.get_attribute(attribute_name)
259
+ end
260
+ end
261
+
262
+ def attribute_setter(attribute_name)
263
+ "#{attribute_name}="
264
+ end
265
+
266
+ def disposed?
267
+ swt_widget.isDisposed
268
+ end
269
+
270
+ def has_style?(style)
271
+ (swt_style & SWT::SWTProxy[style]) == SWT::SWTProxy[style]
272
+ end
273
+
274
+ def pack(*args)
275
+ body_root.pack(*args)
276
+ end
277
+
278
+ # TODO see if it is worth it to eliminate duplication of async_exec/sync_exec
279
+ # delegation to DisplayProxy, via a module
280
+
281
+ def async_exec(&block)
282
+ SWT::DisplayProxy.instance.async_exec(&block)
283
+ end
284
+
285
+ def sync_exec(&block)
286
+ SWT::DisplayProxy.instance.sync_exec(&block)
287
+ end
288
+
289
+ def timer_exec(delay_in_millis, &block)
290
+ SWT::DisplayProxy.instance.timer_exec(delay_in_millis, &block)
291
+ end
292
+
293
+ # Returns content block if used as an attribute reader (no args)
294
+ # Otherwise, if a block is passed, it adds it as content to this custom widget
295
+ def content(&block)
296
+ if block_given?
297
+ body_root.content(&block)
298
+ else
299
+ @content
300
+ end
301
+ end
302
+
303
+ def method_missing(method, *args, &block)
304
+ # TODO Consider supporting a glimmer error silencing option for methods defined here
305
+ # but fail the glimmer DSL for the right reason to avoid seeing noise in the log output
306
+ if can_handle_observation_request?(method)
307
+ handle_observation_request(method, &block)
308
+ else
309
+ body_root.send(method, *args, &block)
310
+ end
311
+ end
312
+
313
+ alias local_respond_to? respond_to?
314
+ def respond_to?(method, *args, &block)
315
+ super or
316
+ can_handle_observation_request?(method) or
317
+ body_root.respond_to?(method, *args, &block)
318
+ end
319
+
320
+ private
321
+
322
+ def execute_hook(hook_name)
323
+ hook_block = self.class.instance_variable_get("@#{hook_name}_block")
324
+ return if hook_block.nil?
325
+ temp_method_name = "#{hook_name}_block_#{hook_block.hash.abs}_#{(Time.now.to_f * 1_000_000).to_i}"
326
+ singleton_class.define_method(temp_method_name, &hook_block)
327
+ send(temp_method_name)
328
+ singleton_class.send(:remove_method, temp_method_name)
329
+ end
330
+ end
331
+ end
332
+ end