gitkraft 1.0.0

GitKraft — Git IDE desktop application (Iced GUI)
Documentation
//! Tab bar widget — renders a horizontal row of repository tabs at the top of
//! the window, similar to GitKraken's tab bar.

use iced::widget::{button, container, row, scrollable, text, Space};
use iced::{Alignment, Element, Length};

use crate::icons;
use crate::message::Message;
use crate::state::GitKraft;
use crate::theme;
use crate::theme::ThemeColors;

/// Render the tab bar above the header toolbar.
pub fn view(state: &GitKraft) -> Element<'_, Message> {
    let c = state.colors();

    let mut tabs_row = row![].spacing(2).align_y(Alignment::Center);

    for (idx, tab) in state.tabs.iter().enumerate() {
        let is_active = idx == state.active_tab;
        let name = tab.display_name().to_string();

        // Repo icon
        let icon = if tab.has_repo() {
            icon!(
                icons::FOLDER_OPEN,
                12,
                if is_active { c.accent } else { c.muted }
            )
        } else {
            icon!(
                icons::PERSON_FILL,
                12,
                if is_active { c.accent } else { c.muted }
            )
        };

        // Tab label
        let label = text(name).size(12).color(if is_active {
            c.text_primary
        } else {
            c.text_secondary
        });

        // Build the inner row: icon + name + optional close button
        let mut tab_content = row![icon, Space::new().width(6), label].align_y(Alignment::Center);

        if state.tabs.len() > 1 {
            let close_btn = button(icon!(icons::X_CIRCLE, 10, c.muted))
                .padding([0, 2])
                .style(theme::ghost_button)
                .on_press(Message::CloseTab(idx));
            tab_content = tab_content.push(Space::new().width(6)).push(close_btn);
        }

        // Wrap in a mouse_area so clicking the label/icon area switches tabs
        // without interfering with the close button.
        let tab_style = if is_active {
            theme::active_tab_button as fn(&iced::Theme, button::Status) -> button::Style
        } else {
            theme::ghost_button as fn(&iced::Theme, button::Status) -> button::Style
        };

        let tab_widget = button(tab_content)
            .padding([6, 10])
            .style(tab_style)
            .on_press(Message::SwitchTab(idx));

        tabs_row = tabs_row.push(tab_widget);
    }

    // "+" button to add a new tab
    let new_tab_btn = button(icon!(icons::PLUS_CIRCLE, 14, c.text_secondary))
        .padding([6, 10])
        .style(theme::ghost_button)
        .on_press(Message::NewTab);

    tabs_row = tabs_row.push(new_tab_btn);

    let scrollable_tabs = scrollable(tabs_row)
        .direction(scrollable::Direction::Horizontal(
            scrollable::Scrollbar::new(),
        ))
        .style(crate::theme::overlay_scrollbar)
        .width(Length::Fill);

    container(scrollable_tabs)
        .width(Length::Fill)
        .style(tab_bar_style)
        .into()
}

/// Dark background style for the tab bar — slightly darker than the header.
fn tab_bar_style(theme: &iced::Theme) -> container::Style {
    let c = ThemeColors::from_theme(theme);
    container::Style {
        background: Some(iced::Background::Color(iced::Color {
            r: (c.bg.r - 0.02).max(0.0),
            g: (c.bg.g - 0.02).max(0.0),
            b: (c.bg.b - 0.02).max(0.0),
            a: 1.0,
        })),
        border: iced::Border {
            color: c.border,
            width: 0.0,
            radius: 0.0.into(),
        },
        ..Default::default()
    }
}