Skip to content

Commit

Permalink
Hello, Computed! Sample functionality with entry/label data-binding s…
Browse files Browse the repository at this point in the history
…upport
  • Loading branch information
AndyObtiva committed Sep 15, 2020
1 parent 116c4a4 commit e5e16b8
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
38 changes: 38 additions & 0 deletions lib/glimmer/tk/entry_proxy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright (c) 2020 Andy Maleh
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

require 'glimmer/tk/widget_proxy'

module Glimmer
module Tk
# Proxy for Tk::Tile::TEntry
#
# Follows the Proxy Design Pattern
class EntryProxy < WidgetProxy
def validatecommand_block=(proc)
tk.validatecommand(proc)
end
def invalidcommand_block=(proc)
tk.invalidcommand(proc)
end
end
end
end
33 changes: 33 additions & 0 deletions lib/glimmer/tk/widget_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ class WidgetProxy
'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,
}

class << self
Expand Down Expand Up @@ -149,6 +155,18 @@ def widget_custom_attribute_mapping
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 }},
},
},
::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

Expand All @@ -166,6 +184,21 @@ def widget_attribute_listener_installers
}
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)
true
else
false
end
}
end,
},
}
end

Expand Down
45 changes: 45 additions & 0 deletions samples/hello/hello_computed.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'glimmer-dsl-tk'

require_relative 'hello_computed/contact'

class HelloComputed
include Glimmer

def initialize
@contact = Contact.new(
first_name: 'Barry',
last_name: 'McKibbin',
year_of_birth: 1985
)
end

def launch
root {
title 'Hello, Computed!'
frame {
label {text 'First Name: '}
entry {
text bind(@contact, :first_name)
}
label {text 'Last Name: '}
entry {
text bind(@contact, :last_name)
}
label {text 'Year of Birth: '}
entry {
text bind(@contact, :year_of_birth)
}
label {text 'Name: '}
label {
text bind(@contact, :name, computed_by: [:first_name, :last_name])
}
label {text 'Age: '}
label {
text bind(@contact, :age, on_write: :to_i, computed_by: [:year_of_birth])
}
}
}.open
end
end

HelloComputed.new.launch
21 changes: 21 additions & 0 deletions samples/hello/hello_computed/contact.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class HelloComputed
class Contact
attr_accessor :first_name, :last_name, :year_of_birth

def initialize(attribute_map)
@first_name = attribute_map[:first_name]
@last_name = attribute_map[:last_name]
@year_of_birth = attribute_map[:year_of_birth]
end

def name
"#{last_name}, #{first_name}"
end

def age
Time.now.year - year_of_birth.to_i
rescue
0
end
end
end

0 comments on commit e5e16b8

Please sign in to comment.