Skip to content

Commit 5f31cf6

Browse files
committed
Add docs for Scenic.Primitive and rename Primitive.put_opts to Primitive.merge_opts
1 parent ad45c5d commit 5f31cf6

File tree

5 files changed

+195
-23
lines changed

5 files changed

+195
-23
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* Simplify rules around user-closing a viewport
66
* Fix bug where captured positional inputs were not casting the transformed location
77
* Deprecate Graph.get_root(). Use Graph.get!(graph, :\_root\_) instead.
8+
* Renamed Primitive.put_opts to Primitive.merge_opts
9+
810

911
## 0.8.0
1012

lib/scenic/primitive.ex

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

66
defmodule Scenic.Primitive do
7-
@moduledoc false
7+
@moduledoc """
8+
Please see [`Primitives Overview`](overview_primitives.html) for a high-level description.
9+
10+
## What is a primitive
11+
12+
A primitive is the simplest thing Scenic knows how to draw on the screen. There is is only
13+
a small, fixed set of them, but they can be combined in a graph to draw very complicated
14+
images.
15+
16+
In general, each Primitive type has a piece of data that it expects to operate on. For
17+
example, Primitive.Text requires a bitstring. Primitive.Circle requires a radius. Please
18+
see the documentation for each primitive for details.
19+
20+
## How to use primitives
21+
22+
By far, the easiest way to use primitives is to import the helper functions in
23+
[`Scenic.Primitives`](Scenic.Primitives.html). These helpers can both add primitives to
24+
a scene you are building and modify later as you react to events.
25+
26+
Once you get a primitive out of a graph via functions such as `Graph.modify`, or `Graph.get`,
27+
You can use the generic helpers in this module to access or manipulate them.
28+
"""
29+
830

931
alias Scenic.Graph
1032
alias Scenic.Primitive
@@ -149,6 +171,21 @@ defmodule Scenic.Primitive do
149171
# build a new primitive
150172
# in general, default the various lists and the assign map to nil to save space
151173
# assume most elements do not hvae these items set.
174+
175+
@doc """
176+
Generic builder to create a new primitive.
177+
178+
This function is used internally. You should almost always use the helpers in
179+
[Scenic.Primitives](Scenic.Primitives.html) instead.
180+
181+
Parameters:
182+
`module` - The module of the primitive you are building
183+
`data` - the primitive-specific data being set into the primitive
184+
`opts` - a list of style and transform options to apply to the primitive
185+
186+
Returns the built primitive.
187+
"""
188+
152189
@spec build(module :: atom, data :: any, opts :: keyword) :: Primitive.t()
153190
def build(module, data, opts \\ []) do
154191
# first build the map with the non-optional fields
@@ -256,22 +293,32 @@ defmodule Scenic.Primitive do
256293

257294
# ============================================================================
258295
# styles
259-
# I'm allowing the styles to not be present on the primitive, which is why
260-
# I'm not parsing it out in the function match
261296

297+
@doc """
298+
Get the styles map from a primitive.
299+
300+
Parameters:
301+
`primitive` - The primitive
302+
303+
Returns the a map of styles set directly onto this primitive. This does
304+
not include any inherited styles.
305+
"""
262306
@spec get_styles(primitive :: Primitive.t()) :: map
263307
def get_styles(primitive)
264308

265309
def get_styles(%Primitive{} = p) do
266310
Map.get(p, :styles, %{})
267311
end
268312

269-
# def get_primitive_styles( primitive )
270-
# def get_primitive_styles( %Primitive{} = p ) do
271-
# Map.get(p, :styles, %{})
272-
# |> Style.primitives()
273-
# end
313+
@doc """
314+
Update the styles map in a primitive.
315+
316+
Parameters:
317+
`primitive` - The primitive
318+
`styles` - The new styles map
274319
320+
Returns the primitive with the updated styles.
321+
"""
275322
@spec put_styles(primitive :: Primitive.t(), styles :: map) :: Primitive.t()
276323
def put_styles(primitve, styles)
277324
def put_styles(%Primitive{} = p, nil), do: Map.delete(p, :styles)
@@ -284,13 +331,33 @@ defmodule Scenic.Primitive do
284331
@spec get_style(primitive :: Primitive.t(), type :: atom, default :: any) :: any
285332
def get_style(primitive, type, default \\ nil)
286333

334+
@doc """
335+
Get the value of a specific style set on the primitive.
336+
337+
If the style is not set, it returns default
338+
339+
Parameters:
340+
`primitive` - The primitive
341+
`type` - atom representing the style to get.
342+
`default` - default value to return if the style is not set.
343+
344+
Returns the value of the style.
345+
"""
287346
def get_style(%Primitive{} = p, type, default) when is_atom(type) do
288347
Map.get(p, :styles, %{})
289348
|> Map.get(type, default)
290349
end
291350

292-
# the public facing put_style gives the primitive module a chance to filter the styles
293-
# do_put_style does the actual work
351+
@doc """
352+
Update the value of a specific style set on the primitive.
353+
354+
Parameters:
355+
`primitive` - The primitive
356+
`type` - atom representing the style to get.
357+
`data` - the value to set on the style.
358+
359+
Returns the updated primitive.
360+
"""
294361
@spec put_style(primitive :: Primitive.t(), type :: atom, data :: any) :: Primitive.t()
295362
def put_style(%Primitive{} = p, type, nil) when is_atom(type) do
296363
Map.get(p, :styles, %{})
@@ -310,6 +377,17 @@ defmodule Scenic.Primitive do
310377
end)
311378
end
312379

380+
@doc """
381+
Deletes a specified style from a primitive.
382+
383+
Does nothing if the style is not set.
384+
385+
Parameters:
386+
`primitive` - The primitive
387+
`type` - atom representing the style to delete.
388+
389+
Returns the updated primitive.
390+
"""
313391
@spec delete_style(primitive :: Primitive.t(), type :: atom) :: Primitive.t()
314392
def delete_style(primitive, type)
315393

@@ -321,16 +399,32 @@ defmodule Scenic.Primitive do
321399

322400
# ============================================================================
323401
# transforms
324-
# I'm allowing the transforms to not be present on the primitive, which is why
325-
# I'm not parsing it out in the function match
326402

403+
@doc """
404+
Get the transforms map from a primitive.
405+
406+
Parameters:
407+
`primitive` - The primitive
408+
409+
Returns the a map of transforms set directly onto this primitive. This does
410+
not include any inherited transforms.
411+
"""
327412
@spec get_transforms(primitive :: Primitive.t()) :: map
328413
def get_transforms(primitive)
329414

330415
def get_transforms(%Primitive{} = p) do
331416
Map.get(p, :transforms, %{})
332417
end
333418

419+
@doc """
420+
Update the transforms map in a primitive.
421+
422+
Parameters:
423+
`primitive` - The primitive
424+
`transforms` - The new transforms map
425+
426+
Returns the primitive with the updated transforms.
427+
"""
334428
@spec put_transforms(primitive :: Primitive.t(), transforms :: map) :: Primitive.t()
335429
def put_transforms(primitve, transforms)
336430
def put_transforms(%Primitive{} = p, nil), do: Map.delete(p, :transforms)
@@ -340,16 +434,37 @@ defmodule Scenic.Primitive do
340434
Map.put(p, :transforms, txs)
341435
end
342436

437+
343438
@spec get_transform(primitive :: Primitive.t(), type :: atom, default :: any) :: any
344439
def get_transform(primitive, tx_type, default \\ nil)
345440

441+
@doc """
442+
Get the value of a specific transform set on the primitive.
443+
444+
If the transform is not set, it returns default
445+
446+
Parameters:
447+
`primitive` - The primitive
448+
`type` - atom representing the transform to get.
449+
`default` - default value to return if the transform is not set.
450+
451+
Returns the value of the transform.
452+
"""
346453
def get_transform(%Primitive{} = p, tx_type, default) when is_atom(tx_type) do
347454
Map.get(p, :transforms, %{})
348455
|> Map.get(tx_type, default)
349456
end
350457

351-
# the public facing put_style gives the primitive module a chance to filter the styles
352-
# do_put_style does the actual work
458+
@doc """
459+
Update the value of a specific transform set on the primitive.
460+
461+
Parameters:
462+
`primitive` - The primitive
463+
`type` - atom representing the transform to get.
464+
`data` - the value to set on the transform.
465+
466+
Returns the updated primitive.
467+
"""
353468
@spec put_transform(primitive :: Primitive.t(), type :: atom, transform :: any) :: Primitive.t()
354469
def put_transform(%Primitive{} = p, tx_type, nil) when is_atom(tx_type) do
355470
Map.get(p, :transforms, %{})
@@ -369,6 +484,18 @@ defmodule Scenic.Primitive do
369484
end)
370485
end
371486

487+
488+
@doc """
489+
Deletes a specified transform from a primitive.
490+
491+
Does nothing if the transform is not set.
492+
493+
Parameters:
494+
`primitive` - The primitive
495+
`type` - atom representing the transform to delete.
496+
497+
Returns the updated primitive.
498+
"""
372499
@spec delete_transform(primitive :: Primitive.t(), type :: atom) :: Primitive.t()
373500
def delete_transform(primitive, tx_type)
374501

@@ -380,6 +507,15 @@ defmodule Scenic.Primitive do
380507

381508
# ============================================================================
382509
# primitive-specific data
510+
511+
@doc """
512+
Get the value of the primitive-specific data.
513+
514+
Parameters:
515+
`primitive` - The primitive
516+
517+
Returns the value of the primitive-specific data.
518+
"""
383519
@spec get(primitive :: Primitive.t()) :: any
384520
def get(primitive)
385521

@@ -388,13 +524,47 @@ defmodule Scenic.Primitive do
388524
mod.get(p)
389525
end
390526

527+
@deprecated "Use Primitive.merge_opts instead."
391528
@spec put_opts(primitive :: Primitive.t(), opts :: keyword) :: Primitive.t()
392529
def put_opts(primitive, opts)
393530

394531
def put_opts(%Primitive{} = p, opts) when is_list(opts) do
532+
raise "Primitive.put_opts has been deprecated. Use Primitive.merge_opts instead."
533+
end
534+
535+
536+
@doc """
537+
Merge an options-list of styles and transforms onto a primitive.
538+
539+
This function might go through a name-change in the future. It is really
540+
more of a merge. The supplied list of styles and transforms
541+
542+
Parameters:
543+
`primitive` - The primitive
544+
545+
Returns the value of the primitive-specific data.
546+
"""
547+
@spec merge_opts(primitive :: Primitive.t(), opts :: keyword) :: Primitive.t()
548+
def merge_opts(primitive, opts)
549+
550+
def merge_opts(%Primitive{} = p, opts) when is_list(opts) do
395551
apply_options(p, opts)
396552
end
397553

554+
@doc """
555+
Put primitive-specific data onto the primitive.
556+
557+
Like many of the functions in the Scenic.Primitive module, you are usually better
558+
off using the helper functions in [`Scenic.Primitives`](Scenic.Primitives.html) instead.
559+
560+
Parameters:
561+
`primitive` - The primitive
562+
`data` - The data to set
563+
`opts` - A list of style/transform options to merge
564+
565+
Returns the updated primitive.
566+
"""
567+
398568
@spec put(primitive :: Primitive.t(), data :: any, opts :: list) :: Primitive.t()
399569
def put(primitive, data, opts \\ [])
400570

lib/scenic/primitives.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ defmodule Scenic.Primitives do
862862
options :: list
863863
) :: Primitive.t()
864864

865-
def update_opts(p, opts), do: Primitive.put_opts(p, opts)
865+
def update_opts(p, opts), do: Primitive.merge_opts(p, opts)
866866

867867
# ============================================================================
868868
# generic workhorse versions

lib/scenic/view_port.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ defmodule Scenic.ViewPort do
399399

400400
# extract the viewport global styles. Do this by reusing tools in Primitive.
401401
p =
402-
Primitive.put_opts(
402+
Primitive.merge_opts(
403403
%Primitive{module: Primitive.Group},
404404
Map.get(config, :opts, [])
405405
)

test/scenic/primitive_test.exs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,24 +179,24 @@ defmodule Scenic.PrimitiveTest do
179179
end
180180

181181
# --------------------------------------------------------
182-
# put
182+
# merge_opts
183183

184-
test "put_opts updates only the options on a primitive" do
185-
assert Primitive.put_opts(@primitive, fill: :blue).styles == %{
184+
test "merge_opts updates only the options on a primitive" do
185+
assert Primitive.merge_opts(@primitive, fill: :blue).styles == %{
186186
fill: :blue,
187187
stroke: {10, :green}
188188
}
189189
end
190190

191-
test "put_opts rejects invalid style" do
191+
test "merge_opts rejects invalid style" do
192192
assert_raise Primitive.Style.FormatError, fn ->
193-
Primitive.put_opts(@primitive, fill: :invalid)
193+
Primitive.merge_opts(@primitive, fill: :invalid)
194194
end
195195
end
196196

197-
test "put_opts rejects invalid transform" do
197+
test "merge_opts rejects invalid transform" do
198198
assert_raise Primitive.Transform.FormatError, fn ->
199-
Primitive.put_opts(@primitive, rotate: :invalid)
199+
Primitive.merge_opts(@primitive, rotate: :invalid)
200200
end
201201
end
202202

0 commit comments

Comments
 (0)