BlueGit

ict rusty-teams

Files

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

message_actions.rs

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