| 1 | use std::rc::Rc; |
| 2 | |
| 3 | use gpui_kit::assets::IconName; |
| 4 | use gpui_kit::base::POPUP_PRIORITY; |
| 5 | use gpui_kit::component::{h_flex, v_flex}; |
| 6 | use gpui_kit::*; |
| 7 | |
| 8 | use super::message_actions::{Action, MessageMenu, picker_button, reaction_button}; |
| 9 | use super::widgets::icon; |
| 10 | use crate::forward_selection::FORWARD_LABEL; |
| 11 | use crate::post_follow::follow_label; |
| 12 | use crate::share_outlook::SHARE_TO_OUTLOOK_LABEL; |
| 13 | use crate::theme::{self, ElevatedSurface as _}; |
| 14 | |
| 15 | pub const CONTEXT_MENU_TAG: &str = "message-context-menu"; |
| 16 | const MENU_WIDTH: f32 = 220.; |
| 17 | const PANEL_GAP: f32 = 6.; |
| 18 | const PILL_PADDING: f32 = 4.; |
| 19 | const LIST_PADDING: f32 = 4.; |
| 20 | const ITEM_HEIGHT: f32 = 32.; |
| 21 | const ITEM_ICON: f32 = 16.; |
| 22 | const WINDOW_MARGIN: f32 = 8.; |
| 23 | const SELECT_LABEL: &str = "Select"; |
| 24 | |
| 25 | pub type Close = Rc<dyn Fn(&mut Window, &mut App)>; |
| 26 | |
| 27 | struct Entry { |
| 28 | label: SharedString, |
| 29 | glyph: IconName, |
| 30 | action: Action, |
| 31 | danger: bool, |
| 32 | } |
| 33 | |
| 34 | fn entry(label: impl Into<SharedString>, glyph: IconName, action: Action) -> Entry { |
| 35 | Entry { |
| 36 | label: label.into(), |
| 37 | glyph, |
| 38 | action, |
| 39 | danger: false, |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | fn entries(menu: &MessageMenu) -> Vec<Entry> { |
| 44 | let mut list = Vec::new(); |
| 45 | if let Some(reply) = menu.reply.clone() { |
| 46 | list.push(entry("Reply", IconName::Reply, reply)); |
| 47 | } |
| 48 | if let Some(edit) = menu.edit.clone() { |
| 49 | list.push(entry("Edit", IconName::Pencil, edit)); |
| 50 | } |
| 51 | if let Some(toggle_pinned) = menu.toggle_pinned.clone() { |
| 52 | list.push(if menu.pinned { |
| 53 | entry("Unpin", IconName::PinOff, toggle_pinned) |
| 54 | } else { |
| 55 | entry("Pin", IconName::Pin, toggle_pinned) |
| 56 | }); |
| 57 | } |
| 58 | list.push(entry("Copy text", IconName::Copy, menu.copy.clone())); |
| 59 | if let Some(copy_link) = menu.copy_link.clone() { |
| 60 | list.push(entry("Copy link", IconName::Link, copy_link)); |
| 61 | } |
| 62 | if let Some(forward) = menu.forward.clone() { |
| 63 | list.push(entry(FORWARD_LABEL, IconName::Forward, forward)); |
| 64 | } |
| 65 | if let Some(save) = menu.save.clone() { |
| 66 | list.push(if menu.saved { |
| 67 | entry("Unsave", IconName::BookmarkOff, save) |
| 68 | } else { |
| 69 | entry("Save", IconName::Bookmark, save) |
| 70 | }); |
| 71 | } |
| 72 | if let Some(toggle_follow) = menu.toggle_follow.clone() { |
| 73 | let glyph = if menu.followed { IconName::BellOff } else { IconName::Bell }; |
| 74 | list.push(entry(follow_label(menu.followed), glyph, toggle_follow)); |
| 75 | } |
| 76 | if let Some(mark_unread) = menu.mark_unread.clone() { |
| 77 | list.push(entry("Mark as unread", IconName::EyeOff, mark_unread)); |
| 78 | } |
| 79 | if let Some(translate) = menu.translate.clone() { |
| 80 | list.push(entry(menu.translate_label, IconName::Languages, translate)); |
| 81 | } |
| 82 | if let Some(share_to_outlook) = menu.share_to_outlook.clone() { |
| 83 | list.push(entry(SHARE_TO_OUTLOOK_LABEL, IconName::Mail, share_to_outlook)); |
| 84 | } |
| 85 | if let Some(refresh_card) = menu.refresh_card.clone() { |
| 86 | list.push(entry("Refresh card", IconName::RefreshCw, refresh_card)); |
| 87 | } |
| 88 | for delegate in &menu.delegate { |
| 89 | list.push(entry(delegate.label.clone(), IconName::Bot, delegate.action.clone())); |
| 90 | } |
| 91 | if let Some(delete) = menu.delete.clone() { |
| 92 | list.push(Entry { |
| 93 | danger: true, |
| 94 | ..entry("Delete", IconName::Trash, delete) |
| 95 | }); |
| 96 | } |
| 97 | if let Some(select) = menu.forward_multiple.clone() { |
| 98 | list.push(entry(SELECT_LABEL, IconName::CircleCheck, select)); |
| 99 | } |
| 100 | list |
| 101 | } |
| 102 | |
| 103 | fn item(key: &str, index: usize, entry: Entry, close: Close) -> AnyElement { |
| 104 | let color = if entry.danger { theme::red() } else { theme::text() }; |
| 105 | let action = entry.action; |
| 106 | h_flex() |
| 107 | .id(ElementId::Name(format!("context-item-{index}-{key}").into())) |
| 108 | .debug_selector(|| format!("{CONTEXT_MENU_TAG}/item").into()) |
| 109 | .h(px(ITEM_HEIGHT)) |
| 110 | .px(px(10.)) |
| 111 | .gap(px(12.)) |
| 112 | .items_center() |
| 113 | .rounded(px(6.)) |
| 114 | .cursor_pointer() |
| 115 | .text_size(px(13.)) |
| 116 | .text_color(color) |
| 117 | .hover(|row| row.bg(theme::popup_hover())) |
| 118 | .child(icon(entry.glyph, ITEM_ICON, if entry.danger { theme::red() } else { theme::text_soft() })) |
| 119 | .child(div().flex_1().min_w_0().truncate().child(entry.label)) |
| 120 | .on_click(move |_, window, cx| { |
| 121 | cx.stop_propagation(); |
| 122 | close(window, cx); |
| 123 | action(window, cx); |
| 124 | }) |
| 125 | .into_any_element() |
| 126 | } |
| 127 | |
| 128 | /// Telegram-style message menu: opens at the cursor, quick reactions on top, the actions below. |
| 129 | pub fn message_context_menu( |
| 130 | mut menu: MessageMenu, |
| 131 | position: Point<Pixels>, |
| 132 | close: Close, |
| 133 | close_from_outside: Close, |
| 134 | cx: &App, |
| 135 | ) -> AnyElement { |
| 136 | let picked = menu.done.clone(); |
| 137 | let after_pick = close.clone(); |
| 138 | menu.done = Rc::new(move |window, cx| { |
| 139 | picked(window, cx); |
| 140 | after_pick(window, cx); |
| 141 | }); |
| 142 | let key = menu.key.clone(); |
| 143 | let mut reactions: Vec<AnyElement> = menu |
| 144 | .quick |
| 145 | .iter() |
| 146 | .enumerate() |
| 147 | .map(|(index, glyph)| reaction_button(&menu, glyph, index)) |
| 148 | .collect(); |
| 149 | reactions.push(picker_button(&menu, cx)); |
| 150 | let items: Vec<AnyElement> = entries(&menu) |
| 151 | .into_iter() |
| 152 | .enumerate() |
| 153 | .map(|(index, entry)| item(&key, index, entry, close.clone())) |
| 154 | .collect(); |
| 155 | deferred( |
| 156 | anchored() |
| 157 | .position(position) |
| 158 | .snap_to_window_with_margin(px(WINDOW_MARGIN)) |
| 159 | .child( |
| 160 | v_flex() |
| 161 | .id(ElementId::Name(format!("{CONTEXT_MENU_TAG}-{key}").into())) |
| 162 | .debug_selector(|| CONTEXT_MENU_TAG.into()) |
| 163 | .w(px(MENU_WIDTH)) |
| 164 | .gap(px(PANEL_GAP)) |
| 165 | .occlude() |
| 166 | .on_mouse_down_out(move |_, window, cx| close_from_outside(window, cx)) |
| 167 | .on_scroll_wheel(|_, _, cx| cx.stop_propagation()) |
| 168 | .child( |
| 169 | h_flex() |
| 170 | .debug_selector(|| format!("{CONTEXT_MENU_TAG}/reactions").into()) |
| 171 | .self_start() |
| 172 | .p(px(PILL_PADDING)) |
| 173 | .gap(px(1.)) |
| 174 | .items_center() |
| 175 | .rounded_full() |
| 176 | .flyout_surface() |
| 177 | .children(reactions), |
| 178 | ) |
| 179 | .child( |
| 180 | v_flex() |
| 181 | .debug_selector(|| format!("{CONTEXT_MENU_TAG}/list").into()) |
| 182 | .w_full() |
| 183 | .p(px(LIST_PADDING)) |
| 184 | .rounded(px(10.)) |
| 185 | .flyout_surface() |
| 186 | .children(items), |
| 187 | ), |
| 188 | ), |
| 189 | ) |
| 190 | .with_priority(POPUP_PRIORITY) |
| 191 | .into_any_element() |
| 192 | } |