BlueGit

ict rusty-teams

Files

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

message_actions.rs

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