Macaw.Frame.Style (macaw_frame v0.1.0)

Copy Markdown View Source

The style grammar of the frame document (spec/02 §3, §13.3): a style declared once under styles, derived with extends, stacked at the site that uses it, defaulted by its containers, and resolved through the theme into what an element draws.

  • Lookup (lookup/3): a name resolves against the design's styles first and then the palette of the theme the Frame draws in (§3.3), each extends chain flattened first and a list stacked left to right by the merge rule (§3.5).
  • The stack at a site (stack/4, and stacks/5 over a site's data): references, inline nodes, field entries naming a style per datum, generator entries, laid left to right over what the element inherits (§3.6, §3.7); with_state/2 lays a view state where the library keeps it.
  • Resolution (resolve/3): one node, two outputs (§3.4): the parts projected onto the element keys, every theme name resolved in the target's mode, :css (var(--mw-…, literal)) or :literal, every size in user units; a field binding stays for bind/4, which reads it from a datum. A color is never dropped: a reading the color scale lacks is the scale's unknown, a missing one :none.

Generators (§3.13) have one home, Macaw.Draw.Generators: a generator entry is called through Macaw.Draw.Generators.call/4 with the registry and context the caller passes (generators:, context:), its field arguments read from the datum, and its result held to its type by Macaw.Frame.Style.Generated; over a site's data (stacks/5) each distinct (name, arguments) is called once, and a result that is no style applies nothing and is answered as a fault.

Nothing raises: a variable reached at resolution is a fault, since variables resolve before it.

iex> theme = Macaw.Layers.Theme.default()
iex> {:ok, style} = Macaw.Frame.Style.resolve(%{stroke: %{type: :solid, color: {:color, "bad"}, width: {3, :pt}}}, theme, :css)
iex> style
%{stroke: "var(--mw-bad, #c23238)", stroke_width: 4.0}

Summary

Types

A render target's mode (macaw_layers 04 §3.1).

Functions

An element style with every field binding read from datum (§3.2, §3.4): a color key's reading passed through scales' "color" scale where it has one, a reading the scale's domain lacks drawing the scale's unknown, a missing reading :none; any other key's reading as it is. A literal color is written for mode.

A resolved style without the parts a layer kind does not draw (§3.8).

Where one element's look came from (§15.3): explain(compiled, at: path, datum: datum, state: state), at the path of a layer, a layer's label, a label, an axis or a legend (or of its style site), datum the row a field or generator entry reads, state a view state laid at its layer. {:ok, %{at, kind, state, parts}}, for each part its resolved value, whether the kind drawn it, and its chain of contributions lowest first, each %{layer, container, style, via, field, value, set, generators, reading} with the layers of §3.7 (:theme, :defaults, :kind, :palette, :style, :field, :generator, :state, :inline, :local), reading the datum's value a field entry read. A generator is called as the Frame calls it; a result that applied nothing is {:fault, reason} in its entry's generators. A path compilation holds no site at is {:unknown, :site, at}.

The flat node a style name resolves to (§3.3, §3.5): the design's entry, else the theme's palette entry, extends flattened first and a list stacked left to right. A name neither declares, and a chain that returns to itself (a fault the validator reports), give %{}.

The merge rule (§3.5): over laid on under, a part merged key by key.

A style node in its parts (§3.1), Macaw.Frame.Style.Parts.normalize/1.

A node's parts as the flat element keys (§3.4).

A style node resolved in mode (§3.4): {:ok, element_style}, its parts projected and every theme name resolved, var(--mw-<name>, <literal>) under :css and the literal under :literal, every size in user units against opts' font_size:; a field binding is kept for bind/4. A variable still in it, a color the theme does not name, or a size in no unit is {:error, faults}, each at its element key.

A site's stack laid into one node (§3.6): each entry left to right over inherited (a Macaw.Frame.Style.Inherited or a node), the later winning. Options: pick: a function from a field's name to the datum's value, which a field entry names a style by (nil, a name no style has, or a value of another type lays nothing), and which a generator's field arguments are read through (nil without one); generators: the Macaw.Draw.Generators registry a generator entry is looked up in (none: every generator is unregistered and lays nothing); context: the context map a generator is called with (%{}). A generator's result is laid when it is a style (Macaw.Frame.Style.Generated.style/1) and nothing otherwise.

A site's stack laid once per datum of data (§3.6, §3.13): {nodes, faults}, one node for each datum in order, every field entry and generator argument read from that datum (a map, by field name). A generator is called once per distinct (name, arguments) over the data, its result reused; a result that is no style, or a name the registry lacks, applies nothing and is a fault reason in faults, each once. Options as stack/4's, but pick:, which each datum gives.

A stack with a view-state entry laid at its layer (§3.7): after its names, field and generator entries and before a trailing inline node, so feedback shows over what the data chose and the local edit stays above everything.

Types

mode()

@type mode() :: :css | :literal

A render target's mode (macaw_layers 04 §3.1).

Functions

bind(style, datum, scales, mode)

@spec bind(term(), term(), term(), term()) :: map()

An element style with every field binding read from datum (§3.2, §3.4): a color key's reading passed through scales' "color" scale where it has one, a reading the scale's domain lacks drawing the scale's unknown, a missing reading :none; any other key's reading as it is. A literal color is written for mode.

iex> scale = Macaw.Draw.Scale.new!(:ordinal, domain: ["ok"], range: [Macaw.Layers.Color.rgb(0, 128, 0)])
iex> style = %{fill: {:field, "state"}, opacity: {:field, "o"}}
iex> Macaw.Frame.Style.bind(style, %{"state" => "ok", "o" => 0.5}, %{"color" => scale}, :literal)
%{fill: Macaw.Layers.Color.rgb(0, 128, 0), opacity: 0.5}
iex> Macaw.Frame.Style.bind(style, %{"o" => 1}, %{"color" => scale}, :literal).fill
:none

drawn(style, parts)

@spec drawn(term(), term()) :: map()

A resolved style without the parts a layer kind does not draw (§3.8).

explain(compiled, opts)

@spec explain(term(), term()) :: {:ok, map()} | {:error, [Macaw.Base.Fault.t(), ...]}

Where one element's look came from (§15.3): explain(compiled, at: path, datum: datum, state: state), at the path of a layer, a layer's label, a label, an axis or a legend (or of its style site), datum the row a field or generator entry reads, state a view state laid at its layer. {:ok, %{at, kind, state, parts}}, for each part its resolved value, whether the kind drawn it, and its chain of contributions lowest first, each %{layer, container, style, via, field, value, set, generators, reading} with the layers of §3.7 (:theme, :defaults, :kind, :palette, :style, :field, :generator, :state, :inline, :local), reading the datum's value a field entry read. A generator is called as the Frame calls it; a result that applied nothing is {:fault, reason} in its entry's generators. A path compilation holds no site at is {:unknown, :site, at}.

lookup(styles, name, theme)

@spec lookup(term(), term(), term()) :: map()

The flat node a style name resolves to (§3.3, §3.5): the design's entry, else the theme's palette entry, extends flattened first and a list stacked left to right. A name neither declares, and a chain that returns to itself (a fault the validator reports), give %{}.

iex> styles = %{"heavy" => %{extends: "axis", stroke: %{width: 2}}}
iex> Macaw.Frame.Style.lookup(styles, "heavy", Macaw.Layers.Theme.default()).stroke.width
2

merge(under, over)

@spec merge(term(), term()) :: term()

The merge rule (§3.5): over laid on under, a part merged key by key.

normalize(node)

@spec normalize(term()) :: term()

A style node in its parts (§3.1), Macaw.Frame.Style.Parts.normalize/1.

project(node)

@spec project(term()) :: map()

A node's parts as the flat element keys (§3.4).

resolve(node, theme, mode, opts \\ [])

@spec resolve(term(), term(), term(), keyword()) ::
  {:ok, map()} | {:error, [Macaw.Base.Fault.t(), ...]}

A style node resolved in mode (§3.4): {:ok, element_style}, its parts projected and every theme name resolved, var(--mw-<name>, <literal>) under :css and the literal under :literal, every size in user units against opts' font_size:; a field binding is kept for bind/4. A variable still in it, a color the theme does not name, or a size in no unit is {:error, faults}, each at its element key.

stack(styles, stack, inherited, opts \\ [])

@spec stack(term(), term(), term(), keyword()) :: map()

A site's stack laid into one node (§3.6): each entry left to right over inherited (a Macaw.Frame.Style.Inherited or a node), the later winning. Options: pick: a function from a field's name to the datum's value, which a field entry names a style by (nil, a name no style has, or a value of another type lays nothing), and which a generator's field arguments are read through (nil without one); generators: the Macaw.Draw.Generators registry a generator entry is looked up in (none: every generator is unregistered and lays nothing); context: the context map a generator is called with (%{}). A generator's result is laid when it is a style (Macaw.Frame.Style.Generated.style/1) and nothing otherwise.

stacks(styles, stack, inherited, data, opts \\ [])

@spec stacks(term(), term(), term(), term(), keyword()) :: {[map()], [term()]}

A site's stack laid once per datum of data (§3.6, §3.13): {nodes, faults}, one node for each datum in order, every field entry and generator argument read from that datum (a map, by field name). A generator is called once per distinct (name, arguments) over the data, its result reused; a result that is no style, or a name the registry lacks, applies nothing and is a fault reason in faults, each once. Options as stack/4's, but pick:, which each datum gives.

iex> theme = Macaw.Layers.Theme.default()
iex> {nodes, faults} = Macaw.Frame.Style.stacks(%{}, [%{opacity: 0.5}], theme, [%{}, %{}])
iex> {length(nodes), faults}
{2, []}

with_state(stack, state)

@spec with_state(term(), term()) :: list()

A stack with a view-state entry laid at its layer (§3.7): after its names, field and generator entries and before a trailing inline node, so feedback shows over what the data chose and the local edit stays above everything.

iex> Macaw.Frame.Style.with_state(["series", {:field, "health"}, %{opacity: 1}], "selected")
["series", {:field, "health"}, "selected", %{opacity: 1}]