| 1 | use std::collections::HashSet; |
| 2 | use std::rc::Rc; |
| 3 | |
| 4 | use gpui_kit::assets::IconName; |
| 5 | use gpui_kit::base::GlobalState; |
| 6 | use gpui_kit::component::{ |
| 7 | Sizable as _, |
| 8 | button::{Button, ButtonCustomVariant, ButtonVariants as _}, |
| 9 | h_flex, |
| 10 | menu::{DropdownMenu as _, PopupMenuItem}, |
| 11 | popover::Popover, |
| 12 | }; |
| 13 | use gpui_kit::prelude::FluentBuilder as _; |
| 14 | use gpui_kit::*; |
| 15 | |
| 16 | use super::reaction_picker::{DoneHandler, PickHandler, ReactionPicker}; |
| 17 | use super::widgets::icon; |
| 18 | use crate::rows::reaction_glyph; |
| 19 | use crate::theme; |
| 20 | |
| 21 | const BAR_BUTTON: f32 = 30.; |
| 22 | const BAR_PADDING: f32 = 3.; |
| 23 | const BAR_GROUP_GAP: f32 = 6.; |
| 24 | pub const QUICK_REACTION_COUNT: usize = 4; |
| 25 | |
| 26 | pub type Action = Rc<dyn Fn(&mut Window, &mut App)>; |
| 27 | pub type OpenChange = Rc<dyn Fn(bool, &mut Window, &mut App)>; |
| 28 | pub type HoverChange = Rc<dyn Fn(bool, &mut App)>; |
| 29 | |
| 30 | pub struct MessageMenu { |
| 31 | pub key: String, |
| 32 | pub react: PickHandler, |
| 33 | pub reply: Option<Action>, |
| 34 | pub forward: Option<Action>, |
| 35 | pub copy_link: Option<Action>, |
| 36 | pub copy: Action, |
| 37 | pub translate: Option<Action>, |
| 38 | pub translate_label: &'static str, |
| 39 | pub save: Option<Action>, |
| 40 | pub saved: bool, |
| 41 | pub toggle_pinned: Option<Action>, |
| 42 | pub pinned: bool, |
| 43 | pub edit: Option<Action>, |
| 44 | pub delete: Option<Action>, |
| 45 | pub mark_unread: Option<Action>, |
| 46 | pub refresh_card: Option<Action>, |
| 47 | pub pin: OpenChange, |
| 48 | pub hover: HoverChange, |
| 49 | pub picker: Entity<ReactionPicker>, |
| 50 | pub quick: Vec<String>, |
| 51 | pub mine: HashSet<String>, |
| 52 | pub done: DoneHandler, |
| 53 | } |
| 54 | |
| 55 | fn element_id(prefix: &str, key: &str) -> ElementId { |
| 56 | ElementId::Name(format!("{prefix}-{key}").into()) |
| 57 | } |
| 58 | |
| 59 | fn bar_variant(cx: &App) -> ButtonCustomVariant { |
| 60 | ButtonCustomVariant::new(cx) |
| 61 | .foreground(theme::text_soft()) |
| 62 | .hover(theme::popup_hover()) |
| 63 | .active(theme::popup_hover()) |
| 64 | } |
| 65 | |
| 66 | pub(super) fn bar_button(id: ElementId) -> Stateful<Div> { |
| 67 | div() |
| 68 | .id(id) |
| 69 | .size(px(BAR_BUTTON)) |
| 70 | .flex() |
| 71 | .items_center() |
| 72 | .justify_center() |
| 73 | .rounded(px(7.)) |
| 74 | .cursor_pointer() |
| 75 | .hover(|button| button.bg(theme::popup_hover())) |
| 76 | } |
| 77 | |
| 78 | fn reaction_button(menu: &MessageMenu, glyph: &str, index: usize) -> AnyElement { |
| 79 | let (react, done, picked) = (menu.react.clone(), menu.done.clone(), glyph.to_owned()); |
| 80 | let selected = menu.mine.contains(&reaction_glyph(glyph)); |
| 81 | bar_button(element_id(&format!("quick-reaction-{index}"), &menu.key)) |
| 82 | .text_size(px(17.)) |
| 83 | .when(selected, |button| button.bg(theme::reaction_on_own())) |
| 84 | .child(glyph.to_owned()) |
| 85 | .on_click(move |_, window, cx| { |
| 86 | cx.stop_propagation(); |
| 87 | react(&picked, window, cx); |
| 88 | done(window, cx); |
| 89 | }) |
| 90 | .into_any_element() |
| 91 | } |
| 92 | |
| 93 | fn picker_button(menu: &MessageMenu, cx: &App) -> AnyElement { |
| 94 | let (picker, picker_reset, react, done, mine, pin) = ( |
| 95 | menu.picker.clone(), |
| 96 | menu.picker.clone(), |
| 97 | menu.react.clone(), |
| 98 | menu.done.clone(), |
| 99 | menu.mine.clone(), |
| 100 | menu.pin.clone(), |
| 101 | ); |
| 102 | Popover::new(element_id("reaction-picker", &menu.key)) |
| 103 | .anchor(Anchor::TopRight) |
| 104 | .trigger( |
| 105 | Button::new(element_id("add-reaction", &menu.key)) |
| 106 | .icon(IconName::Plus) |
| 107 | .custom(bar_variant(cx)) |
| 108 | .rounded(px(7.)) |
| 109 | .xsmall() |
| 110 | .size(px(BAR_BUTTON)), |
| 111 | ) |
| 112 | .on_open_change(move |open, window, cx| { |
| 113 | pin(*open, window, cx); |
| 114 | if *open { |
| 115 | picker_reset.update(cx, |picker, cx| picker.reset(window, cx)); |
| 116 | } |
| 117 | }) |
| 118 | .content(move |_, _, cx| { |
| 119 | let popover = cx.entity().downgrade(); |
| 120 | picker.update(cx, |picker, _| { |
| 121 | picker.set_target(react.clone(), done.clone(), popover, mine.clone()) |
| 122 | }); |
| 123 | picker.clone() |
| 124 | }) |
| 125 | .into_any_element() |
| 126 | } |
| 127 | |
| 128 | fn more_button(menu: &MessageMenu, cx: &App) -> AnyElement { |
| 129 | let ( |
| 130 | reply, |
| 131 | forward, |
| 132 | copy_link, |
| 133 | copy, |
| 134 | translate, |
| 135 | save, |
| 136 | toggle_pinned, |
| 137 | edit, |
| 138 | delete, |
| 139 | mark_unread, |
| 140 | refresh_card, |
| 141 | pin, |
| 142 | ) = ( |
| 143 | menu.reply.clone(), |
| 144 | menu.forward.clone(), |
| 145 | menu.copy_link.clone(), |
| 146 | menu.copy.clone(), |
| 147 | menu.translate.clone(), |
| 148 | menu.save.clone(), |
| 149 | menu.toggle_pinned.clone(), |
| 150 | menu.edit.clone(), |
| 151 | menu.delete.clone(), |
| 152 | menu.mark_unread.clone(), |
| 153 | menu.refresh_card.clone(), |
| 154 | menu.pin.clone(), |
| 155 | ); |
| 156 | let (saved, pinned, translate_label) = (menu.saved, menu.pinned, menu.translate_label); |
| 157 | Button::new(element_id("message-more", &menu.key)) |
| 158 | .icon(IconName::Ellipsis) |
| 159 | .custom(bar_variant(cx)) |
| 160 | .rounded(px(7.)) |
| 161 | .xsmall() |
| 162 | .size(px(BAR_BUTTON)) |
| 163 | .dropdown_menu_with_anchor(Anchor::TopRight, move |popup, _, _| { |
| 164 | let item = |label: &'static str, glyph: IconName, action: Action| { |
| 165 | PopupMenuItem::new(label) |
| 166 | .icon(glyph) |
| 167 | .on_click(move |_, window, cx| action(window, cx)) |
| 168 | }; |
| 169 | let mut popup = popup; |
| 170 | if let Some(reply) = reply.clone() { |
| 171 | popup = popup.item(item("Reply with quote", IconName::Reply, reply)); |
| 172 | } |
| 173 | if let Some(forward) = forward.clone() { |
| 174 | popup = popup.item(item("Forward", IconName::Forward, forward)); |
| 175 | } |
| 176 | if let Some(copy_link) = copy_link.clone() { |
| 177 | popup = popup.item(item("Copy link", IconName::Link, copy_link)); |
| 178 | } |
| 179 | if let Some(refresh_card) = refresh_card.clone() { |
| 180 | popup = popup.item(item("Refresh card", IconName::RefreshCw, refresh_card)); |
| 181 | } |
| 182 | popup = popup.item(item("Copy text", IconName::Copy, copy.clone())); |
| 183 | if let Some(translate) = translate.clone() { |
| 184 | popup = popup.item(item(translate_label, IconName::Languages, translate)); |
| 185 | } |
| 186 | if let Some(save) = save.clone() { |
| 187 | popup = popup.item(if saved { |
| 188 | item("Unsave message", IconName::BookmarkOff, save) |
| 189 | } else { |
| 190 | item("Save message", IconName::Bookmark, save) |
| 191 | }); |
| 192 | } |
| 193 | if let Some(toggle_pinned) = toggle_pinned.clone() { |
| 194 | popup = popup.item(if pinned { |
| 195 | item("Unpin", IconName::PinOff, toggle_pinned) |
| 196 | } else { |
| 197 | item("Pin", IconName::Pin, toggle_pinned) |
| 198 | }); |
| 199 | } |
| 200 | if let Some(edit) = edit.clone() { |
| 201 | popup = popup.item(item("Edit", IconName::Pencil, edit)); |
| 202 | } |
| 203 | if let Some(delete) = delete.clone() { |
| 204 | popup = popup |
| 205 | .separator() |
| 206 | .item(item("Delete", IconName::Trash, delete)); |
| 207 | } |
| 208 | if let Some(mark_unread) = mark_unread.clone() { |
| 209 | let entry = item("Mark as unread", IconName::EyeOff, mark_unread); |
| 210 | popup = popup.separator().item(entry); |
| 211 | } |
| 212 | popup |
| 213 | }) |
| 214 | .on_open_change(move |open, window, cx| pin(*open, window, cx)) |
| 215 | .into_any_element() |
| 216 | } |
| 217 | |
| 218 | pub fn message_toolbar(menu: MessageMenu, cx: &App) -> AnyElement { |
| 219 | let hover = menu.hover.clone(); |
| 220 | let mut children: Vec<AnyElement> = menu |
| 221 | .quick |
| 222 | .iter() |
| 223 | .enumerate() |
| 224 | .map(|(index, glyph)| reaction_button(&menu, glyph, index)) |
| 225 | .collect(); |
| 226 | children.push(picker_button(&menu, cx)); |
| 227 | children.push( |
| 228 | div().w(px(BAR_GROUP_GAP)).into_any_element(), |
| 229 | ); |
| 230 | if let Some(reply) = menu.reply.clone() { |
| 231 | children.push( |
| 232 | bar_button(element_id("toolbar-reply", &menu.key)) |
| 233 | .child(icon(IconName::Reply, 16., theme::text_soft())) |
| 234 | .on_click(move |_, window, cx| { |
| 235 | cx.stop_propagation(); |
| 236 | reply(window, cx); |
| 237 | }) |
| 238 | .into_any_element(), |
| 239 | ); |
| 240 | } |
| 241 | children.push(more_button(&menu, cx)); |
| 242 | h_flex() |
| 243 | .id(element_id("message-toolbar", &menu.key)) |
| 244 | .h(px(BAR_BUTTON + 2. * BAR_PADDING)) |
| 245 | .p(px(BAR_PADDING)) |
| 246 | .gap(px(1.)) |
| 247 | .items_center() |
| 248 | .rounded(px(10.)) |
| 249 | .bg(theme::popup()) |
| 250 | .occlude() |
| 251 | .on_hover(move |hovered, _, cx| hover(*hovered, cx)) |
| 252 | .on_mouse_down(MouseButton::Left, |_, _, cx| { |
| 253 | GlobalState::suppress_text_selection(cx) |
| 254 | }) |
| 255 | .children(children) |
| 256 | .into_any_element() |
| 257 | } |