envision 0.16.0

A ratatui framework for collaborative TUI development with headless testing support
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
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
//! A rich tab bar component with closable, modified, and icon-decorated tabs.
//!
//! [`TabBar`] provides a horizontal tab bar with features commonly found in
//! editors and browsers: closable tabs, modified indicators, optional icons,
//! and horizontal scrolling for overflow. State is stored in [`TabBarState`],
//! updated via [`TabBarMessage`], and produces [`TabBarOutput`].
//!
//! See also [`Tabs`](super::Tabs) for a simpler tab component without
//! closable tabs or overflow scrolling.
//!
//! # Example
//!
//! ```rust
//! use envision::component::{
//!     Component, Tab, TabBar, TabBarMessage, TabBarOutput, TabBarState,
//! };
//!
//! let tabs = vec![
//!     Tab::new("file1", "main.rs"),
//!     Tab::new("file2", "lib.rs").with_modified(true),
//!     Tab::new("file3", "test.rs").with_closable(true),
//! ];
//! let mut state = TabBarState::new(tabs);
//!
//! assert_eq!(state.selected_index(), Some(0));
//! assert_eq!(state.active_tab().map(|t| t.label()), Some("main.rs"));
//!
//! // Navigate to the next tab
//! let output = TabBar::update(&mut state, TabBarMessage::NextTab);
//! assert_eq!(output, Some(TabBarOutput::TabSelected(1)));
//! assert_eq!(state.selected_index(), Some(1));
//! ```

use super::{Component, EventContext, RenderContext};
use crate::input::{Event, Key};
use crate::theme::Theme;

mod render;
#[cfg(test)]
use render::truncate_label;

// ---------------------------------------------------------------------------
// Tab
// ---------------------------------------------------------------------------

/// A single tab in a [`TabBar`].
///
/// Each tab has a unique identifier, a display label, and optional properties
/// for closability, modified state, and an icon prefix.
///
/// # Example
///
/// ```rust
/// use envision::component::Tab;
///
/// let tab = Tab::new("editor-1", "main.rs")
///     .with_closable(true)
///     .with_modified(true)
///     .with_icon("R");
/// assert_eq!(tab.id(), "editor-1");
/// assert_eq!(tab.label(), "main.rs");
/// assert!(tab.closable());
/// assert!(tab.modified());
/// assert_eq!(tab.icon(), Some("R"));
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub struct Tab {
    /// Unique identifier for this tab.
    pub(super) id: String,
    /// Display label.
    pub(super) label: String,
    /// Whether the tab can be closed.
    pub(super) closable: bool,
    /// Whether the tab has unsaved modifications.
    pub(super) modified: bool,
    /// Optional icon prefix (e.g., a file-type indicator).
    pub(super) icon: Option<String>,
}

// Tab methods are in tab.rs
mod tab;

// ---------------------------------------------------------------------------
// TabBarMessage
// ---------------------------------------------------------------------------

/// Messages that can be sent to a [`TabBar`] component.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum TabBarMessage {
    /// Select a specific tab by index.
    SelectTab(usize),
    /// Move to the next (right) tab.
    NextTab,
    /// Move to the previous (left) tab.
    PrevTab,
    /// Close a specific tab by index.
    CloseTab(usize),
    /// Close the currently active tab (only if closable).
    CloseActiveTab,
    /// Add a new tab (appended to the end) and make it active.
    AddTab(Tab),
    /// Jump to the first tab.
    First,
    /// Jump to the last tab.
    Last,
}

// ---------------------------------------------------------------------------
// TabBarOutput
// ---------------------------------------------------------------------------

/// Output messages from a [`TabBar`] component.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum TabBarOutput {
    /// A tab was selected (contains the new active index).
    TabSelected(usize),
    /// A tab was closed (contains the index that was closed).
    TabClosed(usize),
    /// A new tab was added (contains the index of the new tab).
    TabAdded(usize),
}

// ---------------------------------------------------------------------------
// TabBarState
// ---------------------------------------------------------------------------

/// State for a [`TabBar`] component.
///
/// Tracks the list of tabs, the active tab, scroll offset for overflow,
/// an optional maximum tab width, and focus/disabled state.
///
/// # Example
///
/// ```rust
/// use envision::component::{Tab, TabBarState};
///
/// let tabs = vec![
///     Tab::new("a", "Alpha"),
///     Tab::new("b", "Beta"),
///     Tab::new("c", "Gamma"),
/// ];
/// let state = TabBarState::new(tabs);
/// assert_eq!(state.len(), 3);
/// assert_eq!(state.selected_index(), Some(0));
/// ```
#[derive(Clone, Debug, Default)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub struct TabBarState {
    /// The tabs.
    tabs: Vec<Tab>,
    /// Index of the active tab, or `None` when empty.
    active: Option<usize>,
    /// Horizontal scroll offset (number of tabs scrolled past the left edge).
    scroll_offset: usize,
    /// Optional maximum rendered width per tab (in columns).
    max_tab_width: Option<usize>,
}

impl PartialEq for TabBarState {
    fn eq(&self, other: &Self) -> bool {
        self.tabs == other.tabs
            && self.active == other.active
            && self.scroll_offset == other.scroll_offset
            && self.max_tab_width == other.max_tab_width
    }
}

impl TabBarState {
    /// Creates a new tab bar state with the first tab active.
    ///
    /// If `tabs` is empty the active index is `None`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{Tab, TabBarState};
    ///
    /// let state = TabBarState::new(vec![
    ///     Tab::new("1", "One"),
    ///     Tab::new("2", "Two"),
    /// ]);
    /// assert_eq!(state.selected_index(), Some(0));
    /// assert_eq!(state.len(), 2);
    /// ```
    pub fn new(tabs: Vec<Tab>) -> Self {
        let active = if tabs.is_empty() { None } else { Some(0) };
        Self {
            tabs,
            active,
            scroll_offset: 0,
            max_tab_width: None,
        }
    }

    /// Creates a tab bar state with a specific tab selected.
    ///
    /// The index is clamped to the valid range. Empty tabs yield `None` selected.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{Tab, TabBarState};
    ///
    /// let state = TabBarState::with_selected(
    ///     vec![Tab::new("a", "A"), Tab::new("b", "B"), Tab::new("c", "C")],
    ///     1,
    /// );
    /// assert_eq!(state.selected_index(), Some(1));
    /// ```
    pub fn with_selected(tabs: Vec<Tab>, active: usize) -> Self {
        let active = if tabs.is_empty() {
            None
        } else {
            Some(active.min(tabs.len() - 1))
        };
        Self {
            tabs,
            active,
            scroll_offset: 0,
            max_tab_width: None,
        }
    }

    // -- Builder methods -----------------------------------------------------

    /// Sets the max tab width (builder).
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{Tab, TabBarState};
    ///
    /// let state = TabBarState::new(vec![Tab::new("a", "Alpha")])
    ///     .with_max_tab_width(Some(20));
    /// assert_eq!(state.max_tab_width(), Some(20));
    /// ```
    pub fn with_max_tab_width(mut self, max: Option<usize>) -> Self {
        self.max_tab_width = max;
        self
    }

    // -- Accessors -----------------------------------------------------------

    /// Returns the tabs.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{Tab, TabBarState};
    ///
    /// let state = TabBarState::new(vec![
    ///     Tab::new("a", "Alpha"),
    ///     Tab::new("b", "Beta"),
    /// ]);
    /// assert_eq!(state.tabs().len(), 2);
    /// assert_eq!(state.tabs()[0].label(), "Alpha");
    /// ```
    pub fn tabs(&self) -> &[Tab] {
        &self.tabs
    }

    /// Returns a mutable reference to the tabs.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{Tab, TabBarState};
    ///
    /// let mut state = TabBarState::new(vec![Tab::new("a", "Alpha")]);
    /// state.tabs_mut()[0].set_label("Renamed");
    /// assert_eq!(state.tabs()[0].label(), "Renamed");
    /// ```
    pub fn tabs_mut(&mut self) -> &mut [Tab] {
        &mut self.tabs
    }

    /// Returns the currently selected tab index, or `None` if empty.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{Tab, TabBarState};
    ///
    /// let state = TabBarState::new(vec![Tab::new("a", "A"), Tab::new("b", "B")]);
    /// assert_eq!(state.selected_index(), Some(0));
    ///
    /// let empty = TabBarState::new(vec![]);
    /// assert_eq!(empty.selected_index(), None);
    /// ```
    pub fn selected_index(&self) -> Option<usize> {
        self.active
    }

    /// Returns the selected tab index, or `None` if the tab bar is empty.
    ///
    /// This is the getter counterpart to [`set_selected`](Self::set_selected).
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{Tab, TabBarState};
    ///
    /// let mut state = TabBarState::new(vec![
    ///     Tab::new("a", "A"),
    ///     Tab::new("b", "B"),
    /// ]);
    /// assert_eq!(state.selected(), Some(0));
    ///
    /// state.set_selected(Some(1));
    /// assert_eq!(state.selected(), Some(1));
    ///
    /// state.set_selected(None);
    /// assert_eq!(state.selected(), None);
    /// ```
    pub fn selected(&self) -> Option<usize> {
        self.active
    }

    /// Returns the currently active tab, or `None` if empty.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{Tab, TabBarState};
    ///
    /// let state = TabBarState::new(vec![Tab::new("a", "Alpha")]);
    /// assert_eq!(state.active_tab().unwrap().label(), "Alpha");
    /// ```
    pub fn active_tab(&self) -> Option<&Tab> {
        self.tabs.get(self.active?)
    }

    /// Returns a mutable reference to the active tab.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{Tab, TabBarState};
    ///
    /// let mut state = TabBarState::new(vec![Tab::new("a", "Alpha")]);
    /// state.active_tab_mut().unwrap().set_modified(true);
    /// assert!(state.active_tab().unwrap().modified());
    /// ```
    pub fn active_tab_mut(&mut self) -> Option<&mut Tab> {
        let idx = self.active?;
        self.tabs.get_mut(idx)
    }

    /// Returns the number of tabs.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{Tab, TabBarState};
    ///
    /// let state = TabBarState::new(vec![Tab::new("a", "A"), Tab::new("b", "B")]);
    /// assert_eq!(state.len(), 2);
    /// ```
    pub fn len(&self) -> usize {
        self.tabs.len()
    }

    /// Returns true if there are no tabs.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::TabBarState;
    ///
    /// let state = TabBarState::new(vec![]);
    /// assert!(state.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        self.tabs.is_empty()
    }

    /// Returns the scroll offset.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{Tab, TabBarState};
    ///
    /// let state = TabBarState::new(vec![Tab::new("a", "A")]);
    /// assert_eq!(state.scroll_offset(), 0);
    /// ```
    pub fn scroll_offset(&self) -> usize {
        self.scroll_offset
    }

    /// Returns the maximum tab width, if set.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{Tab, TabBarState};
    ///
    /// let state = TabBarState::new(vec![Tab::new("a", "A")]);
    /// assert_eq!(state.max_tab_width(), None);
    ///
    /// let state = state.with_max_tab_width(Some(20));
    /// assert_eq!(state.max_tab_width(), Some(20));
    /// ```
    pub fn max_tab_width(&self) -> Option<usize> {
        self.max_tab_width
    }

    // -- Mutators ------------------------------------------------------------

    /// Sets the selected tab by index.
    ///
    /// `None` clears the selection. `Some(i)` is clamped to the valid range.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{Tab, TabBarState};
    ///
    /// let mut state = TabBarState::new(vec![
    ///     Tab::new("a", "A"),
    ///     Tab::new("b", "B"),
    /// ]);
    /// state.set_selected(Some(1));
    /// assert_eq!(state.selected_index(), Some(1));
    ///
    /// state.set_selected(None);
    /// assert_eq!(state.selected_index(), None);
    /// ```
    pub fn set_selected(&mut self, index: Option<usize>) {
        match index {
            Some(i) if !self.tabs.is_empty() => {
                self.active = Some(i.min(self.tabs.len() - 1));
            }
            _ => self.active = None,
        }
    }

    /// Sets the scroll offset.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{Tab, TabBarState};
    ///
    /// let mut state = TabBarState::new(vec![Tab::new("a", "A")]);
    /// state.set_scroll_offset(5);
    /// assert_eq!(state.scroll_offset(), 5);
    /// ```
    pub fn set_scroll_offset(&mut self, offset: usize) {
        self.scroll_offset = offset;
    }

    /// Sets the max tab width.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{Tab, TabBarState};
    ///
    /// let mut state = TabBarState::new(vec![Tab::new("a", "A")]);
    /// state.set_max_tab_width(Some(15));
    /// assert_eq!(state.max_tab_width(), Some(15));
    /// ```
    pub fn set_max_tab_width(&mut self, max: Option<usize>) {
        self.max_tab_width = max;
    }

    /// Updates a tab at the given index via a closure.
    ///
    /// No-ops if the index is out of bounds. This is safe because
    /// it does not change the number of tabs or their positions,
    /// so the active index remains valid.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{Tab, TabBarState};
    ///
    /// let mut state = TabBarState::new(vec![
    ///     Tab::new("a", "Alpha"),
    ///     Tab::new("b", "Beta"),
    /// ]);
    /// state.update_tab(1, |tab| tab.set_modified(true));
    /// assert!(state.tabs()[1].modified());
    /// ```
    pub fn update_tab(&mut self, index: usize, f: impl FnOnce(&mut Tab)) {
        if let Some(tab) = self.tabs.get_mut(index) {
            f(tab);
        }
    }

    /// Replaces all tabs, clamping or clearing the active index.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{Tab, TabBarState};
    ///
    /// let mut state = TabBarState::new(vec![Tab::new("a", "A")]);
    /// state.set_tabs(vec![Tab::new("x", "X"), Tab::new("y", "Y")]);
    /// assert_eq!(state.len(), 2);
    /// assert_eq!(state.tabs()[0].label(), "X");
    /// ```
    pub fn set_tabs(&mut self, tabs: Vec<Tab>) {
        self.tabs = tabs;
        if self.tabs.is_empty() {
            self.active = None;
            self.scroll_offset = 0;
        } else if let Some(idx) = self.active {
            if idx >= self.tabs.len() {
                self.active = Some(self.tabs.len() - 1);
            }
        }
    }

    /// Returns a tab by its id, if present.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{Tab, TabBarState};
    ///
    /// let state = TabBarState::new(vec![
    ///     Tab::new("file1", "main.rs"),
    ///     Tab::new("file2", "lib.rs"),
    /// ]);
    /// let (index, tab) = state.find_tab_by_id("file2").unwrap();
    /// assert_eq!(index, 1);
    /// assert_eq!(tab.label(), "lib.rs");
    ///
    /// assert!(state.find_tab_by_id("missing").is_none());
    /// ```
    pub fn find_tab_by_id(&self, id: &str) -> Option<(usize, &Tab)> {
        self.tabs.iter().enumerate().find(|(_, t)| t.id() == id)
    }

    // -- Instance methods that delegate to component -------------------------

    /// Updates the tab bar state with a message, returning any output.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{Tab, TabBarState, TabBarMessage, TabBarOutput};
    ///
    /// let mut state = TabBarState::new(vec![
    ///     Tab::new("a", "A"),
    ///     Tab::new("b", "B"),
    /// ]);
    /// let output = state.update(TabBarMessage::NextTab);
    /// assert_eq!(output, Some(TabBarOutput::TabSelected(1)));
    /// assert_eq!(state.selected_index(), Some(1));
    /// ```
    pub fn update(&mut self, msg: TabBarMessage) -> Option<TabBarOutput> {
        TabBar::update(self, msg)
    }

    // -- Scroll helpers (internal) -------------------------------------------

    /// Ensures the active tab is visible by adjusting the scroll offset.
    fn ensure_active_visible(&mut self) {
        if let Some(active) = self.active {
            if active < self.scroll_offset {
                self.scroll_offset = active;
            }
            // The exact right bound depends on the render width, which we
            // do not know here.  We do a best-effort adjustment: if the
            // active index is beyond what we can see, bump the offset.
            // The view function will do a tighter clamp.
        }
    }
}

// ---------------------------------------------------------------------------
// TabBar component
// ---------------------------------------------------------------------------

/// A rich horizontal tab bar component.
///
/// `TabBar` renders a single row of tabs with support for:
///
/// - **Closable tabs** that show a close indicator (`x`)
/// - **Modified tabs** that show a bullet (`*`)
/// - **Optional icon** prefix per tab
/// - **Horizontal scrolling** when tabs overflow the available width
///   (left/right scroll indicators are displayed)
/// - **Max tab width** to keep the bar compact
///
/// # Navigation
///
/// - `Left` / `h` - previous tab
/// - `Right` / `l` - next tab
/// - `Home` - first tab
/// - `End` - last tab
/// - `w` - close the active tab (if closable)
///
/// # Output
///
/// - [`TabBarOutput::TabSelected`] - a tab was activated
/// - [`TabBarOutput::TabClosed`] - a tab was closed
/// - [`TabBarOutput::TabAdded`] - a new tab was added
///
/// # Example
///
/// ```rust
/// use envision::component::{
///     Component, Tab, TabBar, TabBarMessage, TabBarOutput, TabBarState,
/// };
///
/// let tabs = vec![
///     Tab::new("1", "Overview"),
///     Tab::new("2", "Details").with_closable(true),
/// ];
/// let mut state = TabBarState::new(tabs);
///
/// let output = TabBar::update(&mut state, TabBarMessage::NextTab);
/// assert_eq!(output, Some(TabBarOutput::TabSelected(1)));
/// ```
pub struct TabBar;

impl Component for TabBar {
    type State = TabBarState;
    type Message = TabBarMessage;
    type Output = TabBarOutput;

    fn init() -> Self::State {
        TabBarState::default()
    }

    fn update(state: &mut Self::State, msg: Self::Message) -> Option<Self::Output> {
        match msg {
            TabBarMessage::SelectTab(index) => {
                if state.tabs.is_empty() {
                    return None;
                }
                let clamped = index.min(state.tabs.len() - 1);
                if state.active == Some(clamped) {
                    return None;
                }
                state.active = Some(clamped);
                state.ensure_active_visible();
                Some(TabBarOutput::TabSelected(clamped))
            }

            TabBarMessage::NextTab => {
                let active = state.active?;
                if active >= state.tabs.len().saturating_sub(1) {
                    return None;
                }
                state.active = Some(active + 1);
                state.ensure_active_visible();
                Some(TabBarOutput::TabSelected(active + 1))
            }

            TabBarMessage::PrevTab => {
                let active = state.active?;
                if active == 0 {
                    return None;
                }
                state.active = Some(active - 1);
                state.ensure_active_visible();
                Some(TabBarOutput::TabSelected(active - 1))
            }

            TabBarMessage::CloseTab(index) => {
                if index >= state.tabs.len() {
                    return None;
                }
                if !state.tabs[index].closable {
                    return None;
                }
                state.tabs.remove(index);
                if state.tabs.is_empty() {
                    state.active = None;
                    state.scroll_offset = 0;
                } else if let Some(active) = state.active {
                    if index < active {
                        state.active = Some(active - 1);
                    } else if index == active {
                        // Stay at same index or move to last if needed.
                        if active >= state.tabs.len() {
                            state.active = Some(state.tabs.len() - 1);
                        }
                    }
                    // Clamp scroll offset
                    if state.scroll_offset >= state.tabs.len() {
                        state.scroll_offset = state.tabs.len().saturating_sub(1);
                    }
                }
                state.ensure_active_visible();
                Some(TabBarOutput::TabClosed(index))
            }

            TabBarMessage::CloseActiveTab => {
                let active = state.active?;
                if !state.tabs[active].closable {
                    return None;
                }
                // Delegate to CloseTab
                TabBar::update(state, TabBarMessage::CloseTab(active))
            }

            TabBarMessage::AddTab(tab) => {
                state.tabs.push(tab);
                let new_index = state.tabs.len() - 1;
                state.active = Some(new_index);
                state.ensure_active_visible();
                Some(TabBarOutput::TabAdded(new_index))
            }

            TabBarMessage::First => {
                if state.tabs.is_empty() {
                    return None;
                }
                if state.active == Some(0) {
                    return None;
                }
                state.active = Some(0);
                state.scroll_offset = 0;
                Some(TabBarOutput::TabSelected(0))
            }

            TabBarMessage::Last => {
                if state.tabs.is_empty() {
                    return None;
                }
                let last = state.tabs.len() - 1;
                if state.active == Some(last) {
                    return None;
                }
                state.active = Some(last);
                state.ensure_active_visible();
                Some(TabBarOutput::TabSelected(last))
            }
        }
    }

    fn handle_event(
        _state: &Self::State,
        event: &Event,
        ctx: &EventContext,
    ) -> Option<Self::Message> {
        if !ctx.focused || ctx.disabled {
            return None;
        }
        if let Some(key) = event.as_key() {
            match key.code {
                Key::Left | Key::Char('h') => Some(TabBarMessage::PrevTab),
                Key::Right | Key::Char('l') => Some(TabBarMessage::NextTab),
                Key::Home => Some(TabBarMessage::First),
                Key::End => Some(TabBarMessage::Last),
                Key::Char('w') => Some(TabBarMessage::CloseActiveTab),
                _ => None,
            }
        } else {
            None
        }
    }

    fn view(state: &Self::State, ctx: &mut RenderContext<'_, '_>) {
        render::render_tab_bar(
            state,
            ctx.frame,
            ctx.area,
            ctx.theme,
            ctx.focused,
            ctx.disabled,
        );
    }
}

#[cfg(test)]
mod tests;