Overview

The Overlay Engine provides modal custom views and anchored overlay dialogs in the TUI, built on top of Mordant rendering and coroutine-based session control.

Key Components

  • OverlayManager: Central registry and focus authority for overlay sessions. Manages the active overlay and queues background overlays.

  • OverlayCompositor: Composites active overlay panels onto base terminal frames using Mordant cell counts and dimension percentage bounds.

  • CustomViewSession<T>: Manages custom view state, coroutine scope, event flow (events), and one-shot completion handling via finish / cancel.

  • MordantExtensionUI: Extension UI adapter bridging script calls (ctx.ui.customView, ctx.ui.overlay) to OverlayManager and CustomViewSession.

  • InputStateMachine: Intercepts keyboard events for the active overlay before evaluating shortcuts or screen dispatch.

Key Event Routing Flow

  1. User presses a key in TUI mode.

  2. InputStateMachine.handle(event, screen) checks overlayManager?.active.

  3. If an active CustomViewSession is present, the key event is emitted to session.emitKey(event) and processing returns immediately (bypassing shortcut guarding, screen navigation keys, and chat key handlers).

Frame Composition Flow

  1. RenderLoop builds the base frame via RenderFrameBuilder and Mordant rendering (terminal.render(…​)).

  2. OverlayCompositor.composite(baseFrame) checks for an active, non-hidden CustomViewSession.

  3. If active, it calculates panel dimensions (percentage width/height), renders the widget, and splices panel lines onto the base frame buffer while preserving ANSI control sequences.

Custom View & Overlay Lifecycle Sequence

Diagram
sequenceDiagram
    participant User/Script
    participant TuiExtensionUI as MordantExtensionUI
    participant OverlayManager
    participant OverlayCompositor
    participant InputStateMachine
    participant CustomViewSession

    User/Script->>TuiExtensionUI: ctx.ui.overlay(options, view)
    TuiExtensionUI->>CustomViewSession: Create session (onFinish callback)
    TuiExtensionUI->>OverlayManager: activate(session)
    OverlayManager-->>InputStateMachine: active session set

    loop Frame Render Loop
        InputStateMachine->>CustomViewSession: Input event intercepted (emitKey)
        OverlayCompositor->>OverlayManager: Get active session
        OverlayCompositor->>BaseFrame: Composite panel onto base frame
    end

    User/Script->>CustomViewSession: finish(result) or cancel()
    CustomViewSession->>OverlayManager: close(session)
    CustomViewSession->>TuiExtensionUI: Resume continuation with result / null