Class: Glimmer::Tk::WidgetProxy
- Inherits:
-
Object
- Object
- Glimmer::Tk::WidgetProxy
- Defined in:
- lib/glimmer/tk/widget_proxy.rb
Overview
Proxy for Tk Widget objects
Follows the Proxy Design Pattern
Direct Known Subclasses
ButtonProxy, EntryProxy, FrameProxy, LabelProxy, ListProxy, NotebookProxy, RootProxy, TreeviewProxy
Constant Summary collapse
- DEFAULT_INITIALIZERS =
{ '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
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#keyword ⇒ Object
readonly
Returns the value of attribute keyword.
-
#parent_proxy ⇒ Object
readonly
Returns the value of attribute parent_proxy.
-
#tk ⇒ Object
readonly
Returns the value of attribute tk.
Class Method Summary collapse
- .create(keyword, parent, args) ⇒ Object
-
.tk_widget_class_for(underscored_widget_name) ⇒ Object
This supports widgets in and out of basic Tk.
- .widget_exists?(underscored_widget_name) ⇒ Boolean
- .widget_proxy_class(keyword) ⇒ Object
Instance Method Summary collapse
- #add_observer(observer, attribute) ⇒ Object
- #attribute_setter(attribute) ⇒ Object
- #content(&block) ⇒ Object
- #get_attribute(attribute) ⇒ Object
- #has_attribute?(attribute, *args) ⇒ Boolean
-
#initialize(underscored_widget_name, parent_proxy, args) ⇒ WidgetProxy
constructor
Initializes a new Tk Widget.
- #method_missing(method, *args, &block) ⇒ Object
-
#post_add_content ⇒ Object
Subclasses may override to perform post add_content work.
-
#post_initialize_child(child) ⇒ Object
Subclasses may override to perform post initialization work on an added child.
- #respond_to?(method, *args, &block) ⇒ Boolean
- #set_attribute(attribute, *args) ⇒ Object
- #tk_widget_has_attribute_getter_setter?(attribute) ⇒ Boolean
- #tk_widget_has_attribute_setter?(attribute) ⇒ Boolean
- #widget_attribute_listener_installers ⇒ Object
- #widget_custom_attribute_mapping ⇒ Object
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
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 81 def initialize(, parent_proxy, args) @parent_proxy = parent_proxy @args = args @keyword = = self.class.() @tk = .new(@parent_proxy.tk, *args) # a common widget initializer @tk.grid DEFAULT_INITIALIZERS[]&.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
223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 223 def method_missing(method, *args, &block) method = method.to_s if args.empty? && block.nil? && [tk.class] && [tk.class][method] get_attribute(method) elsif [tk.class] && [tk.class][method.sub(/=$/, '')] && method.end_with?('=') && block.nil? 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
#args ⇒ Object (readonly)
Returns the value of attribute args.
64 65 66 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 64 def args @args end |
#keyword ⇒ Object (readonly)
Returns the value of attribute keyword.
64 65 66 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 64 def keyword @keyword end |
#parent_proxy ⇒ Object (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 |
#tk ⇒ Object (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) (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 () = .camelcase(:upper) = [ "::Tk::Tile::#{tk_widget_class_basename}", "::Tk::#{tk_widget_class_basename}", "::Tk#{tk_widget_class_basename}", "::Glimmer::Tk::#{tk_widget_class_basename}Proxy", ] = nil .each do || begin = eval() break rescue RuntimeError, SyntaxError, NameError => e Glimmer::Config.logger.debug e. end end end |
.widget_exists?(underscored_widget_name) ⇒ Boolean
103 104 105 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 103 def self.() !!() end |
.widget_proxy_class(keyword) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 33 def (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
213 214 215 216 217 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 213 def add_observer(observer, attribute) attribute_listener_installers = @tk.class.ancestors.map {|ancestor| [ancestor]}.compact = attribute_listener_installers.map{|installer| installer[attribute.to_s]}.compact if !attribute_listener_installers.empty? .to_a.first&.call(observer) end |
#attribute_setter(attribute) ⇒ Object
154 155 156 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 154 def attribute_setter(attribute) "#{attribute}=" end |
#content(&block) ⇒ Object
219 220 221 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 219 def content(&block) Glimmer::DSL::Engine.add_content(self, Glimmer::DSL::Tk::WidgetExpression.new, keyword, *args, &block) end |
#get_attribute(attribute) ⇒ Object
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 143 def get_attribute(attribute) = [tk.class] && [tk.class][attribute.to_s] if [:getter][:invoker].call(@tk, args) elsif (attribute) @tk.send(attribute) else send(attribute) end end |
#has_attribute?(attribute, *args) ⇒ Boolean
123 124 125 126 127 128 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 123 def has_attribute?(attribute, *args) ([tk.class] && [tk.class][attribute.to_s]) || (attribute) || (attribute) || respond_to?(attribute_setter(attribute), args) end |
#post_add_content ⇒ Object
Subclasses may override to perform post add_content work
99 100 101 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 99 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
94 95 96 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 94 def post_initialize_child(child) # No Op by default end |
#respond_to?(method, *args, &block) ⇒ Boolean
237 238 239 240 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 237 def respond_to?(method, *args, &block) super || tk.respond_to?(method, *args, &block) end |
#set_attribute(attribute, *args) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 130 def set_attribute(attribute, *args) = [tk.class] && [tk.class][attribute.to_s] if [:setter][:invoker].call(@tk, args) elsif (attribute) @tk.send(attribute_setter(attribute), *args) unless @tk.send(attribute) == args.first elsif (attribute) @tk.send(attribute, *args) else send(attribute_setter(attribute), args) end end |
#tk_widget_has_attribute_getter_setter?(attribute) ⇒ Boolean
119 120 121 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 119 def (attribute) @tk.respond_to?(attribute) end |
#tk_widget_has_attribute_setter?(attribute) ⇒ Boolean
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 107 def (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_installers ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 181 def ||= { ::Tk::Tile::TCombobox => { 'text' => lambda do |observer| if observer.is_a?(Glimmer::DataBinding::ModelBinding) model = observer.model = observer.property_name + '_options' @tk.values = model.send() if model.respond_to?() 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_mapping ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 158 def ||= { ::Tk::Tile::TCombobox => { 'text' => { getter: {name: 'text', invoker: lambda { |, args| @tk.textvariable&.value }}, setter: {name: 'text=', invoker: lambda { |, args| @tk.textvariable&.value = args.first }}, }, }, ::Tk::Tile::TLabel => { 'text' => { getter: {name: 'text', invoker: lambda { |, args| @tk.textvariable&.value }}, setter: {name: 'text=', invoker: lambda { |, args| @tk.textvariable&.value = args.first }}, }, }, ::Tk::Tile::TEntry => { 'text' => { getter: {name: 'text', invoker: lambda { |, args| @tk.textvariable&.value }}, setter: {name: 'text=', invoker: lambda { |, args| @tk.textvariable&.value = args.first unless @text_variable_edit }}, }, }, } end |