| 1 | use std::collections::HashSet; |
| 2 | use std::rc::Rc; |
| 3 | |
| 4 | use gpui_kit::assets::IconName; |
| 5 | use gpui_kit::base::GlobalState; |
| 6 | use gpui_kit::component::{ |
| 7 | button::{Button, ButtonCustomVariant, ButtonVariants as _}, |
| 8 | h_flex, |
| 9 | menu::{DropdownMenu as _, PopupMenuItem}, |
| 10 | popover::Popover, |
| 11 | }; |
| 12 | use gpui_kit::prelude::FluentBuilder as _; |
| 13 | use gpui_kit::*; |
| 14 | |
| 15 | use super::reaction_picker::{DoneHandler, PickHandler, ReactionPicker}; |
| 16 | use super::toolbar_placement::{TOOLBAR_HEIGHT, toolbar_slot}; |
| 17 | use super::widgets::icon; |
| 18 | use crate::rows::reaction_glyph; |
| 19 | use crate::theme; |
| 20 | |
| 21 | const BAR_BUTTON: f32 = 30.; |
| 22 | const BAR_PADDING: f32 = 3.; |
| 23 | const BAR_ICON: f32 = 16.; |
| 24 | const DELEGATE_ITEM_WIDTH: f32 = 188.; |
| 25 | const DELEGATE_AGENT_SIZE: f32 = 11.; |
| 26 | pub const QUICK_REACTION_COUNT: usize = 4; |
| 27 | |
| 28 | pub type Action = Rc<dyn Fn(&mut Window, &mut App)>; |
| 29 | pub type OpenChange = Rc<dyn Fn(bool, &mut Window, &mut App)>; |
| 30 | pub type HoverChange = Rc<dyn Fn(bool, &mut App)>; |
| 31 | |
| 32 | #[derive(Clone)] |
| 33 | pub struct DelegateEntry { |
| 34 | pub label: String, |
| 35 | pub agent_name: String, |
| 36 | pub action: Action, |
| 37 | } |
| 38 | |
| 39 | pub 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 | |
| 66 | fn element_id(prefix: &str, key: &str) -> ElementId { |
| 67 | ElementId::Name(format!("{prefix}-{key}").into()) |
| 68 | } |
| 69 | |
| 70 | fn 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 | |
| 77 | pub(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 | |
| 89 | fn 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 | fn picker_button(menu: &MessageMenu, cx: &App) -> AnyElement { |
| 105 | let (picker, picker_reset, react, done, mine, pin) = ( |
| 106 | menu.picker.clone(), |
| 107 | menu.picker.clone(), |
| 108 | menu.react.clone(), |
| 109 | menu.done.clone(), |
| 110 | menu.mine.clone(), |
| 111 | menu.pin.clone(), |
| 112 | ); |
| 113 | Popover::new(element_id("reaction-picker", &menu.key)) |
| 114 | .anchor(Anchor::TopRight) |
| 115 | .trigger( |
| 116 | Button::new(element_id("add-reaction", &menu.key)) |
| 117 | .child(icon(IconName::Plus, BAR_ICON, theme::text_soft())) |
| 118 | .custom(bar_variant(cx)) |
| 119 | .rounded(px(7.)) |
| 120 | .size(px(BAR_BUTTON)), |
| 121 | ) |
| 122 | .on_open_change(move |open, window, cx| { |
| 123 | pin(*open, window, cx); |
| 124 | if *open { |
| 125 | picker_reset.update(cx, |picker, cx| picker.reset(window, cx)); |
| 126 | } |
| 127 | }) |
| 128 | .content(move |_, _, cx| { |
| 129 | let popover = cx.entity().downgrade(); |
| 130 | picker.update(cx, |picker, _| { |
| 131 | picker.set_target(react.clone(), done.clone(), popover, mine.clone()) |
| 132 | }); |
| 133 | picker.clone() |
| 134 | }) |
| 135 | .into_any_element() |
| 136 | } |
| 137 | |
| 138 | fn more_button(menu: &MessageMenu, cx: &App) -> AnyElement { |
| 139 | let ( |
| 140 | reply, |
| 141 | forward, |
| 142 | copy_link, |
| 143 | copy, |
| 144 | translate, |
| 145 | save, |
| 146 | toggle_pinned, |
| 147 | edit, |
| 148 | delete, |
| 149 | mark_unread, |
| 150 | refresh_card, |
| 151 | pin, |
| 152 | ) = ( |
| 153 | menu.reply.clone(), |
| 154 | menu.forward.clone(), |
| 155 | menu.copy_link.clone(), |
| 156 | menu.copy.clone(), |
| 157 | menu.translate.clone(), |
| 158 | menu.save.clone(), |
| 159 | menu.toggle_pinned.clone(), |
| 160 | menu.edit.clone(), |
| 161 | menu.delete.clone(), |
| 162 | menu.mark_unread.clone(), |
| 163 | menu.refresh_card.clone(), |
| 164 | menu.pin.clone(), |
| 165 | ); |
| 166 | let (saved, pinned, translate_label) = (menu.saved, menu.pinned, menu.translate_label); |
| 167 | Button::new(element_id("message-more", &menu.key)) |
| 168 | .child(icon(IconName::Ellipsis, BAR_ICON, theme::text_soft())) |
| 169 | .custom(bar_variant(cx)) |
| 170 | .rounded(px(7.)) |
| 171 | .size(px(BAR_BUTTON)) |
| 172 | .dropdown_menu_with_anchor(Anchor::TopRight, move |popup, _, _| { |
| 173 | let item = |label: &'static str, glyph: IconName, action: Action| { |
| 174 | PopupMenuItem::new(label) |
| 175 | .icon(glyph) |
| 176 | .on_click(move |_, window, cx| action(window, cx)) |
| 177 | }; |
| 178 | let mut popup = popup; |
| 179 | if let Some(reply) = reply.clone() { |
| 180 | popup = popup.item(item("Reply with quote", IconName::Reply, reply)); |
| 181 | } |
| 182 | if let Some(forward) = forward.clone() { |
| 183 | popup = popup.item(item("Forward", IconName::Forward, forward)); |
| 184 | } |
| 185 | if let Some(copy_link) = copy_link.clone() { |
| 186 | popup = popup.item(item("Copy link", IconName::Link, copy_link)); |
| 187 | } |
| 188 | if let Some(refresh_card) = refresh_card.clone() { |
| 189 | popup = popup.item(item("Refresh card", IconName::RefreshCw, refresh_card)); |
| 190 | } |
| 191 | popup = popup.item(item("Copy text", IconName::Copy, copy.clone())); |
| 192 | if let Some(translate) = translate.clone() { |
| 193 | popup = popup.item(item(translate_label, IconName::Languages, translate)); |
| 194 | } |
| 195 | if let Some(save) = save.clone() { |
| 196 | popup = popup.item(if saved { |
| 197 | item("Unsave message", IconName::BookmarkOff, save) |
| 198 | } else { |
| 199 | item("Save message", IconName::Bookmark, save) |
| 200 | }); |
| 201 | } |
| 202 | if let Some(toggle_pinned) = toggle_pinned.clone() { |
| 203 | popup = popup.item(if pinned { |
| 204 | item("Unpin", IconName::PinOff, toggle_pinned) |
| 205 | } else { |
| 206 | item("Pin", IconName::Pin, toggle_pinned) |
| 207 | }); |
| 208 | } |
| 209 | if let Some(edit) = edit.clone() { |
| 210 | popup = popup.item(item("Edit", IconName::Pencil, edit)); |
| 211 | } |
| 212 | if let Some(delete) = delete.clone() { |
| 213 | popup = popup |
| 214 | .separator() |
| 215 | .item(item("Delete", IconName::Trash, delete)); |
| 216 | } |
| 217 | if let Some(mark_unread) = mark_unread.clone() { |
| 218 | let entry = item("Mark as unread", IconName::EyeOff, mark_unread); |
| 219 | popup = popup.separator().item(entry); |
| 220 | } |
| 221 | popup |
| 222 | }) |
| 223 | .on_open_change(move |open, window, cx| pin(*open, window, cx)) |
| 224 | .into_any_element() |
| 225 | } |
| 226 | |
| 227 | fn delegate_item(entry: &DelegateEntry) -> PopupMenuItem { |
| 228 | let (label, agent_name, action) = ( |
| 229 | entry.label.clone(), |
| 230 | entry.agent_name.clone(), |
| 231 | entry.action.clone(), |
| 232 | ); |
| 233 | PopupMenuItem::element(move |_, _| { |
| 234 | h_flex() |
| 235 | .w(px(DELEGATE_ITEM_WIDTH)) |
| 236 | .gap(px(12.)) |
| 237 | .items_center() |
| 238 | .justify_between() |
| 239 | .child(div().flex_1().min_w_0().truncate().child(label.clone())) |
| 240 | .child( |
| 241 | div() |
| 242 | .flex_none() |
| 243 | .text_size(px(DELEGATE_AGENT_SIZE)) |
| 244 | .text_color(theme::text_faint()) |
| 245 | .child(agent_name.clone()), |
| 246 | ) |
| 247 | }) |
| 248 | .icon(IconName::Bot) |
| 249 | .on_click(move |_, window, cx| action(window, cx)) |
| 250 | } |
| 251 | |
| 252 | fn delegate_button(menu: &MessageMenu, cx: &App) -> AnyElement { |
| 253 | let (entries, manage_agents, pin) = ( |
| 254 | menu.delegate.clone(), |
| 255 | menu.manage_agents.clone(), |
| 256 | menu.pin.clone(), |
| 257 | ); |
| 258 | Button::new(element_id("message-delegate", &menu.key)) |
| 259 | .child(icon(IconName::Bot, BAR_ICON, theme::text_soft())) |
| 260 | .custom(bar_variant(cx)) |
| 261 | .rounded(px(7.)) |
| 262 | .size(px(BAR_BUTTON)) |
| 263 | .dropdown_menu_with_anchor(Anchor::TopRight, move |popup, _, _| { |
| 264 | let mut popup = popup; |
| 265 | for entry in &entries { |
| 266 | popup = popup.item(delegate_item(entry)); |
| 267 | } |
| 268 | if let Some(manage_agents) = manage_agents.clone() { |
| 269 | popup = popup.separator().item( |
| 270 | PopupMenuItem::element(|_, _| { |
| 271 | div() |
| 272 | .text_color(theme::text_muted()) |
| 273 | .child("Manage actions...") |
| 274 | }) |
| 275 | .on_click(move |_, window, cx| manage_agents(window, cx)), |
| 276 | ); |
| 277 | } |
| 278 | popup |
| 279 | }) |
| 280 | .on_open_change(move |open, window, cx| pin(*open, window, cx)) |
| 281 | .into_any_element() |
| 282 | } |
| 283 | |
| 284 | pub fn message_toolbar(menu: MessageMenu, cx: &App) -> AnyElement { |
| 285 | let hover = menu.hover.clone(); |
| 286 | let mut children: Vec<AnyElement> = menu |
| 287 | .quick |
| 288 | .iter() |
| 289 | .enumerate() |
| 290 | .map(|(index, glyph)| reaction_button(&menu, glyph, index)) |
| 291 | .collect(); |
| 292 | children.push(picker_button(&menu, cx)); |
| 293 | children.push( |
| 294 | div() |
| 295 | .w(px(1.)) |
| 296 | .h(px(16.)) |
| 297 | .mx(px(4.)) |
| 298 | .flex_none() |
| 299 | .bg(theme::popup_hover()) |
| 300 | .into_any_element(), |
| 301 | ); |
| 302 | if let Some(reply) = menu.reply.clone() { |
| 303 | children.push( |
| 304 | bar_button(element_id("toolbar-reply", &menu.key)) |
| 305 | .child(icon(IconName::Reply, 16., theme::text_soft())) |
| 306 | .on_click(move |_, window, cx| { |
| 307 | cx.stop_propagation(); |
| 308 | reply(window, cx); |
| 309 | }) |
| 310 | .into_any_element(), |
| 311 | ); |
| 312 | } |
| 313 | if !menu.delegate.is_empty() { |
| 314 | children.push(delegate_button(&menu, cx)); |
| 315 | } |
| 316 | children.push(more_button(&menu, cx)); |
| 317 | let bar = h_flex() |
| 318 | .h(px(TOOLBAR_HEIGHT)) |
| 319 | .p(px(BAR_PADDING)) |
| 320 | .gap(px(1.)) |
| 321 | .items_center() |
| 322 | .rounded(px(10.)) |
| 323 | .bg(theme::popup()) |
| 324 | .on_mouse_down(MouseButton::Left, |_, _, cx| { |
| 325 | GlobalState::suppress_text_selection(cx) |
| 326 | }) |
| 327 | .children(children); |
| 328 | toolbar_slot(element_id("message-toolbar", &menu.key), hover, bar).into_any_element() |
| 329 | } |