samples/hello/hello_custom_widget.rb
# Copyright (c) 2007-2024 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-dsl-swt'
# This class declares a `greeting_label` custom widget (by convention)
class GreetingLabel
include Glimmer::UI::CustomWidget
# multiple options without default values
options :name, :colors
# single option with default value
option :greeting, default: 'Hello'
# internal attribute (not a custom widget option)
attr_accessor :label_color
def can_handle_observation_request?(event, &block)
event.to_s == 'on_color_changed' || super
end
def handle_observation_request(event, &block)
if event.to_s == 'on_color_changed'
@color_changed_handlers ||= []
@color_changed_handlers << block
else
super
end
end
before_body do
@font = {height: 24, style: :bold}
@label_color = :black
end
after_body do
return if colors.nil?
@thread = Thread.new do
colors.cycle do |color|
self.label_color = color
@color_changed_handlers&.each {|handler| handler.call(color)}
sleep(1)
end
end
end
body {
# pass received swt style symbols through to label to customize (e.g. :center to center text)
label(*swt_style_symbols) {
text "#{greeting}, #{name}!"
font @font
foreground <=> [self, :label_color]
on_widget_disposed do
@thread&.kill # safe since it does not involve data
end
}
}
end
# including Glimmer enables the Glimmer DSL syntax, including auto-discovery of the `greeting_label` custom widget
include Glimmer
shell {
fill_layout :vertical
minimum_size 215, 215
text 'Hello, Custom Widget!'
# custom widget options are passed in a hash
greeting_label(name: 'Sean')
# pass :center SWT style followed by custom widget options hash
greeting_label(:center, name: 'Laura', greeting: 'Aloha') #
greeting_label(:right, name: 'Rick') {
# you can nest attributes under custom widgets just like any standard widget
foreground :red
}
# the colors option cycles between colors for the label foreground every second
greeting_label(:center, name: 'Mary', greeting: 'Aloha', colors: [:red, :dark_green, :blue]) {
on_color_changed do |color|
puts "Label color changed: #{color}"
end
}
}.open