Kraken TUI’s Theme Module provides a powerful system for managing visual style defaults across widget subtrees. Themes enable consistent styling without repetitive per-widget configuration.
The Theme Module is a separate bounded context within the Native Core. It owns theme definitions and subtree bindings, while the Style Module queries themes during style resolution.
import { Theme } from "kraken-tui";const theme = Theme.create();// Set global defaultstheme.setForeground("#E0E0E0"); // Light gray texttheme.setBackground("#1E1E1E"); // Dark gray backgroundtheme.setBorderColor("#444444"); // Medium gray borderstheme.setOpacity(1.0);// Set text decoration defaultstheme.setBold(false);theme.setItalic(false);theme.setUnderline(false);// Set border style defaulttheme.setBorderStyle("none");
import { Theme } from "kraken-tui";const theme = Theme.create();// Global defaultstheme.setForeground("white");theme.setBackground("black");// Input-specific overridestheme.setTypeColor("input", "fg", "#FFD700"); // Gold text for inputstheme.setTypeColor("input", "bg", "#2C2C2C"); // Dark gray background// Text-specific overridestheme.setTypeColor("text", "fg", "#00FF00"); // Green text for Text widgets
theme.setBold(false); // Global: no bold// Text widgets are bold by defaulttheme.setTypeFlag("text", "bold", true);// Input widgets are italic by defaulttheme.setTypeFlag("input", "italic", true);
theme.setBorderStyle("none"); // Global: no borders// Box widgets get rounded borderstheme.setTypeBorderStyle("box", "rounded");// ScrollBox widgets get single borderstheme.setTypeBorderStyle("scrollBox", "single");theme.setTypeColor("scrollBox", "borderColor", "cyan");
const theme = Theme.create();theme.setForeground("white"); // Global defaulttheme.setTypeColor("text", "fg", "cyan"); // Text-specific defaultconst text1 = new Text({ content: "A" });// Uses: cyan (type-specific wins over global)const text2 = new Text({ content: "B" });text2.setForeground("red");// Uses: red (explicit wins over type-specific)const box = new Box();// Uses: white (no type-specific default for Box, uses global)
Theme resolution uses ancestry traversal (O(depth) per widget). The Style Module caches resolved themes, so cost is paid once per dirty widget per frame.
Per-Type Defaults
Per-type defaults add one additional hash lookup during style resolution. Negligible overhead.
Theme Application
Applying a theme to a widget is O(1) (stores theme handle in subtree binding map). Descendants resolve theme lazily during render.