Widgets
Widgets are the fundamental building blocks of Kraken TUI applications. All visual elements are widgets arranged in a composition tree.Widget Composition Tree
Kraken TUI uses a retained-mode architecture where you build a tree of widgets that persists across frames. Only changed widgets are re-rendered.Create Widgets
Instantiate widget objects in TypeScript. Each widget receives a unique
u32 handle from the Native Core.Build Hierarchy
Use
append(), insertChild(), and removeChild() to establish parent-child relationships.Example: Building a Tree
Widget Handle System
Every widget holds an opaque
u32 handle. The Native Core owns all widget state; TypeScript only holds handles for command dispatch.Handle Semantics
- Unique: Each widget gets a unique sequential handle
- Opaque: TypeScript never inspects handle contents
- Invalid Sentinel:
Handle(0)is permanently invalid - Never Recycled: Handles are never reused after destruction
Widget Lifecycle
Creation
When you instantiate a widget in TypeScript, it calls an FFI function to create the native node:Hierarchy Management
- Append Child
- Insert at Index
- Remove Child
Destruction
- Single Node
- Entire Subtree
Built-in Widget Types
Kraken TUI provides several foundational widget types:Box - Container Widget
Box - Container Widget
Flexible container using Flexbox layout. Supports direction, justification, alignment, gap, padding, and margin.
Text - Content Display
Text - Content Display
Displays text content with optional rich formatting (Markdown, syntax highlighting).
Input - Single-Line Text Entry
Input - Single-Line Text Entry
Editable single-line text field with cursor, placeholder, password masking.
TextArea - Multi-Line Editor
TextArea - Multi-Line Editor
Multi-line text editing with 2D cursor, word wrap, selection, undo/redo.
Select - Option List
Select - Option List
Dropdown-style selection with arrow key navigation.
ScrollBox - Scrollable Container
ScrollBox - Scrollable Container
Scrollable viewport for content larger than available space.
Base Widget API
All widgets inherit from the baseWidget class:
Visibility
Hierarchy Operations
Dirty Marking
Most property setters automatically mark widgets dirty. You rarely need to call
markDirty() manually.Focus Management
Accessibility
Dirty Flag System
Kraken TUI uses dirty-flag optimization to minimize rendering work. Only widgets marked dirty (and their ancestors) are processed during layout and render.
How Dirty Propagation Works
- Property Change: Setting a widget property marks it dirty
- Upward Propagation: Dirty flag propagates up to ancestors
- Layout Pass: Only dirty subtrees recompute layout
- Render Pass: Only dirty cells update the terminal
Benefits
- Performance: O(dirty nodes) instead of O(all nodes)
- Efficiency: Unchanged widgets skip layout and render
- Scalability: Large trees with small changes remain fast
Widget Tree Traversal
The Native Core performs several types of tree traversals:Layout Traversal (Top-Down)
Layout Traversal (Top-Down)
Starting from root, computes layout for each dirty subtree using Flexbox constraints.Order: Parent before children (constraints flow down)Cost: O(dirty nodes)
Render Traversal (Depth-First)
Render Traversal (Depth-First)
Starting from root, renders each dirty widget’s content to the cell buffer.Order: Depth-first, left-to-rightCost: O(dirty nodes)
Focus Traversal (DOM Order)
Focus Traversal (DOM Order)
Tab key advances focus using depth-first, left-to-right order among focusable widgets.Order: Same as render orderCost: O(focusable nodes)
Hit-Testing (Back-to-Front)
Hit-Testing (Back-to-Front)
Mouse clicks traverse in reverse render order to find the topmost widget at (x, y).Order: Reverse depth-first (visual stacking order)Cost: O(all nodes) per click (acceptable for discrete events)
Best Practices
Destroy When Done
Always call
destroySubtree() on root widgets when unmounting to free native resources.Minimize Dirty Flags
Batch property updates before calling
render() to avoid redundant dirty marking.Use insertChild for Lists
When updating lists, use
insertChild(item, index) instead of remove-all + append-all for efficiency.Next Steps
Layout
Learn about Flexbox layout properties
Styling
Explore color and text decoration
Events
Handle keyboard and mouse input
Animation
Animate widget properties