Skip to content

Commit 3fdfd6c

Browse files
committed
add basic documentation to the primitives
1 parent abfdf4e commit 3fdfd6c

File tree

16 files changed

+405
-19
lines changed

16 files changed

+405
-19
lines changed

lib/scenic/component.ex

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,41 @@
44
#
55

66
defmodule Scenic.Component do
7-
@moduledoc false
7+
@moduledoc """
8+
A Component is simply a Scene that is optimized to be referenced by another scene.
9+
10+
All you need to do to create a Component is call
11+
12+
use Scenic.Component
13+
14+
instead of
15+
16+
use Scenic.Scene
17+
18+
At the top of your module definition.
19+
20+
## Verifiers
21+
22+
One of the main differences between a Component and a Scene is the two extra callbacks
23+
that are used to verify incoming data. Since Components are meant to be reused, you
24+
should do some basic validation that the data being set up is valid, then provide
25+
feedback if it isn't.
26+
27+
## No children
28+
29+
There is an optimization you can use. If you know for certain that your component
30+
will not attempt to use any components, you can set `has_children` to `false` like this.
31+
32+
use Scenic.Component, has_children: false
33+
34+
Setting `has_children` to `false` this will do two things. First, it won't create
35+
a dynamic supervisor for this scene, which saves some resources. Second,
36+
`push_graph/1` goes through a fast pass that doesn't scan the graph for dynamic children.
37+
38+
For example, the Button component sets `has_children` to `false`.
39+
40+
This option is available for any Scene, not just components
41+
"""
842

943
alias Scenic.Primitive
1044

lib/scenic/primitive.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,15 @@ defmodule Scenic.Primitive do
9393
quote do
9494
@behaviour Scenic.Primitive
9595

96+
@doc false
9697
def build(data \\ nil, opts \\ [])
97-
9898
def build(data, opts) do
9999
data = verify!(data)
100100
Primitive.build(__MODULE__, data, opts)
101101
end
102102

103+
@doc false
103104
def add_to_graph(graph, data \\ nil, opts \\ [])
104-
105105
def add_to_graph(%Scenic.Graph{} = graph, data, opts) do
106106
Graph.add(graph, __MODULE__, data, opts)
107107
end
@@ -142,8 +142,11 @@ defmodule Scenic.Primitive do
142142
def default_pin(_), do: {0.0, 0.0}
143143

144144
# simple defaults that can be overridden
145+
@doc false
145146
def get(%Primitive{data: data}), do: data
147+
@doc false
146148
def put(p, data), do: Primitive.do_put(p, data)
149+
147150
@doc false
148151
def normalize(data), do: data
149152

lib/scenic/primitive/arc.ex

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

66
defmodule Scenic.Primitive.Arc do
7-
@moduledoc false
7+
@moduledoc """
8+
Draw an arc on the screen.
9+
10+
An arc is a segment that traces part of the outline of a circle. If you are
11+
looking for something shaped like a piece of pie, then you want a segment.
12+
13+
Arcs are often drawn on top of a segment to get an affect where a piece of pie
14+
is filled in, but only the curvy edge is stroked.
15+
16+
Note that you can fill an arc, but that will result in a shape that looks
17+
like a potato wedge.
18+
19+
## Data
20+
21+
`{radius, start, finish}`
22+
23+
The data for an arc is a three-tuple.
24+
* `radius` - the radius of the arc
25+
* `start` - the starting angle in radians
26+
* `finish` - end ending angle in radians
27+
28+
## Styles
29+
30+
This primitive recognizes the following styles
31+
* `hidden` - show or hide the primitive
32+
* `fill` - fill in the area of the primitive
33+
* `stroke` - stroke the outline of the primitive. In this case, only the curvy part.
34+
"""
835

936
use Scenic.Primitive
1037
alias Scenic.Primitive.Sector
@@ -16,6 +43,7 @@ defmodule Scenic.Primitive.Arc do
1643
# data verification and serialization
1744

1845
# --------------------------------------------------------
46+
@doc false
1947
def info(data),
2048
do: """
2149
#{IO.ANSI.red()}#{__MODULE__} data must be: {radius, start, finish}
@@ -24,6 +52,7 @@ defmodule Scenic.Primitive.Arc do
2452
"""
2553

2654
# --------------------------------------------------------
55+
@doc false
2756
def verify(data) do
2857
normalize(data)
2958
{:ok, data}
@@ -32,12 +61,16 @@ defmodule Scenic.Primitive.Arc do
3261
end
3362

3463
# --------------------------------------------------------
64+
@doc false
3565
@spec normalize({number(), number(), number()}) :: {number(), number(), number()}
3666
def normalize({radius, start, finish} = data)
3767
when is_number(start) and is_number(finish) and is_number(radius),
3868
do: data
3969

4070
# ============================================================================
71+
@doc """
72+
Returns a list of styles recognized by this primitive.
73+
"""
4174
@spec valid_styles() :: [:fill | :hidden | :stroke]
4275
def valid_styles(), do: @styles
4376

lib/scenic/primitive/circle.ex

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

66
defmodule Scenic.Primitive.Circle do
7-
@moduledoc false
7+
@moduledoc """
8+
Draw a circle on the screen.
9+
10+
## Data
11+
12+
`radius`
13+
14+
The data for an arc is a single number.
15+
* `radius` - the radius of the arc
16+
17+
## Styles
18+
19+
This primitive recognizes the following styles
20+
* `hidden` - show or hide the primitive
21+
* `fill` - fill in the area of the primitive
22+
* `stroke` - stroke the outline of the primitive.
23+
"""
824

925
use Scenic.Primitive
1026

@@ -14,6 +30,7 @@ defmodule Scenic.Primitive.Circle do
1430
# data verification and serialization
1531

1632
# --------------------------------------------------------
33+
@doc false
1734
def info(data),
1835
do: """
1936
#{IO.ANSI.red()}#{__MODULE__} data must be: radius
@@ -22,6 +39,7 @@ defmodule Scenic.Primitive.Circle do
2239
"""
2340

2441
# --------------------------------------------------------
42+
@doc false
2543
def verify(data) do
2644
normalize(data)
2745
{:ok, data}
@@ -30,12 +48,16 @@ defmodule Scenic.Primitive.Circle do
3048
end
3149

3250
# --------------------------------------------------------
51+
@doc false
3352
@spec normalize(number()) :: number()
3453
def normalize(radius) when is_number(radius) do
3554
radius
3655
end
3756

3857
# ============================================================================
58+
@doc """
59+
Returns a list of styles recognized by this primitive.
60+
"""
3961
@spec valid_styles() :: [:hidden | :fill | :stroke]
4062
def valid_styles(), do: @styles
4163

lib/scenic/primitive/ellipse.ex

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

66
defmodule Scenic.Primitive.Ellipse do
7-
@moduledoc false
7+
@moduledoc """
8+
Draw an ellipse on the screen.
9+
10+
## Data
11+
12+
`{radius_1, radius_2}`
13+
14+
The data for an arc is a single number.
15+
* `radius_1` - the radius of the ellipse in one direction
16+
* `radius_2` - the radius of the ellipse in the other direction
17+
18+
## Styles
19+
20+
This primitive recognizes the following styles
21+
* `hidden` - show or hide the primitive
22+
* `fill` - fill in the area of the primitive
23+
* `stroke` - stroke the outline of the primitive.
24+
25+
Note: you can achieve the same effect with a Circle primitive
26+
by applying a :size transform to it with unequal values on the axes
27+
"""
828

929
use Scenic.Primitive
1030

@@ -14,6 +34,7 @@ defmodule Scenic.Primitive.Ellipse do
1434
# data verification and serialization
1535

1636
# --------------------------------------------------------
37+
@doc false
1738
def info(data),
1839
do: """
1940
#{IO.ANSI.red()}#{__MODULE__} data must be: {radius_1, radius_2}
@@ -22,6 +43,7 @@ defmodule Scenic.Primitive.Ellipse do
2243
"""
2344

2445
# --------------------------------------------------------
46+
@doc false
2547
def verify(data) do
2648
normalize(data)
2749
{:ok, data}
@@ -30,12 +52,16 @@ defmodule Scenic.Primitive.Ellipse do
3052
end
3153

3254
# --------------------------------------------------------
55+
@doc false
3356
@spec normalize({number(), number()}) :: {number(), number()}
3457
def normalize({r1, r2} = data) when is_number(r1) and is_number(r2) do
3558
data
3659
end
3760

3861
# ============================================================================
62+
@doc """
63+
Returns a list of styles recognized by this primitive.
64+
"""
3965
@spec valid_styles() :: [:fill | :hidden | :stroke, ...]
4066
def valid_styles(), do: @styles
4167

lib/scenic/primitive/group.ex

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,25 @@
44
#
55

66
defmodule Scenic.Primitive.Group do
7-
@moduledoc false
7+
@moduledoc """
8+
A container to hold other primitives.
9+
10+
Any styles placed on a group will be inherited by the primitives in the
11+
group. Any transforms placed on a group will be multiplied into the transforms
12+
in the primitives in the group.
13+
14+
## Data
15+
16+
`uids`
17+
18+
The data for an arc is a list of internal uids for the primitives it contains
19+
20+
21+
## Styles
22+
23+
The group is special in that it accepts all styles and transforms, even if they
24+
are non-standard. These are then inherited by any primitives, including SceneRefs
25+
"""
826

927
use Scenic.Primitive
1028
alias Scenic.Primitive
@@ -15,14 +33,15 @@ defmodule Scenic.Primitive.Group do
1533
# data verification and serialization
1634

1735
# --------------------------------------------------------
36+
@doc false
1837
def build(nil, opts), do: build([], opts)
19-
2038
def build(ids, opts) do
2139
verify!(ids)
2240
Primitive.build(__MODULE__, ids, opts)
2341
end
2442

2543
# --------------------------------------------------------
44+
@doc false
2645
def info(data),
2746
do: """
2847
#{IO.ANSI.red()}#{__MODULE__} data must be a list of valid uids of other elements in the graph.
@@ -31,6 +50,7 @@ defmodule Scenic.Primitive.Group do
3150
"""
3251

3352
# --------------------------------------------------------
53+
@doc false
3454
def verify(ids) when is_list(ids) do
3555
if Enum.all?(ids, &is_integer/1), do: {:ok, ids}, else: :invalid_data
3656
end
@@ -40,6 +60,9 @@ defmodule Scenic.Primitive.Group do
4060
# ============================================================================
4161
# filter and gather styles
4262

63+
@doc """
64+
Returns a list of styles recognized by this primitive.
65+
"""
4366
@spec valid_styles() :: [:all, ...]
4467
def valid_styles(), do: [:all]
4568

lib/scenic/primitive/line.ex

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,24 @@
44
#
55

66
defmodule Scenic.Primitive.Line do
7-
@moduledoc false
7+
@moduledoc """
8+
Draw a line on the screen.
9+
10+
## Data
11+
12+
`{point_a, point_b}`
13+
14+
The data for a line is a tuple containing two points.
15+
* `point_a` - position to start drawing from
16+
* `point_b` - position to draw to
17+
18+
## Styles
19+
20+
This primitive recognizes the following styles
21+
* `hidden` - show or hide the primitive
22+
* `cap` - says how to draw the ends of the line.
23+
* `stroke` - stroke the outline of the primitive.
24+
"""
825

926
use Scenic.Primitive
1027

@@ -16,6 +33,7 @@ defmodule Scenic.Primitive.Line do
1633
# data verification and serialization
1734

1835
# --------------------------------------------------------
36+
@doc false
1937
def info(data),
2038
do: """
2139
#{IO.ANSI.red()}#{__MODULE__} data must be two points: {{x0,y0}, {x1,y1}}
@@ -24,13 +42,17 @@ defmodule Scenic.Primitive.Line do
2442
"""
2543

2644
# --------------------------------------------------------
45+
@doc false
2746
def verify({{x0, y0}, {x1, y1}} = data)
2847
when is_number(x0) and is_number(y0) and is_number(x1) and is_number(y1),
2948
do: {:ok, data}
3049

3150
def verify(_), do: :invalid_data
3251

3352
# ============================================================================
53+
@doc """
54+
Returns a list of styles recognized by this primitive.
55+
"""
3456
@spec valid_styles() :: [:cap | :hidden | :stroke, ...]
3557
def valid_styles(), do: @styles
3658

0 commit comments

Comments
 (0)