Class: Glimmer::Tk::WidgetProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/glimmer/tk/widget_proxy.rb

Overview

Proxy for Tk Widget objects

Follows the Proxy Design Pattern

Constant Summary collapse

DEFAULT_INITIALIZERS =
{
  'checkbutton' => lambda do |tk|
    tk.variable = ::TkVariable.new
  end,
  'combobox' => lambda do |tk|
    tk.textvariable = ::TkVariable.new
  end,
  'label' => lambda do |tk|
    tk.textvariable = ::TkVariable.new
  end,
  'entry' => lambda do |tk|
    tk.textvariable = ::TkVariable.new
  end,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(underscored_widget_name, parent_proxy, args) ⇒ WidgetProxy

Initializes a new Tk Widget

Styles is a comma separate list of symbols representing Tk styles in lower case



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/glimmer/tk/widget_proxy.rb', line 84

def initialize(underscored_widget_name, parent_proxy, args)
  @parent_proxy = parent_proxy
  @args = args
  @keyword = underscored_widget_name
  tk_widget_class = self.class.tk_widget_class_for(underscored_widget_name)
  @tk = tk_widget_class.new(@parent_proxy.tk, *args)
  # a common widget initializer
  @tk.grid unless @tk.is_a?(::Tk::Toplevel)
  DEFAULT_INITIALIZERS[underscored_widget_name]&.call(@tk)
  @parent_proxy.post_initialize_child(self)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/glimmer/tk/widget_proxy.rb', line 330

def method_missing(method, *args, &block)
  method = method.to_s
  if args.empty? && block.nil? && ((widget_custom_attribute_mapping[tk.class] && widget_custom_attribute_mapping[tk.class][method]) || has_state?(method) || has_attributes_attribute?(method))
    get_attribute(method)
  elsif method.end_with?('=') && block.nil? && ((widget_custom_attribute_mapping[tk.class] && widget_custom_attribute_mapping[tk.class][method.sub(/=$/, '')]) || has_state?(method) || has_attributes_attribute?(method))
    set_attribute(method.sub(/=$/, ''), *args)
  else
    tk.send(method, *args, &block)
  end
rescue => e
  Glimmer::Config.logger.debug {"Neither WidgetProxy nor #{tk.class.name} can handle the method ##{method}"}
  super(method.to_sym, *args, &block)
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



64
65
66
# File 'lib/glimmer/tk/widget_proxy.rb', line 64

def args
  @args
end

#keywordObject (readonly)

Returns the value of attribute keyword.



64
65
66
# File 'lib/glimmer/tk/widget_proxy.rb', line 64

def keyword
  @keyword
end

#parent_proxyObject (readonly)

Returns the value of attribute parent_proxy.



64
65
66
# File 'lib/glimmer/tk/widget_proxy.rb', line 64

def parent_proxy
  @parent_proxy
end

#tkObject (readonly)

Returns the value of attribute tk.



64
65
66
# File 'lib/glimmer/tk/widget_proxy.rb', line 64

def tk
  @tk
end

Class Method Details

.create(keyword, parent, args) ⇒ Object



29
30
31
# File 'lib/glimmer/tk/widget_proxy.rb', line 29

def create(keyword, parent, args)
  widget_proxy_class(keyword).new(keyword, parent, args)
end

.tk_widget_class_for(underscored_widget_name) ⇒ Object

This supports widgets in and out of basic Tk



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/glimmer/tk/widget_proxy.rb', line 43

def tk_widget_class_for(underscored_widget_name)
  tk_widget_class_basename = underscored_widget_name.camelcase(:upper)
  potential_tk_widget_class_names = [
    "::Tk::Tile::#{tk_widget_class_basename}",
    "::Tk::#{tk_widget_class_basename}",
    "::Tk#{tk_widget_class_basename}",
    "::Glimmer::Tk::#{tk_widget_class_basename}Proxy",
  ]
  tk_widget_class = nil
  potential_tk_widget_class_names.each do |tk_widget_name|
    begin
      tk_widget_class = eval(tk_widget_name)
      break
    rescue RuntimeError, SyntaxError, NameError => e
      Glimmer::Config.logger.debug e.full_message
    end
  end
  tk_widget_class
end

.widget_exists?(underscored_widget_name) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/glimmer/tk/widget_proxy.rb', line 106

def self.widget_exists?(underscored_widget_name)
  !!tk_widget_class_for(underscored_widget_name)
end

.widget_proxy_class(keyword) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/glimmer/tk/widget_proxy.rb', line 33

def widget_proxy_class(keyword)
  begin
    class_name = "#{keyword.camelcase(:upper)}Proxy".to_sym
    Glimmer::Tk.const_get(class_name)
  rescue
    Glimmer::Tk::WidgetProxy
  end
end

Instance Method Details

#add_observer(observer, attribute) ⇒ Object



308
309
310
311
312
# File 'lib/glimmer/tk/widget_proxy.rb', line 308

def add_observer(observer, attribute)
  attribute_listener_installers = @tk.class.ancestors.map {|ancestor| widget_attribute_listener_installers[ancestor]}.compact
  widget_listener_installers = attribute_listener_installers.map{|installer| installer[attribute.to_s]}.compact if !attribute_listener_installers.empty?
  widget_listener_installers.to_a.first&.call(observer)
end

#attribute_setter(attribute) ⇒ Object



206
207
208
# File 'lib/glimmer/tk/widget_proxy.rb', line 206

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

#content(&block) ⇒ Object



326
327
328
# File 'lib/glimmer/tk/widget_proxy.rb', line 326

def content(&block)
  Glimmer::DSL::Engine.add_content(self, Glimmer::DSL::Tk::WidgetExpression.new, keyword, *args, &block)
end

#get_attribute(attribute) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/glimmer/tk/widget_proxy.rb', line 189

def get_attribute(attribute)
  widget_custom_attribute = widget_custom_attribute_mapping[tk.class] && widget_custom_attribute_mapping[tk.class][attribute.to_s]
  if widget_custom_attribute
    widget_custom_attribute[:getter][:invoker].call(@tk, args)
  elsif tk_widget_has_attribute_getter_setter?(attribute)
    @tk.send(attribute)
  elsif has_state?(attribute)
    @tk.tile_instate(attribute.sub(/\?$/, ''))
  elsif has_attributes_attribute?(attribute)
    result = @tk.attributes[attribute.sub(/\?$/, '')]
    result = result == 1 if result.is_a?(Integer)
    result
  else
    send(attribute)
  end
end

#handle_listener(listener_name, &listener) ⇒ Object



314
315
316
317
318
319
320
321
322
323
324
# File 'lib/glimmer/tk/widget_proxy.rb', line 314

def handle_listener(listener_name, &listener)
  listener_name = listener_name.to_s
  begin
    @tk.bind(listener_name, &listener)
  rescue => e
    Glimmer::Config.logger.debug {e.full_message}
    listener_name = "<#{listener_name}" if !listener_name.start_with?('<')
    listener_name = "#{listener_name}>" if !listener_name.end_with?('>')
    @tk.bind(listener_name, &listener)
  end
end

#has_attribute?(attribute, *args) ⇒ Boolean

Returns:

  • (Boolean)


151
152
153
154
155
156
157
158
# File 'lib/glimmer/tk/widget_proxy.rb', line 151

def has_attribute?(attribute, *args)
  (widget_custom_attribute_mapping[tk.class] and widget_custom_attribute_mapping[tk.class][attribute.to_s]) or
    tk_widget_has_attribute_setter?(attribute) or
    tk_widget_has_attribute_getter_setter?(attribute) or
    has_state?(attribute) or
    has_attributes_attribute?(attribute) or
    respond_to?(attribute_setter(attribute), args)
end

#has_attributes_attribute?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
149
# File 'lib/glimmer/tk/widget_proxy.rb', line 146

def has_attributes_attribute?(attribute)
  attribute = attribute.sub(/\?$/, '').sub(/=$/, '')
  @tk.respond_to?(:attributes) && @tk.attributes.keys.include?(attribute.to_s)
end

#has_state?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/glimmer/tk/widget_proxy.rb', line 132

def has_state?(attribute)
  attribute = attribute.sub(/\?$/, '').sub(/=$/, '')
  if @tk.respond_to?(:tile_state)
    begin
      @tk.tile_instate(attribute)
      true
    rescue
      false
    end
  else
    false
  end
end

#image_argument(args) ⇒ Object



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/glimmer/tk/widget_proxy.rb', line 292

def image_argument(args)
  if args.first.is_a?(::TkPhotoImage)
    args.first
  else
    image_args = {}
    image_args.merge!(file: args.first.to_s) if args.first.is_a?(String)
    the_image = ::TkPhotoImage.new(image_args)
    if args.last.is_a?(Hash)
      processed_image = ::TkPhotoImage.new
      processed_image.copy(the_image, args.last)
      the_image = processed_image
    end
    the_image
  end
end

#post_add_contentObject

Subclasses may override to perform post add_content work



102
103
104
# File 'lib/glimmer/tk/widget_proxy.rb', line 102

def post_add_content
  # No Op by default
end

#post_initialize_child(child) ⇒ Object

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



97
98
99
# File 'lib/glimmer/tk/widget_proxy.rb', line 97

def post_initialize_child(child)
  # No Op by default
end

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

Returns:

  • (Boolean)


344
345
346
347
# File 'lib/glimmer/tk/widget_proxy.rb', line 344

def respond_to?(method, *args, &block)
  super ||
    tk.respond_to?(method, *args, &block)
end

#set_attribute(attribute, *args) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/glimmer/tk/widget_proxy.rb', line 160

def set_attribute(attribute, *args)
  widget_custom_attribute = widget_custom_attribute_mapping[tk.class] && widget_custom_attribute_mapping[tk.class][attribute.to_s]
  if widget_custom_attribute
    widget_custom_attribute[:setter][:invoker].call(@tk, args)
  elsif tk_widget_has_attribute_setter?(attribute)
    unless args.size == 1 && @tk.send(attribute) == args.first
      if args.size == 1
        @tk.send(attribute_setter(attribute), *args)
      else
        @tk.send(attribute_setter(attribute), args)
      end
    end
  elsif tk_widget_has_attribute_getter_setter?(attribute)
    @tk.send(attribute, *args)
  elsif has_state?(attribute)
    attribute = attribute.sub(/=$/, '')
    if !!args.first
      @tk.tile_state(attribute)
    else
      @tk.tile_state("!#{attribute}")
    end
  elsif has_attributes_attribute?(attribute)
    attribute = attribute.sub(/=$/, '')
    @tk.attributes(attribute, args.first)
  else
    send(attribute_setter(attribute), args)
  end
end

#tk_widget_has_attribute_getter_setter?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
125
126
127
128
129
130
# File 'lib/glimmer/tk/widget_proxy.rb', line 122

def tk_widget_has_attribute_getter_setter?(attribute)
  begin
    # TK Widget currently doesn't support respond_to? properly, so I have to resort to this trick for now
    @tk.send(attribute)
    true
  rescue
    false
  end
end

#tk_widget_has_attribute_setter?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
113
114
115
116
117
118
119
120
# File 'lib/glimmer/tk/widget_proxy.rb', line 110

def tk_widget_has_attribute_setter?(attribute)
  result = nil
  begin
    # TK Widget currently doesn't support respond_to? properly, so I have to resort to this trick for now
    @tk.send(attribute_setter(attribute), @tk.send(attribute))
    result = true
  rescue => e
    result = false
  end
  result
end

#widget_attribute_listener_installersObject



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/glimmer/tk/widget_proxy.rb', line 253

def widget_attribute_listener_installers
  @tk_widget_attribute_listener_installers ||= {
    ::Tk::Tile::TCheckbutton => {
      'variable' => lambda do |observer|
        @tk.command {
          observer.call(@tk.variable.value.to_i == 1)
        }
      end,
    },
    ::Tk::Tile::TCombobox => {
      'text' => lambda do |observer|
        if observer.is_a?(Glimmer::DataBinding::ModelBinding)
          model = observer.model
          options_model_property = observer.property_name + '_options'
          @tk.values = model.send(options_model_property) if model.respond_to?(options_model_property)
        end
        @tk.bind('<ComboboxSelected>') {
          observer.call(@tk.textvariable.value)
        }
      end,
    },
    ::Tk::Tile::TEntry => {
      'text' => lambda do |observer|
        tk.validate = 'key'
        tk.validatecommand { |new_tk_variable|
          @text_variable_edit = new_tk_variable.value != @tk.textvariable.value
          if @text_variable_edit
            observer.call(new_tk_variable.value)
            @text_variable_edit = nil
            true
          else
            false
          end
        }
      end,
    },
  }
end

#widget_custom_attribute_mappingObject



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/glimmer/tk/widget_proxy.rb', line 210

def widget_custom_attribute_mapping
  @widget_custom_attribute_mapping ||= {
    ::Tk::Tile::TButton => {
      'image' => {
        getter: {name: 'image', invoker: lambda { |widget, args| @tk.image }},
        setter: {name: 'image=', invoker: lambda { |widget, args| @tk.image = image_argument(args) }},
      },
    },
    ::Tk::Tile::TCheckbutton => {
      'image' => {
        getter: {name: 'image', invoker: lambda { |widget, args| @tk.image }},
        setter: {name: 'image=', invoker: lambda { |widget, args| @tk.image = image_argument(args) }},
      },
      'variable' => {
        getter: {name: 'variable', invoker: lambda { |widget, args| @tk.variable&.value.to_i == 1 }},
        setter: {name: 'variable=', invoker: lambda { |widget, args| @tk.variable&.value = args.first.is_a?(Integer) ? args.first : (args.first ? 1 : 0) }},
      },
    },
    ::Tk::Tile::TCombobox => {
      'text' => {
        getter: {name: 'text', invoker: lambda { |widget, args| @tk.textvariable&.value }},
        setter: {name: 'text=', invoker: lambda { |widget, args| @tk.textvariable&.value = args.first }},
      },
    },
    ::Tk::Tile::TLabel => {
      'text' => {
        getter: {name: 'text', invoker: lambda { |widget, args| @tk.textvariable&.value }},
        setter: {name: 'text=', invoker: lambda { |widget, args| @tk.textvariable&.value = args.first }},
      },
      'image' => {
        getter: {name: 'image', invoker: lambda { |widget, args| @tk.image }},
        setter: {name: 'image=', invoker: lambda { |widget, args| @tk.image = image_argument(args) }},
      },
    },
    ::Tk::Tile::TEntry => {
      'text' => {
        getter: {name: 'text', invoker: lambda { |widget, args| @tk.textvariable&.value }},
        setter: {name: 'text=', invoker: lambda { |widget, args| @tk.textvariable&.value = args.first unless @text_variable_edit }},
      },
    },
  }
end