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

The selection as a cell's fields hold it, and the view state each data layer draws it
through (spec/05 §7.1), copied from visualize's `Visualize.Interaction`
(`selection_fields/1`, `holds?/2`) and the selection drawing of `Visualize.Visual`. The
interaction holder itself is not carried over: **a Frame draws the selection it is
handed, never one it holds.**

    @type selection :: nil | [%{fields: [String.t()], value: term()}]

- **One entry per field of the identity**, in field-name order, each with the fields of
  the cell's slots that link to it (links already applied by the host) and the value.
- **A row holds the selection** when, for every entry, one of its fields has the
  entry's value (`holds?/2`), computed over a table's packed columns by `column/2`
  through `Macaw.Frame.Columns`, never row by row.
- **A Frame showing it** (a row of a layer's table holds it) draws every data layer's
  rows in a view state, `"selected"` for a row that holds it and `"muted"` for every
  other (`states/2`), the names resolving as every palette name does (spec/02 §3.7), so a
  document or a theme restyles them; the state is a column beside the table, a list
  aligned to its rows, never a copy of them. **A Frame that does not show it draws
  exactly as before.**
- **Until the core hands the selection in a cell's fields** (spec/01 §9,
  `Macaw.Frame.Contract`), what a host hands is the state's identity map, which is no
  selection of this shape: `entries/1` reads it as `nil`, and the Frame draws none.

# `t`

```elixir
@type t() :: nil | [%{fields: [String.t()], value: term()}]
```

A selection in a cell's fields (§7.1).

# `column`

```elixir
@spec column(term(), term()) :: [boolean()] | nil
```

Which rows of `table` hold the selection (§7.1): a list of booleans aligned to the
table's rows, read from the entries' fields' packed columns through
`Macaw.Frame.Columns.read/3`, each field once; `nil` with no selection, or a table that
has none of the fields an entry names.

    iex> {:ok, t} = Macaw.Data.Table.from([%{"host" => "h1"}, %{"host" => "h2"}])
    iex> Macaw.Frame.Selection.column(t, [%{fields: ["host"], value: "h2"}])
    [false, true]

# `entries`

```elixir
@spec entries(term()) :: t()
```

The selection's entries, sorted by their first field (visualize's
`selection_fields/1`): a list of `%{fields, value}`, each `fields` a non-empty list of
names, else `nil`: no selection, an empty one, or a term of another shape (the identity
map a host hands before the core resolves it in fields).

    iex> Macaw.Frame.Selection.entries([%{fields: ["host", "node"], value: "h2"}])
    [%{fields: ["host", "node"], value: "h2"}]
    iex> Macaw.Frame.Selection.entries(%{"host" => "h2"})
    nil

# `holds?`

```elixir
@spec holds?(term(), term()) :: boolean()
```

Whether a row holds the selection (visualize's `holds?/2`): for every entry, one of its
fields has the entry's value; `false` with no selection. A row's keys may be atoms or
strings.

    iex> s = [%{fields: ["host"], value: "h2"}]
    iex> {Macaw.Frame.Selection.holds?(s, %{"host" => "h2"}), Macaw.Frame.Selection.holds?(s, %{host: "h1"})}
    {true, false}

# `shown?`

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

Whether a held column shows the selection: some row holds it.

    iex> {Macaw.Frame.Selection.shown?([false, true]), Macaw.Frame.Selection.shown?(nil)}
    {true, false}

# `states`

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

The view state of each row (§7.1): `"selected"` where the row holds the selection,
`"muted"` elsewhere, as the style grammar names them.

    iex> Macaw.Frame.Selection.states([true, false])
    ["selected", "muted"]

---

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