| 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 | button::{Button, ButtonCustomVariant, ButtonVariants as _}, |
| 8 | h_flex, |
| 9 | menu::{DropdownMenu as _, PopupMenu, PopupMenuItem}, |
| 10 | popover::Popover, |
| 11 | }; |
| 12 | use gpui_kit::prelude::FluentBuilder as _; |
| 13 | use gpui_kit::*; |
| 14 | |
| 15 | use super::reaction_picker::{DoneHandler, PickHandler, ReactionPicker}; |
| 16 | use super::toolbar_placement::{HoverChange, TOOLBAR_HEIGHT, toolbar_slot}; |
| 17 | use super::widgets::{icon, symbol}; |
| 18 | use crate::forward_selection::{FORWARD_LABEL, MULTIPLE_MESSAGES_LABEL, THIS_MESSAGE_LABEL}; |
| 19 | use crate::post_follow::follow_label; |
| 20 | use crate::rows::reaction_glyph; |
| 21 | use crate::share_outlook::SHARE_TO_OUTLOOK_LABEL; |
| 22 | use crate::theme::{self, ElevatedSurface as _}; |
| 23 | |
| 24 | const TRANSLATION_LABEL: &str = "Translation"; |
| 25 | const AGENTS_LABEL: &str = "Agents"; |
| 26 | const BAR_BUTTON: f32 = 30.; |
| 27 | const BAR_PADDING: f32 = 3.; |
| 28 | const BAR_ICON: f32 = 16.; |
| 29 | const DELEGATE_ITEM_WIDTH: f32 = 188.; |
| 30 | const DELEGATE_AGENT_SIZE: f32 = 11.; |
| 31 | const ADD_REACTION_ICON: f32 = 15.; |
| 32 | const ADD_REACTION_EXTRA_WIDTH: f32 = 8.; |
| 33 | |
| 34 | pub type Action = Rc<dyn Fn(&mut Window, &mut App)>; |
| 35 | pub type OpenChange = Rc<dyn Fn(bool, &mut Window, &mut App)>; |
| 36 | |
| 37 | #[derive(Clone)] |
| 38 | pub struct DelegateEntry { |
| 39 | pub label: String, |
| 40 | pub agent_name: String, |
| 41 | pub action: Action, |
| 42 | } |
| 43 | |
| 44 | pub struct MessageMenu { |
| 45 | pub key: String, |
| 46 | pub react: PickHandler, |
| 47 | pub reply: Option<Action>, |
| 48 | pub forward: Option<Action>, |
| 49 | pub forward_multiple: Option<Action>, |
| 50 | pub copy_link: Option<Action>, |
| 51 | pub copy: Action, |
| 52 | pub translate: Option<Action>, |
| 53 | pub translate_label: &'static str, |
| 54 | pub share_to_outlook: Option<Action>, |
| 55 | pub save: Option<Action>, |
| 56 | pub saved: bool, |
| 57 | pub toggle_pinned: Option<Action>, |
| 58 | pub pinned: bool, |
| 59 | pub toggle_follow: Option<Action>, |
| 60 | pub followed: bool, |
| 61 | pub edit: Option<Action>, |
| 62 | pub delete: Option<Action>, |
| 63 | pub mark_unread: Option<Action>, |
| 64 | pub refresh_card: Option<Action>, |
| 65 | pub delegate: Vec<DelegateEntry>, |
| 66 | pub manage_agents: Option<Action>, |
| 67 | pub pin: OpenChange, |
| 68 | pub hover: HoverChange, |
| 69 | pub picker: Entity<ReactionPicker>, |
| 70 | pub quick: Vec<String>, |
| 71 | pub mine: HashSet<String>, |
| 72 | pub done: DoneHandler, |
| 73 | } |
| 74 | |
| 75 | fn element_id(prefix: &str, key: &str) -> ElementId { |
| 76 | ElementId::Name(format!("{prefix}-{key}").into()) |
| 77 | } |
| 78 | |
| 79 | fn bar_variant(cx: &App) -> ButtonCustomVariant { |
| 80 | ButtonCustomVariant::new(cx) |
| 81 | .foreground(theme::text_soft()) |
| 82 | .hover(theme::popup_hover()) |
| 83 | .active(theme::popup_hover()) |
| 84 | } |
| 85 | |
| 86 | pub(super) fn bar_button(id: ElementId) -> Stateful<Div> { |
| 87 | div() |
| 88 | .id(id) |
| 89 | .size(px(BAR_BUTTON)) |
| 90 | .flex() |
| 91 | .items_center() |
| 92 | .justify_center() |
| 93 | .rounded(px(7.)) |
| 94 | .cursor_pointer() |
| 95 | .hover(|button| button.bg(theme::popup_hover())) |
| 96 | } |
| 97 | |
| 98 | pub(super) fn reaction_button(menu: &MessageMenu, glyph: &str, index: usize) -> AnyElement { |
| 99 | let (react, done, picked) = (menu.react.clone(), menu.done.clone(), glyph.to_owned()); |
| 100 | let selected = menu.mine.contains(&reaction_glyph(glyph)); |
| 101 | bar_button(element_id(&format!("quick-reaction-{index}"), &menu.key)) |
| 102 | .text_size(px(17.)) |
| 103 | .font_family(theme::EMOJI_FONT_FAMILY) |
| 104 | .when(selected, |button| button.bg(theme::reaction_on_own())) |
| 105 | .child(glyph.to_owned()) |
| 106 | .on_click(move |_, window, cx| { |
| 107 | cx.stop_propagation(); |
| 108 | react(&picked, window, cx); |
| 109 | done(window, cx); |
| 110 | }) |
| 111 | .into_any_element() |
| 112 | } |
| 113 | |
| 114 | #[derive(Clone)] |
| 115 | pub struct ReactionAdder { |
| 116 | pub key: String, |
| 117 | pub picker: Entity<ReactionPicker>, |
| 118 | pub react: PickHandler, |
| 119 | pub done: DoneHandler, |
| 120 | pub mine: HashSet<String>, |
| 121 | pub pin: OpenChange, |
| 122 | } |
| 123 | |
| 124 | impl MessageMenu { |
| 125 | pub fn reaction_adder(&self) -> ReactionAdder { |
| 126 | ReactionAdder { |
| 127 | key: self.key.clone(), |
| 128 | picker: self.picker.clone(), |
| 129 | react: self.react.clone(), |
| 130 | done: self.done.clone(), |
| 131 | mine: self.mine.clone(), |
| 132 | pin: self.pin.clone(), |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | fn picker_popover( |
| 138 | adder: ReactionAdder, |
| 139 | prefix: &str, |
| 140 | trigger: Button, |
| 141 | anchor: Anchor, |
| 142 | ) -> AnyElement { |
| 143 | let ReactionAdder { |
| 144 | key, |
| 145 | picker, |
| 146 | react, |
| 147 | done, |
| 148 | mine, |
| 149 | pin, |
| 150 | } = adder; |
| 151 | let picker_reset = picker.clone(); |
| 152 | Popover::new(element_id(prefix, &key)) |
| 153 | .anchor(anchor) |
| 154 | .trigger(trigger) |
| 155 | .on_open_change(move |open, window, cx| { |
| 156 | pin(*open, window, cx); |
| 157 | if *open { |
| 158 | picker_reset.update(cx, |picker, cx| picker.reset(window, cx)); |
| 159 | } |
| 160 | }) |
| 161 | .content(move |_, _, cx| { |
| 162 | let popover = cx.entity().downgrade(); |
| 163 | picker.update(cx, |picker, _| { |
| 164 | picker.set_target(react.clone(), done.clone(), popover, mine.clone()) |
| 165 | }); |
| 166 | picker.clone() |
| 167 | }) |
| 168 | .into_any_element() |
| 169 | } |
| 170 | |
| 171 | pub(super) fn picker_button(menu: &MessageMenu, cx: &App) -> AnyElement { |
| 172 | let trigger = Button::new(element_id("add-reaction", &menu.key)) |
| 173 | .child(icon(IconName::Plus, BAR_ICON, theme::text_soft())) |
| 174 | .custom(bar_variant(cx)) |
| 175 | .rounded(px(7.)) |
| 176 | .size(px(BAR_BUTTON)); |
| 177 | picker_popover( |
| 178 | menu.reaction_adder(), |
| 179 | "reaction-picker", |
| 180 | trigger, |
| 181 | Anchor::TopRight, |
| 182 | ) |
| 183 | } |
| 184 | |
| 185 | pub fn add_reaction_pill(adder: ReactionAdder, height: f32, cx: &App) -> AnyElement { |
| 186 | let trigger = Button::new(element_id("pill-add-reaction", &adder.key)) |
| 187 | .child(symbol("mood", ADD_REACTION_ICON, theme::text_muted())) |
| 188 | .custom( |
| 189 | ButtonCustomVariant::new(cx) |
| 190 | .color(theme::border_strong()) |
| 191 | .foreground(theme::text_muted()) |
| 192 | .hover(theme::popup_hover()) |
| 193 | .active(theme::popup_hover()), |
| 194 | ) |
| 195 | .rounded(px(height / 2.)) |
| 196 | .h(px(height)) |
| 197 | .w(px(height + ADD_REACTION_EXTRA_WIDTH)); |
| 198 | picker_popover(adder, "pill-reaction-picker", trigger, Anchor::TopLeft) |
| 199 | } |
| 200 | |
| 201 | /// The actions of one message, shared by the toolbar's more menu and the right-click menu. |
| 202 | #[derive(Clone)] |
| 203 | pub struct MenuActions { |
| 204 | reply: Option<Action>, |
| 205 | forward: Option<Action>, |
| 206 | forward_multiple: Option<Action>, |
| 207 | copy_link: Option<Action>, |
| 208 | copy: Action, |
| 209 | translate: Option<Action>, |
| 210 | translate_label: &'static str, |
| 211 | save: Option<Action>, |
| 212 | saved: bool, |
| 213 | toggle_pinned: Option<Action>, |
| 214 | pinned: bool, |
| 215 | toggle_follow: Option<Action>, |
| 216 | followed: bool, |
| 217 | edit: Option<Action>, |
| 218 | delete: Option<Action>, |
| 219 | mark_unread: Option<Action>, |
| 220 | refresh_card: Option<Action>, |
| 221 | share_to_outlook: Option<Action>, |
| 222 | delegate: Vec<DelegateEntry>, |
| 223 | manage_agents: Option<Action>, |
| 224 | } |
| 225 | |
| 226 | impl MenuActions { |
| 227 | pub fn of(menu: &MessageMenu) -> Self { |
| 228 | MenuActions { |
| 229 | reply: menu.reply.clone(), |
| 230 | forward: menu.forward.clone(), |
| 231 | forward_multiple: menu.forward_multiple.clone(), |
| 232 | copy_link: menu.copy_link.clone(), |
| 233 | copy: menu.copy.clone(), |
| 234 | translate: menu.translate.clone(), |
| 235 | translate_label: menu.translate_label, |
| 236 | save: menu.save.clone(), |
| 237 | saved: menu.saved, |
| 238 | toggle_pinned: menu.toggle_pinned.clone(), |
| 239 | pinned: menu.pinned, |
| 240 | toggle_follow: menu.toggle_follow.clone(), |
| 241 | followed: menu.followed, |
| 242 | edit: menu.edit.clone(), |
| 243 | delete: menu.delete.clone(), |
| 244 | mark_unread: menu.mark_unread.clone(), |
| 245 | refresh_card: menu.refresh_card.clone(), |
| 246 | share_to_outlook: menu.share_to_outlook.clone(), |
| 247 | delegate: menu.delegate.clone(), |
| 248 | manage_agents: menu.manage_agents.clone(), |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /// The right-click menu adds Reply on top and the agent actions as a submenu; the toolbar has its own buttons for both. |
| 253 | pub fn fill( |
| 254 | &self, |
| 255 | popup: PopupMenu, |
| 256 | right_click: bool, |
| 257 | window: &mut Window, |
| 258 | cx: &mut Context<PopupMenu>, |
| 259 | ) -> PopupMenu { |
| 260 | let mut popup = popup; |
| 261 | if right_click { |
| 262 | if let Some(reply) = self.reply.clone() { |
| 263 | popup = popup.item( |
| 264 | PopupMenuItem::new("Reply") |
| 265 | .icon(IconName::Reply) |
| 266 | .on_click(move |_, window, cx| reply(window, cx)), |
| 267 | ); |
| 268 | } |
| 269 | if !self.delegate.is_empty() { |
| 270 | let (entries, manage_agents) = (self.delegate.clone(), self.manage_agents.clone()); |
| 271 | popup = popup.submenu_with_icon( |
| 272 | Some(IconName::Bot.into()), |
| 273 | AGENTS_LABEL, |
| 274 | window, |
| 275 | cx, |
| 276 | move |submenu, _, _| delegate_entries(submenu, &entries, manage_agents.clone()), |
| 277 | ); |
| 278 | } |
| 279 | } |
| 280 | popup = self.fill_rest(popup, window, cx); |
| 281 | popup |
| 282 | } |
| 283 | |
| 284 | fn fill_rest( |
| 285 | &self, |
| 286 | popup: PopupMenu, |
| 287 | window: &mut Window, |
| 288 | cx: &mut Context<PopupMenu>, |
| 289 | ) -> PopupMenu { |
| 290 | let item = |label: &'static str, glyph: IconName, action: Action| { |
| 291 | PopupMenuItem::new(label) |
| 292 | .icon(glyph) |
| 293 | .on_click(move |_, window, cx| action(window, cx)) |
| 294 | }; |
| 295 | let mut popup = popup; |
| 296 | match (self.forward.clone(), self.forward_multiple.clone()) { |
| 297 | (Some(forward), Some(forward_multiple)) => { |
| 298 | popup = popup.submenu_with_icon( |
| 299 | Some(IconName::Forward.into()), |
| 300 | FORWARD_LABEL, |
| 301 | window, |
| 302 | cx, |
| 303 | move |submenu, _, _| { |
| 304 | let (single, multiple) = (forward.clone(), forward_multiple.clone()); |
| 305 | submenu |
| 306 | .item( |
| 307 | PopupMenuItem::new(THIS_MESSAGE_LABEL) |
| 308 | .on_click(move |_, window, cx| single(window, cx)), |
| 309 | ) |
| 310 | .item( |
| 311 | PopupMenuItem::new(MULTIPLE_MESSAGES_LABEL) |
| 312 | .on_click(move |_, window, cx| multiple(window, cx)), |
| 313 | ) |
| 314 | }, |
| 315 | ); |
| 316 | } |
| 317 | (Some(forward), None) => { |
| 318 | popup = popup.item(item(FORWARD_LABEL, IconName::Forward, forward)); |
| 319 | } |
| 320 | (None, _) => {} |
| 321 | } |
| 322 | if let Some(copy_link) = self.copy_link.clone() { |
| 323 | popup = popup.item(item("Copy link", IconName::Link, copy_link)); |
| 324 | } |
| 325 | popup = popup.item(item("Copy text", IconName::Copy, self.copy.clone())); |
| 326 | if let Some(save) = self.save.clone() { |
| 327 | popup = popup.item(if self.saved { |
| 328 | item("Unsave this message", IconName::BookmarkOff, save) |
| 329 | } else { |
| 330 | item("Save this message", IconName::Bookmark, save) |
| 331 | }); |
| 332 | } |
| 333 | if let Some(toggle_pinned) = self.toggle_pinned.clone() { |
| 334 | popup = popup.item(if self.pinned { |
| 335 | item("Unpin", IconName::PinOff, toggle_pinned) |
| 336 | } else { |
| 337 | item("Pin for everyone", IconName::Pin, toggle_pinned) |
| 338 | }); |
| 339 | } |
| 340 | if let Some(edit) = self.edit.clone() { |
| 341 | popup = popup.item(item("Edit", IconName::Pencil, edit)); |
| 342 | } |
| 343 | if let Some(toggle_follow) = self.toggle_follow.clone() { |
| 344 | popup = popup.item(if self.followed { |
| 345 | item(follow_label(true), IconName::BellOff, toggle_follow) |
| 346 | } else { |
| 347 | item(follow_label(false), IconName::Bell, toggle_follow) |
| 348 | }); |
| 349 | } |
| 350 | if let Some(refresh_card) = self.refresh_card.clone() { |
| 351 | popup = popup.item(item("Refresh card", IconName::RefreshCw, refresh_card)); |
| 352 | } |
| 353 | popup = popup.separator(); |
| 354 | if let Some(mark_unread) = self.mark_unread.clone() { |
| 355 | popup = popup.item(item("Mark as unread", IconName::EyeOff, mark_unread)); |
| 356 | } |
| 357 | if let Some(share_to_outlook) = self.share_to_outlook.clone() { |
| 358 | popup = popup.item(item( |
| 359 | SHARE_TO_OUTLOOK_LABEL, |
| 360 | IconName::Mail, |
| 361 | share_to_outlook, |
| 362 | )); |
| 363 | } |
| 364 | let translate_label = self.translate_label; |
| 365 | if let Some(translate) = self.translate.clone() { |
| 366 | popup = popup.submenu_with_icon( |
| 367 | Some(IconName::Languages.into()), |
| 368 | TRANSLATION_LABEL, |
| 369 | window, |
| 370 | cx, |
| 371 | move |submenu, _, _| { |
| 372 | let translate = translate.clone(); |
| 373 | submenu.item( |
| 374 | PopupMenuItem::new(translate_label) |
| 375 | .on_click(move |_, window, cx| translate(window, cx)), |
| 376 | ) |
| 377 | }, |
| 378 | ); |
| 379 | } |
| 380 | if let Some(delete) = self.delete.clone() { |
| 381 | popup = popup |
| 382 | .separator() |
| 383 | .item(item("Delete", IconName::Trash, delete)); |
| 384 | } |
| 385 | popup |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | fn more_button(menu: &MessageMenu, cx: &App) -> AnyElement { |
| 390 | let actions = MenuActions::of(menu); |
| 391 | let pin = menu.pin.clone(); |
| 392 | Button::new(element_id("message-more", &menu.key)) |
| 393 | .child(icon(IconName::Ellipsis, BAR_ICON, theme::text_soft())) |
| 394 | .custom(bar_variant(cx)) |
| 395 | .rounded(px(7.)) |
| 396 | .size(px(BAR_BUTTON)) |
| 397 | .dropdown_menu_with_anchor(Anchor::TopRight, move |popup, window, cx| { |
| 398 | actions.fill(popup, false, window, cx) |
| 399 | }) |
| 400 | .on_open_change(move |open, window, cx| pin(*open, window, cx)) |
| 401 | .into_any_element() |
| 402 | } |
| 403 | |
| 404 | fn delegate_entries( |
| 405 | popup: PopupMenu, |
| 406 | entries: &[DelegateEntry], |
| 407 | manage_agents: Option<Action>, |
| 408 | ) -> PopupMenu { |
| 409 | let mut popup = popup; |
| 410 | for entry in entries { |
| 411 | popup = popup.item(delegate_item(entry)); |
| 412 | } |
| 413 | if let Some(manage_agents) = manage_agents { |
| 414 | popup = popup.separator().item( |
| 415 | PopupMenuItem::element(|_, _| { |
| 416 | div() |
| 417 | .text_color(theme::text_muted()) |
| 418 | .child("Manage actions...") |
| 419 | }) |
| 420 | .on_click(move |_, window, cx| manage_agents(window, cx)), |
| 421 | ); |
| 422 | } |
| 423 | popup |
| 424 | } |
| 425 | |
| 426 | fn delegate_item(entry: &DelegateEntry) -> PopupMenuItem { |
| 427 | let (label, agent_name, action) = ( |
| 428 | entry.label.clone(), |
| 429 | entry.agent_name.clone(), |
| 430 | entry.action.clone(), |
| 431 | ); |
| 432 | PopupMenuItem::element(move |_, _| { |
| 433 | h_flex() |
| 434 | .w(px(DELEGATE_ITEM_WIDTH)) |
| 435 | .gap(px(12.)) |
| 436 | .items_center() |
| 437 | .justify_between() |
| 438 | .child(div().flex_1().min_w_0().truncate().child(label.clone())) |
| 439 | .child( |
| 440 | div() |
| 441 | .flex_none() |
| 442 | .text_size(px(DELEGATE_AGENT_SIZE)) |
| 443 | .text_color(theme::text_faint()) |
| 444 | .child(agent_name.clone()), |
| 445 | ) |
| 446 | }) |
| 447 | .icon(IconName::Bot) |
| 448 | .on_click(move |_, window, cx| action(window, cx)) |
| 449 | } |
| 450 | |
| 451 | fn delegate_button(menu: &MessageMenu, cx: &App) -> AnyElement { |
| 452 | let (entries, manage_agents, pin) = ( |
| 453 | menu.delegate.clone(), |
| 454 | menu.manage_agents.clone(), |
| 455 | menu.pin.clone(), |
| 456 | ); |
| 457 | Button::new(element_id("message-delegate", &menu.key)) |
| 458 | .child(icon(IconName::Bot, BAR_ICON, theme::text_soft())) |
| 459 | .custom(bar_variant(cx)) |
| 460 | .rounded(px(7.)) |
| 461 | .size(px(BAR_BUTTON)) |
| 462 | .dropdown_menu_with_anchor(Anchor::TopRight, move |popup, _, _| { |
| 463 | delegate_entries(popup, &entries, manage_agents.clone()) |
| 464 | }) |
| 465 | .on_open_change(move |open, window, cx| pin(*open, window, cx)) |
| 466 | .into_any_element() |
| 467 | } |
| 468 | |
| 469 | pub fn message_toolbar(menu: MessageMenu, cx: &App) -> AnyElement { |
| 470 | let hover = menu.hover.clone(); |
| 471 | let mut children: Vec<AnyElement> = menu |
| 472 | .quick |
| 473 | .iter() |
| 474 | .enumerate() |
| 475 | .map(|(index, glyph)| reaction_button(&menu, glyph, index)) |
| 476 | .collect(); |
| 477 | children.push(picker_button(&menu, cx)); |
| 478 | children.push( |
| 479 | div() |
| 480 | .w(px(1.)) |
| 481 | .h_full() |
| 482 | .mx(px(4.)) |
| 483 | .flex_none() |
| 484 | .bg(theme::popup_hover()) |
| 485 | .into_any_element(), |
| 486 | ); |
| 487 | if let Some(reply) = menu.reply.clone() { |
| 488 | children.push( |
| 489 | bar_button(element_id("toolbar-reply", &menu.key)) |
| 490 | .child(icon(IconName::Reply, 16., theme::text_soft())) |
| 491 | .on_click(move |_, window, cx| { |
| 492 | cx.stop_propagation(); |
| 493 | reply(window, cx); |
| 494 | }) |
| 495 | .into_any_element(), |
| 496 | ); |
| 497 | } |
| 498 | if !menu.delegate.is_empty() { |
| 499 | children.push(delegate_button(&menu, cx)); |
| 500 | } |
| 501 | children.push(more_button(&menu, cx)); |
| 502 | let bar = h_flex() |
| 503 | .h(px(TOOLBAR_HEIGHT)) |
| 504 | .px(px(BAR_PADDING)) |
| 505 | .gap(px(1.)) |
| 506 | .items_center() |
| 507 | .rounded(px(10.)) |
| 508 | .flyout_surface() |
| 509 | .on_mouse_down(MouseButton::Left, |_, _, cx| { |
| 510 | GlobalState::suppress_text_selection(cx) |
| 511 | }) |
| 512 | .children(children); |
| 513 | toolbar_slot(element_id("message-toolbar", &menu.key), hover, bar).into_any_element() |
| 514 | } |