Module: Glimmer::UI::CustomWidget

Includes:
DataBinding::ObservableModel
Included in:
SWT::Custom::CheckboxGroup, SWT::Custom::RadioGroup, CustomShell, GreetingLabel, Tetris::View::Block, Tetris::View::Playfield, Tetris::View::ScoreLane, Tetris::View::TetrisMenuBar
Defined in:
lib/glimmer/ui/custom_widget.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



279
280
281
282
283
284
285
# File 'lib/glimmer/ui/custom_widget.rb', line 279

def method_missing(method, *args, &block)
  if can_handle_observation_request?(method)
    handle_observation_request(method, block)
  else
    body_root.send(method, *args, &block)
  end
end

Instance Attribute Details

#body_rootObject (readonly)

Returns the value of attribute body_root.



160
161
162
# File 'lib/glimmer/ui/custom_widget.rb', line 160

def body_root
  @body_root
end

#optionsObject (readonly)

Returns the value of attribute options.



160
161
162
# File 'lib/glimmer/ui/custom_widget.rb', line 160

def options
  @options
end

#parentObject (readonly) Also known as: parent_proxy

Returns the value of attribute parent.



160
161
162
# File 'lib/glimmer/ui/custom_widget.rb', line 160

def parent
  @parent
end

#swt_styleObject (readonly)

Returns the value of attribute swt_style.



160
161
162
# File 'lib/glimmer/ui/custom_widget.rb', line 160

def swt_style
  @swt_style
end

Class Method Details

.add_custom_widget_namespaces_for(klass) ⇒ Object



135
136
137
138
139
# File 'lib/glimmer/ui/custom_widget.rb', line 135

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

.custom_widget_namespacesObject



149
150
151
# File 'lib/glimmer/ui/custom_widget.rb', line 149

def custom_widget_namespaces
  @custom_widget_namespaces ||= reset_custom_widget_namespaces
end

.for(underscored_custom_widget_name) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/glimmer/ui/custom_widget.rb', line 107

def for(underscored_custom_widget_name)
  extracted_namespaces = underscored_custom_widget_name.
    to_s.
    split(/__/).map do |namespace|
      namespace.camelcase(:upper)
    end
  custom_widget_namespaces.each do |base|
    extracted_namespaces.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
        constant = result.const_get(namespace)
        return constant if constant&.respond_to?(:ancestors) && constant&.ancestors&.to_a.include?(Glimmer::UI::CustomWidget)
        constant
      rescue => e
        # Glimmer::Config.logger.debug {"#{e.message}\n#{e.backtrace.join("\n")}"}
        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")}"}
  nil
end

.included(klass) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/glimmer/ui/custom_widget.rb', line 99

def included(klass)
  klass.extend(ClassMethods)
  unless klass.name.include?('Glimmer::UI::CustomShell')
    klass.include(Glimmer)
    Glimmer::UI::CustomWidget.add_custom_widget_namespaces_for(klass)
  end
end

.namespaces_for_class(m) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/glimmer/ui/custom_widget.rb', line 141

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

.reset_custom_widget_namespacesObject



153
154
155
# File 'lib/glimmer/ui/custom_widget.rb', line 153

def reset_custom_widget_namespaces
  @custom_widget_namespaces = Set[Object, Glimmer::UI]
end

Instance Method Details

#add_observer(observer, attribute_name) ⇒ Object



215
216
217
218
219
220
221
# File 'lib/glimmer/ui/custom_widget.rb', line 215

def add_observer(observer, attribute_name)
  if has_instance_method?(attribute_name)
    super
  else
    @body_root.add_observer(observer, attribute_name)
  end
end

#async_exec(&block) ⇒ Object



260
261
262
# File 'lib/glimmer/ui/custom_widget.rb', line 260

def async_exec(&block)
  SWT::DisplayProxy.instance.async_exec(&block)
end

#attribute_setter(attribute_name) ⇒ Object



252
253
254
# File 'lib/glimmer/ui/custom_widget.rb', line 252

def attribute_setter(attribute_name)
  "#{attribute_name}="
end

#can_add_observer?(attribute_name) ⇒ Boolean

Returns:

  • (Boolean)


211
212
213
# File 'lib/glimmer/ui/custom_widget.rb', line 211

def can_add_observer?(attribute_name)
  has_instance_method?(attribute_name) || has_instance_method?("#{attribute_name}?") || @body_root.can_add_observer?(attribute_name)
end

#can_handle_observation_request?(observation_request) ⇒ Boolean

Returns:

  • (Boolean)


191
192
193
194
195
196
197
198
199
# File 'lib/glimmer/ui/custom_widget.rb', line 191

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 || body_root&.can_handle_observation_request?(observation_request)
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



271
272
273
274
275
276
277
# File 'lib/glimmer/ui/custom_widget.rb', line 271

def content(&block)
  if block_given?
    Glimmer::DSL::Engine.add_content(self, Glimmer::DSL::Opal::CustomWidgetExpression.new, self.class.keyword, &block)
  else
    @content
  end
end

#get_attribute(attribute_name) ⇒ Object



244
245
246
247
248
249
250
# File 'lib/glimmer/ui/custom_widget.rb', line 244

def get_attribute(attribute_name)
  if has_instance_method?(attribute_name)
    send(attribute_name)
  else
    @body_root.get_attribute(attribute_name)
  end
end

#handle_observation_request(observation_request, block) ⇒ Object



201
202
203
204
205
206
207
208
209
# File 'lib/glimmer/ui/custom_widget.rb', line 201

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_/, '') # TODO look into eliminating duplication from above
    add_observer(DataBinding::Observer.proc(&block), property) if can_add_observer?(property)
  else
    body_root.handle_observation_request(observation_request, block)
  end
end

#has_attribute?(attribute_name, *args) ⇒ Boolean

Returns:

  • (Boolean)


223
224
225
226
# File 'lib/glimmer/ui/custom_widget.rb', line 223

def has_attribute?(attribute_name, *args)
  has_instance_method?(attribute_setter(attribute_name)) ||
    @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

Returns:

  • (Boolean)


237
238
239
240
241
242
# File 'lib/glimmer/ui/custom_widget.rb', line 237

def has_instance_method?(method_name)
  respond_to?(method_name) and
    !body_root&.respond_to?(method_name) 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?(symbol) ⇒ Boolean

Returns:

  • (Boolean)


256
257
258
# File 'lib/glimmer/ui/custom_widget.rb', line 256

def has_style?(symbol)
  @args.include?(symbol) # not a very solid implementation. Bring SWT constants eventually
end

#initialize(parent, args, options, &content) ⇒ Object

Raises:

  • (Glimmer::Error)


163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/glimmer/ui/custom_widget.rb', line 163

def initialize(parent, args, options, &content)
  @parent = parent
  options = args.delete_at(-1) if args.is_a?(Array) && args.last.is_a?(Hash)
  if args.is_a?(Hash)
    options = args
    args = []
  end
  options ||= {}
  args = options.delete('swt_style').split(',').map(&:to_sym) if options['swt_style']
  @args = args
  @swt_style = SWT::SWTProxy[*@args]
  options ||= {}
  @options = self.class.options.merge(options)
  @content = Util::ProcTracker.new(content) if content
  execute_hooks('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 = instance_exec(&body_block)
  @parent ||= @body_root.parent
  raise Glimmer::Error, 'Invalid custom widget for having an empty body! Please fill body block!' if @body_root.nil?
  execute_hooks('after_body')
end

#local_respond_to?Object



287
# File 'lib/glimmer/ui/custom_widget.rb', line 287

alias local_respond_to? respond_to?

#post_initialize_child(child) ⇒ Object

Subclasses may override to perform post initialization work on an added child



187
188
189
# File 'lib/glimmer/ui/custom_widget.rb', line 187

def post_initialize_child(child)
  # No Op by default
end

#respond_to?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


288
289
290
291
292
# File 'lib/glimmer/ui/custom_widget.rb', line 288

def respond_to?(method_name, include_private = false)
  super(method_name, include_private) or
    can_handle_observation_request?(method_name) or
    body_root.respond_to?(method_name, include_private)
end

#set_attribute(attribute_name, *args) ⇒ Object



228
229
230
231
232
233
234
# File 'lib/glimmer/ui/custom_widget.rb', line 228

def set_attribute(attribute_name, *args)
  if has_instance_method?(attribute_setter(attribute_name))
    send(attribute_setter(attribute_name), *args)
  else
    @body_root.set_attribute(attribute_name, *args)
  end
end

#sync_exec(&block) ⇒ Object

sync_exec kept for API compatibility reasons



265
266
267
# File 'lib/glimmer/ui/custom_widget.rb', line 265

def sync_exec(&block)
  SWT::DisplayProxy.instance.async_exec(&block)
end