Module: Glimmer::UI::CustomShape

Includes:
DataBinding::ObservableModel, SuperModule
Defined in:
lib/glimmer/ui/custom_shape.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_name, *args, &block) ⇒ Object



299
300
301
302
303
304
305
306
307
# File 'lib/glimmer/ui/custom_shape.rb', line 299

def method_missing(method_name, *args, &block)
  # TODO Consider supporting a glimmer error silencing option for methods defined here
  # but fail the glimmer DSL for the right reason to avoid seeing noise in the log output
  if block && can_handle_observation_request?(method_name)
    handle_observation_request(method_name, &block)
  else
    body_root.send(method_name, *args, &block)
  end
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



163
164
165
# File 'lib/glimmer/ui/custom_shape.rb', line 163

def args
  @args
end

#body_rootObject (readonly)

Returns the value of attribute body_root.



163
164
165
# File 'lib/glimmer/ui/custom_shape.rb', line 163

def body_root
  @body_root
end

#optionsObject (readonly)

Returns the value of attribute options.



163
164
165
# File 'lib/glimmer/ui/custom_shape.rb', line 163

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent.



163
164
165
# File 'lib/glimmer/ui/custom_shape.rb', line 163

def parent
  @parent
end

#parent_proxyObject (readonly)

Returns the value of attribute parent_proxy.



163
164
165
# File 'lib/glimmer/ui/custom_shape.rb', line 163

def parent_proxy
  @parent_proxy
end

Class Method Details

.add_custom_shape_namespaces_for(klass) ⇒ Object



91
92
93
94
95
# File 'lib/glimmer/ui/custom_shape.rb', line 91

def add_custom_shape_namespaces_for(klass)
  Glimmer::UI::CustomShape.namespaces_for_class(klass).drop(1).each do |namespace|
    Glimmer::UI::CustomShape.custom_shape_namespaces << namespace
  end
end

.after_body(&block) ⇒ Object



158
159
160
# File 'lib/glimmer/ui/custom_shape.rb', line 158

def after_body(&block)
  @after_body_block = block
end

.before_body(&block) ⇒ Object



150
151
152
# File 'lib/glimmer/ui/custom_shape.rb', line 150

def before_body(&block)
  @before_body_block = block
end

.body(&block) ⇒ Object



154
155
156
# File 'lib/glimmer/ui/custom_shape.rb', line 154

def body(&block)
  @body_block = block
end

.custom_shape_namespacesObject



105
106
107
# File 'lib/glimmer/ui/custom_shape.rb', line 105

def custom_shape_namespaces
  @custom_shape_namespaces ||= reset_custom_shape_namespaces
end

.def_option_attr_accessors(new_options) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/glimmer/ui/custom_shape.rb', line 137

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

.flyweight_custom_shape_classesObject

Flyweight Design Pattern memoization cache. Can be cleared if memory is needed.



77
78
79
# File 'lib/glimmer/ui/custom_shape.rb', line 77

def flyweight_custom_shape_classes
  @flyweight_custom_shape_classes ||= {}
end

.for(underscored_custom_shape_name) ⇒ Object



43
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
# File 'lib/glimmer/ui/custom_shape.rb', line 43

def for(underscored_custom_shape_name)
  unless flyweight_custom_shape_classes.keys.include?(underscored_custom_shape_name)
    begin
      extracted_namespaces = underscored_custom_shape_name.
        to_s.
        split(/__/).map do |namespace|
          namespace.camelcase(:upper)
        end
      custom_shape_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
            flyweight_custom_shape_classes[underscored_custom_shape_name] = constant = result.const_get(namespace)
            return constant if constant.ancestors.include?(Glimmer::UI::CustomShape)
            flyweight_custom_shape_classes[underscored_custom_shape_name] = constant
          rescue => e
            # Glimmer::Config.logger.debug {"#{e.message}\n#{e.backtrace.join("\n")}"}
            flyweight_custom_shape_classes[underscored_custom_shape_name] = result
          end
        end
      end
      raise "#{underscored_custom_shape_name} has no custom shape class!"
    rescue => e
      Glimmer::Config.logger.debug {e.message}
      Glimmer::Config.logger.debug {"#{e.message}\n#{e.backtrace.join("\n")}"}
      flyweight_custom_shape_classes[underscored_custom_shape_name] = nil
    end
  end
  flyweight_custom_shape_classes[underscored_custom_shape_name]
end

.keywordObject

Returns keyword to use for this custom shape



82
83
84
# File 'lib/glimmer/ui/custom_shape.rb', line 82

def keyword
  self.name.underscore.gsub('::', '__')
end

.namespaces_for_class(m) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/glimmer/ui/custom_shape.rb', line 97

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



130
131
132
133
134
135
# File 'lib/glimmer/ui/custom_shape.rb', line 130

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



119
120
121
122
123
124
125
126
127
128
# File 'lib/glimmer/ui/custom_shape.rb', line 119

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_shape_namespacesObject



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

def reset_custom_shape_namespaces
  @custom_shape_namespaces = Set[Object, Glimmer::UI]
end

.shortcut_keywordObject

Returns shortcut keyword to use for this custom shape (keyword minus namespace)



87
88
89
# File 'lib/glimmer/ui/custom_shape.rb', line 87

def shortcut_keyword
  self.name.underscore.gsub('::', '__').split('__').last
end

Instance Method Details

#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



243
244
245
# File 'lib/glimmer/ui/custom_shape.rb', line 243

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

#attribute_setter(attribute_name) ⇒ Object



236
237
238
# File 'lib/glimmer/ui/custom_shape.rb', line 236

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

#can_handle_observation_request?(observation_request) ⇒ Boolean

TODO consider bringing observer_registrations method from CustomWidget if needed

Returns:

  • (Boolean)


267
268
269
# File 'lib/glimmer/ui/custom_shape.rb', line 267

def can_handle_observation_request?(observation_request)
  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 shape



257
258
259
260
261
262
263
# File 'lib/glimmer/ui/custom_shape.rb', line 257

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

#dispose(dispose_images: true, dispose_patterns: true, redraw: true) ⇒ Object



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

def dispose(dispose_images: true, dispose_patterns: true, redraw: true)
  body_root.dispose(dispose_images: dispose_images, dispose_patterns: dispose_patterns, redraw: redraw)
#         @dispose_listener_registration.deregister # TODO enable once returning a true listener object or observer proc registration
end

#disposedObject Also known as: disposed?, is_disposed



293
294
295
# File 'lib/glimmer/ui/custom_shape.rb', line 293

def disposed
  body_root.disposed
end

#get_attribute(attribute_name) ⇒ Object



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

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

#get_data(key = nil) ⇒ Object Also known as: getData, data

Gets data just like SWT widgets



282
283
284
# File 'lib/glimmer/ui/custom_shape.rb', line 282

def get_data(key=nil)
  body_root.get_data(key)
end

#handle_observation_request(observation_request, &block) ⇒ Object



271
272
273
# File 'lib/glimmer/ui/custom_shape.rb', line 271

def handle_observation_request(observation_request, &block)
  body_root.handle_observation_request(observation_request, &block)
end

#has_attribute?(attribute_name, *args) ⇒ Boolean

Returns:

  • (Boolean)


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

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)


220
221
222
223
224
225
226
# File 'lib/glimmer/ui/custom_shape.rb', line 220

def has_instance_method?(method_name)
  respond_to?(method_name) and
    !body_root&.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/custom/shape.rb')
end

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

Raises:

  • (Glimmer::Error)


165
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
# File 'lib/glimmer/ui/custom_shape.rb', line 165

def initialize(parent, *args, 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')
  @args = args
  options ||= {}
  @options = self.class.options.merge(options)
  @content = Util::ProcTracker.new(content) if content
  execute_hook('before_body')
  body_block = self.class.instance_variable_get("@body_block")
  raise Glimmer::Error, 'Invalid custom shape for having no body! Please define body block!' if body_block.nil?
  @body_root = instance_exec(&body_block)
  raise Glimmer::Error, 'Invalid custom shape for having an empty body! Please fill body block!' if @body_root.nil?
  auto_exec do # TODO is this necessary given shape is a lightweight construct (not SWT widget) ?
    @body_root.set_data('custom_shape', self)
  end
  auto_exec do
    @dispose_listener_registration = @body_root.on_shape_disposed do
      unless @body_root.shell_proxy.last_shell_closing?
        observer_registrations.compact.each(&:deregister)
        observer_registrations.clear
      end
    end
  end
  execute_hook('after_body')
  post_add_content if content.nil?
end

#local_respond_to?Object



309
# File 'lib/glimmer/ui/custom_shape.rb', line 309

alias local_respond_to? respond_to?

#observer_registrationsObject



202
203
204
# File 'lib/glimmer/ui/custom_shape.rb', line 202

def observer_registrations
  @observer_registrations ||= []
end

#post_add_contentObject



198
199
200
# File 'lib/glimmer/ui/custom_shape.rb', line 198

def post_add_content
  SWT::DisplayProxy.current_custom_widgets_and_shapes.delete(self)
end

#post_initialize_child(child) ⇒ Object

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



194
195
196
# File 'lib/glimmer/ui/custom_shape.rb', line 194

def post_initialize_child(child)
  # No Op by default
end

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

Returns:

  • (Boolean)


310
311
312
313
314
# File 'lib/glimmer/ui/custom_shape.rb', line 310

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

#set_attribute(attribute_name, *args) ⇒ Object



211
212
213
214
215
216
217
# File 'lib/glimmer/ui/custom_shape.rb', line 211

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

#set_data(key = nil, value) ⇒ Object Also known as: setData

Sets data just like SWT widgets



276
277
278
# File 'lib/glimmer/ui/custom_shape.rb', line 276

def set_data(key=nil, value)
  body_root.set_data(key, value)
end

#sync_exec(&block) ⇒ Object



247
248
249
# File 'lib/glimmer/ui/custom_shape.rb', line 247

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

#timer_exec(delay_in_millis, &block) ⇒ Object



251
252
253
# File 'lib/glimmer/ui/custom_shape.rb', line 251

def timer_exec(delay_in_millis, &block)
  SWT::DisplayProxy.instance.timer_exec(delay_in_millis, &block)
end