vtcode-ui 0.153.0

Unified UI crate for VT Code: design system, theme registry, and TUI framework
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
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
use hashbrown::{HashMap, HashSet};
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

/// Rebindable user-facing actions.
///
/// Each variant corresponds to a command-level action that a user may want to
/// remap.  Fine-grained editing shortcuts (character insertion, cursor movement,
/// text selection, Backspace, Delete, Home/End, Ctrl+A/E/W/U/K, Enter/Tab/Esc
/// with their context-sensitive logic) remain hardcoded in `events.rs`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Action {
    /// Send an interrupt signal (Ctrl+C) to the running agent.
    Interrupt,
    /// Exit the TUI session.
    Exit,
    /// Run the current operation in the background.
    BackgroundOperation,
    /// Open the model picker dialog.
    OpenModelPicker,
    /// Clear the terminal screen.
    ClearScreen,
    /// Scroll the conversation view up by one page.
    ScrollPageUp,
    /// Scroll the conversation view down by one page.
    ScrollPageDown,
    /// Open the input edit queue for reordering queued prompts.
    EditQueue,
    /// Recall the previous input from history.
    HistoryPrevious,
    /// Recall the next input from history.
    HistoryNext,
    /// Toggle the log panel visibility.
    ToggleLogs,
    /// Toggle compact per-call tool summaries.
    ToggleToolDisplayMode,
    /// Toggle the task panel visibility.
    ToggleTaskPanel,
    /// Open or close the whole-conversation transcript review overlay.
    OpenTranscriptReview,
    /// Toggle rich and raw transcript review rendering.
    ToggleTranscriptRenderMode,
    /// Generate an inline prompt suggestion via the LLM.
    GeneratePromptSuggestion,
}

impl Action {
    /// Human-readable name for config file serialization.
    fn name(self) -> &'static str {
        match self {
            Action::Interrupt => "interrupt",
            Action::Exit => "exit",
            Action::BackgroundOperation => "background_operation",
            Action::OpenModelPicker => "open_model_picker",
            Action::ClearScreen => "clear_screen",
            Action::ScrollPageUp => "scroll_page_up",
            Action::ScrollPageDown => "scroll_page_down",
            Action::EditQueue => "edit_queue",
            Action::HistoryPrevious => "history_previous",
            Action::HistoryNext => "history_next",
            Action::ToggleLogs => "toggle_logs",
            Action::ToggleToolDisplayMode => "toggle_tool_display_mode",
            Action::ToggleTaskPanel => "toggle_task_panel",
            Action::OpenTranscriptReview => "open_transcript_review",
            Action::ToggleTranscriptRenderMode => "toggle_transcript_render_mode",
            Action::GeneratePromptSuggestion => "generate_prompt_suggestion",
        }
    }

    /// All defined actions.
    fn all() -> &'static [Action] {
        &[
            Action::Interrupt,
            Action::Exit,
            Action::BackgroundOperation,
            Action::OpenModelPicker,
            Action::ClearScreen,
            Action::ScrollPageUp,
            Action::ScrollPageDown,
            Action::EditQueue,
            Action::HistoryPrevious,
            Action::HistoryNext,
            Action::ToggleLogs,
            Action::ToggleToolDisplayMode,
            Action::ToggleTaskPanel,
            Action::OpenTranscriptReview,
            Action::ToggleTranscriptRenderMode,
            Action::GeneratePromptSuggestion,
        ]
    }

    /// Look up an action by its serialized name.
    fn from_name(name: &str) -> Option<Self> {
        Self::all().iter().find(|a| a.name() == name).copied()
    }
}

/// Parse a key binding spec like `"ctrl+c"`, `"alt+shift+enter"`, `"pageup"`.
///
/// Supported modifiers: `ctrl`, `shift`, `alt`, `meta`, `cmd`, `super`.
/// Key names: single characters (`a`, `?`), `enter`, `tab`, `backtab`, `esc`,
/// `backspace`, `delete`, `space`, `up`, `down`, `left`, `right`, `pageup`,
/// `pagedown`, `home`, `end`, `f1`…`f12`.
pub fn parse_key_binding(s: &str) -> Option<(KeyCode, KeyModifiers)> {
    let normalized = s.trim().to_ascii_lowercase();
    let s = normalized.as_str();
    if s.is_empty() {
        return None;
    }

    let parts: Vec<&str> = s.split('+').collect();
    let (modifiers, key_part) = if parts.len() == 1 {
        (KeyModifiers::empty(), parts[0])
    } else {
        let mut mods = KeyModifiers::empty();
        for part in &parts[..parts.len() - 1] {
            match *part {
                "ctrl" | "control" => mods.insert(KeyModifiers::CONTROL),
                "shift" => mods.insert(KeyModifiers::SHIFT),
                "alt" | "option" => mods.insert(KeyModifiers::ALT),
                "meta" => mods.insert(KeyModifiers::META),
                "cmd" | "command" | "super" | "gui" | "win" => {
                    mods.insert(KeyModifiers::SUPER);
                }
                _ => return None,
            }
        }
        (mods, parts[parts.len() - 1])
    };

    let code = match key_part {
        "enter" => KeyCode::Enter,
        "tab" => KeyCode::Tab,
        "backtab" => KeyCode::BackTab,
        "esc" | "escape" => KeyCode::Esc,
        "backspace" => KeyCode::Backspace,
        "delete" => KeyCode::Delete,
        "space" => KeyCode::Char(' '),
        "up" => KeyCode::Up,
        "down" => KeyCode::Down,
        "left" => KeyCode::Left,
        "right" => KeyCode::Right,
        "pageup" => KeyCode::PageUp,
        "pagedown" => KeyCode::PageDown,
        "home" => KeyCode::Home,
        "end" => KeyCode::End,
        "insert" => KeyCode::Insert,
        "null" => KeyCode::Null,
        "capslock" => KeyCode::CapsLock,
        "scrolllock" => KeyCode::ScrollLock,
        "numlock" => KeyCode::NumLock,
        "printscreen" => KeyCode::PrintScreen,
        "pause" => KeyCode::Pause,
        "menu" => KeyCode::Menu,
        name if name.starts_with('f') && name.len() > 1 => {
            let n: u8 = name[1..].parse().ok()?;
            match n {
                1 => KeyCode::F(1),
                2 => KeyCode::F(2),
                3 => KeyCode::F(3),
                4 => KeyCode::F(4),
                5 => KeyCode::F(5),
                6 => KeyCode::F(6),
                7 => KeyCode::F(7),
                8 => KeyCode::F(8),
                9 => KeyCode::F(9),
                10 => KeyCode::F(10),
                11 => KeyCode::F(11),
                12 => KeyCode::F(12),
                _ => return None,
            }
        }
        ch => {
            let chars: Vec<char> = ch.chars().collect();
            if chars.len() == 1 {
                KeyCode::Char(chars[0])
            } else {
                return None;
            }
        }
    };

    Some((code, modifiers))
}

/// Default key → action mappings matching the current hardcoded dispatch.
fn default_bindings() -> HashMap<Action, Vec<(KeyCode, KeyModifiers)>> {
    use Action::*;
    let mut m = HashMap::new();

    m.insert(
        Interrupt,
        vec![
            (KeyCode::Char('c'), KeyModifiers::CONTROL),
            (KeyCode::Char('C'), KeyModifiers::CONTROL),
            (KeyCode::Char('\u{3}'), KeyModifiers::empty()),
        ],
    );
    m.insert(
        Exit,
        vec![
            (KeyCode::Char('d'), KeyModifiers::CONTROL),
            (KeyCode::Char('D'), KeyModifiers::CONTROL),
        ],
    );
    m.insert(
        BackgroundOperation,
        vec![
            (KeyCode::Char('b'), KeyModifiers::CONTROL),
            (KeyCode::Char('B'), KeyModifiers::CONTROL),
        ],
    );
    m.insert(
        OpenModelPicker,
        vec![
            (KeyCode::Char('m'), KeyModifiers::CONTROL),
            (KeyCode::Char('M'), KeyModifiers::CONTROL),
        ],
    );
    m.insert(
        ClearScreen,
        vec![
            (KeyCode::Char('l'), KeyModifiers::CONTROL),
            (KeyCode::Char('L'), KeyModifiers::CONTROL),
        ],
    );
    m.insert(ScrollPageUp, vec![(KeyCode::PageUp, KeyModifiers::empty())]);
    m.insert(ScrollPageDown, vec![(KeyCode::PageDown, KeyModifiers::empty())]);

    m.insert(EditQueue, vec![(KeyCode::Up, KeyModifiers::ALT), (KeyCode::Up, KeyModifiers::META)]);

    m.insert(HistoryPrevious, vec![(KeyCode::Up, KeyModifiers::empty())]);
    m.insert(HistoryNext, vec![(KeyCode::Down, KeyModifiers::empty())]);

    m.insert(
        ToggleToolDisplayMode,
        vec![
            (KeyCode::Char('t'), KeyModifiers::ALT),
            (KeyCode::Char('T'), KeyModifiers::ALT),
        ],
    );
    m.insert(
        ToggleTaskPanel,
        vec![
            (KeyCode::Char('g'), KeyModifiers::ALT),
            (KeyCode::Char('G'), KeyModifiers::ALT),
        ],
    );
    m.insert(
        OpenTranscriptReview,
        vec![
            (KeyCode::Char('t'), KeyModifiers::CONTROL),
            (KeyCode::Char('T'), KeyModifiers::CONTROL),
        ],
    );
    m.insert(ToggleTranscriptRenderMode, vec![(KeyCode::Char('r'), KeyModifiers::empty())]);
    m.insert(
        GeneratePromptSuggestion,
        vec![
            (KeyCode::Char('p'), KeyModifiers::ALT),
            (KeyCode::Char('P'), KeyModifiers::ALT),
        ],
    );

    m
}

/// Compiled store of key → action mappings, built from defaults + user overrides.
///
/// Lookup is O(number of mapped keys) — the total is small (<50 entries) so a
/// simple linear scan is faster than a nested hash map.
#[derive(Debug, Clone)]
pub struct BindingStore {
    /// Flat list of (key, modifiers) → action for O(n) scan.
    entries: Vec<(KeyCode, KeyModifiers, Action)>,
    /// First configured key for each action, used by UI affordances.
    primary_labels: HashMap<Action, String>,
    /// Actions whose defaults were explicitly replaced or unbound.
    overridden_actions: HashSet<Action>,
}

impl Default for BindingStore {
    fn default() -> Self {
        Self::defaults()
    }
}

impl BindingStore {
    /// Build from a user-provided overlay on top of the built-in defaults.
    ///
    /// `overlay` is a `HashMap<action_name, Vec<key_spec_string>>` — exactly
    /// the shape of `KeyBindingConfig::bindings` and
    /// `UserPreferences::keybindings`.
    pub(crate) fn new(overlay: HashMap<String, Vec<String>>) -> Self {
        let mut merged: HashMap<Action, Vec<(KeyCode, KeyModifiers)>> = default_bindings();
        let mut configured_actions = HashSet::new();

        for (action_name, key_specs) in overlay {
            let Some(action) = Action::from_name(&action_name) else {
                tracing::debug!(%action_name, "unknown action in keybinding overlay, skipping");
                continue;
            };

            let parsed: Vec<(KeyCode, KeyModifiers)> = key_specs.iter().filter_map(|s| parse_key_binding(s)).collect();

            if key_specs.is_empty() {
                // Empty list → unbind (remove defaults).
                merged.remove(&action);
                configured_actions.insert(action);
            } else if parsed.is_empty() {
                tracing::warn!(
                    action = action.name(),
                    "keybinding overlay contains no valid bindings; keeping defaults"
                );
            } else {
                merged.insert(action, parsed);
                configured_actions.insert(action);
            }
        }

        let mut entries = Vec::new();
        // HashMap iteration is intentionally randomized. Keep configured
        // actions ahead of defaults, but use the stable Action::all order for
        // both groups so a collision always has the same winner.
        for action in Action::all()
            .iter()
            .copied()
            .filter(|action| configured_actions.contains(action))
            .chain(
                Action::all()
                    .iter()
                    .copied()
                    .filter(|action| !configured_actions.contains(action)),
            )
        {
            let Some(keys) = merged.get(&action) else {
                continue;
            };
            for &(code, mods) in keys {
                entries.push((code, mods, action));
            }
        }

        // Derive labels from the effective entries rather than the requested
        // lists. If two actions claim the same key, the deterministic entry
        // order above decides the winner; the losing action must not advertise
        // a shortcut that can never reach it.
        let mut primary_labels = HashMap::new();
        let mut claimed_bindings = Vec::with_capacity(entries.len());
        for &(code, mods, action) in &entries {
            let is_reachable = !claimed_bindings
                .iter()
                .any(|&(claimed_code, claimed_mods)| binding_covers(claimed_code, claimed_mods, code, mods));
            if is_reachable {
                primary_labels.entry(action).or_insert_with(|| format_key_label(code, mods));
            }
            claimed_bindings.push((code, mods));
        }

        Self {
            entries,
            primary_labels,
            overridden_actions: configured_actions,
        }
    }

    /// Build with only the default bindings.
    fn defaults() -> Self {
        Self::new(HashMap::new())
    }

    /// Look up the action bound to a given key event.
    ///
    /// Returns `None` when the key has no binding (fall through to hardcoded
    /// dispatch).
    pub(crate) fn resolve(&self, key: &KeyEvent) -> Option<Action> {
        // Terminals may report Ctrl+letter as the corresponding C0 control
        // character without a CONTROL modifier. Normalize that form before
        // matching so configured bindings behave consistently across terminal
        // implementations (notably macOS terminal paths for Ctrl+T).
        let (key_code, key_modifiers) = normalize_terminal_control_key(key.code, key.modifiers);
        let mut best: Option<(usize, Action)> = None;

        // Iterate entries. For `Char` codes we also try a case-insensitive
        // match to handle terminal ambiguity (e.g. Ctrl+C vs Ctrl+Shift+C).
        for (i, &(code, mods, action)) in self.entries.iter().enumerate() {
            let code_match = match (code, key_code) {
                (KeyCode::Char(bc), KeyCode::Char(kc)) if bc.eq_ignore_ascii_case(&kc) => true,
                _ => code == key_code,
            };

            if !code_match {
                continue;
            }

            // All declared modifiers must be present.
            if !key_modifiers.contains(mods) {
                continue;
            }

            // For Char codes, SHIFT is already reflected in character case,
            // so allow it as an "extra" modifier without penalty.
            let char_shift_grace = if let KeyCode::Char(_) = key_code {
                KeyModifiers::SHIFT
            } else {
                KeyModifiers::empty()
            };
            let extra = key_modifiers.difference(mods);
            if extra.intersection(!char_shift_grace) != KeyModifiers::empty() {
                continue;
            }

            // Prefer earlier entries (user overrides come first, or we use
            // insertion order and give priority to the first binding).
            best = match best {
                None => Some((i, action)),
                Some((bi, _)) if i < bi => Some((i, action)),
                Some(other) => Some(other),
            };
        }

        best.map(|(_, action)| action)
    }

    pub(crate) fn primary_key_label(&self, action: Action) -> Option<&str> {
        self.primary_labels.get(&action).map(String::as_str)
    }

    pub(crate) fn action_is_overridden(&self, action: Action) -> bool {
        self.overridden_actions.contains(&action)
    }
}

fn binding_covers(
    existing_code: KeyCode,
    existing_modifiers: KeyModifiers,
    candidate_code: KeyCode,
    candidate_modifiers: KeyModifiers,
) -> bool {
    let code_matches = match (existing_code, candidate_code) {
        (KeyCode::Char(existing), KeyCode::Char(candidate)) => existing.eq_ignore_ascii_case(&candidate),
        _ => existing_code == candidate_code,
    };
    if !code_matches || !candidate_modifiers.contains(existing_modifiers) {
        return false;
    }

    let char_shift_grace = if matches!(candidate_code, KeyCode::Char(_)) {
        KeyModifiers::SHIFT
    } else {
        KeyModifiers::empty()
    };
    candidate_modifiers
        .difference(existing_modifiers)
        .intersection(!char_shift_grace)
        == KeyModifiers::empty()
}

pub(crate) fn normalize_terminal_control_event(mut key: KeyEvent) -> KeyEvent {
    let (code, modifiers) = normalize_terminal_control_key(key.code, key.modifiers);
    key.code = code;
    key.modifiers = modifiers;
    key
}

/// Return whether a key belongs to the composer shortcuts that intentionally
/// remain outside the configurable action dispatch.
pub(crate) fn is_readline_editing_key(key: &KeyEvent) -> bool {
    let modifiers = key.modifiers;
    let has_control = modifiers.contains(KeyModifiers::CONTROL);
    let has_alt = modifiers.contains(KeyModifiers::ALT);
    let has_command = modifiers.intersects(KeyModifiers::SUPER | KeyModifiers::META);

    if has_command {
        return false;
    }

    (has_control
        && !has_alt
        && matches!(
            key.code,
            KeyCode::Char('f')
                | KeyCode::Char('F')
                | KeyCode::Char('b')
                | KeyCode::Char('B')
                | KeyCode::Char('p')
                | KeyCode::Char('P')
                | KeyCode::Char('n')
                | KeyCode::Char('N')
                | KeyCode::Char('t')
                | KeyCode::Char('T')
        ))
        || (has_alt
            && !has_control
            && matches!(
                key.code,
                KeyCode::Char('d')
                    | KeyCode::Char('D')
                    | KeyCode::Char('u')
                    | KeyCode::Char('U')
                    | KeyCode::Char('l')
                    | KeyCode::Char('L')
                    | KeyCode::Char('c')
                    | KeyCode::Char('C')
                    | KeyCode::Char('\\')
            ))
}

fn normalize_terminal_control_key(code: KeyCode, modifiers: KeyModifiers) -> (KeyCode, KeyModifiers) {
    let Some(control) = (match code {
        KeyCode::Char(character) => Some(character as u32),
        _ => None,
    }) else {
        return (code, modifiers);
    };

    // Preserve the terminal's dedicated Tab and Enter paths. In particular,
    // treating C0 tab/return bytes as Ctrl+I/Ctrl+M would bypass multiline
    // input and submit handling on terminals that encode those keys directly.
    if !(1..=26).contains(&control) || matches!(control, 9 | 13) {
        return (code, modifiers);
    }

    let Some(letter) = char::from_u32(u32::from(b'a') + control - 1) else {
        return (code, modifiers);
    };
    (KeyCode::Char(letter), modifiers | KeyModifiers::CONTROL)
}

fn format_key_label(code: KeyCode, modifiers: KeyModifiers) -> String {
    let mut parts = Vec::with_capacity(5);
    if modifiers.contains(KeyModifiers::CONTROL) {
        parts.push("Ctrl");
    }
    if modifiers.contains(KeyModifiers::ALT) {
        parts.push("Alt");
    }
    if modifiers.contains(KeyModifiers::SUPER) {
        parts.push("Cmd");
    } else if modifiers.contains(KeyModifiers::META) {
        parts.push("Meta");
    }
    if modifiers.contains(KeyModifiers::SHIFT) {
        parts.push("Shift");
    }

    let key = match code {
        KeyCode::Char(' ') => "Space".to_string(),
        KeyCode::Char(character) => character.to_ascii_uppercase().to_string(),
        KeyCode::Enter => "Enter".to_string(),
        KeyCode::Tab => "Tab".to_string(),
        KeyCode::BackTab => "Backtab".to_string(),
        KeyCode::Esc => "Esc".to_string(),
        KeyCode::Backspace => "Backspace".to_string(),
        KeyCode::Delete => "Delete".to_string(),
        KeyCode::Insert => "Insert".to_string(),
        KeyCode::Up => "Up".to_string(),
        KeyCode::Down => "Down".to_string(),
        KeyCode::Left => "Left".to_string(),
        KeyCode::Right => "Right".to_string(),
        KeyCode::PageUp => "PageUp".to_string(),
        KeyCode::PageDown => "PageDown".to_string(),
        KeyCode::Home => "Home".to_string(),
        KeyCode::End => "End".to_string(),
        KeyCode::F(number) => format!("F{number}"),
        other => format!("{other:?}"),
    };
    parts.push(key.as_str());
    parts.join("+")
}

impl super::Session {
    pub(crate) fn resolve_rebindable_action(&self, key: &KeyEvent) -> Option<Action> {
        self.bindings.resolve(key)
    }

    pub(crate) fn dispatch_rebindable_action(&mut self, action: Action) -> Option<super::super::types::InlineEvent> {
        super::events::dispatch_rebindable_action(self, action)
    }

    pub(crate) fn primary_binding_label(&self, action: Action) -> Option<&str> {
        self.bindings.primary_key_label(action)
    }

    pub(crate) fn rebindable_action_is_overridden(&self, action: Action) -> bool {
        self.bindings.action_is_overridden(action)
    }

    pub(crate) fn set_bindings(&mut self, bindings: BindingStore) {
        self.bindings = bindings;
        self.needs_redraw = true;
    }

    pub(crate) fn transcript_review_hints_visible(&self) -> bool {
        self.appearance.show_transcript_review_hints
    }

    pub(crate) fn transcript_review_shortcut_guide_visible(&self) -> bool {
        self.appearance.show_transcript_review_shortcut_guide
    }

    pub(crate) fn transcript_review_close_button_visible(&self) -> bool {
        self.appearance.show_transcript_review_close_button
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use ratatui::crossterm::event::KeyEvent;

    #[test]
    fn test_parse_key_binding_simple() {
        let (code, mods) = parse_key_binding("ctrl+c").unwrap();
        assert_eq!(code, KeyCode::Char('c'));
        assert!(mods.contains(KeyModifiers::CONTROL));
        assert!(!mods.contains(KeyModifiers::SHIFT));
    }

    #[test]
    fn test_parse_key_binding_modifier_combos() {
        let (code, mods) = parse_key_binding("ctrl+shift+enter").unwrap();
        assert_eq!(code, KeyCode::Enter);
        assert!(mods.contains(KeyModifiers::CONTROL));
        assert!(mods.contains(KeyModifiers::SHIFT));
    }

    #[test]
    fn test_parse_key_binding_is_case_insensitive() {
        let (code, mods) = parse_key_binding("CTRL+SHIFT+X").unwrap();
        assert_eq!(code, KeyCode::Char('x'));
        assert!(mods.contains(KeyModifiers::CONTROL | KeyModifiers::SHIFT));
    }

    #[test]
    fn test_parse_key_binding_func_keys() {
        let (code, _) = parse_key_binding("f5").unwrap();
        assert_eq!(code, KeyCode::F(5));
    }

    #[test]
    fn test_parse_key_binding_special() {
        let (code, _) = parse_key_binding("pageup").unwrap();
        assert_eq!(code, KeyCode::PageUp);
        let (code, _) = parse_key_binding("backtab").unwrap();
        assert_eq!(code, KeyCode::BackTab);
    }

    #[test]
    fn test_default_bindings_resolve() {
        let store = BindingStore::defaults();

        // Ctrl+C → Interrupt
        let key = KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL);
        assert_eq!(store.resolve(&key), Some(Action::Interrupt));

        // PageUp → ScrollPageUp
        let key = KeyEvent::new(KeyCode::PageUp, KeyModifiers::empty());
        assert_eq!(store.resolve(&key), Some(Action::ScrollPageUp));

        // Alt+Up → EditQueue
        let key = KeyEvent::new(KeyCode::Up, KeyModifiers::ALT);
        assert_eq!(store.resolve(&key), Some(Action::EditQueue));

        // Unbound → None
        let key = KeyEvent::new(KeyCode::Char('x'), KeyModifiers::CONTROL);
        assert_eq!(store.resolve(&key), None);
    }

    #[test]
    fn test_default_bindings_case_insensitive() {
        let store = BindingStore::defaults();

        // Ctrl+C (uppercase C) → Interrupt
        let key = KeyEvent::new(KeyCode::Char('C'), KeyModifiers::CONTROL);
        assert_eq!(store.resolve(&key), Some(Action::Interrupt));
    }

    #[test]
    fn test_default_tool_display_binding_is_alt_t() {
        let store = BindingStore::defaults();
        let key = KeyEvent::new(KeyCode::Char('t'), KeyModifiers::ALT);
        assert_eq!(store.resolve(&key), Some(Action::ToggleToolDisplayMode));
    }

    #[test]
    fn test_default_task_panel_binding_is_alt_g() {
        let store = BindingStore::defaults();
        let key = KeyEvent::new(KeyCode::Char('g'), KeyModifiers::ALT);
        assert_eq!(store.resolve(&key), Some(Action::ToggleTaskPanel));
    }

    #[test]
    fn test_default_transcript_review_bindings() {
        let store = BindingStore::defaults();
        assert_eq!(
            store.resolve(&KeyEvent::new(KeyCode::Char('t'), KeyModifiers::CONTROL)),
            Some(Action::OpenTranscriptReview)
        );
        assert_eq!(
            store.resolve(&KeyEvent::new(KeyCode::Char('r'), KeyModifiers::empty())),
            Some(Action::ToggleTranscriptRenderMode)
        );
    }

    #[test]
    fn test_raw_control_character_matches_transcript_review_binding() {
        let store = BindingStore::defaults();

        assert_eq!(
            store.resolve(&KeyEvent::new(KeyCode::Char('\u{14}'), KeyModifiers::empty())),
            Some(Action::OpenTranscriptReview)
        );
    }

    #[test]
    fn test_transcript_review_bindings_can_be_rebound() {
        let mut overlay = HashMap::new();
        overlay.insert("open_transcript_review".to_string(), vec!["ctrl+x".to_string()]);
        overlay.insert("toggle_transcript_render_mode".to_string(), vec!["alt+x".to_string()]);
        let store = BindingStore::new(overlay);

        assert_eq!(
            store.resolve(&KeyEvent::new(KeyCode::Char('x'), KeyModifiers::CONTROL)),
            Some(Action::OpenTranscriptReview)
        );
        assert_eq!(
            store.resolve(&KeyEvent::new(KeyCode::Char('x'), KeyModifiers::ALT)),
            Some(Action::ToggleTranscriptRenderMode)
        );
        assert_eq!(store.resolve(&KeyEvent::new(KeyCode::Char('r'), KeyModifiers::empty())), None);
    }

    #[test]
    fn test_task_panel_binding_can_be_rebound() {
        let mut overlay = HashMap::new();
        overlay.insert("toggle_task_panel".to_string(), vec!["ctrl+x".to_string()]);
        let store = BindingStore::new(overlay);

        assert_eq!(
            store.resolve(&KeyEvent::new(KeyCode::Char('x'), KeyModifiers::CONTROL)),
            Some(Action::ToggleTaskPanel)
        );
        assert_eq!(store.resolve(&KeyEvent::new(KeyCode::Char('g'), KeyModifiers::ALT)), None);
    }

    #[test]
    fn test_tool_display_binding_can_be_rebound() {
        let mut overlay = HashMap::new();
        overlay.insert("toggle_tool_display_mode".to_string(), vec!["ctrl+x".to_string()]);
        let store = BindingStore::new(overlay);

        assert_eq!(
            store.resolve(&KeyEvent::new(KeyCode::Char('x'), KeyModifiers::CONTROL)),
            Some(Action::ToggleToolDisplayMode)
        );
        assert_eq!(store.resolve(&KeyEvent::new(KeyCode::Char('t'), KeyModifiers::ALT)), None);
    }

    #[test]
    fn test_user_overlay_overrides_default() {
        let mut overlay = HashMap::new();
        overlay.insert("interrupt".to_string(), vec!["ctrl+x".to_string()]);
        let store = BindingStore::new(overlay);

        // Old binding no longer works
        let key_c = KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL);
        assert_eq!(store.resolve(&key_c), None);

        // New binding works
        let key_x = KeyEvent::new(KeyCode::Char('x'), KeyModifiers::CONTROL);
        assert_eq!(store.resolve(&key_x), Some(Action::Interrupt));
    }

    #[test]
    fn test_user_overlay_unbind() {
        let mut overlay = HashMap::new();
        overlay.insert("interrupt".to_string(), Vec::new());
        let store = BindingStore::new(overlay);

        let key = KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL);
        assert_eq!(store.resolve(&key), None);
    }

    #[test]
    fn test_invalid_overlay_keeps_defaults_and_does_not_override_action() {
        let mut overlay = HashMap::new();
        overlay.insert("interrupt".to_string(), vec!["not-a-key".to_string()]);
        let store = BindingStore::new(overlay);

        assert_eq!(store.resolve(&KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL)), Some(Action::Interrupt));
        assert!(!store.action_is_overridden(Action::Interrupt));
    }

    #[test]
    fn test_configured_binding_collision_is_deterministic() {
        let mut overlay = HashMap::new();
        overlay.insert("interrupt".to_string(), vec!["ctrl+x".to_string()]);
        overlay.insert("open_model_picker".to_string(), vec!["ctrl+x".to_string()]);
        let store = BindingStore::new(overlay);

        assert_eq!(store.resolve(&KeyEvent::new(KeyCode::Char('x'), KeyModifiers::CONTROL)), Some(Action::Interrupt));
        assert_eq!(store.primary_key_label(Action::Interrupt), Some("Ctrl+X"));
        assert_eq!(store.primary_key_label(Action::OpenModelPicker), None);
    }

    #[test]
    fn test_parse_invalid_key() {
        assert!(parse_key_binding("").is_none());
        assert!(parse_key_binding("invalid_key_name").is_none());
        assert!(parse_key_binding("ctrl+invalid").is_none());
        assert!(parse_key_binding("+ctrl+c").is_none());
    }

    #[test]
    fn test_action_name_roundtrip() {
        for action in Action::all() {
            let name = action.name();
            let parsed = Action::from_name(name);
            assert_eq!(parsed, Some(*action));
        }
    }

    #[test]
    fn test_action_from_name_unknown() {
        assert_eq!(Action::from_name("nonexistent"), None);
    }
}