1
1
defmodule Scenic.Component.Button do
2
+ @ default_radius 3
3
+ @ default_font :roboto
4
+ @ default_font_size 20
5
+ @ default_alignment :center
6
+
2
7
@ moduledoc """
3
8
Add a button to a graph
4
9
@@ -51,17 +56,30 @@ defmodule Scenic.Component.Button do
51
56
The default is `false`.
52
57
* `:theme` - The color set used to draw. See below. The default is `:primary`
53
58
54
- ## Additional Styles
59
+ ## Sendable Styles
60
+ Styles can be sent directly to the Button Component by adding a :styles list.
55
61
56
- Buttons honor the following list of additional styles.
62
+ graph
63
+ |> button(
64
+ "Example",
65
+ styles: [font_size: 32, text_align: :right]
66
+ )
67
+
68
+ The following standard styles are supported
69
+
70
+ * `:font` - The default is #{ inspect ( @ default_font ) }
71
+ * `:font_size` - The default is #{ inspect ( @ default_font_size ) }
72
+ * `:text_align` - The default is #{ inspect ( @ default_alignment ) }
73
+
74
+
75
+ ## Options
76
+
77
+ Buttons the following options.
57
78
58
79
* `:width` - :auto (default) or pass in a number to set the width of the button
59
80
* `:height` - :auto (default) or pass in a number to set the height of the button.
60
81
* `:radius` - pass in a number to set the radius of the button's rounded
61
- rectangle.
62
- * `:alignment` - set the alignment of the text inside the button. Can be one
63
- of `:left, :right, :center`. The default is `:center`.
64
- * `:button_font_size` - the size of the font in the button
82
+ rectangle. The default is #{ inspect ( @ default_radius ) }
65
83
66
84
Buttons do not use the inherited `:font_size` style as they should look
67
85
consistent regardless of what size the surrounding text is.
@@ -96,6 +114,18 @@ defmodule Scenic.Component.Button do
96
114
97
115
graph
98
116
|> button("Example", id: :button_id, translate: {20, 20}, theme: :warning)
117
+
118
+ The final example changes the text size and alignment
119
+
120
+ graph
121
+ |> button(
122
+ "Example",
123
+ id: :button_id,
124
+ translate: {20, 20},
125
+ theme: :warning,
126
+ styles: [text_size: 32, text_align: :right]
127
+ )
128
+
99
129
"""
100
130
use Scenic.Component , has_children: false
101
131
@@ -107,13 +137,6 @@ defmodule Scenic.Component.Button do
107
137
import Scenic.Primitives , only: [ { :rrect , 3 } , { :text , 3 } , { :update_opts , 2 } ]
108
138
109
139
# import IEx
110
-
111
- @ default_radius 3
112
-
113
- @ default_font :roboto
114
- @ default_font_size 20
115
- @ default_alignment :center
116
-
117
140
@ impl Scenic.Component
118
141
def validate ( text ) when is_bitstring ( text ) do
119
142
{ :ok , text }
@@ -135,6 +158,7 @@ defmodule Scenic.Component.Button do
135
158
@ doc false
136
159
@ impl Scenic.Scene
137
160
def init ( scene , text , opts ) when is_bitstring ( text ) and is_list ( opts ) do
161
+ styles = Keyword . get ( opts , :styles , [ ] )
138
162
id = opts [ :id ]
139
163
140
164
# theme is passed in as an inherited style
@@ -148,9 +172,10 @@ defmodule Scenic.Component.Button do
148
172
|> Theme . normalize ( )
149
173
150
174
# font related info
151
- font = @ default_font
152
- { :ok , { Static.Font , fm } } = Static . meta ( @ default_font )
153
- font_size = opts [ :button_font_size ] || @ default_font_size
175
+ font = Keyword . get ( styles , :font , @ default_font )
176
+ { :ok , { Static.Font , fm } } = Static . meta ( font )
177
+ font_size = Keyword . get ( styles , :font_size , @ default_font_size )
178
+ alignment = Keyword . get ( styles , :text_align , @ default_alignment )
154
179
155
180
ascent = FontMetrics . ascent ( font_size , fm )
156
181
descent = FontMetrics . descent ( font_size , fm )
@@ -171,7 +196,6 @@ defmodule Scenic.Component.Button do
171
196
end
172
197
173
198
radius = opts [ :radius ] || @ default_radius
174
- alignment = opts [ :alignment ] || @ default_alignment
175
199
176
200
vpos = height / 2 + ascent / 2 + descent / 3
177
201
0 commit comments