BlueGit

ict rusty-teams

Files

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

message_actions.rs

message_actions.rs 17.0 KiB Download Raw
1use std::collections::HashSet;
2use std::rc::Rc;
3
4use gpui_kit::assets::IconName;
5use gpui_kit::base::GlobalState;
6use gpui_kit::component::{
7 button::{Button, ButtonCustomVariant, ButtonVariants as _},
8 h_flex,
9 menu::{DropdownMenu as _, PopupMenu, PopupMenuItem},
10 popover::Popover,
11};
12use gpui_kit::prelude::FluentBuilder as _;
13use gpui_kit::*;
14
15use super::reaction_picker::{DoneHandler, PickHandler, ReactionPicker};
16use super::toolbar_placement::{HoverChange, TOOLBAR_HEIGHT, toolbar_slot};
17use super::widgets::{icon, symbol};
18use crate::forward_selection::{FORWARD_LABEL, MULTIPLE_MESSAGES_LABEL, THIS_MESSAGE_LABEL};
19use crate::post_follow::follow_label;
20use crate::rows::reaction_glyph;
21use crate::share_outlook::SHARE_TO_OUTLOOK_LABEL;
22use crate::theme::{self, ElevatedSurface as _};
23
24const TRANSLATION_LABEL: &str = "Translation";
25const AGENTS_LABEL: &str = "Agents";
26const BAR_BUTTON: f32 = 30.;
27const BAR_PADDING: f32 = 3.;
28const BAR_ICON: f32 = 16.;
29const DELEGATE_ITEM_WIDTH: f32 = 188.;
30const DELEGATE_AGENT_SIZE: f32 = 11.;
31const ADD_REACTION_ICON: f32 = 15.;
32const ADD_REACTION_EXTRA_WIDTH: f32 = 8.;
33
34pub type Action = Rc<dyn Fn(&mut Window, &mut App)>;
35pub type OpenChange = Rc<dyn Fn(bool, &mut Window, &mut App)>;
36
37#[derive(Clone)]
38pub struct DelegateEntry {
39 pub label: String,
40 pub agent_name: String,
41 pub action: Action,
42}
43
44pub 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
75fn element_id(prefix: &str, key: &str) -> ElementId {
76 ElementId::Name(format!("{prefix}-{key}").into())
77}
78
79fn 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
86pub(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
98pub(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)]
115pub 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
124impl 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
137fn 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
171pub(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
185pub 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)]
203pub 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
226impl 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(&self, popup: PopupMenu, window: &mut Window, cx: &mut Context<PopupMenu>) -> PopupMenu {
285 let item = |label: &'static str, glyph: IconName, action: Action| {
286 PopupMenuItem::new(label)
287 .icon(glyph)
288 .on_click(move |_, window, cx| action(window, cx))
289 };
290 let mut popup = popup;
291 match (self.forward.clone(), self.forward_multiple.clone()) {
292 (Some(forward), Some(forward_multiple)) => {
293 popup = popup.submenu_with_icon(
294 Some(IconName::Forward.into()),
295 FORWARD_LABEL,
296 window,
297 cx,
298 move |submenu, _, _| {
299 let (single, multiple) = (forward.clone(), forward_multiple.clone());
300 submenu
301 .item(
302 PopupMenuItem::new(THIS_MESSAGE_LABEL)
303 .on_click(move |_, window, cx| single(window, cx)),
304 )
305 .item(
306 PopupMenuItem::new(MULTIPLE_MESSAGES_LABEL)
307 .on_click(move |_, window, cx| multiple(window, cx)),
308 )
309 },
310 );
311 }
312 (Some(forward), None) => {
313 popup = popup.item(item(FORWARD_LABEL, IconName::Forward, forward));
314 }
315 (None, _) => {}
316 }
317 if let Some(copy_link) = self.copy_link.clone() {
318 popup = popup.item(item("Copy link", IconName::Link, copy_link));
319 }
320 popup = popup.item(item("Copy text", IconName::Copy, self.copy.clone()));
321 if let Some(save) = self.save.clone() {
322 popup = popup.item(if self.saved {
323 item("Unsave this message", IconName::BookmarkOff, save)
324 } else {
325 item("Save this message", IconName::Bookmark, save)
326 });
327 }
328 if let Some(toggle_pinned) = self.toggle_pinned.clone() {
329 popup = popup.item(if self.pinned {
330 item("Unpin", IconName::PinOff, toggle_pinned)
331 } else {
332 item("Pin for everyone", IconName::Pin, toggle_pinned)
333 });
334 }
335 if let Some(edit) = self.edit.clone() {
336 popup = popup.item(item("Edit", IconName::Pencil, edit));
337 }
338 if let Some(toggle_follow) = self.toggle_follow.clone() {
339 popup = popup.item(if self.followed {
340 item(follow_label(true), IconName::BellOff, toggle_follow)
341 } else {
342 item(follow_label(false), IconName::Bell, toggle_follow)
343 });
344 }
345 if let Some(refresh_card) = self.refresh_card.clone() {
346 popup = popup.item(item("Refresh card", IconName::RefreshCw, refresh_card));
347 }
348 popup = popup.separator();
349 if let Some(mark_unread) = self.mark_unread.clone() {
350 popup = popup.item(item("Mark as unread", IconName::EyeOff, mark_unread));
351 }
352 if let Some(share_to_outlook) = self.share_to_outlook.clone() {
353 popup = popup.item(item(
354 SHARE_TO_OUTLOOK_LABEL,
355 IconName::Mail,
356 share_to_outlook,
357 ));
358 }
359 let translate_label = self.translate_label;
360 if let Some(translate) = self.translate.clone() {
361 popup = popup.submenu_with_icon(
362 Some(IconName::Languages.into()),
363 TRANSLATION_LABEL,
364 window,
365 cx,
366 move |submenu, _, _| {
367 let translate = translate.clone();
368 submenu.item(
369 PopupMenuItem::new(translate_label)
370 .on_click(move |_, window, cx| translate(window, cx)),
371 )
372 },
373 );
374 }
375 if let Some(delete) = self.delete.clone() {
376 popup = popup
377 .separator()
378 .item(item("Delete", IconName::Trash, delete));
379 }
380 popup
381 }
382}
383
384fn more_button(menu: &MessageMenu, cx: &App) -> AnyElement {
385 let actions = MenuActions::of(menu);
386 let pin = menu.pin.clone();
387 Button::new(element_id("message-more", &menu.key))
388 .child(icon(IconName::Ellipsis, BAR_ICON, theme::text_soft()))
389 .custom(bar_variant(cx))
390 .rounded(px(7.))
391 .size(px(BAR_BUTTON))
392 .dropdown_menu_with_anchor(Anchor::TopRight, move |popup, window, cx| {
393 actions.fill(popup, false, window, cx)
394 })
395 .on_open_change(move |open, window, cx| pin(*open, window, cx))
396 .into_any_element()
397}
398
399fn delegate_entries(popup: PopupMenu, entries: &[DelegateEntry], manage_agents: Option<Action>) -> PopupMenu {
400 let mut popup = popup;
401 for entry in entries {
402 popup = popup.item(delegate_item(entry));
403 }
404 if let Some(manage_agents) = manage_agents {
405 popup = popup.separator().item(
406 PopupMenuItem::element(|_, _| {
407 div()
408 .text_color(theme::text_muted())
409 .child("Manage actions...")
410 })
411 .on_click(move |_, window, cx| manage_agents(window, cx)),
412 );
413 }
414 popup
415}
416
417fn delegate_item(entry: &DelegateEntry) -> PopupMenuItem {
418 let (label, agent_name, action) = (
419 entry.label.clone(),
420 entry.agent_name.clone(),
421 entry.action.clone(),
422 );
423 PopupMenuItem::element(move |_, _| {
424 h_flex()
425 .w(px(DELEGATE_ITEM_WIDTH))
426 .gap(px(12.))
427 .items_center()
428 .justify_between()
429 .child(div().flex_1().min_w_0().truncate().child(label.clone()))
430 .child(
431 div()
432 .flex_none()
433 .text_size(px(DELEGATE_AGENT_SIZE))
434 .text_color(theme::text_faint())
435 .child(agent_name.clone()),
436 )
437 })
438 .icon(IconName::Bot)
439 .on_click(move |_, window, cx| action(window, cx))
440}
441
442fn delegate_button(menu: &MessageMenu, cx: &App) -> AnyElement {
443 let (entries, manage_agents, pin) = (
444 menu.delegate.clone(),
445 menu.manage_agents.clone(),
446 menu.pin.clone(),
447 );
448 Button::new(element_id("message-delegate", &menu.key))
449 .child(icon(IconName::Bot, BAR_ICON, theme::text_soft()))
450 .custom(bar_variant(cx))
451 .rounded(px(7.))
452 .size(px(BAR_BUTTON))
453 .dropdown_menu_with_anchor(Anchor::TopRight, move |popup, _, _| {
454 delegate_entries(popup, &entries, manage_agents.clone())
455 })
456 .on_open_change(move |open, window, cx| pin(*open, window, cx))
457 .into_any_element()
458}
459
460pub fn message_toolbar(menu: MessageMenu, cx: &App) -> AnyElement {
461 let hover = menu.hover.clone();
462 let mut children: Vec<AnyElement> = menu
463 .quick
464 .iter()
465 .enumerate()
466 .map(|(index, glyph)| reaction_button(&menu, glyph, index))
467 .collect();
468 children.push(picker_button(&menu, cx));
469 children.push(
470 div()
471 .w(px(1.))
472 .h_full()
473 .mx(px(4.))
474 .flex_none()
475 .bg(theme::popup_hover())
476 .into_any_element(),
477 );
478 if let Some(reply) = menu.reply.clone() {
479 children.push(
480 bar_button(element_id("toolbar-reply", &menu.key))
481 .child(icon(IconName::Reply, 16., theme::text_soft()))
482 .on_click(move |_, window, cx| {
483 cx.stop_propagation();
484 reply(window, cx);
485 })
486 .into_any_element(),
487 );
488 }
489 if !menu.delegate.is_empty() {
490 children.push(delegate_button(&menu, cx));
491 }
492 children.push(more_button(&menu, cx));
493 let bar = h_flex()
494 .h(px(TOOLBAR_HEIGHT))
495 .px(px(BAR_PADDING))
496 .gap(px(1.))
497 .items_center()
498 .rounded(px(10.))
499 .flyout_surface()
500 .on_mouse_down(MouseButton::Left, |_, _, cx| {
501 GlobalState::suppress_text_selection(cx)
502 })
503 .children(children);
504 toolbar_slot(element_id("message-toolbar", &menu.key), hover, bar).into_any_element()
505}