Skip to main content

fc_ui/capabilities/overlays/
popover_menu.rs

1//! Popover menu component with positioned menu items.
2
3use crate::capabilities::foundation::theme::use_theme;
4use crate::capabilities::primitives::icon::Icon;
5use gpui::prelude::FluentBuilder;
6use gpui::*;
7use std::rc::Rc;
8
9pub struct PopoverMenuItem {
10    pub id: SharedString,
11    pub label: SharedString,
12    pub icon: Option<SharedString>,
13    pub on_click: Option<Rc<dyn Fn(&mut Window, &mut App) + 'static>>,
14    pub disabled: bool,
15}
16
17impl PopoverMenuItem {
18    pub fn new(id: impl Into<SharedString>, label: impl Into<SharedString>) -> Self {
19        Self {
20            id: id.into(),
21            label: label.into(),
22            icon: None,
23            on_click: None,
24            disabled: false,
25        }
26    }
27
28    pub fn icon(mut self, icon: impl Into<SharedString>) -> Self {
29        self.icon = Some(icon.into());
30        self
31    }
32
33    pub fn on_click<F>(mut self, handler: F) -> Self
34    where
35        F: Fn(&mut Window, &mut App) + 'static,
36    {
37        self.on_click = Some(Rc::new(handler));
38        self
39    }
40
41    pub fn disabled(mut self, disabled: bool) -> Self {
42        self.disabled = disabled;
43        self
44    }
45}
46
47#[derive(IntoElement)]
48pub struct PopoverMenu {
49    position: Point<Pixels>,
50    items: Vec<PopoverMenuItem>,
51    on_close: Option<Rc<dyn Fn(&mut Window, &mut App) + 'static>>,
52    style: StyleRefinement,
53}
54
55impl PopoverMenu {
56    pub fn new(position: Point<Pixels>, items: Vec<PopoverMenuItem>) -> Self {
57        Self {
58            position,
59            items,
60            on_close: None,
61            style: StyleRefinement::default(),
62        }
63    }
64
65    pub fn on_close<F>(mut self, handler: F) -> Self
66    where
67        F: Fn(&mut Window, &mut App) + 'static,
68    {
69        self.on_close = Some(Rc::new(handler));
70        self
71    }
72}
73
74impl Styled for PopoverMenu {
75    fn style(&mut self) -> &mut StyleRefinement {
76        &mut self.style
77    }
78}
79
80impl RenderOnce for PopoverMenu {
81    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
82        let theme = use_theme(cx);
83        let on_close_backdrop = self.on_close.clone();
84        let user_style = self.style;
85
86        div()
87            .absolute()
88            .top_0()
89            .left_0()
90            .size_full()
91            .on_mouse_down(MouseButton::Left, move |_, window, cx| {
92                if let Some(ref handler) = on_close_backdrop {
93                    handler(window, cx);
94                }
95            })
96            .child(
97                deferred(
98                    anchored().snap_to_window().position(self.position).child(
99                        div().occlude().child(
100                            div()
101                                .min_w(px(200.0))
102                                .max_w(px(300.0))
103                                .flex()
104                                .flex_col()
105                                .bg(theme.tokens.popover)
106                                .text_color(theme.tokens.popover_foreground)
107                                .border_1()
108                                .border_color(theme.tokens.border)
109                                .rounded(theme.tokens.radius_md)
110                                .shadow_lg()
111                                .p(px(4.0))
112                                .map(|this| {
113                                    let mut div = this;
114                                    div.style().refine(&user_style);
115                                    div
116                                })
117                                .on_mouse_down(MouseButton::Left, |_, _, cx| {
118                                    cx.stop_propagation();
119                                })
120                                .children(self.items.into_iter().map(|item| {
121                                    let on_click = item.on_click;
122                                    let disabled = item.disabled;
123
124                                    div()
125                                        .flex()
126                                        .items_center()
127                                        .gap(px(8.0))
128                                        .px(px(12.0))
129                                        .py(px(8.0))
130                                        .rounded(px(4.0))
131                                        .cursor(if disabled {
132                                            CursorStyle::Arrow
133                                        } else {
134                                            CursorStyle::PointingHand
135                                        })
136                                        .when(!disabled, |this| {
137                                            this.hover(|style| {
138                                                style.bg(theme.tokens.accent.opacity(0.1))
139                                            })
140                                        })
141                                        .when(disabled, |this| this.opacity(0.5))
142                                        .when_some(item.icon, |this, icon_name| {
143                                            this.child(
144                                                Icon::new(icon_name)
145                                                    .size(px(16.0))
146                                                    .color(theme.tokens.foreground),
147                                            )
148                                        })
149                                        .child(div().text_size(px(14.0)).child(item.label))
150                                        .when(!disabled && on_click.is_some(), |this| {
151                                            this.on_mouse_down(
152                                                MouseButton::Left,
153                                                move |_, window, cx| {
154                                                    if let Some(ref handler) = on_click {
155                                                        handler(window, cx);
156                                                    }
157                                                    cx.stop_propagation();
158                                                },
159                                            )
160                                        })
161                                })),
162                        ),
163                    ),
164                )
165                .with_priority(1),
166            )
167    }
168}