BlueGit

ict rusty-teams

Files

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

message_actions.rs

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