# `Macaw.Frame.Coordinates`
[🔗](https://github.com/dcoai/macaw_frame/blob/v0.1.0/lib/macaw/frame/coordinates.ex#L1)

The coordinate-model behavior and the model registry (spec/03 §1).

A frame places its layers through **one coordinate model**: the module that owns its
scales (`Macaw.Frame.Scales`), its furniture, its clip, how it answers a pointer, and
what it may write to the page's state. The Frame asks the model, never a `case` over
model names. A model is a registered module implementing this behavior; the five of
spec/03 are `macaw_frame`'s (`Macaw.Frame.Package`): `Cartesian`, `Polar`,
`Geographic`, `Layout` and `Facet` under this namespace.

**Every callback is pure** and answers every input: a refusal is `{:error, faults}`,
each fault a `Macaw.Base.Fault` with its path in the frame document; none raises.

## What a model realizes from

`realize/3` takes the frame node (canonical, as `Macaw.Frame.Document` holds it), the
**inputs** and options. The inputs are what the Frame has after the layers' data steps:

  * `:layers` - the frame's layers in design order, each a map: `:index` (its position
    among the frame's layers), `:kind` (the layer kind's name), `:channels` (channel to
    its reading: `{:field, f}`, a field's name, a number, `{:scale, :min | :max}`),
    `:scales` (the layer's renames), `:offset`, `:colors` (the fields its style binds
    to color), `:slot` (the slot it reads) or `:output` (a layout output it reads),
    `:table` (its rows after its data steps, a `Macaw.Data.Table`), `:label` (its
    inline label node) and `:order`;
  * `:slots` - each slot by name: `:table` (the delivery's rows before any step),
    `:schema` (its `Macaw.Data.Schema`) and `:served` (the delivery's served facts);
  * `:design` - the whole design, for scales adopted from other frames (§2.5) and the
    design's interaction.

The options: `:name` (the frame's name), `:size` (`{width, height}` of the Frame
component, `{600, 400}` by default), `:layout` (the design's grid), `:theme`, `:units`
and `:zone` (the viewer's preferences), `:domains` (extents given in place of
inference), `:sync` (the cell's group), `:operations` (the operations registry a layout
runs through) and `:warm` (a layout's carried state).

## The registry

Models are members of packages (§1.3): a package's `__members__/0` lists modules, and
each one implementing this behavior is a model, named by `name/0`'s spelling.
`registry/1` builds the registry of a list of packages, `macaw_frame`'s own always in;
`fetch/2` reads it by spelling. The registry is the Frame module's, never the
component context's.

# `clip`

```elixir
@type clip() :: {:rect, number(), number(), number(), number()}
```

A clip: a rectangle in the plot group's own coordinates.

# `error`

```elixir
@type error() :: {:error, [Macaw.Base.Fault.t(), ...]}
```

A refusal: faults with their paths.

# `frame`

```elixir
@type frame() :: map()
```

A frame node of the frame document, canonical.

# `inputs`

```elixir
@type inputs() :: map()
```

What a model realizes from (see the module doc).

# `registry`

```elixir
@type registry() :: %{required(String.t()) =&gt; module()}
```

A registry of models by name.

# `auto_range`

```elixir
@callback auto_range(String.t(), map(), map()) :: {:ok, list()} | :none
```

The range of a scale whose `range` is `:auto` (§2.4), `:none` for no default.

# `bend`

```elixir
@callback bend(Macaw.Frame.Coordinates.Model.t(), Macaw.Draw.IR.Element.t()) ::
  Macaw.Draw.IR.Element.t()
```

A layer's scene element in the model's layout space, turned into plot pixels.

# `channel_scale`

```elixir
@callback channel_scale(String.t(), String.t() | atom(), frame()) :: String.t() | :pixels
```

The scale family a layer's channel reads through (§2.2), `:pixels` for none.

# `clip`

```elixir
@callback clip(Macaw.Frame.Coordinates.Model.t()) :: clip() | [clip()]
```

The plot's clip rectangle in the plot group's coordinates; a facet's, one a panel.

# `context`

```elixir
@callback context(Macaw.Frame.Coordinates.Model.t()) :: map()
```

The spatial context the frame's spatial transforms run under.

# `furniture`

```elixir
@callback furniture(Macaw.Frame.Coordinates.Model.t(), keyword()) ::
  {:ok, [Macaw.Draw.IR.Element.t()]} | error()
```

The grid, the axes, the legend and the labels as scene elements, placed (§3).

# `gesture`

```elixir
@callback gesture(Macaw.Frame.Coordinates.Model.t(), map()) ::
  {:ok, Macaw.Frame.Writes.event()} | :none
```

A finished gesture as the event the Frame emits (§5.3), `:none` for one not taken.

# `invert`

```elixir
@callback invert(Macaw.Frame.Coordinates.Model.t(), {number(), number()}) ::
  {:ok, term()} | :none
```

A plot pixel back to a domain point, where the position scales invert (§4.3).

# `keys`

```elixir
@callback keys() :: [{String.t(), Macaw.Data.Operations.Option.t()}]
```

The frame keys the model reads beyond every frame's, each declared as an option.

# `local`

```elixir
@callback local(Macaw.Frame.Coordinates.Model.t()) :: %{required(String.t()) =&gt; atom()}
```

The `:local` keys it shares under the cell's `sync` group (§5.2).

# `name`

```elixir
@callback name() :: atom()
```

The model's name, unique in a registry: `:cartesian`, `:polar`, … (§1.1).

# `place`

```elixir
@callback place(Macaw.Frame.Coordinates.Model.t(), term()) :: {number(), number()} | nil
```

A domain point to plot pixels, `nil` for one the model cannot place.

# `realize`

```elixir
@callback realize(frame(), inputs(), keyword()) ::
  {:ok, Macaw.Frame.Coordinates.Model.t()} | error()
```

The realized model (§1.2).

# `summary`

```elixir
@callback summary(Macaw.Frame.Coordinates.Model.t()) :: map()
```

The model's part of the text summary: its kind word and its axes' descriptions.

# `validate`

```elixir
@callback validate(frame(), map()) :: [Macaw.Base.Fault.t()]
```

The model's own checks of a frame node: every fault at once, `[]` for a good one.

# `writes`

```elixir
@callback writes(Macaw.Frame.Coordinates.Model.t()) :: %{
  required(String.t()) =&gt; :identity | :window
}
```

The standard state sources the realized model can write (§5.1).

# `box_of`

```elixir
@spec box_of(term(), term(), term()) :: {:ok, [number()]} | error()
```

A frame's box as fractions of the Frame component's size (§1.4): its `box`, or the
share of the document's `layout` grid its `cell` and `span` take, the weights
normalized over their sum and the `gap` (pixels) taken out of the cells, never the
grid's outer edges. The one place a cell becomes a box. `size` is `{width, height}`.

    iex> Macaw.Frame.Coordinates.box_of(%{box: [0.5, 0, 0.5, 1]}, nil, {600, 400})
    {:ok, [0.5, 0, 0.5, 1]}
    iex> Macaw.Frame.Coordinates.box_of(%{cell: [1, 0]}, %{columns: [3, 1]}, {600, 400})
    {:ok, [0.75, 0.0, 0.25, 1.0]}

# `callbacks`

```elixir
@spec callbacks() :: keyword(non_neg_integer())
```

The behavior's callbacks, `[{name, arity}]`.

# `default_size`

```elixir
@spec default_size() :: {600, 400}
```

The size a frame is realized at when none is given: `{600, 400}`.

# `fetch`

```elixir
@spec fetch(term(), term()) :: {:ok, module()} | :error
```

The model a registry holds by name, read by spelling: `{:ok, module}` or `:error`.

    iex> {:ok, registry} = Macaw.Frame.Coordinates.registry([])
    iex> Macaw.Frame.Coordinates.fetch(registry, :polar)
    {:ok, Macaw.Frame.Coordinates.Polar}
    iex> Macaw.Frame.Coordinates.fetch(registry, "geo")
    :error

# `model?`

```elixir
@spec model?(term()) :: boolean()
```

Whether a module implements every callback of the behavior.

# `names`

```elixir
@spec names(term()) :: [String.t()]
```

The registry's model names, sorted.

# `order`

```elixir
@spec order(term()) :: [String.t()]
```

The frames of a document in drawing order (§1.4): by `z`, ties by name.

    iex> Macaw.Frame.Coordinates.order(%{"b" => %{z: 1}, "a" => %{z: 1}, "c" => %{}})
    ["c", "a", "b"]

# `realize`

```elixir
@spec realize(term(), term(), term(), term()) ::
  {:ok, Macaw.Frame.Coordinates.Model.t()} | error()
```

The frame realized by the model its `kind` names in the registry: the model's
`realize/3`, or `{:unknown, :coordinates, name}` at the frame's `kind` (§1.1).

# `realize_all`

```elixir
@spec realize_all(term(), term(), term(), term()) ::
  {:ok, %{required(String.t()) =&gt; Macaw.Frame.Coordinates.Model.t()}} | error()
```

Every frame of a design realized, with its adopted scales shared (§2.5): `{:ok, %{name
=> model}}`, or every fault at once. `inputs` are each frame's by name (the design is
added to each). Two passes: every frame realizes over its own layers, then each
adoption group's domains are united (`Macaw.Frame.Scales.unite/1`) and its frames
realize again with the union under `domains`, each keeping its own range. Adoption is
within one design: two Frame components never share a scale.

# `refresh`

```elixir
@spec refresh(term(), term(), term(), term(), term()) ::
  {:ok, Macaw.Frame.Coordinates.Model.t()} | error()
```

The model after a change (§1.2): `:init` and `:size` always realize; `{:delivery, slot}`
realizes only when a scale the model infers reads the slot, its fit reads it or its
layout does, and otherwise answers the model unchanged; `:render` never realizes. A
realization reuses every fixed scale the model already holds.

# `registry`

```elixir
@spec registry(term()) :: {:ok, registry()} | error()
```

The registry of the models the packages hold, `macaw_frame`'s own (`Macaw.Frame.Package`)
always in: `{:ok, %{name => module}}`, or every fault at once. Two members naming one
model are `{[:coordinates, name], {:named_twice, module, other}}`; a module that is not a
package `{[module], :not_a_package}`. A member that implements no model is skipped: it
is another registry's (a layer kind, an operation).

    iex> {:ok, registry} = Macaw.Frame.Coordinates.registry([])
    iex> Macaw.Frame.Coordinates.names(registry)
    ["cartesian", "facet", "geographic", "layout", "polar"]

---

*Consult [api-reference.md](api-reference.md) for complete listing*
