BlueGit

ict rusty-teams

Files

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

message_actions.rs

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