From 7eb36b0ebd4d69a805ef9598e65d77addc97104e Mon Sep 17 00:00:00 2001 From: Nj221102 Date: Tue, 13 Feb 2024 22:45:19 +0530 Subject: [PATCH] Fixed the margin shorthand --- examples/font_family.rb | 6 +- examples/margin.rb | 13 +++ examples/margin_check.rb | 2 +- examples/spacing.rb | 2 +- lacci/lib/shoes/drawable.rb | 5 +- lacci/lib/shoes/margin_helper.rb | 57 +++++++++++++ lacci/test/test_margin_helper.rb | 82 +++++++++++++++++++ .../lib/scarpe/components/calzini.rb | 8 +- .../test/calzini/test_calzini_slots.rb | 48 +---------- test/test_stack.rb | 11 --- test/wv/html_fixtures/font_family.html | 10 +-- test/wv/html_fixtures/margin.html | 11 +++ 12 files changed, 183 insertions(+), 72 deletions(-) create mode 100644 examples/margin.rb create mode 100644 lacci/lib/shoes/margin_helper.rb create mode 100644 lacci/test/test_margin_helper.rb create mode 100644 test/wv/html_fixtures/margin.html diff --git a/examples/font_family.rb b/examples/font_family.rb index be818583f..94111ed4f 100644 --- a/examples/font_family.rb +++ b/examples/font_family.rb @@ -1,16 +1,14 @@ Shoes.app do - font "#{DIR}/fonts/Pacifico.ttf" - para "This is arial" ,size:"40px" , font:"arial" para "This is time new roman" , size:"40px", font:"'Times New Roman'" para "this is cursive", font: "cursive", size:"40px" - para "this is pacifico", font: "'pacifico'", size:"40px" + para "this is pacifico", font: "'Monaco'", size:"40px" - para "this is pacifico with quotes which is same", font: "pacifico", size:"40px" + para "this is pacifico with quotes which is same", font: "Monaco", size:"40px" para "This is helvetica", font: "Helvetica", size:"40px" diff --git a/examples/margin.rb b/examples/margin.rb new file mode 100644 index 000000000..672430a49 --- /dev/null +++ b/examples/margin.rb @@ -0,0 +1,13 @@ +Shoes.app do + + para "All margins with array input", margin:[60,30,80,40] + + para "All margins with string input", margin: "30 20 10 20" + + para "One Number to set all margins", margin:20 + + para "Specific property can overwrite shorthand" , margin:[20,10,20,30], margin_top:100 + + para "margins using a hash input" , margin:{left:20,top:100,bottom:10,right:20} + +end \ No newline at end of file diff --git a/examples/margin_check.rb b/examples/margin_check.rb index be79901c8..eeef7ae57 100644 --- a/examples/margin_check.rb +++ b/examples/margin_check.rb @@ -3,7 +3,7 @@ @stack1 = stack(width: 100, height: 100) do background red - b = button("Push me", margin: [10, 5, 25, 10]) do + b = button("Push me", margin: [10, 25, 5, 10]) do alert "Aha! Click!" end end diff --git a/examples/spacing.rb b/examples/spacing.rb index 219984d00..2e3e6f7b9 100644 --- a/examples/spacing.rb +++ b/examples/spacing.rb @@ -7,7 +7,7 @@ background "red" para "with 10px margin-left and margin-right, and 20px margin-bottom", stroke: "white" end - stack margin: [15, 15, nil, 40] do + stack margin: [15, nil, 15, 40] do background "blue" para "with 15px margin-left and margin-right, and 40px margin-bottom", stroke: "white" end diff --git a/lacci/lib/shoes/drawable.rb b/lacci/lib/shoes/drawable.rb index 386e8f0a6..05fba12bf 100644 --- a/lacci/lib/shoes/drawable.rb +++ b/lacci/lib/shoes/drawable.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true - +require_relative 'margin_helper' class Shoes # Shoes::Drawable # @@ -256,7 +256,10 @@ def shoes_style_name?(name) # Their value is set at drawable-create time. DRAW_CONTEXT_STYLES = [:fill, :stroke, :strokewidth, :rotate, :transform, :translate] + include MarginHelper + def initialize(*args, **kwargs) + kwargs = margin_parse(kwargs) log_init("Shoes::#{self.class.name}") unless @log # First, get the list of allowed and disallowed styles for the given features diff --git a/lacci/lib/shoes/margin_helper.rb b/lacci/lib/shoes/margin_helper.rb new file mode 100644 index 000000000..e38efeb4a --- /dev/null +++ b/lacci/lib/shoes/margin_helper.rb @@ -0,0 +1,57 @@ +module MarginHelper + + def margin_parse(kwargs) + + if kwargs[:margin] + + if kwargs[:margin].is_a?(Numeric) + + return kwargs + elsif kwargs[:margin].is_a?(Hash) + + kwargs[:margin].each do |key,value| + kwargs[:"margin_#{key}"] = value + end + + else + margin_props = kwargs[:margin].is_a?(String) ? kwargs[:margin].split(/\s+|\,|-/) : kwargs[:margin] + if margin_props.length == 1 + + kwargs[:margin] = margin_props[0] + return kwargs + + elsif margin_props.length == 2 + + raise(Shoes::Errors::InvalidAttributeValueError, "Margin don't support 2-3 values as Array/string input for using 2-3 input you can use the hash input method like '{left:value, right:value, top:value, bottom:value}'") + + elsif margin_props.length == 3 + + raise(Shoes::Errors::InvalidAttributeValueError, "Margin don't support 2-3 values as Array/string input for using 2-3 input you can use the hash input method like '{left:value,right:value,top:value,bottom:value}'") + + else + + if !kwargs[:margin_top] + kwargs[:margin_top] = margin_props[1] + end + if !kwargs[:margin_bottom] + kwargs[:margin_bottom] = margin_props[3] + end + if !kwargs[:margin_left] + kwargs[:margin_left] = margin_props[0] + end + if !kwargs[:margin_right] + kwargs[:margin_right] = margin_props[2] + end + + end + end + kwargs[:margin] = nil + end + if kwargs["options"] && !kwargs[:margin] && !kwargs[:margin_left] && !kwargs[:margin_right] && !kwargs[:margin_top] && !kwargs[:margin_bottom] + kwargs[options].each do |key,value| + kwargs[key] = value + end + end + kwargs + end +end diff --git a/lacci/test/test_margin_helper.rb b/lacci/test/test_margin_helper.rb new file mode 100644 index 000000000..825ea821c --- /dev/null +++ b/lacci/test/test_margin_helper.rb @@ -0,0 +1,82 @@ +# frozen_string_literal: true + +load "lacci/lib/shoes/margin_helper.rb" + + +class TestMarginHelper < Minitest::Test +include MarginHelper + + def test_one_Number_margin + + kwargs = {:margin => 20} + + assert_equal({:margin => 20},margin_parse(kwargs)) + + end + + def test_Array_four_margin + + kwargs = {:margin => [20,20,30,40]} + + assert_equal({:margin => nil, :margin_left => 20, :margin_top => 20, :margin_right => 30, :margin_bottom => 40},margin_parse(kwargs)) + + end + + def test_Array_one_margin + + kwargs = {:margin => [20]} + + assert_equal({:margin => 20},margin_parse(kwargs)) + + end + + + def test_String_four_margin + + kwargs = {:margin => "20 30 40 50"} + + assert_equal({:margin => nil, :margin_left => "20", :margin_top => "30", :margin_right => "40", :margin_bottom => "50"},margin_parse(kwargs)) + + end + + def test_String_one_margin + + kwargs = {:margin => "20"} + + assert_equal({:margin => "20"},margin_parse(kwargs)) + + end + + def test_Hash_four_margin + + kwargs = {:margin => {left:20,top:20,right:30,bottom:30}} + + assert_equal({:margin => nil, :margin_left => 20, :margin_top => 20, :margin_right => 30, :margin_bottom => 30},margin_parse(kwargs)) + + end + + def test_Hash_three_margin + + kwargs = {:margin => {top:20,right:30,bottom:30}} + + assert_equal({:margin => nil, :margin_top => 20, :margin_right => 30, :margin_bottom => 30},margin_parse(kwargs)) + + end + + def test_Hash_two_margin + + kwargs = {:margin => {left:20,top:20}} + + assert_equal({:margin => nil, :margin_left => 20, :margin_top => 20},margin_parse(kwargs)) + + end + + def test_Hash_one_margin + + kwargs = {:margin => {bottom:30}} + + assert_equal({:margin => nil, :margin_bottom => 30},margin_parse(kwargs)) + + end + +end diff --git a/scarpe-components/lib/scarpe/components/calzini.rb b/scarpe-components/lib/scarpe/components/calzini.rb index 874fac33a..d646a24fb 100644 --- a/scarpe-components/lib/scarpe/components/calzini.rb +++ b/scarpe-components/lib/scarpe/components/calzini.rb @@ -126,8 +126,14 @@ def drawable_style(props) styles[:left] = dimensions_length(props["left"]) if props["left"] styles[:width] = dimensions_length(props["width"]) if props["width"] styles[:height] = dimensions_length(props["height"]) if props["height"] + styles[:"margin"] = dimensions_length(props["margin"]) if props["margin"] + styles[:"margin-left"] = dimensions_length(props["margin_left"]) if props["margin_left"] + styles[:"margin-right"] = dimensions_length(props["margin_right"]) if props["margin_right"] + styles[:"margin-top"] = dimensions_length(props["margin_top"]) if props["margin_top"] + styles[:"margin-bottom"] = dimensions_length(props["margin_bottom"]) if props["margin_bottom"] - styles = spacing_styles_for_attr("margin", props, styles) + + styles = spacing_styles_for_attr("padding", props, styles) styles diff --git a/scarpe-components/test/calzini/test_calzini_slots.rb b/scarpe-components/test/calzini/test_calzini_slots.rb index 7b144e1fd..137ce923f 100644 --- a/scarpe-components/test/calzini/test_calzini_slots.rb +++ b/scarpe-components/test/calzini/test_calzini_slots.rb @@ -59,26 +59,6 @@ def test_stack_border_attrs @calzini.render("stack", { "border_color" => "red", "options" => { "strokewidth" => 3, "curve" => 2 } }) { "contents" } end - def test_stack_margin - assert_equal %{
} + - %{#{@inner_div_tag}contents
}, - @calzini.render("stack", { "margin" => 25 }) { "contents" } - end - - def test_stack_options_margin - assert_equal %{
} + - %{#{@inner_div_tag}contents
}, - @calzini.render("stack", { "options" => { "margin" => 15 } }) { "contents" } - end - - def test_stack_margin_overrides_options - assert_equal %{
} + - %{#{@inner_div_tag}contents
}, - @calzini.render("stack", { "margin" => 25, "options" => { "margin" => 15 } }) { "contents" } - end def test_stack_padding assert_equal %{
25, "options" => { "padding" => 15 } }) { "contents" } end - def test_stack_options_hash_margin - assert_equal %{
} + - %{#{@inner_div_tag}contents
}, - @calzini.render("stack", { "margin" => { left: 5, right: 10, top: 15, bottom: 20 } }) { "contents" } - end - - def test_stack_options_array_margin - assert_equal %{
} + - %{#{@inner_div_tag}contents
}, - @calzini.render("stack", { "margin" => [5, 10, 15, 20] }) { "contents" } - end - - def test_stack_options_margin_left_overrides_options - assert_equal %{
} + - %{#{@inner_div_tag}contents
}, - @calzini.render("stack", { "margin_left" => 25, "options" => { "margin" => 15 } }) { "contents" } - end - - def test_stack_margin_left_overrides_margin - assert_equal %{
} + - %{#{@inner_div_tag}contents
}, - @calzini.render("stack", { "margin" => 25, "margin_left" => 15 }) { "contents" } - end + end diff --git a/test/test_stack.rb b/test/test_stack.rb index 9ee0a851f..f14a66d82 100644 --- a/test/test_stack.rb +++ b/test/test_stack.rb @@ -25,17 +25,6 @@ def test_it_accepts_margin assert_includes stack.to_html, "margin:25px" end - def test_it_accepts_margin_array - stack = Scarpe::Webview::Stack.new(@default_properties.merge("margin" => [1, 2, 3, 4])) - - assert_includes stack.to_html, "margin-left:1px;margin-right:2px;margin-top:3px;margin-bottom:4px" - end - - def test_it_accepts_margin_hash - stack = Scarpe::Webview::Stack.new(@default_properties.merge("margin" => { left: 1, bottom: 4 })) - - assert_includes stack.to_html, "margin-left:1px;margin-bottom:4px" - end #def test_it_can_have_a_background # stack = Scarpe::Stack.new do diff --git a/test/wv/html_fixtures/font_family.html b/test/wv/html_fixtures/font_family.html index fe73aded1..9969e6d55 100644 --- a/test/wv/html_fixtures/font_family.html +++ b/test/wv/html_fixtures/font_family.html @@ -3,13 +3,11 @@

This is arial

This is time new roman

this is cursive

-

this is pacifico

-

this is pacifico with quotes which is same

+

this is pacifico

+

this is pacifico with quotes which is same

This is helvetica

This is 'Trebuchet MS'

-
+
- + \ No newline at end of file diff --git a/test/wv/html_fixtures/margin.html b/test/wv/html_fixtures/margin.html new file mode 100644 index 000000000..1962496de --- /dev/null +++ b/test/wv/html_fixtures/margin.html @@ -0,0 +1,11 @@ +
+
+

All margins with array input

+

All margins with string input

+

One Number to set all margins

+

Specific property can overwrite shorthand

+

margins using a hash input

+
+
+
+
\ No newline at end of file