tab_bar.rs

 1use gpui::{Div, FocusHandle, Render};
 2use story::Story;
 3
 4use crate::{prelude::*, Tab, TabBar, TabPosition};
 5
 6pub struct TabBarStory {
 7    tab_bar_focus_handle: FocusHandle,
 8}
 9
10impl TabBarStory {
11    pub fn new(cx: &mut ViewContext<Self>) -> Self {
12        Self {
13            tab_bar_focus_handle: cx.focus_handle(),
14        }
15    }
16}
17
18impl Render for TabBarStory {
19    type Element = Div;
20
21    fn render(&mut self, _cx: &mut ViewContext<Self>) -> Self::Element {
22        let tab_count = 20;
23        let selected_tab_index = 3;
24
25        let tabs = (0..tab_count)
26            .map(|index| {
27                Tab::new(index)
28                    .selected(index == selected_tab_index)
29                    .position(if index == 0 {
30                        TabPosition::First
31                    } else if index == tab_count - 1 {
32                        TabPosition::Last
33                    } else {
34                        TabPosition::Middle(index.cmp(&selected_tab_index))
35                    })
36                    .child(Label::new(format!("Tab {}", index + 1)).color(
37                        if index == selected_tab_index {
38                            Color::Default
39                        } else {
40                            Color::Muted
41                        },
42                    ))
43            })
44            .collect::<Vec<_>>();
45
46        Story::container()
47            .child(Story::title_for::<TabBar>())
48            .child(Story::label("Default"))
49            .child(
50                h_stack().child(
51                    TabBar::new("tab_bar_1", self.tab_bar_focus_handle.clone())
52                        .start_child(
53                            IconButton::new("navigate_backward", Icon::ArrowLeft)
54                                .icon_size(IconSize::Small),
55                        )
56                        .start_child(
57                            IconButton::new("navigate_forward", Icon::ArrowRight)
58                                .icon_size(IconSize::Small),
59                        )
60                        .end_child(IconButton::new("new", Icon::Plus).icon_size(IconSize::Small))
61                        .end_child(
62                            IconButton::new("split_pane", Icon::Split).icon_size(IconSize::Small),
63                        )
64                        .children(tabs),
65                ),
66            )
67    }
68}