BlueGit

ict rusty-teams

Files

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

message_context_menu.rs

message_context_menu.rs 6.5 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::menu::PopupMenu;
7use gpui_kit::component::h_flex;
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(&self, menu: &MessageMenu, close: Close, window: &mut Window, cx: &mut App) -> Entity<PopupMenu> {
52 if let Some((popup, _)) = self.popup.borrow().as_ref() {
53 return popup.clone();
54 }
55 let actions = MenuActions::of(menu);
56 let popup = PopupMenu::build(window, cx, move |popup, window, cx| {
57 actions.fill(popup, true, window, cx)
58 });
59 popup.focus_handle(cx).focus(window, cx);
60 let bar_pressed = self.bar_pressed.clone();
61 let subscription = cx.subscribe(&popup, move |_, _: &DismissEvent, cx| {
62 if !bar_pressed.replace(false) {
63 close(cx);
64 }
65 });
66 *self.popup.borrow_mut() = Some((popup.clone(), subscription));
67 popup
68 }
69}
70
71fn measure_height(height: Rc<Cell<Option<f32>>>) -> impl IntoElement {
72 canvas(
73 move |bounds, _, _| height.set(Some(f32::from(bounds.size.height))),
74 |_, _, _, _| {},
75 )
76 .absolute()
77 .size_full()
78}
79
80fn animated_reaction(button: AnyElement, index: usize, serial: u64) -> AnyElement {
81 let delay = REACTION_STAGGER * index as u32;
82 let total = delay + REACTION_OPEN;
83 let start = delay.as_secs_f32() / total.as_secs_f32();
84 div()
85 .relative()
86 .child(button)
87 .with_animation(
88 ElementId::Name(format!("context-reaction-{serial}-{index}").into()),
89 Animation::new(total).with_easing(ease_out_quint()),
90 move |element, delta| {
91 let local = ((delta - start) / (1. - start)).clamp(0., 1.);
92 element.opacity(local).top(px((1. - local) * REACTION_RISE))
93 },
94 )
95 .into_any_element()
96}
97
98/// Telegram's behaviour in our look: reactions above the cursor, the menu below; near the bottom edge it turns upside down.
99pub fn message_context_menu(
100 mut menu: MessageMenu,
101 open: &OpenContextMenu,
102 close: Close,
103 close_from_outside: Close,
104 window: &mut Window,
105 cx: &mut App,
106) -> AnyElement {
107 let popup = open.popup(&menu, close_from_outside, window, cx);
108 let picked = menu.done.clone();
109 let after_pick = close.clone();
110 menu.done = Rc::new(move |window, cx| {
111 picked(window, cx);
112 after_pick(cx);
113 });
114 let serial = open.serial;
115 let mut reactions: Vec<AnyElement> = menu
116 .quick
117 .iter()
118 .enumerate()
119 .map(|(index, glyph)| animated_reaction(reaction_button(&menu, glyph, index), index, serial))
120 .collect();
121 reactions.push(animated_reaction(picker_button(&menu, cx), menu.quick.len(), serial));
122 let viewport_height = f32::from(window.viewport_size().height);
123 let cursor_y = f32::from(open.position.y);
124 let upside_down = open
125 .list_height
126 .get()
127 .is_some_and(|height| cursor_y + height + WINDOW_MARGIN > viewport_height);
128 let bar_pressed = open.bar_pressed.clone();
129 let bar = h_flex()
130 .debug_selector(|| format!("{CONTEXT_MENU_TAG}/reactions").into())
131 .absolute()
132 .left_0()
133 .map(|bar| {
134 if upside_down {
135 bar.top_full().mt(px(PANEL_GAP))
136 } else {
137 bar.bottom_full().mb(px(PANEL_GAP))
138 }
139 })
140 .occlude()
141 .on_scroll_wheel(|_, _, cx| cx.stop_propagation())
142 .h(px(TOOLBAR_HEIGHT))
143 .px(px(BAR_PADDING))
144 .gap(px(1.))
145 .items_center()
146 .rounded(px(10.))
147 .flyout_surface()
148 .on_mouse_down(MouseButton::Left, move |_, _, cx| {
149 bar_pressed.set(true);
150 GlobalState::suppress_text_selection(cx);
151 })
152 .children(reactions);
153 let slide = if upside_down { LIST_SLIDE } else { -LIST_SLIDE };
154 let list = div()
155 .relative()
156 .debug_selector(|| format!("{CONTEXT_MENU_TAG}/list").into())
157 .child(popup)
158 .child(measure_height(open.list_height.clone()))
159 .with_animation(
160 ElementId::Name(format!("context-list-{serial}-{upside_down}").into()),
161 Animation::new(LIST_OPEN).with_easing(ease_out_quint()),
162 move |element, delta| element.opacity(delta).top(px((1. - delta) * slide)),
163 );
164 let (anchor_at, corner) = if upside_down {
165 let lowest = viewport_height - WINDOW_MARGIN - BAR_LIFT;
166 (point(open.position.x, px(cursor_y.min(lowest))), Anchor::BottomLeft)
167 } else {
168 (
169 point(open.position.x, open.position.y.max(px(BAR_LIFT + WINDOW_MARGIN))),
170 Anchor::TopLeft,
171 )
172 };
173 deferred(
174 anchored().position(anchor_at).anchor(corner).child(
175 div()
176 .id(ElementId::Name(format!("{CONTEXT_MENU_TAG}-{}", open.key).into()))
177 .debug_selector(|| CONTEXT_MENU_TAG.into())
178 .relative()
179 .occlude()
180 .on_scroll_wheel(|_, _, cx| cx.stop_propagation())
181 .child(list)
182 .child(bar),
183 ),
184 )
185 .with_priority(POPUP_PRIORITY)
186 .into_any_element()
187}