tui-lipan 0.2.0

Opinionated, component-based TUI framework for Rust - declarative components, reconciliation, layout engine, focus, overlays, and rich widgets on top of ratatui.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
use crate::app::input::key_dispatch::{
    ChordMismatchPolicy, CommandConflictPolicy, KeyDispatchPolicy, TerminalKeyPolicy,
};
use crate::app::input::keymap::{FrameworkAction, FrameworkKeymap, UserKeymapPolicy};
#[cfg(not(target_arch = "wasm32"))]
use crate::app::runner::AppRunner;
use crate::clipboard::{ClipboardConfig, ClipboardError, ClipboardProvider, ClipboardReporter};
#[cfg(not(target_arch = "wasm32"))]
use crate::core::component::Component;
use crate::input::KeyBindings;
use crate::layout::tag::Tag;
use crate::overlay::ToastPlacement;
use crate::style::Padding;
use crate::style::{Color, Paint, Style, Theme};
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;

/// How the app occupies terminal space.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(crate) enum ViewportMode {
    /// Take over the full terminal using the alternate screen.
    #[default]
    Fullscreen,
    /// Render inline at the current cursor position.
    Inline { height: InlineHeight },
}

/// Height policy for inline viewports.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum InlineHeight {
    /// A fixed number of rows, clamped to at least one row.
    Fixed(u16),
    /// Follow the content's measured height, re-sizing the viewport as the
    /// view changes.
    Auto {
        /// Optional row cap. Regardless of the cap, the viewport never grows
        /// past the host terminal height.
        max: Option<u16>,
    },
}

impl InlineHeight {
    /// Content-sized height, capped only by the host terminal height.
    pub const fn auto() -> Self {
        Self::Auto { max: None }
    }

    /// Content-sized height, capped at `max` rows.
    pub const fn auto_capped(max: u16) -> Self {
        Self::Auto { max: Some(max) }
    }

    pub(crate) fn normalized(self) -> Self {
        match self {
            Self::Fixed(rows) => Self::Fixed(rows.max(1)),
            Self::Auto { max } => Self::Auto {
                max: max.map(|rows| rows.max(1)),
            },
        }
    }

    /// Rows to reserve for the viewport before the first frame is measured.
    pub(crate) fn initial_rows(self) -> u16 {
        match self {
            Self::Fixed(rows) => rows.max(1),
            // Auto starts minimal: the first render measures the content and
            // grows the viewport before anything is painted.
            Self::Auto { .. } => 1,
        }
    }
}

impl From<u16> for InlineHeight {
    fn from(rows: u16) -> Self {
        Self::Fixed(rows)
    }
}

/// Startup behavior for transcript inline mode.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum InlineStartupPolicy {
    /// Preserve host terminal content above the inline viewport.
    #[default]
    PreserveHost,
    /// Clear the host terminal before the first inline render.
    ClearHost,
}

/// Public surface mode taxonomy for app rendering.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum SurfaceMode {
    /// Take over the full terminal using the alternate screen.
    #[default]
    Fullscreen,
    /// Inline viewport intended for ephemeral UI sessions.
    InlineEphemeral {
        /// Requested inline viewport height.
        height: InlineHeight,
    },
    /// Inline viewport intended for transcript-friendly sessions.
    InlineTranscript {
        /// Requested inline viewport height.
        height: InlineHeight,
        /// Startup behavior for the host terminal.
        startup: InlineStartupPolicy,
    },
}

impl SurfaceMode {
    pub(crate) fn is_inline(&self) -> bool {
        !matches!(self, Self::Fullscreen)
    }

    pub(crate) fn normalized(self) -> Self {
        match self {
            Self::Fullscreen => Self::Fullscreen,
            Self::InlineEphemeral { height } => Self::InlineEphemeral {
                height: height.normalized(),
            },
            Self::InlineTranscript { height, startup } => Self::InlineTranscript {
                height: height.normalized(),
                startup,
            },
        }
    }

    pub(crate) fn viewport_mode(self) -> ViewportMode {
        match self.normalized() {
            Self::Fullscreen => ViewportMode::Fullscreen,
            Self::InlineEphemeral { height } | Self::InlineTranscript { height, .. } => {
                ViewportMode::Inline { height }
            }
        }
    }

    pub(crate) fn clear_on_start(self) -> bool {
        matches!(
            self,
            Self::InlineTranscript {
                startup: InlineStartupPolicy::ClearHost,
                ..
            }
        )
    }
}

/// Controls which Enter key combinations insert new lines in `TextArea`.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TextAreaNewlineBinding {
    /// Use plain Enter.
    #[default]
    Enter,
    /// Use Shift+Enter only.
    ShiftEnter,
    /// Accept both Enter and Shift+Enter.
    EnterOrShiftEnter,
}

/// Controls framework-initiated focus movement.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum FocusPolicy {
    /// Focus the first focusable widget at startup and whenever focus cannot be restored.
    Auto,
    /// Start unfocused, then allow Tab and pointer interaction to establish focus.
    #[default]
    OnDemand,
    /// Never move focus through global traversal or pointer interaction.
    ///
    /// Explicit focus requests and focus traversal helpers remain available. Capturing overlays
    /// also continue to establish and trap focus.
    Manual,
}

/// Public identity of a focused widget at a focus transition boundary.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FocusEntry {
    /// Optional stable widget key.
    pub key: Option<crate::core::element::Key>,
    /// Widget kind.
    pub tag: Tag,
}

/// App-level focus transition payload.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FocusChanged {
    /// Previously focused widget, if any.
    pub old: Option<FocusEntry>,
    /// Newly focused widget, if any.
    pub new: Option<FocusEntry>,
}

pub(crate) type FocusChangedHook = std::rc::Rc<dyn Fn(&FocusChanged)>;

/// One host-application metric displayed by the built-in DevTools panel.
///
/// Rows are intentionally just a terse label/value pair. The application
/// supplies their display order when calling
/// [`Context::set_devtools_metrics`](crate::Context::set_devtools_metrics).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DevToolsMetric {
    /// Short metric label, such as `"Panes"` or `"Queue"`.
    pub label: Arc<str>,
    /// Already-formatted metric value, such as `"12"` or `"3.2 MiB"`.
    pub value: Arc<str>,
}

impl DevToolsMetric {
    /// Create a metric row from a label and its formatted value.
    pub fn new(label: impl Into<Arc<str>>, value: impl Into<Arc<str>>) -> Self {
        Self {
            label: label.into(),
            value: value.into(),
        }
    }
}

/// Controls automatic foreground contrast adjustments for widget text.
#[cfg_attr(
    feature = "terminal-serde",
    derive(serde::Serialize, serde::Deserialize)
)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ContrastPolicy {
    /// Keep user-provided foreground colors unchanged.
    Off,
    /// Auto-adjust foreground on colored backgrounds when contrast is too low
    /// using WCAG 2.1 contrast ratio (AA normal text: >= 4.5:1).
    #[default]
    Wcag,
    /// Keep the current foreground when it is readable under WCAG 2.1;
    /// otherwise snap to black or white, whichever has higher contrast.
    BlackOrWhite,
    /// Auto-adjust using APCA perceptual contrast (WCAG 3.0 draft).
    ///
    /// Better for dark themes and polarity-aware readability. Uses `|Lc|` >= 60
    /// as the minimum body-text threshold.
    Apca,
}

#[cfg(all(test, feature = "terminal-serde"))]
mod terminal_serde_tests {
    use super::*;

    #[test]
    fn contrast_policy_round_trips() {
        let policy = ContrastPolicy::BlackOrWhite;
        let json = serde_json::to_string(&policy).unwrap();
        assert_eq!(
            serde_json::from_str::<ContrastPolicy>(&json).unwrap(),
            policy
        );
    }
}

/// Runtime devtools subsystem configuration.
#[cfg(feature = "devtools")]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct DevToolsConfig {
    /// Enable devtools log ingestion and sink wiring.
    pub logs: bool,
    /// Enable runtime frame metrics collection.
    pub metrics: bool,
    /// Show tui-lipan's own framework-internal logs in the log view.
    ///
    /// Set to `false` so the devtools log view starts with framework noise
    /// (key events, dirty tracking, etc.) hidden, showing only the host
    /// application's own `debug_log!` lines. Can still be toggled at runtime
    /// from the "tui-lipan" button in the Logs tab.
    pub show_framework_logs: bool,
}

#[cfg(feature = "devtools")]
impl Default for DevToolsConfig {
    fn default() -> Self {
        Self {
            logs: true,
            metrics: true,
            show_framework_logs: true,
        }
    }
}

/// How the root viewport background is painted before the UI tree renders.
///
/// By default the framework paints nothing behind the tree, so the host
/// terminal background shows through ([`Transparent`](Self::Transparent)). Opt
/// into a filled background when you want a fully "designed" surface rather than
/// text floating on the terminal color — useful for kiosk-style apps, themes
/// with a strong identity, or matching a brand backdrop.
///
/// ```no_run
/// use tui_lipan::prelude::*;
///
/// // Fill with the active theme's backdrop surface:
/// let app = App::new().theme(Theme::lipan()).fill_background();
///
/// // Or an explicit color:
/// let app = App::new().screen_background(Color::hex_u24(0x04090D));
/// ```
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum ScreenBackground {
    /// Leave the host terminal background untouched. (default)
    #[default]
    Transparent,
    /// Fill the viewport with the active root theme's backdrop surface
    /// ([`Theme::surface`]`.backdrop`).
    ///
    /// This tracks the theme of the realized root node, so apps that swap themes
    /// at runtime via a root `ThemeProvider` (rather than rebuilding the `App`)
    /// keep the backdrop in sync without any extra wiring.
    Theme,
    /// Fill the viewport with an explicit style (typically just a background color).
    Custom(Style),
}

impl ScreenBackground {
    /// Resolve to the concrete fill style for `theme`, or `None` when nothing
    /// should be painted.
    pub(crate) fn resolve(self, theme: &Theme) -> Option<Style> {
        match self {
            Self::Transparent => None,
            Self::Theme => Some(Style::new().bg(theme.surface.backdrop)),
            Self::Custom(style) => (!style.is_empty()).then_some(style),
        }
    }
}

impl From<Style> for ScreenBackground {
    fn from(style: Style) -> Self {
        Self::Custom(style)
    }
}

impl From<Color> for ScreenBackground {
    fn from(color: Color) -> Self {
        Self::Custom(Style::new().bg(color))
    }
}

impl From<Paint> for ScreenBackground {
    fn from(paint: Paint) -> Self {
        Self::Custom(Style::new().bg(paint))
    }
}

/// Application builder.
pub struct App {
    pub(crate) title: Option<String>,
    pub(crate) surface_mode: SurfaceMode,
    pub(crate) mouse_enabled: Option<bool>,
    pub(crate) scroll_wheel_multiplier: u16,
    pub(crate) theme: Theme,
    pub(crate) toast_placement: ToastPlacement,
    pub(crate) toast_gap: u16,
    pub(crate) toast_margin: Padding,
    pub(crate) clipboard_config: ClipboardConfig,
    pub(crate) keymap_path: Option<PathBuf>,
    pub(crate) framework_keymap: FrameworkKeymap,
    pub(crate) user_keymap_policy: UserKeymapPolicy,
    pub(crate) key_dispatch_policy: KeyDispatchPolicy,
    pub(crate) focus_policy: FocusPolicy,
    pub(crate) on_focus_changed: Option<FocusChangedHook>,
    pub(crate) terminal_key_policy: TerminalKeyPolicy,
    pub(crate) command_conflict_policy: CommandConflictPolicy,
    pub(crate) chord_mismatch_policy: ChordMismatchPolicy,
    pub(crate) command_chord_reveal_delay: Duration,
    pub(crate) text_area_newline_binding: TextAreaNewlineBinding,
    pub(crate) contrast_policy: ContrastPolicy,
    pub(crate) clipboard_provider: Option<Box<dyn ClipboardProvider>>,
    pub(crate) clipboard_reporter: ClipboardReporter,
    pub(crate) terminal_bg: Option<Color>,
    pub(crate) live_host_terminal_colors: bool,
    pub(crate) system_theme: bool,
    pub(crate) screen_background: ScreenBackground,
    #[cfg(feature = "devtools")]
    pub(crate) devtools_config: DevToolsConfig,
}

impl Default for App {
    fn default() -> Self {
        let theme = Theme::default();
        Self {
            title: None,
            surface_mode: SurfaceMode::default(),
            mouse_enabled: None,
            scroll_wheel_multiplier: 1,
            theme,
            toast_placement: ToastPlacement::default(),
            toast_gap: 1,
            toast_margin: Padding::BORDER,
            clipboard_config: ClipboardConfig::default(),
            keymap_path: None,
            framework_keymap: FrameworkKeymap::default(),
            user_keymap_policy: UserKeymapPolicy::default(),
            key_dispatch_policy: KeyDispatchPolicy::WidgetFirst,
            focus_policy: FocusPolicy::default(),
            on_focus_changed: None,
            terminal_key_policy: TerminalKeyPolicy::FrameworkFirst,
            command_conflict_policy: CommandConflictPolicy::default(),
            chord_mismatch_policy: ChordMismatchPolicy::default(),
            command_chord_reveal_delay: Duration::ZERO,
            text_area_newline_binding: TextAreaNewlineBinding::default(),
            contrast_policy: ContrastPolicy::default(),
            clipboard_provider: None,
            clipboard_reporter: crate::clipboard::default_clipboard_reporter(),
            terminal_bg: None,
            live_host_terminal_colors: false,
            system_theme: false,
            screen_background: ScreenBackground::default(),
            #[cfg(feature = "devtools")]
            devtools_config: DevToolsConfig::default(),
        }
    }
}

impl App {
    /// Create a new app.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the terminal window title (via OSC 2 escape sequence).
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }

    /// Set the app surface mode explicitly.
    pub fn surface(mut self, mode: SurfaceMode) -> Self {
        self.surface_mode = mode.normalized();
        self
    }

    /// Use fullscreen alternate-screen rendering.
    pub fn fullscreen(self) -> Self {
        self.surface(SurfaceMode::Fullscreen)
    }

    /// Render the app inline for ephemeral (non-transcript) sessions.
    ///
    /// Accepts a fixed row count (clamped to at least one row) or an
    /// [`InlineHeight`] policy such as [`InlineHeight::auto()`], which sizes
    /// the viewport to the content every frame.
    pub fn inline_ephemeral(self, height: impl Into<InlineHeight>) -> Self {
        self.surface(SurfaceMode::InlineEphemeral {
            height: height.into(),
        })
    }

    /// Render the app inline for transcript sessions.
    ///
    /// Accepts a fixed row count or an [`InlineHeight`] policy such as
    /// [`InlineHeight::auto()`]. Defaults to preserving host terminal content
    /// on startup.
    pub fn inline_transcript(self, height: impl Into<InlineHeight>) -> Self {
        self.surface(SurfaceMode::InlineTranscript {
            height: height.into(),
            startup: InlineStartupPolicy::PreserveHost,
        })
    }

    /// Render the app inline for transcript sessions with explicit startup behavior.
    ///
    /// Accepts a fixed row count (clamped to at least one row) or an
    /// [`InlineHeight`] policy such as [`InlineHeight::auto()`].
    pub fn inline_transcript_with_startup(
        self,
        height: impl Into<InlineHeight>,
        startup: InlineStartupPolicy,
    ) -> Self {
        self.surface(SurfaceMode::InlineTranscript {
            height: height.into(),
            startup,
        })
    }

    /// Configure mouse capture behavior.
    ///
    /// This sets the initial runtime state. Components can later change it with
    /// `Context::set_mouse_capture(...)` or `Context::toggle_mouse_capture()`.
    ///
    /// Defaults:
    /// - fullscreen mode: enabled
    /// - inline mode: disabled
    pub fn mouse(mut self, enabled: bool) -> Self {
        self.mouse_enabled = Some(enabled);
        self
    }

    /// Set the app-wide mouse wheel step multiplier.
    ///
    /// Each wheel tick scrolls `multiplier` lines instead of the default single
    /// line. Coalesced wheel bursts multiply by this value too, so two ticks with
    /// `multiplier = 3` scroll six lines total.
    pub fn scroll_wheel_multiplier(mut self, multiplier: u16) -> Self {
        self.scroll_wheel_multiplier = multiplier.max(1);
        self
    }

    /// Set the app-wide default theme.
    ///
    /// This theme is applied to the root tree every render.
    /// Use `ThemeProvider` to override a specific subtree.
    pub fn theme(mut self, theme: Theme) -> Self {
        self.theme = theme;
        self
    }

    /// Paint the root viewport background before rendering the UI tree.
    ///
    /// By default the framework paints nothing behind the tree (the host
    /// terminal background shows through). This opts into a filled background so
    /// the UI reads as a designed surface. Accepts a [`Color`], [`Paint`],
    /// [`Style`], or a [`ScreenBackground`] directly:
    ///
    /// ```no_run
    /// use tui_lipan::prelude::*;
    ///
    /// let app = App::new().screen_background(Color::hex_u24(0x04090D));
    /// ```
    ///
    /// Use [`fill_background`](Self::fill_background) to track the active theme's
    /// backdrop automatically.
    pub fn screen_background(mut self, background: impl Into<ScreenBackground>) -> Self {
        self.screen_background = background.into();
        self
    }

    /// Fill the root viewport with the active theme's backdrop surface.
    ///
    /// Shorthand for `screen_background(ScreenBackground::Theme)`. The fill tracks
    /// the app theme, so swapping themes keeps the backdrop in sync.
    pub fn fill_background(mut self) -> Self {
        self.screen_background = ScreenBackground::Theme;
        self
    }

    /// Set where toasts appear on screen.
    pub fn toast_placement(mut self, placement: ToastPlacement) -> Self {
        self.toast_placement = placement;
        self
    }

    /// Set vertical gap between stacked toasts.
    pub fn toast_gap(mut self, gap: u16) -> Self {
        self.toast_gap = gap;
        self
    }

    /// Set outside margin between toasts and the viewport edge.
    pub fn toast_margin(mut self, margin: impl Into<Padding>) -> Self {
        self.toast_margin = margin.into();
        self
    }

    /// Configure clipboard behavior.
    pub fn clipboard_config(mut self, config: ClipboardConfig) -> Self {
        self.clipboard_config = config;
        self
    }

    /// Use a specific keymap file path for this app instance.
    ///
    /// This path has higher priority than `TUI_LIPAN_KEYMAP` and the default
    /// `$XDG_CONFIG_HOME/tui-lipan/keymap.conf` fallback.
    pub fn keymap_path(mut self, path: impl Into<PathBuf>) -> Self {
        self.keymap_path = Some(path.into());
        self
    }

    /// Override framework key bindings from Rust after file and built-in keymaps are loaded.
    pub fn framework_keymap(mut self, keymap: FrameworkKeymap) -> Self {
        self.framework_keymap = keymap;
        self
    }

    /// Configure the global quit shortcut. `None` disables framework quit bindings.
    pub fn global_quit(mut self, bindings: Option<KeyBindings>) -> Self {
        self.framework_keymap = match bindings {
            Some(bindings) => self.framework_keymap.bind(FrameworkAction::Quit, bindings),
            None => self.framework_keymap.unbind(FrameworkAction::Quit),
        };
        self
    }

    /// Enable or disable loading user keymap files.
    pub fn user_keymap_policy(mut self, policy: UserKeymapPolicy) -> Self {
        self.user_keymap_policy = policy;
        self
    }

    /// Configure app command versus widget key dispatch ordering.
    pub fn key_dispatch_policy(mut self, policy: KeyDispatchPolicy) -> Self {
        self.key_dispatch_policy = policy;
        self
    }

    /// Delay before a pending command chord is reported as *revealed*.
    ///
    /// [`Context::command_chord_pending`](crate::Context::command_chord_pending) stays immediate;
    /// this governs [`Context::command_chord_revealed`](crate::Context::command_chord_revealed)
    /// only, which is the signal a which-key panel or similar chord affordance should read. The
    /// runtime schedules a frame at the moment the delay elapses, so such a view needs no timer of
    /// its own. The default is [`Duration::ZERO`] - revealed as soon as it is pending.
    pub fn command_chord_reveal_delay(mut self, delay: Duration) -> Self {
        self.command_chord_reveal_delay = delay;
        self
    }

    /// Configure framework-initiated focus movement.
    pub fn focus_policy(mut self, policy: FocusPolicy) -> Self {
        self.focus_policy = policy;
        self
    }

    /// Observe completed focus transitions after widget blur/focus callbacks are emitted.
    pub fn on_focus_changed(mut self, hook: impl Fn(&FocusChanged) + 'static) -> Self {
        self.on_focus_changed = Some(std::rc::Rc::new(hook));
        self
    }

    /// Configure key dispatch behavior while terminal widgets are focused.
    pub fn terminal_key_policy(mut self, policy: TerminalKeyPolicy) -> Self {
        self.terminal_key_policy = policy;
        self
    }

    /// Configure how app command shortcut conflicts are resolved.
    pub fn command_conflict_policy(mut self, policy: CommandConflictPolicy) -> Self {
        self.command_conflict_policy = policy;
        self
    }

    /// Configure how mismatched keys are handled during pending command chords.
    pub fn chord_mismatch_policy(mut self, policy: ChordMismatchPolicy) -> Self {
        self.chord_mismatch_policy = policy;
        self
    }

    /// Configure which Enter key combination inserts new lines in `TextArea`.
    ///
    /// This policy is scoped to `TextArea` and does not change single-line
    /// `Input` behavior.
    pub fn text_area_newline_binding(mut self, binding: TextAreaNewlineBinding) -> Self {
        self.text_area_newline_binding = binding;
        self
    }

    /// Configure app-wide text contrast behavior for interactive widget states.
    ///
    /// Individual styles can override this per-state by setting
    /// `Style::contrast_policy(...)` on the relevant style (base, hover,
    /// selection, focus, theme role, etc.).
    pub fn contrast_policy(mut self, policy: ContrastPolicy) -> Self {
        self.contrast_policy = policy;
        self
    }

    /// Provide a custom clipboard provider implementation.
    pub fn clipboard_provider(mut self, provider: impl ClipboardProvider + 'static) -> Self {
        self.clipboard_provider = Some(Box::new(provider));
        self
    }

    /// Provide a custom clipboard error reporter.
    pub fn clipboard_reporter(mut self, reporter: impl Fn(ClipboardError) + 'static) -> Self {
        self.clipboard_reporter = std::rc::Rc::new(reporter);
        self
    }

    /// Set the resolved terminal background color.
    ///
    /// When set, [`crate::style::ColorTransform::Opacity`] can blend foreground colors
    /// toward the real terminal background even when a cell's background is
    /// [`Color::Reset`].  Obtain this value from [`crate::style::query_host_colors()`]
    /// before starting the app:
    ///
    /// ```ignore
    /// let bg = query_host_colors().map(|c| c.bg);
    /// App::new().terminal_bg(bg).run(MyComponent);
    /// ```
    pub fn terminal_bg(mut self, color: Option<Color>) -> Self {
        self.terminal_bg = color;
        self
    }

    /// Enable runner-managed host terminal color refreshes.
    ///
    /// When enabled, the runner queries the host terminal palette before component
    /// init, refreshes on terminal focus gained, and services
    /// `Context::request_host_terminal_color_refresh()` on the UI thread while
    /// coordinating with tui-lipan's input reader. Refreshed colors are exposed
    /// through `Context::host_terminal_colors()` and the resolved terminal
    /// background is kept in sync for opacity blending.
    ///
    /// On Unix fullscreen surfaces, compatible terminals that implement DEC
    /// private mode 2031 also trigger an immediate refresh when their palette
    /// changes. Inline, non-Unix, and unsupported terminals retain startup,
    /// focus-gained, and manual refresh behavior.
    ///
    /// Disabled by default so static apps do not poll the terminal.
    pub fn live_host_terminal_colors(mut self, enabled: bool) -> Self {
        self.live_host_terminal_colors = enabled;
        self
    }

    /// Use the host terminal palette as the app theme once colors are probed.
    ///
    /// The current app theme remains the fallback until the runner successfully
    /// receives host colors. Later failed refreshes keep the last applied theme.
    /// Unix fullscreen surfaces also subscribe to compatible terminals' DEC mode
    /// 2031 palette-change notifications while the app is running.
    pub fn system_theme(mut self) -> Self {
        self.system_theme = true;
        self
    }

    /// Configure runtime devtools subsystem behavior.
    #[cfg(feature = "devtools")]
    pub fn devtools_config(mut self, config: DevToolsConfig) -> Self {
        self.devtools_config = config;
        self
    }

    /// Mount the root component with default properties.
    #[cfg(not(target_arch = "wasm32"))]
    pub fn mount<C>(self, component: C) -> AppRunner<C>
    where
        C: Component,
        C::Properties: Default,
    {
        self.mount_with_props(component, C::Properties::default())
    }

    /// Mount the root component with explicit properties.
    #[cfg(not(target_arch = "wasm32"))]
    pub fn mount_with_props<C>(self, component: C, props: C::Properties) -> AppRunner<C>
    where
        C: Component,
    {
        AppRunner::new(self, component, props)
    }
}