BlueGit

ict rusty-teams

Files

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

message_actions.rs

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