BlueGit

ict rusty-teams

Files

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

message_context_menu.rs

message_context_menu.rs 5.4 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::*;
9
10use super::message_actions::{MenuActions, MessageMenu, picker_button, reaction_button};
11use super::toolbar_placement::TOOLBAR_HEIGHT;
12use crate::theme::ElevatedSurface as _;
13
14pub const CONTEXT_MENU_TAG: &str = "message-context-menu";
15const PANEL_GAP: f32 = 6.;
16const BAR_PADDING: f32 = 3.;
17const WINDOW_MARGIN: f32 = 8.;
18const BAR_LIFT: f32 = TOOLBAR_HEIGHT + PANEL_GAP;
19const LIST_OPEN: Duration = Duration::from_millis(160);
20const REACTION_OPEN: Duration = Duration::from_millis(140);
21const REACTION_STAGGER: Duration = Duration::from_millis(35);
22const LIST_SLIDE: f32 = 6.;
23const REACTION_RISE: f32 = 8.;
24
25pub type Close = Rc<dyn Fn(&mut App)>;
26
27/// One open right-click menu: the menu entity lives as long as the menu is open, so hover and submenus keep their state.
28#[derive(Clone)]
29pub struct OpenContextMenu {
30 pub key: String,
31 pub position: Point<Pixels>,
32 pub serial: u64,
33 popup: Rc<RefCell<Option<(Entity<PopupMenu>, Subscription)>>>,
34 bar_pressed: Rc<Cell<bool>>,
35}
36
37impl OpenContextMenu {
38 pub fn new(key: &str, position: Point<Pixels>, serial: u64) -> Self {
39 OpenContextMenu {
40 key: key.to_owned(),
41 position,
42 serial,
43 popup: Rc::default(),
44 bar_pressed: Rc::default(),
45 }
46 }
47
48 fn popup(&self, menu: &MessageMenu, close: Close, window: &mut Window, cx: &mut App) -> Entity<PopupMenu> {
49 if let Some((popup, _)) = self.popup.borrow().as_ref() {
50 return popup.clone();
51 }
52 let actions = MenuActions::of(menu);
53 let popup = PopupMenu::build(window, cx, move |popup, window, cx| {
54 actions.fill(popup, true, window, cx)
55 });
56 popup.focus_handle(cx).focus(window, cx);
57 let bar_pressed = self.bar_pressed.clone();
58 let subscription = cx.subscribe(&popup, move |_, _: &DismissEvent, cx| {
59 if !bar_pressed.replace(false) {
60 close(cx);
61 }
62 });
63 *self.popup.borrow_mut() = Some((popup.clone(), subscription));
64 popup
65 }
66}
67
68fn animated_reaction(button: AnyElement, index: usize, serial: u64) -> AnyElement {
69 let delay = REACTION_STAGGER * index as u32;
70 let total = delay + REACTION_OPEN;
71 let start = delay.as_secs_f32() / total.as_secs_f32();
72 div()
73 .relative()
74 .child(button)
75 .with_animation(
76 ElementId::Name(format!("context-reaction-{serial}-{index}").into()),
77 Animation::new(total).with_easing(ease_out_quint()),
78 move |element, delta| {
79 let local = ((delta - start) / (1. - start)).clamp(0., 1.);
80 element.opacity(local).top(px((1. - local) * REACTION_RISE))
81 },
82 )
83 .into_any_element()
84}
85
86/// Telegram's behaviour in our look: the menu opens at the cursor (flipped above it near the bottom edge), quick reactions sit on top of it.
87pub fn message_context_menu(
88 mut menu: MessageMenu,
89 open: &OpenContextMenu,
90 close: Close,
91 close_from_outside: Close,
92 window: &mut Window,
93 cx: &mut App,
94) -> AnyElement {
95 let popup = open.popup(&menu, close_from_outside, window, cx);
96 let picked = menu.done.clone();
97 let after_pick = close.clone();
98 menu.done = Rc::new(move |window, cx| {
99 picked(window, cx);
100 after_pick(cx);
101 });
102 let serial = open.serial;
103 let mut reactions: Vec<AnyElement> = menu
104 .quick
105 .iter()
106 .enumerate()
107 .map(|(index, glyph)| animated_reaction(reaction_button(&menu, glyph, index), index, serial))
108 .collect();
109 reactions.push(animated_reaction(picker_button(&menu, cx), menu.quick.len(), serial));
110 let bar_pressed = open.bar_pressed.clone();
111 let bar = h_flex()
112 .debug_selector(|| format!("{CONTEXT_MENU_TAG}/reactions").into())
113 .absolute()
114 .left_0()
115 .bottom_full()
116 .mb(px(PANEL_GAP))
117 .occlude()
118 .on_scroll_wheel(|_, _, cx| cx.stop_propagation())
119 .h(px(TOOLBAR_HEIGHT))
120 .px(px(BAR_PADDING))
121 .gap(px(1.))
122 .items_center()
123 .rounded(px(10.))
124 .flyout_surface()
125 .on_mouse_down(MouseButton::Left, move |_, _, cx| {
126 bar_pressed.set(true);
127 GlobalState::suppress_text_selection(cx);
128 })
129 .children(reactions);
130 let list = div()
131 .relative()
132 .debug_selector(|| format!("{CONTEXT_MENU_TAG}/list").into())
133 .child(popup)
134 .with_animation(
135 ElementId::Name(format!("context-list-{serial}").into()),
136 Animation::new(LIST_OPEN).with_easing(ease_out_quint()),
137 |element, delta| element.opacity(delta).top(px((delta - 1.) * LIST_SLIDE)),
138 );
139 let anchor_at = point(
140 open.position.x,
141 open.position.y.max(px(BAR_LIFT + WINDOW_MARGIN)),
142 );
143 deferred(
144 anchored().position(anchor_at).child(
145 div()
146 .id(ElementId::Name(format!("{CONTEXT_MENU_TAG}-{}", open.key).into()))
147 .debug_selector(|| CONTEXT_MENU_TAG.into())
148 .relative()
149 .occlude()
150 .on_scroll_wheel(|_, _, cx| cx.stop_propagation())
151 .child(list)
152 .child(bar),
153 ),
154 )
155 .with_priority(POPUP_PRIORITY)
156 .into_any_element()
157}