Skip to content

Commit 6332fce

Browse files
committed
Allow styles to be passed through to components by including a styles: [] option. Change appropriate components to use them. This is mostly just the Button component
1 parent 2a300ed commit 6332fce

File tree

4 files changed

+46
-21
lines changed

4 files changed

+46
-21
lines changed

guides/upgrading_to_v0.11.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Version v0.11 is a MAJOR overhaul from the top to the bottom. For the first time
2525
* The SceneRef primitive is gone, and replaced with a combination of Component and Script primitives.
2626
* The `:font_blur` primitive is gone. Sorry. Didn't have a close enough analog in Canvas
2727
* The format of the input messages have changed - see the docs
28+
* The optional styles on some of the standard components (Button) have changed to be more consistent with the standard styles. See documentation.
2829

2930
__Important__
3031
* `:scenic_driver_local` is the new standard renderer for Scenic. Both `:scenic_driver_glfw` and `:scenic_drives_nerves_rpi` are retired as of v0.11.

lib/scenic/component/button.ex

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
defmodule Scenic.Component.Button do
2+
@default_radius 3
3+
@default_font :roboto
4+
@default_font_size 20
5+
@default_alignment :center
6+
27
@moduledoc """
38
Add a button to a graph
49
@@ -51,17 +56,30 @@ defmodule Scenic.Component.Button do
5156
The default is `false`.
5257
* `:theme` - The color set used to draw. See below. The default is `:primary`
5358
54-
## Additional Styles
59+
## Sendable Styles
60+
Styles can be sent directly to the Button Component by adding a :styles list.
5561
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.
5778
5879
* `:width` - :auto (default) or pass in a number to set the width of the button
5980
* `:height` - :auto (default) or pass in a number to set the height of the button.
6081
* `: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)}
6583
6684
Buttons do not use the inherited `:font_size` style as they should look
6785
consistent regardless of what size the surrounding text is.
@@ -96,6 +114,18 @@ defmodule Scenic.Component.Button do
96114
97115
graph
98116
|> 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+
99129
"""
100130
use Scenic.Component, has_children: false
101131

@@ -107,13 +137,6 @@ defmodule Scenic.Component.Button do
107137
import Scenic.Primitives, only: [{:rrect, 3}, {:text, 3}, {:update_opts, 2}]
108138

109139
# import IEx
110-
111-
@default_radius 3
112-
113-
@default_font :roboto
114-
@default_font_size 20
115-
@default_alignment :center
116-
117140
@impl Scenic.Component
118141
def validate(text) when is_bitstring(text) do
119142
{:ok, text}
@@ -135,6 +158,7 @@ defmodule Scenic.Component.Button do
135158
@doc false
136159
@impl Scenic.Scene
137160
def init(scene, text, opts) when is_bitstring(text) and is_list(opts) do
161+
styles = Keyword.get(opts, :styles, [])
138162
id = opts[:id]
139163

140164
# theme is passed in as an inherited style
@@ -148,9 +172,10 @@ defmodule Scenic.Component.Button do
148172
|> Theme.normalize()
149173

150174
# 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)
154179

155180
ascent = FontMetrics.ascent(font_size, fm)
156181
descent = FontMetrics.descent(font_size, fm)
@@ -171,7 +196,6 @@ defmodule Scenic.Component.Button do
171196
end
172197

173198
radius = opts[:radius] || @default_radius
174-
alignment = opts[:alignment] || @default_alignment
175199

176200
vpos = height / 2 + ascent / 2 + descent / 3
177201

lib/scenic/component/input/text_field.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ defmodule Scenic.Component.Input.TextField do
2828
The default is `false`.
2929
* `:theme` - The color set used to draw. See below. The default is `:dark`
3030
31-
## Additional Styles
31+
## Additional Options
3232
33-
Text fields honor the following list of additional styles.
33+
Text fields honor the following list of additional options.
3434
3535
* `:filter` - Adding a filter option restricts which characters can be
3636
entered into the text_field component. The value of filter can be one of:

lib/scenic/component/input/toggle.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ defmodule Scenic.Component.Input.Toggle do
1616
is `false`.
1717
* `:theme` - The color set used to draw. See below. The default is `:dark`
1818
19-
## Additional Styles
19+
## Additional Options
2020
21-
Toggles also honor the following additional styles.
21+
Toggles also honor the following additional options.
2222
2323
* `:border_width` - the border width. Defaults to `2`.
2424
* `:padding` - the space between the border and the thumb. Defaults to `2`

0 commit comments

Comments
 (0)