BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / message_context_menu.rs

message_context_menu.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
message_context_menu.rs 6.7 KiB Download Raw
1use std::cell::{Cell, RefCell};
2use std::rc::Rc;
3use std::time::Duration;
4
5use gpui_kit::base::{GlobalState, POPUP_PRIORITY};
6use gpui_kit::component::h_flex;
7use gpui_kit::component::menu::PopupMenu;
8use gpui_kit::prelude::FluentBuilder as _;
9use gpui_kit::*;
10
11use super::message_actions::{MenuActions, MessageMenu, picker_button, reaction_button};
12use super::toolbar_placement::TOOLBAR_HEIGHT;
13use crate::theme::ElevatedSurface as _;
14
15pub const CONTEXT_MENU_TAG: &str = "message-context-menu";
16const PANEL_GAP: f32 = 6.;
17const BAR_PADDING: f32 = 3.;
18const WINDOW_MARGIN: f32 = 8.;
19const BAR_LIFT: f32 = TOOLBAR_HEIGHT + PANEL_GAP;
20const LIST_OPEN: Duration = Duration::from_millis(160);
21const REACTION_OPEN: Duration = Duration::from_millis(140);
22const REACTION_STAGGER: Duration = Duration::from_millis(35);
23const LIST_SLIDE: f32 = 6.;
24const REACTION_RISE: f32 = 8.;
25
26pub type Close = Rc<dyn Fn(&mut App)>;
27
28/// One open right-click menu: the menu entity lives as long as the menu is open, so hover and submenus keep their state.
29#[derive(Clone)]
30pub struct OpenContextMenu {
31 pub key: String,
32 pub position: Point<Pixels>,
33 pub serial: u64,
34 popup: Rc<RefCell<Option<(Entity<PopupMenu>, Subscription)>>>,
35 bar_pressed: Rc<Cell<bool>>,
36 list_height: Rc<Cell<Option<f32>>>,
37}
38
39impl OpenContextMenu {
40 pub fn new(key: &str, position: Point<Pixels>, serial: u64) -> Self {
41 OpenContextMenu {
42 key: key.to_owned(),
43 position,
44 serial,
45 popup: Rc::default(),
46 bar_pressed: Rc::default(),
47 list_height: Rc::default(),
48 }
49 }
50
51 fn popup(
52 &self,
53 menu: &MessageMenu,
54 close: Close,
55 window: &mut Window,
56 cx: &mut App,
57 ) -> Entity<PopupMenu> {
58 if let Some((popup, _)) = self.popup.borrow().as_ref() {
59 return popup.clone();
60 }
61 let actions = MenuActions::of(menu);
62 let popup = PopupMenu::build(window, cx, move |popup, window, cx| {
63 actions.fill(popup, true, window, cx)
64 });
65 popup.focus_handle(cx).focus(window, cx);
66 let bar_pressed = self.bar_pressed.clone();
67 let subscription = cx.subscribe(&popup, move |_, _: &DismissEvent, cx| {
68 if !bar_pressed.replace(false) {
69 close(cx);
70 }
71 });
72 *self.popup.borrow_mut() = Some((popup.clone(), subscription));
73 popup
74 }
75}
76
77fn measure_height(height: Rc<Cell<Option<f32>>>) -> impl IntoElement {
78 canvas(
79 move |bounds, _, _| height.set(Some(f32::from(bounds.size.height))),
80 |_, _, _, _| {},
81 )
82 .absolute()
83 .size_full()
84}
85
86fn animated_reaction(button: AnyElement, index: usize, serial: u64) -> AnyElement {
87 let delay = REACTION_STAGGER * index as u32;
88 let total = delay + REACTION_OPEN;
89 let start = delay.as_secs_f32() / total.as_secs_f32();
90 div()
91 .relative()
92 .child(button)
93 .with_animation(
94 ElementId::Name(format!("context-reaction-{serial}-{index}").into()),
95 Animation::new(total).with_easing(ease_out_quint()),
96 move |element, delta| {
97 let local = ((delta - start) / (1. - start)).clamp(0., 1.);
98 element.opacity(local).top(px((1. - local) * REACTION_RISE))
99 },
100 )
101 .into_any_element()
102}
103
104/// Telegram's behaviour in our look: reactions above the cursor, the menu below; near the bottom edge it turns upside down.
105pub fn message_context_menu(
106 mut menu: MessageMenu,
107 open: &OpenContextMenu,
108 close: Close,
109 close_from_outside: Close,
110 window: &mut Window,
111 cx: &mut App,
112) -> AnyElement {
113 let popup = open.popup(&menu, close_from_outside, window, cx);
114 let picked = menu.done.clone();
115 let after_pick = close.clone();
116 menu.done = Rc::new(move |window, cx| {
117 picked(window, cx);
118 after_pick(cx);
119 });
120 let serial = open.serial;
121 let mut reactions: Vec<AnyElement> = menu
122 .quick
123 .iter()
124 .enumerate()
125 .map(|(index, glyph)| {
126 animated_reaction(reaction_button(&menu, glyph, index), index, serial)
127 })
128 .collect();
129 reactions.push(animated_reaction(
130 picker_button(&menu, cx),
131 menu.quick.len(),
132 serial,
133 ));
134 let viewport_height = f32::from(window.viewport_size().height);
135 let cursor_y = f32::from(open.position.y);
136 let upside_down = open
137 .list_height
138 .get()
139 .is_some_and(|height| cursor_y + height + WINDOW_MARGIN > viewport_height);
140 let bar_pressed = open.bar_pressed.clone();
141 let bar = h_flex()
142 .debug_selector(|| format!("{CONTEXT_MENU_TAG}/reactions").into())
143 .absolute()
144 .left_0()
145 .map(|bar| {
146 if upside_down {
147 bar.top_full().mt(px(PANEL_GAP))
148 } else {
149 bar.bottom_full().mb(px(PANEL_GAP))
150 }
151 })
152 .occlude()
153 .on_scroll_wheel(|_, _, cx| cx.stop_propagation())
154 .h(px(TOOLBAR_HEIGHT))
155 .px(px(BAR_PADDING))
156 .gap(px(1.))
157 .items_center()
158 .rounded(px(10.))
159 .flyout_surface()
160 .on_mouse_down(MouseButton::Left, move |_, _, cx| {
161 bar_pressed.set(true);
162 GlobalState::suppress_text_selection(cx);
163 })
164 .children(reactions);
165 let slide = if upside_down { LIST_SLIDE } else { -LIST_SLIDE };
166 let list = div()
167 .relative()
168 .debug_selector(|| format!("{CONTEXT_MENU_TAG}/list").into())
169 .child(popup)
170 .child(measure_height(open.list_height.clone()))
171 .with_animation(
172 ElementId::Name(format!("context-list-{serial}-{upside_down}").into()),
173 Animation::new(LIST_OPEN).with_easing(ease_out_quint()),
174 move |element, delta| element.opacity(delta).top(px((1. - delta) * slide)),
175 );
176 let (anchor_at, corner) = if upside_down {
177 let lowest = viewport_height - WINDOW_MARGIN - BAR_LIFT;
178 (
179 point(open.position.x, px(cursor_y.min(lowest))),
180 Anchor::BottomLeft,
181 )
182 } else {
183 (
184 point(
185 open.position.x,
186 open.position.y.max(px(BAR_LIFT + WINDOW_MARGIN)),
187 ),
188 Anchor::TopLeft,
189 )
190 };
191 deferred(
192 anchored().position(anchor_at).anchor(corner).child(
193 div()
194 .id(ElementId::Name(
195 format!("{CONTEXT_MENU_TAG}-{}", open.key).into(),
196 ))
197 .debug_selector(|| CONTEXT_MENU_TAG.into())
198 .relative()
199 .occlude()
200 .on_scroll_wheel(|_, _, cx| cx.stop_propagation())
201 .child(list)
202 .child(bar),
203 ),
204 )
205 .with_priority(POPUP_PRIORITY)
206 .into_any_element()
207}