# `Macaw.Draw.Layout.Sequence`
[🔗](https://github.com/dcoai/macaw_draw/blob/v0.1.0/lib/macaw/draw/layout/sequence.ex#L1)

Sequence lanes (spec/08 §12): a sequence diagram's participants as lanes across and its
events down, the second cut's sequence diagram layout.

    iex> alias Macaw.Draw.Layout.Sequence
    iex> {:ok, sequence} = Sequence.new(participant_sep: 20)
    iex> participants = [%{id: "a", width: 40, height: 20}, %{id: "b", width: 40, height: 20}]
    iex> {:ok, result} =
    ...>   Sequence.generate(sequence, participants, [{:message, %{from: "a", to: "b", label: {80, 10}}}])
    iex> Enum.map(result.participants, & &1.x)
    [20.0, 116.0]

**Lanes across** (§12.3). Lane 0's center is `margin + width_0 / 2`, each next gap starts
at `(width_i + width_{i+1}) / 2 + participant_sep`, and every requirement widens the gaps:
a message's label between its lanes, a self-message's loop and label or a note beside a
lane in the gap after it (before it for `left_of`), a note over lanes between them. The
requirements are applied narrowest span first, then in event order, each deficit added
to the **last** gap it spans, as Mermaid's sequence layout does (spec/08 decision 11).

**Events down** (§12.4). Headers occupy `[margin, margin + H]`, `H` the tallest, and a
cursor starting `message_sep` below them is advanced by each event: a message's arrow
sits under its label, a note's box at the cursor, a fragment's label and sections, a
gap. Activations nest per lane, each `activation_offset` right of the one it is inside,
and an arrow ends at the edge of its target's innermost bar. Fragments enclose their
events, padded by `fragment_pad` per level so a nested one sits inside its parent. A
created participant's header is placed at its create event; a destroyed one's lifeline
ends at the destroy event.

Options (§12.2), checked by `new/1`, each a non-negative number but `mirror`:

| Option | Default |
|---|---|
| `participant_sep` | `40` |
| `message_sep` | `16` |
| `label_pad` | `8` |
| `self_width`, `self_height` | `30`, `24` |
| `activation_width` | `10` |
| `activation_offset` | `5` |
| `fragment_pad` | `10` |
| `note_pad` | `10` |
| `mirror` | `false` |
| `margin` | `0` |

# `event`

```elixir
@type event() ::
  {:message, map()}
  | {:create, map()}
  | {:destroy, term()}
  | {:note, map()}
  | {:open, map()}
  | {:section, map() | nil}
  | {:close, nil}
  | {:gap, number()}
```

One event (§12.1).

# `t`

```elixir
@type t() :: %Macaw.Draw.Layout.Sequence{
  activation_offset: number(),
  activation_width: number(),
  fragment_pad: number(),
  label_pad: number(),
  margin: number(),
  message_sep: number(),
  mirror: boolean(),
  note_pad: number(),
  participant_sep: number(),
  self_height: number(),
  self_width: number()
}
```

# `columns`

```elixir
@spec columns(Macaw.Draw.Layout.Sequence.Result.t(), atom() | String.t()) ::
  %{required(String.t()) =&gt; [term()]} | Macaw.Draw.Layout.error()
```

The columns of a sequence diagram (§12.5), `"row"` the event's index (a participant's
its position in the list):

- `:participants`: `"row"`, `"id"`, `"x"`, `"x0"`, `"y0"`, `"x1"`, `"y1"` (the header),
  `"footer_y0"`, `"footer_y1"` (`nil` without a footer), `"lifeline_y0"`, `"lifeline_y1"`;
- `:messages`: `"row"`, `"from"`, `"to"`, `"kind"` (its spelling), `"x0"`, `"x1"`, `"y"`,
  `"path"`, `"label_x"`, `"label_y"`, `"end_angle"`;
- `:activations`: `"row"` (the opening event), `"lane"`, `"depth"`, `"x0"`, `"y0"`,
  `"x1"`, `"y1"`;
- `:notes`: `"row"`, `"placement"`, `"x0"`, `"y0"`, `"x1"`, `"y1"`;
- `:fragments`: `"row"` (the `:open`), `"kind"`, `"depth"`, `"x0"`, `"y0"`, `"x1"`,
  `"y1"`, `"label_x"`, `"label_y"`, `"sections"` (each row its dividers' y);
- `:destroys`: `"row"`, `"lane"`, `"x"`, `"y"`, `"path"` (the cross).

# `generate`

```elixir
@spec generate(t(), [map()], [event()]) ::
  {:ok, Macaw.Draw.Layout.Sequence.Result.t()} | Macaw.Draw.Layout.error()
```

The sequence diagram of `participants`, ordered `%{id, width, height}` header boxes in
lane order, and `events`, ordered as §12.1's table lists them (§12.3, §12.4).

A message is `%{from, to}` with optional `kind` (one of `kinds/0` or its spelling,
`:sync` by default), `label` (`{w, h}` or `nil`), `activate` and `deactivate` (`:target`,
`:source` or `nil`); a note `%{over: [id] | [id, id]}`, `%{left_of: id}` or
`%{right_of: id}` with `size: {w, h}`; a fragment `%{kind, label}`.

Answers `{:ok, %Sequence.Result{}}` or `{:error, faults}`, each at its event's path: a
participant no list names `{[:events, i, field], {:unknown_participant, id}}`; a
`:close` or `:section` with no open fragment `{[:events, i], :no_fragment}`; a fragment
left open `{[:events, i], :unclosed}` at its `:open`; a `deactivate` with no open
activation `{[:events, i, :deactivate], :no_activation}`; a message from or to a
destroyed lane `{[:events, i, field], {:destroyed, id}}`; an event of no known shape
`{[:events, i], {:type, :event}}`.

# `kinds`

```elixir
@spec kinds() :: [atom()]
```

The message kinds: `[:sync, :async, :reply]`.

# `new`

```elixir
@spec new() :: {:ok, t()}
```

`new/1` with every option at its default.

# `new`

```elixir
@spec new(keyword() | map()) :: {:ok, t()} | Macaw.Draw.Layout.error()
```

The layout of `opts` (§12.2): `{:ok, sequence}` or `{:error, faults}`.

---

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