fc-ui 0.9.7

A comprehensive, professional UI component library for GPUI inspired by shadcn/ui. 85+ polished components for building beautiful desktop applications.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//! Menu system for dropdown and context menus.

use crate::capabilities::foundation::theme::{use_theme, Theme};
use crate::capabilities::primitives::icon::Icon;
use crate::capabilities::primitives::icon_source::IconSource;
use crate::capabilities::primitives::text::{body, caption};
use gpui::{prelude::FluentBuilder as _, InteractiveElement, *};
use std::rc::Rc;

#[derive(Clone, Debug)]
pub enum MenuItemKind {
    Action,
    Checkbox { checked: bool },
    Radio { checked: bool },
    Submenu,
    Separator,
}

#[derive(Clone)]
pub struct MenuItem {
    pub id: SharedString,
    pub label: SharedString,
    pub icon: Option<IconSource>,
    pub shortcut: Option<SharedString>,
    pub kind: MenuItemKind,
    pub disabled: bool,
    pub on_click: Option<Rc<dyn Fn(&mut Window, &mut App)>>,
    pub children: Vec<MenuItem>,
}

impl MenuItem {
    pub fn new(id: impl Into<SharedString>, label: impl Into<SharedString>) -> Self {
        Self {
            id: id.into(),
            label: label.into(),
            icon: None,
            shortcut: None,
            kind: MenuItemKind::Action,
            disabled: false,
            on_click: None,
            children: Vec::new(),
        }
    }

    pub fn separator() -> Self {
        Self {
            id: SharedString::from("separator"),
            label: SharedString::from(""),
            icon: None,
            shortcut: None,
            kind: MenuItemKind::Separator,
            disabled: false,
            on_click: None,
            children: Vec::new(),
        }
    }

    pub fn checkbox(
        id: impl Into<SharedString>,
        label: impl Into<SharedString>,
        checked: bool,
    ) -> Self {
        Self {
            id: id.into(),
            label: label.into(),
            icon: None,
            shortcut: None,
            kind: MenuItemKind::Checkbox { checked },
            disabled: false,
            on_click: None,
            children: Vec::new(),
        }
    }

    pub fn submenu(id: impl Into<SharedString>, label: impl Into<SharedString>) -> Self {
        Self {
            id: id.into(),
            label: label.into(),
            icon: None,
            shortcut: None,
            kind: MenuItemKind::Submenu,
            disabled: false,
            on_click: None,
            children: Vec::new(),
        }
    }

    pub fn with_icon(mut self, icon: impl Into<IconSource>) -> Self {
        self.icon = Some(icon.into());
        self
    }

    pub fn with_shortcut(mut self, shortcut: impl Into<SharedString>) -> Self {
        self.shortcut = Some(shortcut.into());
        self
    }

    pub fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    pub fn on_click<F>(mut self, handler: F) -> Self
    where
        F: Fn(&mut Window, &mut App) + 'static,
    {
        self.on_click = Some(Rc::new(handler));
        self
    }

    pub fn with_children(mut self, children: Vec<MenuItem>) -> Self {
        self.children = children;
        self
    }
}

#[derive(IntoElement)]
pub struct Menu {
    items: Vec<MenuItem>,
    min_width: Pixels,
    max_height: Option<Pixels>,
    style: StyleRefinement,
}

impl Menu {
    pub fn new(items: Vec<MenuItem>) -> Self {
        Self {
            items,
            min_width: px(200.0),
            max_height: Some(px(400.0)),
            style: StyleRefinement::default(),
        }
    }

    pub fn min_width(mut self, width: Pixels) -> Self {
        self.min_width = width;
        self
    }

    pub fn max_height(mut self, height: Option<Pixels>) -> Self {
        self.max_height = height;
        self
    }
}

impl Styled for Menu {
    fn style(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

impl RenderOnce for Menu {
    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
        let theme = use_theme(cx);
        let user_style = self.style;

        div()
            .min_w(self.min_width)
            .when_some(self.max_height, |div, h| div.max_h(h))
            .flex()
            .flex_col()
            .bg(theme.tokens.popover)
            .border_1()
            .border_color(theme.tokens.border)
            .rounded(theme.tokens.radius_md)
            .shadow_lg()
            .p(px(4.0))
            .children(
                self.items
                    .into_iter()
                    .map(|item| render_menu_item(item, theme.clone())),
            )
            .map(|this| {
                let mut div = this;
                div.style().refine(&user_style);
                div
            })
    }
}

fn render_menu_item(item: MenuItem, theme: Theme) -> impl IntoElement {
    match item.kind {
        MenuItemKind::Separator => div()
            .h(px(1.0))
            .bg(theme.tokens.border)
            .my(px(4.0))
            .mx(px(8.0)),
        _ => {
            let is_checked = matches!(
                item.kind,
                MenuItemKind::Checkbox { checked: true } | MenuItemKind::Radio { checked: true }
            );
            let has_submenu = matches!(item.kind, MenuItemKind::Submenu);

            div()
                .flex()
                .items_center()
                .gap(px(12.0))
                .px(px(12.0))
                .py(px(8.0))
                .rounded(theme.tokens.radius_sm)
                .cursor(if item.disabled {
                    CursorStyle::Arrow
                } else {
                    CursorStyle::PointingHand
                })
                .when(item.disabled, |div| div.opacity(0.5))
                .when(!item.disabled, |div| {
                    div.hover(|style| style.bg(theme.tokens.accent))
                })
                .when_some(item.on_click.filter(|_| !item.disabled), |div, handler| {
                    div.on_mouse_down(MouseButton::Left, move |_event, window, cx| {
                        handler(window, cx);
                    })
                })
                .child(
                    div()
                        .w(px(16.0))
                        .h(px(16.0))
                        .flex()
                        .items_center()
                        .justify_center()
                        .when(is_checked, |div| {
                            div.child(
                                Icon::new("check")
                                    .size(px(12.0))
                                    .color(theme.tokens.foreground),
                            )
                        }),
                )
                .when_some(item.icon, |div, icon| {
                    div.child(Icon::new(icon).size(px(16.0)).color(if item.disabled {
                        theme.tokens.muted_foreground
                    } else {
                        theme.tokens.foreground
                    }))
                })
                .child(
                    div()
                        .flex_1()
                        .child(body(item.label).color(if item.disabled {
                            theme.tokens.muted_foreground
                        } else {
                            theme.tokens.foreground
                        })),
                )
                .when_some(item.shortcut, |div, shortcut| {
                    div.child(
                        caption(shortcut)
                            .color(theme.tokens.muted_foreground)
                            .no_wrap(),
                    )
                })
                .when(has_submenu, |div| {
                    div.child(
                        Icon::new("chevron-right")
                            .size(px(14.0))
                            .color(theme.tokens.muted_foreground),
                    )
                })
        }
    }
}

#[derive(Clone)]
pub struct MenuBarItem {
    pub id: SharedString,
    pub label: SharedString,
    pub menu_items: Vec<MenuItem>,
}

impl MenuBarItem {
    pub fn new(id: impl Into<SharedString>, label: impl Into<SharedString>) -> Self {
        Self {
            id: id.into(),
            label: label.into(),
            menu_items: Vec::new(),
        }
    }

    pub fn with_items(mut self, items: Vec<MenuItem>) -> Self {
        self.menu_items = items;
        self
    }
}

actions!(menu, [DismissMenu]);

pub struct MenuBar {
    items: Vec<MenuBarItem>,
    active_menu: Option<usize>,
    item_bounds: Vec<Option<Bounds<Pixels>>>,
    focus_handle: Option<FocusHandle>,
}

impl MenuBar {
    pub fn new(items: Vec<MenuBarItem>) -> Self {
        let item_bounds = items.iter().map(|_| None).collect();
        Self {
            items,
            active_menu: None,
            item_bounds,
            focus_handle: None,
        }
    }
}

impl Render for MenuBar {
    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
        let theme = use_theme(cx);
        let focus_handle = self
            .focus_handle
            .get_or_insert_with(|| cx.focus_handle())
            .clone();

        div()
            .key_context("MenuBar")
            .track_focus(&focus_handle)
            .on_mouse_down_out(cx.listener(|this, _, _, cx| {
                if this.active_menu.is_some() {
                    this.active_menu = None;
                    cx.notify();
                }
            }))
            .when(self.active_menu.is_some(), |this| {
                this.on_action(cx.listener(|this, _: &DismissMenu, _window, cx| {
                    this.active_menu = None;
                    cx.notify();
                }))
            })
            .flex()
            .items_center()
            .h(px(40.0))
            .px(px(8.0))
            .gap(px(2.0))
            .bg(theme.tokens.background)
            .border_b_1()
            .border_color(theme.tokens.border)
            .children(self.items.iter().enumerate().map(|(idx, item)| {
                let is_active = self.active_menu == Some(idx);
                let label = item.label.clone();
                let focus_handle = focus_handle.clone();
                let entity = cx.entity().clone();

                let dropdown = if is_active {
                    self.item_bounds.get(idx).copied().flatten().map(|bounds| {
                        deferred(
                            anchored()
                                .snap_to_window_with_margin(px(8.0))
                                .position(point(bounds.left(), bounds.bottom()))
                                .child(div().occlude().child(Menu::new(item.menu_items.clone()))),
                        )
                        .with_priority(1)
                        .into_any_element()
                    })
                } else {
                    None
                };

                div()
                    .relative()
                    .px(px(12.0))
                    .py(px(6.0))
                    .rounded(theme.tokens.radius_sm)
                    .cursor(CursorStyle::PointingHand)
                    .when(is_active, |div| div.bg(theme.tokens.accent))
                    .when(!is_active, |div| {
                        div.hover(|style| style.bg(theme.tokens.muted))
                    })
                    .on_mouse_down(
                        MouseButton::Left,
                        cx.listener(move |this, _event, window, cx| {
                            let opening = this.active_menu != Some(idx);
                            this.active_menu = if opening { Some(idx) } else { None };
                            if opening {
                                window.focus(&focus_handle);
                            }
                            cx.notify();
                        }),
                    )
                    .child(body(label).color(theme.tokens.foreground))
                    .child(
                        canvas(
                            move |bounds, _window, cx| {
                                entity.update(cx, |this, _| {
                                    if let Some(slot) = this.item_bounds.get_mut(idx) {
                                        *slot = Some(bounds);
                                    }
                                });
                            },
                            |_bounds, _, _window, _cx| {},
                        )
                        .absolute()
                        .inset_0(),
                    )
                    .children(dropdown)
            }))
    }
}

pub fn init_menu_bar(cx: &mut App) {
    if !crate::capabilities::foundation::initialization::begin(cx, "menu-bar") {
        return;
    }
    cx.bind_keys([KeyBinding::new("escape", DismissMenu, Some("MenuBar"))]);
}

#[derive(IntoElement)]
pub struct ContextMenu {
    items: Vec<MenuItem>,
    position: Point<Pixels>,
}

impl ContextMenu {
    pub fn new(items: Vec<MenuItem>, position: Point<Pixels>) -> Self {
        Self { items, position }
    }
}

impl RenderOnce for ContextMenu {
    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
        let theme = use_theme(cx);

        anchored()
            .snap_to_window_with_margin(px(8.0))
            .anchor(Corner::TopLeft)
            .position(self.position)
            .child(
                div()
                    .min_w(px(200.0))
                    .max_h(px(400.0))
                    .flex()
                    .flex_col()
                    .bg(theme.tokens.popover)
                    .border_1()
                    .border_color(theme.tokens.border)
                    .rounded(theme.tokens.radius_md)
                    .shadow_lg()
                    .p(px(4.0))
                    .children(
                        self.items
                            .into_iter()
                            .map(|item| render_menu_item(item, theme.clone())),
                    ),
            )
    }
}