Raxol.UI.Components.Input.Scrubber (Raxol v2.9.2)

View Source

Transport control for anything with an ordered position: a track, a playhead, optional tick marks, a clock, and play/pause.

Raxol has three unrelated timelines -- asciicast recordings (Raxol.Recording.Session, µs-stamped byte events), time-travel snapshots (Raxol.Debug.TimeTravel, absolute integer indices over a ring), and the web's prerecorded frame lists (uniform interval_ms ticks). Each had grown its own hand-assembled position readout and its own keymap. This is the one widget all three project onto.

Features

  • Track with a sub-column playhead, filled and remaining runs distinguished
  • Tick marks at arbitrary positions (input events, changed snapshots, chapters)
  • Clock in mm:ss / mm:ss when a duration is known, position/max otherwise
  • Play/pause with a speed ladder matching Raxol.Recording.Player
  • Keyboard transport: step, jump to percent, jump to mark, ends
  • Pure renderers (track/1, clock/1, line/1) usable without a component tree, so an imperative caller writing to a TTY gets the same pixels

Public API

Props

  • :id - Widget identifier. Auto-minted from Raxol.Core.ID when absent.
  • :min - Lowest addressable position (integer, default 0). Not always zero: a wrapped TimeTravel ring addresses first..last.
  • :max - Highest addressable position (integer, default 0).
  • :position - Current position, clamped into min..max.
  • :playing? - Whether the transport is advancing (default false).
  • :speed - Playback multiplier (default 1.0). Rendered only when not 1.0.
  • :marks - Positions to tick on the track (default []). Out-of-range marks are dropped rather than clamped onto the ends, where they would read as a playhead at rest.
  • :width - Track width in columns (default 24, floor 3).
  • :elapsed_ms / :duration_ms - Wall-clock position and total. Supplied together by the caller rather than derived from position, because only a uniform-tick timeline has a linear index-to-time map; an asciicast does not.
  • :label - Optional leading label.
  • :on_seek - (position -> message), run on every position change.
  • :on_play / :on_pause - (-> message), run on the transport toggle.
  • :on_speed - (speed -> message), run when the speed ladder moves.
  • :disabled - Inert: no key handling, no MCP tools.
  • :aria_label, :tooltip - Accessibility passthrough.

Keys

space play/pause; left/h/,/< back one; right/l/./> forward one; home/end to the ends; [/] to the previous/next mark; 0-9 to 0%-90%; +/= and - up and down the speed ladder. Mined from Raxol.Recording.Player, so a player rebuilt on this widget keeps the bindings its docstring already advertises.

Theming and Style Precedence

Resolved by Raxol.UI.StyleHelper.merge_component_styles/3 under the :scrubber key. :disabled_fg wins over :focused_fg, which wins over :fg; the filled run reads :fg and the remaining run :track_fg (falling back to :dim_fg, then :fg).

Example

A doctest, not a transcription: the previous hand-written "Rendered:" line disagreed with its own props in six ways (separator width, the mark at column 0, filled glyphs right of the playhead, two missing marks, two missing columns, and a 2x label that speed_label/1 suppresses at 1.0), and nothing executed it.

iex> alias Raxol.UI.Components.Input.Scrubber
iex> Scrubber.new(
...>   min: 0,
...>   max: 47,
...>   position: 12,
...>   duration_ms: 4_300,
...>   elapsed_ms: 1_100,
...>   marks: [0, 18, 33],
...>   playing?: true
...> )
...> |> Scrubber.line()
">   00:01 / 00:04  ┃━━━━━●──┃──────┃───────"

Summary

Functions

The columns line/1 spends on everything except the track.

Renders the position readout.

Normalizes props into a view-tree node without minting an id.

Renders the whole transport as one line: state glyph, clock, track, speed.

The set of track columns carrying a mark, for a given width and range.

Creates a scrubber state. See init/1.

Moves the playhead, clamped into min..max.

Renders the state as a declaration node carrying type: :scrubber.

Renders the track as a single string.

The transport state glyph: > playing, || paused.

Types

t()

@type t() :: %{
  id: String.t(),
  label: String.t() | nil,
  min: integer(),
  max: integer(),
  position: integer(),
  playing?: boolean(),
  speed: float(),
  marks: [integer()],
  width: pos_integer(),
  elapsed_ms: non_neg_integer() | nil,
  duration_ms: non_neg_integer() | nil,
  on_seek: (integer() -> any()) | nil,
  on_play: (-> any()) | nil,
  on_pause: (-> any()) | nil,
  on_speed: (float() -> any()) | nil,
  disabled: boolean(),
  focused: boolean(),
  style: map(),
  theme: map(),
  aria_label: String.t() | nil,
  tooltip: String.t() | nil
}

Functions

broadcast(msg)

chrome_width(opts)

@spec chrome_width(t() | keyword() | map()) :: non_neg_integer()

The columns line/1 spends on everything except the track.

A caller sizing the track to a terminal needs this and nothing else, so it must not be obtained by rendering a whole line and subtracting: that draws the track (and resolves every mark) purely to measure the parts beside it, which on a per-frame repaint is the dominant cost of the frame.

clock(opts)

@spec clock(t() | keyword() | map()) :: String.t()

Renders the position readout.

mm:ss / mm:ss when :duration_ms is known, position/max otherwise -- an index pair rather than a fabricated time, because only a uniform-tick timeline can turn an index into a timestamp.

iex> Raxol.UI.Components.Input.Scrubber.clock(position: 12, min: 0, max: 47)
"12/47"

iex> Raxol.UI.Components.Input.Scrubber.clock(elapsed_ms: 63_000, duration_ms: 125_000)
"01:03 / 02:05"

command(cmd)

declaration(props)

@spec declaration(keyword() | map()) :: map()

Normalizes props into a view-tree node without minting an id.

init/1 mints one when :id is absent, which is right for a mounted component: it runs once, in the owning app's process, which is the premise Raxol.Core.ID's per-process counter is documented against. A view/1 helper runs on every frame, so minting there yielded scrubber-1, scrubber-2, ... on successive repaints -- churning the MCP tool handles (TreeWalker names them "#{widget_id}.seek"), making FocusHelper.focused?/2 unable to match a stable focused_element, and rewriting the "#{id}-track" segment ids every frame.

A declaration therefore carries whatever :id the caller gave, nil included. TreeWalker.do_walk/4 requires a non-empty binary id, so an unnamed scrubber derives no tools rather than tools nobody can address -- the same contract progress/1 and the other declaration helpers have.

line(opts)

@spec line(t() | keyword() | map()) :: String.t()

Renders the whole transport as one line: state glyph, clock, track, speed.

This is the form an imperative caller writes straight to a TTY (see Raxol.Recording.Player), and it is what render/2 composes from.

mark_columns(opts)

@spec mark_columns(t() | keyword() | map()) :: MapSet.t(non_neg_integer())

The set of track columns carrying a mark, for a given width and range.

track/1 computes this itself unless :mark_columns is supplied, which is the point of exposing it: the work is O(marks log marks) and it depends on nothing that changes between frames. A caller repainting per event (Raxol.Recording.Player repaints its status bar on every output event) computes it once and passes it back, instead of re-sorting the whole mark list to draw at most width columns.

mount(state)

Callback implementation for Raxol.UI.Components.Base.Component.mount/1.

new(props \\ [])

@spec new(keyword() | map()) :: t()

Creates a scrubber state. See init/1.

schedule(msg, delay)

seek(state, position)

@spec seek(t(), integer()) :: t()

Moves the playhead, clamped into min..max.

to_node(state, context \\ %{})

@spec to_node(t(), map()) :: map()

Renders the state as a declaration node carrying type: :scrubber.

render/2 alone is not discoverable: it lays out as a :row, and both Raxol.MCP.TreeWalker and Raxol.Core.Accessibility.Projection dispatch on the declaration type. This stamps the discovery alias and copies the transport fields onto the node, because mcp_tools/1 and a11y_node/1 read the node rather than component state.

Emit this from a TEA view/1 when the scrubber should be drivable by an agent or announced by a screen reader; render/2 is enough for a purely visual bar.

track(opts)

@spec track(t() | keyword() | map()) :: String.t()

Renders the track as a single string.

Accepts the component state or a bare map/keyword of :width, :min, :max, :position, :marks. Column precedence is playhead, then mark, then the filled/remaining run, so a mark under the playhead never hides it.

iex> Raxol.UI.Components.Input.Scrubber.track(width: 8, min: 0, max: 7, position: 3)
"━━━●────"

transport(opts)

@spec transport(t() | keyword() | map()) :: String.t()

The transport state glyph: > playing, || paused.

State, not the action a button would take. Padded to two columns so the line does not shift when playback toggles.

unmount(state)

Callback implementation for Raxol.UI.Components.Base.Component.unmount/1.