package mosaic

  1. Overview
  2. Docs

Module Mosaic_ui.RenderableSource

Mutable UI tree nodes with layout integration.

Mutable UI tree nodes with layout integration.

A t combines a layout node with rendering callbacks, event handlers, focus state, and lifecycle hooks. Nodes form a mutable tree: parent-child relationships drive both layout computation and rendering order.

Widgets create nodes with create, configure them with setters, and render content through callbacks. The renderer drives the pipeline through Private.

Types

Sourcetype t

The type for mutable nodes in the UI tree.

Sourcetype render = t -> Grid.t -> delta:float -> unit

The type for render callbacks. render self grid ~delta draws self into grid. delta is the elapsed time in milliseconds since the last frame.

Sourcetype measure = known_dimensions:float option Toffee.Geometry.Size.t -> available_space:Toffee.Available_space.t Toffee.Geometry.Size.t -> style:Toffee.Style.t -> float Toffee.Geometry.Size.t

The type for intrinsic size computation callbacks. Called during layout when a node lacks explicit dimensions.

Sourcetype cursor = {
  1. x : int;
  2. y : int;
  3. style : [ `Block | `Line | `Underline ];
  4. color : Ansi.Color.t;
  5. blinking : bool;
}

The type for hardware cursor descriptions. Coordinates are absolute terminal cell positions.

Sourceval equal_cursor : cursor -> cursor -> bool

equal_cursor a b is true iff all fields of a and b match.

Sourceval pp_cursor : Format.formatter -> cursor -> unit

pp_cursor ppf c formats c on ppf for debugging.

Constructors

Sourceval create : parent:t -> ?index:int -> ?id:string -> ?style:Toffee.Style.t -> ?visible:bool -> ?z_index:int -> ?opacity:float -> ?live:bool -> ?render:render -> unit -> t

create ~parent () is a new node attached as a child of child_target parent. When index is omitted the node is appended last; otherwise it is inserted at the given position, clamped to [0;child_count].

Optional parameters:

  • id: unique identifier string. Defaults to "node-N".
  • style: flexbox style. Defaults to Toffee.Style.default.
  • visible: initial visibility. Defaults to true.
  • z_index: rendering order; higher values render on top. Defaults to 0.
  • opacity: opacity clamped to [0.0;1.0]. Defaults to 1.0.
  • live: continuous rendering every frame. Defaults to false.

Raises Invalid_argument if the layout tree rejects the node.

Sourceval attach : parent:t -> ?index:int -> t -> unit

attach ~parent t attaches t as a child of child_target parent, detaching t from any previous parent first. When index is omitted the node is appended last.

Raises Invalid_argument if parent and t belong to different trees, or if either node is destroyed.

Sourceval detach : t -> unit

detach t removes t from its parent. The node remains valid and can be reattached with attach. Blurs t if focused. No-op if already detached.

Sourceval destroy : t -> unit

destroy t detaches t, removes all children (clearing their parent pointers and lifecycle registrations), frees its layout node, and clears all handlers. The node becomes unusable. No-op if already destroyed.

Children are removed but not themselves destroyed — they become orphaned nodes that can be reattached elsewhere. Use destroy_recursively to destroy the entire subtree.

Sourceval destroy_recursively : t -> unit

destroy_recursively t destroys t and all descendants depth-first. See also destroy.

Sourceval destroyed : t -> bool

destroyed t is true iff destroy has been called on t.

Identity

Sourceval id : t -> string

id t is t's string identifier.

Sourceval parent : t -> t option

parent t is t's parent, or None if t is detached or a root.

Sourceval children : t -> t list

children t is t's children in insertion order. The list is a snapshot; subsequent mutations are not reflected.

Sourceval child_target : t -> t

child_target t is the node that receives children on behalf of t. Defaults to t itself. Composite widgets override this to redirect children to an internal container (e.g. a scroll box routes children to its content node).

See also set_child_target.

Sourceval set_child_target : t -> t option -> unit

set_child_target t target sets the node that receives children on behalf of t. None resets to t itself.

Layout

Sourceval set_style : t -> Toffee.Style.t -> unit

set_style t style updates t's flexbox style, marks layout dirty, and schedules a re-render. When explicit dimensions are present and flex_shrink is still at its default 1.0, it is automatically set to 0.0 to match terminal layout conventions.

Sourceval style : t -> Toffee.Style.t

style t is t's current flexbox style.

Sourceval set_measure : t -> measure option -> unit

set_measure t fn assigns a custom measure function for intrinsic sizing and marks layout dirty. None clears it.

Sourceval mark_dirty : t -> unit

mark_dirty t flags t for re-layout on the next frame.

Sourceval x : t -> int

x t is t's absolute horizontal position in terminal cells.

Sourceval y : t -> int

y t is t's absolute vertical position in terminal cells.

Sourceval width : t -> int

width t is t's layout width in terminal cells. Returns 0 when t is hidden or layout has not yet been computed.

Sourceval height : t -> int

height t is t's layout height in terminal cells. Returns 0 when t is hidden or layout has not yet been computed.

Sourceval bounds : t -> Grid.region

bounds t is { x = x t; y = y t; width = width t; height = height t }.

Sourceval set_translate : t -> x:int -> y:int -> unit

set_translate t ~x ~y shifts t's rendering position by (x, y) without affecting layout. Useful for scrolling.

Sourceval translate : t -> int * int

translate t is t's current translation offset as (x, y).

Rendering

Sourceval set_render : t -> render -> unit

set_render t fn replaces t's render callback and schedules a re-render.

Sourceval request_render : t -> unit

request_render t schedules a re-render for t.

Sourceval set_render_before : t -> render option -> unit

set_render_before t hook assigns an optional pre-render hook. None clears it.

Sourceval set_render_after : t -> render option -> unit

set_render_after t hook assigns an optional post-render hook. None clears it.

Sourceval set_child_clip : t -> (t -> Grid.region option) option -> unit

set_child_clip t fn overrides the clipping rectangle applied to t's children. When set, the renderer calls fn t to obtain a scissor rectangle before rendering children. Useful for scroll containers and bordered boxes. None clears the override.

Visual properties

Sourceval set_visible : t -> bool -> unit

set_visible t v shows or hides t. Blurs t if it is focused while being hidden.

Sourceval visible : t -> bool

visible t is true iff t is currently visible.

Sourceval set_z_index : t -> int -> unit

set_z_index t z sets t's rendering order. Higher values render on top.

Sourceval z_index : t -> int

z_index t is t's z-index.

Sourceval set_opacity : t -> float -> unit

set_opacity t v sets t's opacity, clamped to [0.0;1.0]. Values below 1.0 cause the renderer to push an opacity context around t and its descendants.

Sourceval opacity : t -> float

opacity t is t's opacity in [0.0;1.0].

Sourceval set_buffered : t -> bool -> unit

set_buffered t v enables or disables offscreen buffering. When true, t renders into a private grid that is blitted to the parent grid.

Sourceval buffered : t -> bool

buffered t is true iff offscreen buffering is enabled.

Sourceval set_live : t -> bool -> unit

set_live t v enables or disables continuous rendering. When live, the renderer runs t's render callback every frame rather than only on request.

Sourceval live : t -> bool

live t is true iff continuous rendering is enabled.

Focus

Sourceval set_focusable : t -> bool -> unit

set_focusable t v marks t as focusable or not.

Sourceval focusable : t -> bool

focusable t is true iff t can receive focus.

Sourceval focused : t -> bool

focused t is true iff t currently holds focus.

Sourceval focus : t -> bool

focus t requests focus for t. Returns false if t is not focusable. Delegates to the renderer's focus controller.

Sourceval blur : t -> unit

blur t removes focus from t. No-op if t is not focused.

Sourceval set_cursor_provider : t -> (t -> cursor option) -> unit

set_cursor_provider t f registers a hardware cursor provider consulted when t is focused. f is called with t and returns the desired cursor state, or None to hide the cursor.

Sourceval clear_cursor_provider : t -> unit

clear_cursor_provider t removes t's cursor provider.

Sourceval cursor : t -> cursor option

cursor t is the current hardware cursor state as reported by t's cursor provider, or None if no provider is set.

Events

Sourceval on_mouse : t -> (Event.mouse -> unit) -> unit

on_mouse t handler registers a mouse event handler on t. Handlers accumulate and run newest-first. Mouse events bubble to ancestors unless Event.Mouse.stop_propagation is called.

Sourceval on_key : t -> (Event.key -> unit) -> unit

on_key t handler registers a keyboard handler on t. Handlers accumulate and run newest-first until one calls Event.Key.prevent_default.

Sourceval set_default_key_handler : t -> (Event.key -> unit) option -> unit

set_default_key_handler t handler assigns a fallback key handler that runs after on_key handlers. Only one fallback handler per node. None clears it.

Sourceval set_paste_handler : t -> (Event.paste -> unit) option -> unit

set_paste_handler t handler assigns a paste handler on t. Only one paste handler per node. None clears it.

Selection

Sourceval set_selection : t -> should_start:(x:int -> y:int -> bool) -> on_change:(Selection.t option -> bool) -> clear:(unit -> unit) -> get_text:(unit -> string) -> unit

set_selection t ~should_start ~on_change ~clear ~get_text enables text selection on t. The renderer calls these callbacks during mouse interactions and clipboard operations. The contract is:

  • should_start ~x ~y is true iff a selection drag starting at (x, y) should be initiated.
  • on_change sel is called when the selection changes; returns true to accept the selection.
  • clear () discards any active selection state.
  • get_text () returns the currently selected text.
Sourceval unset_selection : t -> unit

unset_selection t disables text selection on t.

Sourceval selectable : t -> bool

selectable t is true iff t has selection callbacks registered.

Line information

Sourcetype line_info = {
  1. line_count : int;
  2. display_line_count : int;
  3. line_sources : int array;
  4. line_wrap_indices : int array;
  5. scroll_y : int;
}

The type for display line metrics used by line-numbering widgets.

  • line_count: number of logical lines.
  • display_line_count: number of visual lines after wrapping.
  • line_sources.(i): logical line index for display line i.
  • line_wrap_indices.(i): sub-line index within the logical line — 0 for the first display line, 1 for the first continuation, and so on.
  • scroll_y: vertical scroll offset in display lines.
Sourceval set_line_info_provider : t -> (unit -> line_info) option -> unit

set_line_info_provider t f registers a function that supplies line metrics for t. Used by composite widgets (e.g. line-number gutters) to read display line information from a content child without coupling to its concrete type. None clears the provider.

Sourceval line_info : t -> line_info option

line_info t is the current line metrics from t's provider, or None if no provider is registered.

Lifecycle

Sourceval set_on_frame : t -> (t -> delta:float -> unit) option -> unit

set_on_frame t callback registers a per-frame update hook. callback t ~delta is called every frame with delta as the elapsed milliseconds. None clears the hook.

Sourceval set_on_resize : t -> (t -> unit) option -> unit

set_on_resize t callback registers a size-change hook. The callback is called when width or height changes after layout. None clears the hook.

Formatting and inspecting

Sourceval pp : Format.formatter -> t -> unit

pp ppf t formats t on ppf for debugging.

Renderer integration

Privileged operations used by the renderer during the render pipeline and event dispatch. Widget authors must not call these directly.

Sourcemodule Private : sig ... end