BlueGit

ict rusty-teams

Files

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

message_actions.rs

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