gpui_component/status_bar.rs
1use gpui::{
2 AnyElement, App, IntoElement, ParentElement, RenderOnce, StyleRefinement, Styled, Window,
3 prelude::FluentBuilder as _,
4};
5use smallvec::SmallVec;
6
7use crate::{ActiveTheme, StyledExt, h_flex};
8use gpui_base::LogicalStyledExt as _;
9
10/// A horizontal status bar, usually placed at the bottom of a window or pane.
11///
12/// It is split into three regions — `left`, `center`, and `right`. This mirrors
13/// the status bars found in native UI frameworks (Windows `StatusStrip`, WPF
14/// `StatusBar`, macOS `NSStatusBar`): a container that holds a row of items
15/// aligned to either end.
16///
17/// Each region accepts any [`IntoElement`], so a string, an [`Icon`](crate::Icon),
18/// a ghost `Button`, a vertical `Separator`, a custom layout, etc. can be passed
19/// directly. Use a plain string for a non-interactive label.
20///
21/// `left` and `right` pin items to each end. `child`/`children` add to the
22/// center region, whose alignment follows the pinned ends: centered with both
23/// `left` and `right`, end-aligned with only `left`, and start-aligned
24/// otherwise (only `right`, or neither — like a plain container).
25///
26/// ```
27/// # mod gpui_kit { pub extern crate gpui_component as component; }
28/// use gpui_kit::component::status_bar::StatusBar;
29///
30/// let _ = StatusBar::new().left("Ln 1, Col 1").right("UTF-8");
31/// ```
32#[derive(IntoElement)]
33pub struct StatusBar {
34 style: StyleRefinement,
35 left: SmallVec<[AnyElement; 1]>,
36 right: SmallVec<[AnyElement; 1]>,
37 children: SmallVec<[AnyElement; 1]>,
38}
39
40impl StatusBar {
41 /// Create a new, empty [`StatusBar`].
42 pub fn new() -> Self {
43 Self {
44 style: StyleRefinement::default(),
45 left: SmallVec::new(),
46 right: SmallVec::new(),
47 children: SmallVec::new(),
48 }
49 }
50
51 /// Append an element to the left region. Call multiple times to add more.
52 pub fn left(mut self, child: impl IntoElement) -> Self {
53 self.left.push(child.into_any_element());
54 self
55 }
56
57 /// Append an element to the right region. Call multiple times to add more.
58 pub fn right(mut self, child: impl IntoElement) -> Self {
59 self.right.push(child.into_any_element());
60 self
61 }
62}
63
64/// `child` / `children` add to the center region, so a `StatusBar` without
65/// `left`/`right` items behaves like a plain container.
66impl ParentElement for StatusBar {
67 fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
68 self.children.extend(elements);
69 }
70}
71
72impl Styled for StatusBar {
73 fn style(&mut self) -> &mut StyleRefinement {
74 &mut self.style
75 }
76}
77
78impl RenderOnce for StatusBar {
79 fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
80 // The center aligns by which ends are pinned: centered with both left
81 // and right, end-aligned with only left, otherwise start-aligned (only
82 // right, or neither) — so a bar with just `child`s reads like a container.
83 let has_left = !self.left.is_empty();
84 let has_right = !self.right.is_empty();
85 // `left` and `right` are the start and end regions: mirrored in RTL.
86 let region = || {
87 h_flex()
88 .flex_row_dir(cx)
89 .overflow_hidden()
90 .items_center()
91 .gap_2()
92 };
93
94 h_flex()
95 .flex_row_dir(cx)
96 .items_center()
97 .gap_2()
98 .py_1()
99 .px_2()
100 .border_t_1()
101 .border_color(cx.theme().status_bar_border)
102 .bg(cx.theme().tokens.status_bar)
103 .text_xs()
104 .text_color(cx.theme().muted_foreground)
105 .refine_style(&self.style)
106 .when(has_left, |this| this.child(region().children(self.left)))
107 .child(
108 region()
109 .flex_1()
110 .when(has_left && has_right, |this| this.justify_center())
111 .when(has_left && !has_right, |this| this.justify_flex_end())
112 .children(self.children),
113 )
114 .when(has_right, |this| this.child(region().children(self.right)))
115 }
116}