Skip to content

Commit 439e1d5

Browse files
Convert background as style to background as drawable
1 parent fb319e7 commit 439e1d5

File tree

11 files changed

+115
-39
lines changed

11 files changed

+115
-39
lines changed

examples/stack/background.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
Shoes.app do
22
stack width: 0.33 do
3-
background "purple"
3+
background "purple", :curve => 15
44
button "a button"
55
end
6-
stack width: 0.33 do
7-
background "red".."green"
6+
stack width: 0.33, :height => 20 do
7+
background "red".."green", :curve => 20, :margin => 20
88
para "Red to green gradient"
99
end
1010
stack width: 0.33 do

lacci/lib/shoes/background.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# frozen_string_literal: true
22

3-
class Shoes
4-
module Background
5-
def self.included(includer)
6-
includer.shoes_style(:background_color)
7-
end
3+
# class Shoes
4+
# module Background
5+
# def self.included(includer)
6+
# includer.shoes_style(:background_color)
7+
# end
88

9-
# NOTE: this needs to be passed through in order for the styling to work
10-
def background(color, options = {})
11-
self.background_color = color
12-
end
13-
end
14-
end
9+
# # NOTE: this needs to be passed through in order for the styling to work
10+
# def background(color, options = {})
11+
# self.background_color = color
12+
# end
13+
# end
14+
# end

lacci/lib/shoes/drawables.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@
3030
require "shoes/drawables/video"
3131
require "shoes/drawables/progress"
3232
require "shoes/drawables/border"
33+
require "shoes/drawables/background"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# class_template.erb
2+
# frozen_string_literal: true
3+
4+
class Shoes
5+
class Background < Shoes::Drawable
6+
7+
shoes_styles :fill
8+
9+
shoes_style(:curve) { |val| convert_to_integer(val, "curve") }
10+
11+
Shoes::Drawable.drawable_default_styles[Shoes::Border][:curve] = 0
12+
13+
init_args :fill
14+
opt_init_args :curve
15+
def initialize(*args, **kwargs)
16+
17+
super
18+
@draw_context = Shoes::App.instance.current_draw_context
19+
20+
create_display_drawable
21+
end
22+
23+
end
24+
end

lacci/lib/shoes/drawables/flow.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
class Shoes
44
class Flow < Shoes::Slot
5-
include Shoes::Background
6-
75
Shoes::Drawable.drawable_default_styles[Shoes::Flow][:width] = "100%"
86

97
shoes_events

lacci/lib/shoes/drawables/stack.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
class Shoes
44
class Stack < Shoes::Slot
5-
include Shoes::Background
6-
75
shoes_styles :scroll
86

97
shoes_events # No Stack-specific events

lacci/lib/shoes/drawables/widget.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
# widget propertly, sets the linkable_id, etc.
3737

3838
class Shoes::Widget < Shoes::Slot
39-
include Shoes::Background
40-
4139
shoes_events
4240

4341
def self.inherited(subclass)

lib/scarpe/wv.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,4 @@ class Scarpe::Webview::Drawable < Shoes::Linkable
106106
require_relative "wv/scarpe_extensions"
107107
require_relative "assets"
108108
require_relative "wv/border"
109+
require_relative "wv/background"

lib/scarpe/wv/background.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
module Scarpe::Webview
4+
class Background < Drawable
5+
6+
def initialize(properties)
7+
super
8+
end
9+
10+
# If the drawable is intended to be overridable, add element and style to Calzini instead
11+
def element
12+
render("background")
13+
end
14+
15+
private
16+
end
17+
end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
module Scarpe::Components::Calzini
2+
def background_element(props)
3+
HTML.render do |h|
4+
h.div(id: html_id, style: background_style(props))
5+
end
6+
end
7+
8+
private
9+
10+
def background_style(props)
11+
styles = {
12+
height: :inherit,
13+
width: :inherit,
14+
position: :absolute,
15+
top: 0,
16+
left: 0,
17+
'z-index': -99,
18+
'box-sizing': 'border-box',
19+
}
20+
21+
styles = drawable_style(props).merge(styles)
22+
23+
bc = props["fill"]
24+
return styles unless bc
25+
26+
variable_styles = case bc
27+
when Array
28+
{
29+
background: "rgba(#{bc.join(", ")})",
30+
'border-color': "rgba(#{bc.join(", ")})",
31+
'border-width': '1px',
32+
'border-radius': "#{props['curve']}px",
33+
}
34+
when Range
35+
{
36+
background: "linear-gradient(45deg, #{bc.first}, #{bc.last})",
37+
'border-color': "linear-gradient(45deg, #{bc.first}, #{bc.last})",
38+
'border-width': '1px',
39+
'border-radius': "#{props['curve']}px",
40+
}
41+
when ->(value) { File.exist?(value) }
42+
{
43+
background: "url(data:image/png;base64,#{encode_file_to_base64(bc)})"
44+
}
45+
else
46+
{
47+
background: bc,
48+
'border-color': bc,
49+
'border-width': '1px',
50+
'border-radius': "#{props['curve']}px",
51+
}
52+
end
53+
54+
# binding.irb
55+
56+
styles.merge(variable_styles)
57+
end
58+
end

0 commit comments

Comments
 (0)