Skip to main content

fc_ui/capabilities/navigation/
status_bar.rs

1//! Status bar component with customizable sections.
2
3use crate::capabilities::foundation::theme::{use_theme, Theme};
4use crate::capabilities::primitives::display::badge::{Badge, BadgeVariant};
5use crate::capabilities::primitives::icon::Icon;
6use crate::capabilities::primitives::icon_source::IconSource;
7use crate::capabilities::primitives::text::caption;
8use gpui::{prelude::FluentBuilder as _, InteractiveElement, *};
9use std::rc::Rc;
10
11#[derive(Clone)]
12pub struct StatusItem {
13    pub icon: Option<IconSource>,
14    pub text: Option<SharedString>,
15    pub badge: Option<SharedString>,
16    pub badge_variant: BadgeVariant,
17    pub on_click: Option<Rc<dyn Fn(&mut Window, &mut App)>>,
18    pub disabled: bool,
19    pub tooltip: Option<SharedString>,
20}
21
22impl StatusItem {
23    pub fn text(text: impl Into<SharedString>) -> Self {
24        Self {
25            icon: None,
26            text: Some(text.into()),
27            badge: None,
28            badge_variant: BadgeVariant::Default,
29            on_click: None,
30            disabled: false,
31            tooltip: None,
32        }
33    }
34
35    pub fn icon(icon: impl Into<IconSource>) -> Self {
36        Self {
37            icon: Some(icon.into()),
38            text: None,
39            badge: None,
40            badge_variant: BadgeVariant::Default,
41            on_click: None,
42            disabled: false,
43            tooltip: None,
44        }
45    }
46
47    pub fn icon_text(icon: impl Into<IconSource>, text: impl Into<SharedString>) -> Self {
48        Self {
49            icon: Some(icon.into()),
50            text: Some(text.into()),
51            badge: None,
52            badge_variant: BadgeVariant::Default,
53            on_click: None,
54            disabled: false,
55            tooltip: None,
56        }
57    }
58
59    pub fn badge(text: impl Into<SharedString>, tooltip: impl Into<SharedString>) -> Self {
60        Self {
61            icon: None,
62            text: None,
63            badge: Some(text.into()),
64            badge_variant: BadgeVariant::Default,
65            on_click: None,
66            disabled: false,
67            tooltip: Some(tooltip.into()),
68        }
69    }
70
71    pub fn icon_badge(icon: impl Into<IconSource>, badge: impl Into<SharedString>) -> Self {
72        Self {
73            icon: Some(icon.into()),
74            text: None,
75            badge: Some(badge.into()),
76            badge_variant: BadgeVariant::Default,
77            on_click: None,
78            disabled: false,
79            tooltip: None,
80        }
81    }
82
83    pub fn badge_variant(mut self, variant: BadgeVariant) -> Self {
84        self.badge_variant = variant;
85        self
86    }
87
88    pub fn on_click<F>(mut self, handler: F) -> Self
89    where
90        F: Fn(&mut Window, &mut App) + 'static,
91    {
92        self.on_click = Some(Rc::new(handler));
93        self
94    }
95
96    pub fn disabled(mut self, disabled: bool) -> Self {
97        self.disabled = disabled;
98        self
99    }
100
101    pub fn tooltip(mut self, tooltip: impl Into<SharedString>) -> Self {
102        self.tooltip = Some(tooltip.into());
103        self
104    }
105}
106
107#[derive(Clone, Copy, Debug, PartialEq, Eq)]
108pub enum StatusBarSection {
109    Left,
110    Center,
111    Right,
112}
113
114pub struct StatusBar {
115    left_items: Vec<StatusItem>,
116    center_items: Vec<StatusItem>,
117    right_items: Vec<StatusItem>,
118    height: Pixels,
119    style: StyleRefinement,
120}
121
122impl StatusBar {
123    pub fn new() -> Self {
124        Self {
125            left_items: Vec::new(),
126            center_items: Vec::new(),
127            right_items: Vec::new(),
128            height: px(28.0),
129            style: StyleRefinement::default(),
130        }
131    }
132
133    pub fn left(mut self, items: Vec<StatusItem>) -> Self {
134        self.left_items = items;
135        self
136    }
137
138    pub fn center(mut self, items: Vec<StatusItem>) -> Self {
139        self.center_items = items;
140        self
141    }
142
143    pub fn right(mut self, items: Vec<StatusItem>) -> Self {
144        self.right_items = items;
145        self
146    }
147
148    pub fn height(mut self, height: Pixels) -> Self {
149        self.height = height;
150        self
151    }
152
153    pub fn add_left(mut self, item: StatusItem) -> Self {
154        self.left_items.push(item);
155        self
156    }
157
158    pub fn add_center(mut self, item: StatusItem) -> Self {
159        self.center_items.push(item);
160        self
161    }
162
163    pub fn add_right(mut self, item: StatusItem) -> Self {
164        self.right_items.push(item);
165        self
166    }
167}
168
169impl Styled for StatusBar {
170    fn style(&mut self) -> &mut StyleRefinement {
171        &mut self.style
172    }
173}
174
175impl Default for StatusBar {
176    fn default() -> Self {
177        Self::new()
178    }
179}
180
181impl Render for StatusBar {
182    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
183        let theme = use_theme(cx);
184        let user_style = self.style.clone();
185
186        div()
187            .flex()
188            .items_center()
189            .justify_between()
190            .h(self.height)
191            .px(px(12.0))
192            .py(px(6.0))
193            .gap(px(12.0))
194            .bg(theme.tokens.card)
195            .border_t_1()
196            .border_color(theme.tokens.border)
197            .map(|this| {
198                let mut div = this;
199                div.style().refine(&user_style);
200                div
201            })
202            .child(
203                div().flex().items_center().gap(px(12.0)).children(
204                    self.left_items
205                        .iter()
206                        .map(|item| render_status_item(item.clone(), theme.clone())),
207                ),
208            )
209            .child(
210                div().flex().items_center().gap(px(12.0)).children(
211                    self.center_items
212                        .iter()
213                        .map(|item| render_status_item(item.clone(), theme.clone())),
214                ),
215            )
216            .child(
217                div().flex().items_center().gap(px(12.0)).children(
218                    self.right_items
219                        .iter()
220                        .map(|item| render_status_item(item.clone(), theme.clone())),
221                ),
222            )
223    }
224}
225
226fn render_status_item(item: StatusItem, theme: Theme) -> impl IntoElement {
227    div()
228        .flex()
229        .items_center()
230        .gap(px(6.0))
231        .px(px(8.0))
232        .py(px(4.0))
233        .rounded(theme.tokens.radius_sm)
234        .when(!item.disabled && item.on_click.is_some(), |div| {
235            div.cursor(CursorStyle::PointingHand)
236                .hover(|style| style.bg(theme.tokens.muted))
237        })
238        .when(item.disabled, |div| div.opacity(0.5))
239        .when_some(item.on_click.filter(|_| !item.disabled), |div, handler| {
240            div.on_mouse_down(MouseButton::Left, move |_event, window, cx| {
241                handler(window, cx);
242            })
243        })
244        .when_some(item.icon, |div, icon| {
245            div.child(Icon::new(icon).size(px(14.0)).color(if item.disabled {
246                theme.tokens.muted_foreground
247            } else {
248                theme.tokens.foreground
249            }))
250        })
251        .when_some(item.text, |div, text| {
252            div.child(caption(text).color(if item.disabled {
253                theme.tokens.muted_foreground
254            } else {
255                theme.tokens.foreground
256            }))
257        })
258        .when_some(item.badge, |div, badge_text| {
259            div.child(Badge::new(badge_text).variant(item.badge_variant))
260        })
261}