Skip to content

Commit 818ead9

Browse files
authored
Merge pull request #89 from boydm/boyd
A lot of documentation.
2 parents df0328e + f47e027 commit 818ead9

37 files changed

+1208
-92
lines changed

lib/scenic/component.ex

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,39 @@ defmodule Scenic.Component do
1717
1818
At the top of your module definition.
1919
20+
## Standard Components
21+
22+
Scenic includes a small number of standard components that you can simply reuse in your
23+
scenes. These were chosen to be in the main library because a) they are used frequently,
24+
and b) their use promotes a certain amount of "common" look and feel.
25+
26+
All of these components are typically added/modified via the helper functions in the
27+
[`Scenic.Components`](Scenic.Components.html) module.
28+
29+
* [`Button`](Scenic.Component.Button.html) a simple button.
30+
* [`Checkbox`](Scenic.Component.Input.Checkbox.html) a checkbox input field.
31+
* [`Dropdown`](Scenic.Component.Input.Dropdown.html) a dropdown / select input field.
32+
* [`RadioGroup`](Scenic.Component.Input.RadioGroup.html) a group of radio button inputs.
33+
* [`Slider`](Scenic.Component.Input.Slider.html) a slider input.
34+
* [`TextField`](Scenic.Component.Input.TextField.html) a text / password input field.
35+
* [`Toggle`](Scenic.Component.Input.Toggle.html) an on/off toggle input.
36+
37+
## Other Components
38+
39+
For completeness, Scenic also includes the following standard components. They are used
40+
by the components above, although you are free to use them as well if they fit your needs.
41+
42+
* [`Caret`](Scenic.Component.Input.Caret.html) the vertical, blinking, caret line in a text field.
43+
* [`RadioButton`](Scenic.Component.Input.RadioButton.html) a single radio button in a radio group.
44+
2045
## Verifiers
2146
2247
One of the main differences between a Component and a Scene is the two extra callbacks
2348
that are used to verify incoming data. Since Components are meant to be reused, you
2449
should do some basic validation that the data being set up is valid, then provide
2550
feedback if it isn't.
2651
27-
## No children
52+
## Optional: No Children
2853
2954
There is an optimization you can use. If you know for certain that your component
3055
will not attempt to use any components, you can set `has_children` to `false` like this.

lib/scenic/component/button.ex

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,77 @@
11
defmodule Scenic.Component.Button do
2-
@moduledoc false
2+
@moduledoc """
3+
Add a button to a graph
34
5+
A button is a small scene that is pretty much just some text drawn over a
6+
rounded rectangle. The button scene contains logic to detect when the button
7+
is pressed, tracks it as the pointer moves around, and when it is released.
8+
9+
## Data
10+
11+
`title`
12+
13+
* `title` - a bitstring describing the text to show in the button
14+
15+
## Messages
16+
17+
If a button press is successful, it sends an event message to the host scene
18+
in the form of:
19+
20+
{:click, id}
21+
22+
## Styles
23+
24+
Buttons honor the following standard styles
25+
26+
* `:hidden` - If `false` the component is rendered. If `true`, it is skipped.
27+
The default is `false`.
28+
* `:theme` - The color set used to draw. See below. The default is `:primary`
29+
30+
## Additional Styles
31+
32+
Buttons honor the following list of additional styles.
33+
34+
* `:width` - pass in a number to set the width of the button.
35+
* `:height` - pass in a number to set the height of the button.
36+
* `:radius` - pass in a number to set the radius of the button's rounded
37+
rectangle.
38+
* `:alignment` - set the alignment of the text inside the button. Can be one
39+
of `:left, :right, :center`. The default is `:center`.
40+
* `:button_font_size` - the size of the font in the button
41+
42+
Buttons do not use the inherited `:font_size` style as they should look
43+
consistent regardless of what size the surrounding text is.
44+
45+
## Theme
46+
47+
Buttons work well with the following predefined themes:
48+
`:primary`, `:secondary`, `:success`, `:danger`, `:warning`, `:info`,
49+
`:text`, `:light`, `:dark`
50+
51+
To pass in a custom theme, supply a map with at least the following entries:
52+
53+
* `:text` - the color of the text in the button
54+
* `:background` - the normal background of the button
55+
* `:active` - the background while the button is pressed
56+
57+
## Usage
58+
59+
You should add/modify components via the helper functions in
60+
[`Scenic.Components`](Scenic.Components.html#button/3)
61+
62+
### Examples
63+
64+
The following example creates a simple button and positions it on the screen.
65+
66+
graph
67+
|> button("Example", id: :button_id, translate: {20, 20})
68+
69+
The next example makes the same button as before, but colors it as a warning
70+
button. See the options list above for more details.
71+
72+
graph
73+
|> button("Example", id: :button_id, translate: {20, 20}, theme: :warning)
74+
"""
475
use Scenic.Component, has_children: false
576

677
alias Scenic.Graph
@@ -20,6 +91,7 @@ defmodule Scenic.Component.Button do
2091
@default_alignment :center
2192

2293
# --------------------------------------------------------
94+
@doc false
2395
def info(data) do
2496
"""
2597
#{IO.ANSI.red()}Button data must be a bitstring: initial_text
@@ -29,10 +101,12 @@ defmodule Scenic.Component.Button do
29101
end
30102

31103
# --------------------------------------------------------
104+
@doc false
32105
def verify(text) when is_bitstring(text), do: {:ok, text}
33106
def verify(_), do: :invalid_data
34107

35108
# --------------------------------------------------------
109+
@doc false
36110
def init(text, opts) when is_bitstring(text) and is_list(opts) do
37111
id = opts[:id]
38112
styles = opts[:styles]
@@ -114,6 +188,7 @@ defmodule Scenic.Component.Button do
114188
end
115189

116190
# --------------------------------------------------------
191+
@doc false
117192
def handle_input(
118193
{:cursor_enter, _uid},
119194
_context,

lib/scenic/component/input/caret.ex

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,28 @@
44
#
55

66
defmodule Scenic.Component.Input.Caret do
7-
@moduledoc false
7+
@moduledoc """
8+
Add a blinking text-input caret to a graph.
89
10+
11+
## Data
12+
13+
`{height, color}`
14+
15+
* `height` - integer greater than zero
16+
* `color` - any [valid color](Scenic.Primitive.Style.Paint.Color.html).
17+
18+
## Usage
19+
20+
The caret component is used by the TextField component and usually isn't accessed directly,
21+
although you are free to do so if it fits your needs. There is no short-cut helper
22+
function so you will need to add it to the graph manually.
23+
24+
The following example adds a caret to a graph.
25+
26+
graph
27+
|> Caret.add_to_graph({height, theme.text}, id: :caret)
28+
"""
929
use Scenic.Component, has_children: false
1030

1131
import Scenic.Primitives,
@@ -28,6 +48,7 @@ defmodule Scenic.Component.Input.Caret do
2848
# setup
2949

3050
# --------------------------------------------------------
51+
@doc false
3152
def info(data) do
3253
"""
3354
#{IO.ANSI.red()}Caret data must be: {height, color}
@@ -37,6 +58,7 @@ defmodule Scenic.Component.Input.Caret do
3758
end
3859

3960
# --------------------------------------------------------
61+
@doc false
4062
@spec verify(any()) :: :invalid_data | {:ok, {number(), any()}}
4163
def verify({height, color} = data)
4264
when is_number(height) and height > 0 do
@@ -49,6 +71,7 @@ defmodule Scenic.Component.Input.Caret do
4971
def verify(_), do: :invalid_data
5072

5173
# --------------------------------------------------------
74+
@doc false
5275
def init({height, color}, _opts) do
5376
# build the graph, initially not showing
5477
graph =
@@ -72,6 +95,7 @@ defmodule Scenic.Component.Input.Caret do
7295
end
7396

7497
# --------------------------------------------------------
98+
@doc false
7599
def handle_cast(:start_caret, %{graph: graph, timer: nil} = state) do
76100
# turn on the caret
77101
graph =
@@ -127,6 +151,7 @@ defmodule Scenic.Component.Input.Caret do
127151
def handle_cast(_, state), do: {:noreply, state}
128152

129153
# --------------------------------------------------------
154+
@doc false
130155
def handle_info(:blink, %{graph: graph, hidden: hidden} = state) do
131156
# invert the hidden flag
132157
hidden = !hidden

lib/scenic/component/input/checkbox.ex

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,53 @@
11
defmodule Scenic.Component.Input.Checkbox do
2-
@moduledoc false
2+
@moduledoc """
3+
Add a checkbox to a graph
4+
5+
## Data
6+
7+
`{text, checked?}`
8+
9+
* `text` - must be a bitstring
10+
* `checked?` - must be a boolean and indicates if the checkbox is set.
11+
12+
## Messages
13+
14+
When the state of the checkbox, it sends an event message to the host scene
15+
in the form of:
16+
17+
`{:value_changed, id, checked?}`
18+
19+
## Styles
20+
21+
Buttons honor the following standard styles
22+
23+
* `:hidden` - If `false` the component is rendered. If `true`, it is skipped.
24+
The default is `false`.
25+
* `:theme` - The color set used to draw. See below. The default is `:dark`
26+
27+
## Theme
28+
29+
Checkboxes work well with the following predefined themes: `:light`, `:dark`
30+
31+
To pass in a custom theme, supply a map with at least the following entries:
32+
33+
* `:text` - the color of the text in the button
34+
* `:background` - the background of the box
35+
* `:border` - the border of the box
36+
* `:active` - the border of the box while the button is pressed
37+
* `:thumb` - the color of the check mark itself
38+
39+
## Usage
40+
41+
You should add/modify components via the helper functions in
42+
[`Scenic.Components`](Scenic.Components.html#checkbox/3)
43+
44+
### Examples
45+
46+
The following example creates a checkbox and positions it on the screen.
47+
48+
graph
49+
|> checkbox({"Example", true}, id: :checkbox_id, translate: {20, 20})
50+
"""
351

452
use Scenic.Component, has_children: false
553

@@ -18,7 +66,8 @@ defmodule Scenic.Component.Input.Checkbox do
1866
# @default_height 16
1967
# @default_radius 3
2068

21-
# #--------------------------------------------------------
69+
# --------------------------------------------------------
70+
@doc false
2271
def info(data) do
2372
"""
2473
#{IO.ANSI.red()}Checkbox data must be: {text, checked?}
@@ -28,13 +77,15 @@ defmodule Scenic.Component.Input.Checkbox do
2877
end
2978

3079
# --------------------------------------------------------
80+
@doc false
3181
def verify({text, checked} = data) when is_bitstring(text) and is_boolean(checked) do
3282
{:ok, data}
3383
end
3484

3585
def verify(_), do: :invalid_data
3686

3787
# --------------------------------------------------------
88+
@doc false
3889
def init({text, checked?}, opts) do
3990
id = opts[:id]
4091
styles = opts[:styles]
@@ -101,6 +152,7 @@ defmodule Scenic.Component.Input.Checkbox do
101152
end
102153

103154
# --------------------------------------------------------
155+
@doc false
104156
def handle_input({:cursor_enter, _uid}, _, %{pressed: true} = state) do
105157
state = Map.put(state, :contained, true)
106158
graph = update_graph(state)

0 commit comments

Comments
 (0)