gitkraft_gui/widgets/
tab_bar.rs1use iced::widget::{button, container, row, scrollable, text, Space};
5use iced::{Alignment, Element, Length};
6
7use crate::icons;
8use crate::message::Message;
9use crate::state::GitKraft;
10use crate::theme;
11use crate::theme::ThemeColors;
12
13pub fn view(state: &GitKraft) -> Element<'_, Message> {
15 let c = state.colors();
16
17 let mut tabs_row = row![].spacing(0).align_y(Alignment::Center);
18
19 for (idx, tab) in state.tabs.iter().enumerate() {
20 let is_active = idx == state.active_tab;
21 let name = tab.display_name().to_string();
22
23 let icon = if tab.has_repo() {
25 icon!(
26 icons::FOLDER_OPEN,
27 12,
28 if is_active { c.accent } else { c.muted }
29 )
30 } else {
31 icon!(
32 icons::PERSON_FILL,
33 12,
34 if is_active { c.accent } else { c.muted }
35 )
36 };
37
38 let label = text(name).size(12).color(if is_active {
40 c.text_primary
41 } else {
42 c.text_secondary
43 });
44
45 let close_btn: Element<'_, Message> = if state.tabs.len() > 1 {
47 button(icon!(icons::X_CIRCLE, 10, c.muted))
48 .padding([0, 4])
49 .style(theme::ghost_button)
50 .on_press(Message::CloseTab(idx))
51 .into()
52 } else {
53 Space::new(0, 0).into()
54 };
55
56 let tab_content = row![icon, Space::new(6, 0), label, Space::new(4, 0), close_btn]
57 .align_y(Alignment::Center);
58
59 let tab_btn = button(tab_content)
60 .padding([6, 12])
61 .style(if is_active {
62 theme::active_tab_button
63 } else {
64 theme::ghost_button
65 })
66 .on_press(Message::SwitchTab(idx));
67
68 tabs_row = tabs_row.push(tab_btn);
69 }
70
71 let new_tab_btn = button(icon!(icons::PLUS_CIRCLE, 14, c.text_secondary))
73 .padding([6, 10])
74 .style(theme::ghost_button)
75 .on_press(Message::NewTab);
76
77 tabs_row = tabs_row.push(new_tab_btn);
78
79 let scrollable_tabs = scrollable(tabs_row)
80 .direction(scrollable::Direction::Horizontal(
81 scrollable::Scrollbar::new(),
82 ))
83 .style(crate::theme::overlay_scrollbar)
84 .width(Length::Fill);
85
86 container(scrollable_tabs)
87 .width(Length::Fill)
88 .style(tab_bar_style)
89 .into()
90}
91
92fn tab_bar_style(theme: &iced::Theme) -> container::Style {
94 let c = ThemeColors::from_theme(theme);
95 container::Style {
96 background: Some(iced::Background::Color(iced::Color {
97 r: (c.bg.r - 0.02).max(0.0),
98 g: (c.bg.g - 0.02).max(0.0),
99 b: (c.bg.b - 0.02).max(0.0),
100 a: 1.0,
101 })),
102 border: iced::Border {
103 color: c.border,
104 width: 0.0,
105 radius: 0.0.into(),
106 },
107 ..Default::default()
108 }
109}