Skip to main content

fc_ui/capabilities/navigation/
toolbar.rs

1//! Toolbar component with icon buttons and grouping.
2
3use crate::capabilities::foundation::theme::{use_theme, Theme};
4use crate::capabilities::primitives::icon::Icon;
5use crate::capabilities::primitives::icon_source::IconSource;
6use gpui::{prelude::FluentBuilder as _, *};
7use std::rc::Rc;
8
9#[derive(Copy, Clone, Debug, PartialEq, Eq)]
10pub enum ToolbarButtonVariant {
11    Default,
12    Toggle,
13    Dropdown,
14}
15
16#[derive(Copy, Clone, Debug, PartialEq, Eq)]
17pub enum ToolbarSize {
18    Sm,
19    Md,
20    Lg,
21}
22
23impl ToolbarSize {
24    fn button_size(&self) -> Pixels {
25        match self {
26            Self::Sm => px(32.0),
27            Self::Md => px(36.0),
28            Self::Lg => px(40.0),
29        }
30    }
31
32    fn icon_size(&self) -> Pixels {
33        match self {
34            Self::Sm => px(14.0),
35            Self::Md => px(16.0),
36            Self::Lg => px(18.0),
37        }
38    }
39}
40
41#[derive(Clone)]
42pub struct ToolbarButton {
43    pub id: SharedString,
44    pub icon: IconSource,
45    pub tooltip: Option<SharedString>,
46    pub variant: ToolbarButtonVariant,
47    pub pressed: bool,
48    pub disabled: bool,
49    pub on_click: Option<Rc<dyn Fn(&mut Window, &mut App)>>,
50}
51
52impl ToolbarButton {
53    pub fn new(id: impl Into<SharedString>, icon: impl Into<IconSource>) -> Self {
54        Self {
55            id: id.into(),
56            icon: icon.into(),
57            tooltip: None,
58            variant: ToolbarButtonVariant::Default,
59            pressed: false,
60            disabled: false,
61            on_click: None,
62        }
63    }
64
65    pub fn tooltip(mut self, tooltip: impl Into<SharedString>) -> Self {
66        self.tooltip = Some(tooltip.into());
67        self
68    }
69
70    pub fn variant(mut self, variant: ToolbarButtonVariant) -> Self {
71        self.variant = variant;
72        self
73    }
74
75    pub fn pressed(mut self, pressed: bool) -> Self {
76        self.pressed = pressed;
77        self
78    }
79
80    pub fn disabled(mut self, disabled: bool) -> Self {
81        self.disabled = disabled;
82        self
83    }
84
85    pub fn on_click<F>(mut self, handler: F) -> Self
86    where
87        F: Fn(&mut Window, &mut App) + 'static,
88    {
89        self.on_click = Some(Rc::new(handler));
90        self
91    }
92}
93
94#[derive(Clone)]
95pub enum ToolbarItem {
96    Button(ToolbarButton),
97    Separator,
98    Spacer,
99}
100
101#[derive(Clone)]
102pub struct ToolbarGroup {
103    pub items: Vec<ToolbarItem>,
104}
105
106impl ToolbarGroup {
107    pub fn new() -> Self {
108        Self { items: Vec::new() }
109    }
110
111    pub fn button(mut self, button: ToolbarButton) -> Self {
112        self.items.push(ToolbarItem::Button(button));
113        self
114    }
115
116    pub fn separator(mut self) -> Self {
117        self.items.push(ToolbarItem::Separator);
118        self
119    }
120
121    pub fn spacer(mut self) -> Self {
122        self.items.push(ToolbarItem::Spacer);
123        self
124    }
125
126    pub fn buttons(mut self, buttons: Vec<ToolbarButton>) -> Self {
127        for button in buttons {
128            self.items.push(ToolbarItem::Button(button));
129        }
130        self
131    }
132}
133
134impl Default for ToolbarGroup {
135    fn default() -> Self {
136        Self::new()
137    }
138}
139
140pub struct Toolbar {
141    groups: Vec<ToolbarGroup>,
142    size: ToolbarSize,
143    style: StyleRefinement,
144}
145
146impl Toolbar {
147    pub fn new() -> Self {
148        Self {
149            groups: Vec::new(),
150            size: ToolbarSize::Md,
151            style: StyleRefinement::default(),
152        }
153    }
154
155    pub fn size(mut self, size: ToolbarSize) -> Self {
156        self.size = size;
157        self
158    }
159
160    pub fn group(mut self, group: ToolbarGroup) -> Self {
161        self.groups.push(group);
162        self
163    }
164
165    pub fn groups(mut self, groups: Vec<ToolbarGroup>) -> Self {
166        self.groups.extend(groups);
167        self
168    }
169}
170
171impl Default for Toolbar {
172    fn default() -> Self {
173        Self::new()
174    }
175}
176
177impl Styled for Toolbar {
178    fn style(&mut self) -> &mut StyleRefinement {
179        &mut self.style
180    }
181}
182
183impl Render for Toolbar {
184    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
185        let theme = use_theme(cx);
186        let button_size = self.size.button_size();
187        let icon_size = self.size.icon_size();
188        let user_style = self.style.clone();
189
190        div()
191            .flex()
192            .items_center()
193            .gap(px(8.0))
194            .px(px(8.0))
195            .py(px(6.0))
196            .bg(theme.tokens.background)
197            .border_b_1()
198            .border_color(theme.tokens.border)
199            .children(self.groups.iter().enumerate().map(|(group_idx, group)| {
200                let is_last_group = group_idx == self.groups.len() - 1;
201
202                div()
203                    .flex()
204                    .items_center()
205                    .gap(px(4.0))
206                    .children(group.items.iter().map(|item| {
207                        match item {
208                            ToolbarItem::Button(button) => render_toolbar_button(
209                                button.clone(),
210                                button_size,
211                                icon_size,
212                                theme.clone(),
213                            )
214                            .into_any_element(),
215                            ToolbarItem::Separator => div()
216                                .w(px(1.0))
217                                .h(button_size * 0.6)
218                                .bg(theme.tokens.border)
219                                .mx(px(4.0))
220                                .into_any_element(),
221                            ToolbarItem::Spacer => div().flex_1().into_any_element(),
222                        }
223                    }))
224                    .when(!is_last_group, |this| {
225                        this.child(
226                            div()
227                                .w(px(1.0))
228                                .h(button_size * 0.6)
229                                .bg(theme.tokens.border)
230                                .mx(px(4.0)),
231                        )
232                    })
233            }))
234            .map(|this| {
235                let mut div = this;
236                div.style().refine(&user_style);
237                div
238            })
239    }
240}
241
242fn render_toolbar_button(
243    button: ToolbarButton,
244    button_size: Pixels,
245    icon_size: Pixels,
246    theme: Theme,
247) -> impl IntoElement {
248    div()
249        .size(button_size)
250        .flex()
251        .items_center()
252        .justify_center()
253        .rounded(theme.tokens.radius_sm)
254        .cursor(if button.disabled {
255            CursorStyle::Arrow
256        } else {
257            CursorStyle::PointingHand
258        })
259        .when(button.disabled, |div| div.opacity(0.5))
260        .when(button.pressed && !button.disabled, |div| {
261            div.bg(theme.tokens.accent)
262        })
263        .when(!button.disabled, |div| {
264            div.hover(|style| {
265                if button.pressed {
266                    style.bg(theme.tokens.accent.opacity(0.8))
267                } else {
268                    style.bg(theme.tokens.muted)
269                }
270            })
271        })
272        .when_some(
273            button.on_click.filter(|_| !button.disabled),
274            |div, handler| {
275                div.on_mouse_down(MouseButton::Left, move |_event, window, cx| {
276                    handler(window, cx);
277                })
278            },
279        )
280        .child(
281            Icon::new(button.icon)
282                .size(icon_size)
283                .color(if button.disabled {
284                    theme.tokens.muted_foreground
285                } else {
286                    theme.tokens.foreground
287                }),
288        )
289        .children((button.variant == ToolbarButtonVariant::Dropdown).then(|| {
290            div()
291                .absolute()
292                .bottom(px(2.0))
293                .right(px(2.0))
294                .child(
295                    Icon::new("chevron-down")
296                        .size(px(10.0))
297                        .color(theme.tokens.foreground),
298                )
299                .into_any_element()
300        }))
301}