package mosaic

  1. Overview
  2. Docs

Module Mosaic_ui.Text_surfaceSource

Text rendering surface with wrapping, viewport, and selection.

Text rendering surface with wrapping, viewport, and selection.

A surface wraps a Text_buffer.t and a Renderable.t, providing line wrapping, viewport-based scrolling, intrinsic measurement, and selection overlay rendering. The surface registers render and measure callbacks on the renderable automatically.

Used by Text for static rich text display, by Textarea for multi-line editing, and potentially by other renderables that need text buffer rendering.

See Text_buffer for the underlying styled text storage.

Sourcetype t

A text rendering surface.

Sourcetype wrap = [
  1. | `None
  2. | `Char
  3. | `Word
]

Wrapping mode.

  • `None: no wrapping; lines extend beyond the viewport.
  • `Char: break at grapheme cluster boundaries.
  • `Word: break at word boundaries (spaces, hyphens, etc.); falls back to `Char when a word exceeds the wrap width.

Construction

create node buffer is a surface that renders buffer through node. Registers render and measure callbacks on node.

Accessors

Sourceval buffer : t -> Text_buffer.t

buffer t is the underlying text buffer.

Sourceval node : t -> Renderable.t

node t is the underlying renderable.

Wrapping

Sourceval wrap : t -> wrap

wrap t is the current wrapping mode.

Sourceval set_wrap : t -> wrap -> unit

set_wrap t mode changes the wrapping mode. Invalidates display lines and marks layout dirty.

Sourceval wrap_width : t -> int option

wrap_width t is the explicit wrap width, or None when the width is derived from the renderable's layout width.

Sourceval set_wrap_width : t -> int option -> unit

set_wrap_width t w sets an explicit wrap width. None derives the width from the renderable's layout. Invalidates display lines and marks layout dirty.

Sourceval truncate : t -> bool

truncate t is true when lines are truncated with an ellipsis. Only applies when wrap t = `None.

Sourceval set_truncate : t -> bool -> unit

set_truncate t v enables or disables line truncation. When enabled and wrap = `None, lines exceeding the viewport width are cut with an ellipsis character. Invalidates display lines and marks layout dirty.

Viewport

Sourceval scroll_x : t -> int

scroll_x t is the horizontal scroll offset in columns.

Sourceval scroll_y : t -> int

scroll_y t is the vertical scroll offset in display lines.

Sourceval set_scroll_x : t -> int -> unit

set_scroll_x t x sets the horizontal scroll offset, clamped to [0;max_scroll_x]. Requests a render if changed.

Sourceval set_scroll_y : t -> int -> unit

set_scroll_y t y sets the vertical scroll offset, clamped to [0;max_scroll_y]. Requests a render if changed.

Sourceval scroll_height : t -> int

scroll_height t is the total number of display lines.

Sourceval scroll_width : t -> int

scroll_width t is the maximum display line width in columns.

Sourceval max_scroll_x : t -> int

max_scroll_x t is max 0 ({!scroll_width} t - viewport_width).

Sourceval max_scroll_y : t -> int

max_scroll_y t is max 0 ({!scroll_height} t - viewport_height).

Display lines

Sourcetype display_line = Text_buffer.span list

A display line is a list of styled spans that fit within the wrap width.

Sourcetype display_info = {
  1. lines : display_line array;
    (*

    The wrapped display lines.

    *)
  2. line_sources : int array;
    (*

    line_sources.(i) is the logical line index that display line i originates from.

    *)
  3. line_grapheme_offsets : int array;
    (*

    line_grapheme_offsets.(i) is the grapheme offset of display line i in the full text. Used for mapping highlights and cursor positions.

    *)
  4. line_wrap_indices : int array;
    (*

    line_wrap_indices.(i) is the sub-line index within its logical line: 0 for the first display line, 1 for the first continuation, etc. Useful for line numbering and gutter rendering.

    *)
  5. max_line_width : int;
    (*

    The widest display line in columns.

    *)
}

Wrapped display line metrics.

Sourceval display_info : t -> display_info

display_info t is the current wrapped line metrics. Cached; recomputed when content or wrap settings change.

Sourceval display_line_count : t -> int

display_line_count t is Array.length (display_info t).lines.

Selection

Sourceval set_selection_bg : t -> Ansi.Color.t option -> unit

set_selection_bg t color sets the selection background color.

Sourceval set_selection_fg : t -> Ansi.Color.t option -> unit

set_selection_fg t color sets the selection foreground color.

Sourceval set_selection : t -> start:int -> end_:int -> bool

set_selection t ~start ~end_ sets the selection range by grapheme offsets into the buffer content. Offsets are clamped to content bounds. The selection is active when start <> end_. Returns true if the selection state changed.

Sourceval selection : t -> (int * int) option

selection t is Some (start, end_) when a selection is active, with start < end_ as normalized grapheme offsets. None when no selection is active.

Sourceval set_local_selection : t -> anchor_x:int -> anchor_y:int -> focus_x:int -> focus_y:int -> bool

set_local_selection t ~anchor_x ~anchor_y ~focus_x ~focus_y sets the selection from viewport-local coordinates. Coordinates are converted to grapheme offsets accounting for scroll position. Activates the selection unconditionally. Returns true if the selection changed.

See update_local_selection for updating an existing drag.

Sourceval update_local_selection : t -> anchor_x:int -> anchor_y:int -> focus_x:int -> focus_y:int -> bool

update_local_selection t ~anchor_x ~anchor_y ~focus_x ~focus_y is like set_local_selection but only considers offset changes (does not re-check active state). Returns true if the selection changed.

Sourceval reset_selection : t -> unit

reset_selection t clears any active selection and requests a render.

Sourceval has_selection : t -> bool

has_selection t is true iff a selection is active.

Sourceval selected_text : t -> string

selected_text t is the text within the current selection. Returns "" if no selection is active.

Invalidation

Sourceval invalidate : t -> unit

invalidate t clears cached display lines, marks layout dirty, and requests a render. Call after modifying the underlying buffer externally.