fc_ui/capabilities/overlays/
context_menu.rs1use gpui::{prelude::FluentBuilder as _, *};
4use std::rc::Rc;
5use std::time::Duration;
6
7use crate::capabilities::foundation::theme::use_theme;
8use crate::capabilities::motion::animations::easings;
9use crate::capabilities::primitives::icon::Icon;
10
11#[derive(Clone)]
12pub struct ContextMenuItem {
13 id: SharedString,
14 label: SharedString,
15 icon: Option<SharedString>,
16 disabled: bool,
17 divider: bool,
18 on_click: Option<Rc<dyn Fn(&mut Window, &mut App)>>,
19}
20
21impl ContextMenuItem {
22 pub fn new(id: impl Into<SharedString>, label: impl Into<SharedString>) -> Self {
23 Self {
24 id: id.into(),
25 label: label.into(),
26 icon: None,
27 disabled: false,
28 divider: false,
29 on_click: None,
30 }
31 }
32
33 pub fn icon(mut self, icon: impl Into<SharedString>) -> Self {
34 self.icon = Some(icon.into());
35 self
36 }
37
38 pub fn disabled(mut self, disabled: bool) -> Self {
39 self.disabled = disabled;
40 self
41 }
42
43 pub fn divider(mut self, divider: bool) -> Self {
44 self.divider = divider;
45 self
46 }
47
48 pub fn on_click<F>(mut self, handler: F) -> Self
49 where
50 F: Fn(&mut Window, &mut App) + 'static,
51 {
52 self.on_click = Some(Rc::new(handler));
53 self
54 }
55
56 pub fn separator() -> Self {
57 Self {
58 id: "separator".into(),
59 label: "".into(),
60 icon: None,
61 disabled: true,
62 divider: true,
63 on_click: None,
64 }
65 }
66}
67
68#[derive(IntoElement)]
69pub struct ContextMenu {
70 position: Point<Pixels>,
71 items: Vec<ContextMenuItem>,
72 on_close: Option<Rc<dyn Fn(&mut Window, &mut App)>>,
73 dismissing: bool,
74 style: StyleRefinement,
75}
76
77impl ContextMenu {
78 pub fn new(position: Point<Pixels>) -> Self {
79 Self {
80 position,
81 items: Vec::new(),
82 on_close: None,
83 dismissing: false,
84 style: StyleRefinement::default(),
85 }
86 }
87
88 pub fn dismissing(mut self, dismissing: bool) -> Self {
89 self.dismissing = dismissing;
90 self
91 }
92
93 pub fn item(mut self, item: ContextMenuItem) -> Self {
94 self.items.push(item);
95 self
96 }
97
98 pub fn items(mut self, items: Vec<ContextMenuItem>) -> Self {
99 self.items.extend(items);
100 self
101 }
102
103 pub fn on_close<F>(mut self, handler: F) -> Self
104 where
105 F: Fn(&mut Window, &mut App) + 'static,
106 {
107 self.on_close = Some(Rc::new(handler));
108 self
109 }
110}
111
112impl Styled for ContextMenu {
113 fn style(&mut self) -> &mut StyleRefinement {
114 &mut self.style
115 }
116}
117
118impl RenderOnce for ContextMenu {
119 fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
120 let theme = use_theme(cx);
121 let position = self.position;
122 let on_close_handler = self.on_close.clone();
123 let user_style = self.style;
124 let dismissing = self.dismissing;
125
126 div()
127 .absolute()
128 .inset_0()
129 .when(on_close_handler.is_some(), |this: Div| {
130 let handler = on_close_handler.clone().unwrap();
131 let handler2 = handler.clone();
132 this.on_mouse_down(MouseButton::Left, move |_, window, cx| {
133 handler(window, cx);
134 })
135 .on_mouse_down(MouseButton::Right, move |_, window, cx| {
136 handler2(window, cx);
137 })
138 })
139 .child(
140 div()
141 .absolute()
142 .occlude()
143 .left(position.x)
144 .top(position.y)
145 .min_w(px(200.0))
146 .bg(theme.tokens.popover)
147 .border_1()
148 .border_color(theme.tokens.border)
149 .rounded(theme.tokens.radius_md)
150 .shadow(smallvec::smallvec![BoxShadow {
151 color: hsla(0.0, 0.0, 0.0, 0.1),
152 offset: point(px(0.0), px(2.0)),
153 blur_radius: px(8.0),
154 spread_radius: px(0.0),
155 inset: false,
156 }])
157 .p(px(4.0))
158 .map(|this| {
159 let mut div = this;
160 div.style().refine(&user_style);
161 div
162 })
163 .on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
164 .children(self.items.into_iter().map(|item| {
165 if item.label.is_empty() && item.divider {
166 return div()
167 .h(px(1.0))
168 .my(px(4.0))
169 .bg(theme.tokens.border)
170 .into_any_element();
171 }
172
173 let on_close = self.on_close.clone();
174 let handler = item.on_click.clone();
175 let disabled = item.disabled;
176
177 div()
178 .id(item.id)
179 .flex()
180 .items_center()
181 .gap(px(8.0))
182 .px(px(8.0))
183 .py(px(6.0))
184 .rounded(theme.tokens.radius_sm)
185 .text_size(px(14.0))
186 .cursor(if disabled {
187 CursorStyle::Arrow
188 } else {
189 CursorStyle::PointingHand
190 })
191 .when(disabled, |this: Stateful<Div>| {
192 this.text_color(theme.tokens.muted_foreground).opacity(0.5)
193 })
194 .when(!disabled, |this: Stateful<Div>| {
195 this.text_color(theme.tokens.popover_foreground)
196 .hover(|style| style.bg(theme.tokens.accent.opacity(0.1)))
197 })
198 .when(!disabled && handler.is_some(), |this: Stateful<Div>| {
199 let handler = handler.unwrap();
200 let on_close = on_close.clone();
201 this.on_mouse_down(MouseButton::Left, move |_, window, cx| {
202 handler(window, cx);
203 if let Some(close_handler) = &on_close {
204 close_handler(window, cx);
205 }
206 })
207 })
208 .when_some(item.icon, |this: Stateful<Div>, icon| {
209 this.child(Icon::new(icon).size(px(14.0)))
210 })
211 .child(item.label)
212 .into_any_element()
213 }))
214 .with_animation(
215 if self.dismissing {
216 "ctx-menu-exit"
217 } else {
218 "ctx-menu-enter"
219 },
220 Animation::new(Duration::from_millis(if self.dismissing {
221 100
222 } else {
223 120
224 }))
225 .with_easing(if self.dismissing {
226 easings::ease_in_cubic as fn(f32) -> f32
227 } else {
228 easings::ease_out_cubic as fn(f32) -> f32
229 }),
230 move |el, delta| {
231 if dismissing {
232 el.opacity(1.0 - delta).mt(px(4.0 * delta))
233 } else {
234 el.opacity(delta).mt(px(4.0 * (1.0 - delta)))
235 }
236 },
237 ),
238 )
239 }
240}