Module: Glimmer::UI::CustomWidget

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DataBinding::ObservableModel

#add_property_writer_observers, #array_object_observer_for, #ensure_array_object_observer, #has_observer?, #has_observer_for_any_property?, #notify_observers, #property_observer_hash, #property_observer_list, #remove_observer, #unregister_dependent_observers

Methods included from DataBinding::Observable

#remove_observer

Instance Attribute Details

#body_rootObject (readonly)

Returns the value of attribute body_root.



104
105
106
# File 'lib/glimmer/ui/custom_widget.rb', line 104

def body_root
  @body_root
end

#optionsObject (readonly)

Returns the value of attribute options.



104
105
106
# File 'lib/glimmer/ui/custom_widget.rb', line 104

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent.



104
105
106
# File 'lib/glimmer/ui/custom_widget.rb', line 104

def parent
  @parent
end

#swt_styleObject (readonly)

Returns the value of attribute swt_style.



104
105
106
# File 'lib/glimmer/ui/custom_widget.rb', line 104

def swt_style
  @swt_style
end

#swt_widgetObject (readonly)

Returns the value of attribute swt_widget.



104
105
106
# File 'lib/glimmer/ui/custom_widget.rb', line 104

def swt_widget
  @swt_widget
end

Class Method Details

.after_body(&block) ⇒ Object



98
99
100
101
# File 'lib/glimmer/ui/custom_widget.rb', line 98

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

.before_body(&block) ⇒ Object



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

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

.body(&block) ⇒ Object

TODO rename to content as well as before and after blocks



94
95
96
# File 'lib/glimmer/ui/custom_widget.rb', line 94

def body(&block)
  @body_block = block
end

.def_option_attr_readers(new_options) ⇒ Object



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

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

.for(underscored_custom_widget_name) ⇒ Object



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
48
49
50
51
52
# File 'lib/glimmer/ui/custom_widget.rb', line 22

def for(underscored_custom_widget_name)
  namespaces = underscored_custom_widget_name.
    to_s.
    split(/__/).map do |namespace|
      namespace.camelcase(:upper)
    end
  #TODO update code to avoid using reduce and going through all of them, yet stop right when it finds something
  custom_widget_class = [Object, Glimmer::UI].reduce([]) do |found, base|
    if found.empty?
      found << namespaces.reduce(base) do |result, namespace|
        namespace = namespace.to_sym
        if !result.constants.include?(namespace)
          namespace = result.constants.detect {|c| c.to_s.upcase == namespace.to_s.upcase } || namespace
        end
        begin
          result.const_get(namespace)
        rescue => e
          # Glimmer.logger&.debug "#{e.message}\n#{e.backtrace.join("\n")}"
          result
        end
      end
    end
    found - [Object, Glimmer::UI]
  end.first
  raise "#{underscored_custom_widget_name} has no custom widget class!" if custom_widget_class.nil?
  custom_widget_class if custom_widget_class.ancestors.include?(Glimmer::UI::CustomWidget)
rescue => e
  Glimmer.logger&.debug e.message
  Glimmer.logger&.debug "#{e.message}\n#{e.backtrace.join("\n")}"
  nil
end

.option(new_option, new_option_default = nil) ⇒ Object



71
72
73
74
75
76
# File 'lib/glimmer/ui/custom_widget.rb', line 71

def option(new_option, new_option_default = nil)
  new_option = new_option.to_s.to_sym
  new_options = {new_option => new_option_default}
  @options = options.merge(new_options)
  def_option_attr_readers(new_options)
end

.options(*new_options) ⇒ Object

Allows defining convenience option readers 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



60
61
62
63
64
65
66
67
68
69
# File 'lib/glimmer/ui/custom_widget.rb', line 60

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_readers(new_options)
  end
end

Instance Method Details

#add_observer(observer, attribute_name) ⇒ Object



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

def add_observer(observer, attribute_name)
  if respond_to?(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



183
184
185
# File 'lib/glimmer/ui/custom_widget.rb', line 183

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

#attribute_setter(attribute_name) ⇒ Object



171
172
173
# File 'lib/glimmer/ui/custom_widget.rb', line 171

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

#can_add_observer?(attribute_name) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/glimmer/ui/custom_widget.rb', line 138

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

#can_handle_observation_request?(observation_request) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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

#disposeObject



201
202
203
# File 'lib/glimmer/ui/custom_widget.rb', line 201

def dispose
  body_root.dispose
end

#get_attribute(attribute_name) ⇒ Object



163
164
165
166
167
168
169
# File 'lib/glimmer/ui/custom_widget.rb', line 163

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

#handle_observation_request(observation_request, &block) ⇒ Object



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

def handle_observation_request(observation_request, &block)
  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)


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

def has_attribute?(attribute_name, *args)
  respond_to?(attribute_setter(attribute_name), args) ||
    @body_root.has_attribute?(attribute_name, *args)
end

#has_style?(style) ⇒ Boolean

Returns:

  • (Boolean)


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

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

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

Raises:



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/glimmer/ui/custom_widget.rb', line 106

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)
  execute_hooks('after_body')
  @swt_widget = @body_root.swt_widget
end

#set_attribute(attribute_name, *args) ⇒ Object



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

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

#sync_exec(&block) ⇒ Object



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

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