Skip to content

Commit

Permalink
Clean up Lacci initialize() methods to properly handle shoes styles a…
Browse files Browse the repository at this point in the history
…nd keyword args
  • Loading branch information
noahgibbs committed Dec 4, 2023
1 parent 3074656 commit 2d4d07c
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 25 deletions.
6 changes: 5 additions & 1 deletion lacci/lib/shoes/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ def self.find_lib_dir
HALF_PI = 1.57079632679489661923
PI = 3.14159265358979323846

# This should be set up by the Display Service when it loads
# These should be set up by the Display Service when it loads. They are intentionally
# *not* frozen so that the Display Service can add to them (and then optionally
# freeze them.)

# Fonts currently loaded and available
FONTS = []
end

Expand Down
39 changes: 26 additions & 13 deletions lacci/lib/shoes/drawable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def dsl_name
end

def drawable_class_by_name(name)
drawable_classes.detect { |k| k.dsl_name == name.to_s }
name = name.to_s
drawable_classes.detect { |k| k.dsl_name == name }
end

def is_widget_class?(name)
Expand Down Expand Up @@ -125,6 +126,9 @@ def linkable_properties_hash
# Shoes styles in Shoes Linkables are automatically sync'd with the display side objects.
# If a block is passed to shoes_style, that's the validation for the property. It should
# convert a given value to a valid value for the property or throw an exception.
#
# @param name [String,Symbol] the style name
# @block if block is given, call it to map the given style value to a valid value, or raise an exception
def shoes_style(name, &validator)
name = name.to_s

Expand All @@ -134,9 +138,9 @@ def shoes_style(name, &validator)
linkable_properties_hash[name] = true
end

# Add these names as Shoes styles
def shoes_styles(*names)
names.each { |n| shoes_style(n) }
# Add these names as Shoes styles with the given validator, if any
def shoes_styles(*names, &validator)
names.each { |n| shoes_style(n, &validator) }
end

def shoes_style_names
Expand Down Expand Up @@ -166,16 +170,25 @@ def initialize(*args, **kwargs)
log_init("Shoes::#{self.class.name}")

default_styles = Shoes::Drawable.drawable_default_styles[self.class]
this_drawable_styles = self.class.shoes_style_names.map(&:to_sym)

self.class.shoes_style_names.each do |prop|
prop_sym = prop.to_sym
if kwargs.key?(prop_sym)
val = self.class.validate_as(prop, kwargs[prop_sym])
instance_variable_set("@" + prop, val)
elsif default_styles.key?(prop_sym)
val = self.class.validate_as(prop, default_styles[prop_sym])
instance_variable_set("@" + prop, val)
end
# No keyword arg specified for a property with a default value? Set it to default.
(default_styles.keys - kwargs.keys).each do |key|
val = self.class.validate_as(prop, default_styles[key])
instance_variable_set("@#{key}", val)
end

# If we have a keyword arg for a style, set it normally.
(this_drawable_styles & kwargs.keys).each do |key|
val = self.class.validate_as(key, kwargs[key])
instance_variable_set("@#{key}", val)
end

# We'd like to avoid unexpected keywords. But we're not disciplined enough to do
# this by default yet.
unexpected = (kwargs.keys - this_drawable_styles)
unless unexpected.empty?
STDERR.puts "Unexpected non-style keyword(s) in #{self.class} initialize: #{unexpected.inspect}"
end

super(linkable_id: Shoes::Drawable.allocate_drawable_id)
Expand Down
2 changes: 1 addition & 1 deletion lacci/lib/shoes/drawables/para.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class Shoes
class Para < Shoes::Drawable
shoes_styles :text_items, :size, :font, :html_attributes, :hidden
shoes_styles :text_items, :size, :font, :html_attributes
shoes_style(:stroke) { |val| Shoes::Colors.to_rgb(val) }

shoes_events # No Para-specific events yet
Expand Down
2 changes: 1 addition & 1 deletion lacci/lib/shoes/drawables/slot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def method_missing(name, *args, **kwargs, &block)
end

def respond_to_missing?(name, include_private = false)
return true if Drawable.drawable_class_by_name(name.to_s)
return true if ::Shoes::Drawable.drawable_class_by_name(name.to_s)

false
end
Expand Down
3 changes: 1 addition & 2 deletions lacci/lib/shoes/drawables/stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ class Stack < Shoes::Slot
include Shoes::Border
include Shoes::Spacing

# TODO: sort out various margin and padding properties, including putting stuff into spacing
shoes_styles :width, :height, :scroll

shoes_events # No Stack-specific events yet
shoes_events # No Stack-specific events

def initialize(width: nil, height: nil, margin: nil, padding: nil, scroll: false, margin_top: nil, margin_bottom: nil, margin_left: nil,
margin_right: nil, **options, &block)
Expand Down
14 changes: 7 additions & 7 deletions lacci/test/test_lacci.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
class TestLacci < NienteTest
def test_simple_button_click
run_test_niente_code(<<~SHOES_APP, app_test_code: <<~SHOES_SPEC)
Shoes.app do
@b = button "OK" do
@b.text = "Yup"
Shoes.app do
@b = button "OK" do
@b.text = "Yup"
end
end
end
SHOES_APP
button().trigger_click
assert_equal "Yup", button().text
SHOES_APP
button().trigger_click
assert_equal "Yup", button().text
SHOES_SPEC
end
end

0 comments on commit 2d4d07c

Please sign in to comment.