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