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