Module: Glimmer::UI::CustomWidget

Includes:
DataBinding::ObservableModel, SuperModule
Included in:
CustomShell
Defined in:
lib/glimmer/ui/custom_widget.rb

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



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

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.



123
124
125
# File 'lib/glimmer/ui/custom_widget.rb', line 123

def body_root
  @body_root
end

#optionsObject (readonly)

Returns the value of attribute options.



123
124
125
# File 'lib/glimmer/ui/custom_widget.rb', line 123

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent.



123
124
125
# File 'lib/glimmer/ui/custom_widget.rb', line 123

def parent
  @parent
end

#swt_styleObject (readonly)

Returns the value of attribute swt_style.



123
124
125
# File 'lib/glimmer/ui/custom_widget.rb', line 123

def swt_style
  @swt_style
end

#swt_widgetObject (readonly)

Returns the value of attribute swt_widget.



123
124
125
# File 'lib/glimmer/ui/custom_widget.rb', line 123

def swt_widget
  @swt_widget
end

Class Method Details

.add_custom_widget_namespaces_for(klass) ⇒ Object



49
50
51
52
53
# File 'lib/glimmer/ui/custom_widget.rb', line 49

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



117
118
119
120
# File 'lib/glimmer/ui/custom_widget.rb', line 117

def after_body(&block)
  @after_body_blocks ||= []
  @after_body_blocks << block
end

.before_body(&block) ⇒ Object



108
109
110
111
# File 'lib/glimmer/ui/custom_widget.rb', line 108

def before_body(&block)
  @before_body_blocks ||= []
  @before_body_blocks << block
end

.body(&block) ⇒ Object



113
114
115
# File 'lib/glimmer/ui/custom_widget.rb', line 113

def body(&block)
  @body_block = block
end

.custom_widget_namespacesObject



63
64
65
# File 'lib/glimmer/ui/custom_widget.rb', line 63

def custom_widget_namespaces
  @custom_widget_namespaces ||= reset_custom_widget_namespaces
end

.def_option_attr_accessors(new_options) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/glimmer/ui/custom_widget.rb', line 95

def def_option_attr_accessors(new_options)
  new_options.each do |option, default|
    class_eval <<-end_eval, __FILE__, __LINE__
      def #{option}
        options[:#{option}]
      end
      def #{option}=(option_value)
        self.options[:#{option}] = option_value
      end
    end_eval
  end
end

.for(underscored_custom_widget_name) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/glimmer/ui/custom_widget.rb', line 21

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.ancestors.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

.namespaces_for_class(m) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/glimmer/ui/custom_widget.rb', line 55

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



88
89
90
91
92
93
# File 'lib/glimmer/ui/custom_widget.rb', line 88

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



77
78
79
80
81
82
83
84
85
86
# File 'lib/glimmer/ui/custom_widget.rb', line 77

def options(*new_options)
  new_options = new_options.compact.map(&:to_s).map(&:to_sym)
  if new_options.empty?
    @options ||= {} # maps options to defaults
  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

.reset_custom_widget_namespacesObject



67
68
69
# File 'lib/glimmer/ui/custom_widget.rb', line 67

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

Instance Method Details

#add_observer(observer, attribute_name) ⇒ Object



169
170
171
172
173
174
175
# File 'lib/glimmer/ui/custom_widget.rb', line 169

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

TODO see if it is worth it to eliminate duplication of async_exec/sync_exec delegation to DisplayProxy, via a module



217
218
219
# File 'lib/glimmer/ui/custom_widget.rb', line 217

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

#attribute_setter(attribute_name) ⇒ Object



206
207
208
# File 'lib/glimmer/ui/custom_widget.rb', line 206

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

#can_add_observer?(attribute_name) ⇒ Boolean



165
166
167
# File 'lib/glimmer/ui/custom_widget.rb', line 165

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



145
146
147
148
149
150
151
152
153
# File 'lib/glimmer/ui/custom_widget.rb', line 145

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



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

def content(&block)
  if block_given?
    body_root.content(&block)
  else
    @content
  end
end

#get_attribute(attribute_name) ⇒ Object



198
199
200
201
202
203
204
# File 'lib/glimmer/ui/custom_widget.rb', line 198

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



155
156
157
158
159
160
161
162
163
# File 'lib/glimmer/ui/custom_widget.rb', line 155

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



177
178
179
180
# File 'lib/glimmer/ui/custom_widget.rb', line 177

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



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

def has_instance_method?(method_name)
  respond_to?(method_name) && 
    !swt_widget.respond_to?(method_name) &&
    !method(method_name)&.source_location&.first&.include?('glimmer/dsl/engine.rb') && 
    !method(method_name)&.source_location&.first&.include?('glimmer/swt/widget_proxy.rb')
end

#has_style?(style) ⇒ Boolean



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

def has_style?(style)
  (swt_style & SWT::SWTProxy[style]) == SWT::SWTProxy[style]
end

#initialize(parent, *swt_constants, options, &content) ⇒ Object

Raises:

  • (Glimmer::Error)


125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/glimmer/ui/custom_widget.rb', line 125

def initialize(parent, *swt_constants, options, &content)
  @parent = parent
  @swt_style = SWT::SWTProxy[*swt_constants]
  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)
  @swt_widget = @body_root.swt_widget
  @swt_widget.set_data('custom_widget', self)
  execute_hooks('after_body')
end

#local_respond_to?Object



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

alias local_respond_to? respond_to?

#post_initialize_child(child) ⇒ Object

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



141
142
143
# File 'lib/glimmer/ui/custom_widget.rb', line 141

def post_initialize_child(child)
  # No Op by default
end

#respond_to?(method, *args, &block) ⇒ Boolean



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

def respond_to?(method, *args, &block)
  super or
    can_handle_observation_request?(method) or
    body_root.respond_to?(method, *args, &block)
end

#set_attribute(attribute_name, *args) ⇒ Object



182
183
184
185
186
187
188
# File 'lib/glimmer/ui/custom_widget.rb', line 182

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



221
222
223
# File 'lib/glimmer/ui/custom_widget.rb', line 221

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