| 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 | Sizable as _, |
| 8 | button::{Button, ButtonVariants as _}, |
| 9 | h_flex, |
| 10 | menu::{DropdownMenu as _, PopupMenuItem}, |
| 11 | popover::Popover, |
| 12 | }; |
| 13 | use gpui_kit::prelude::FluentBuilder as _; |
| 14 | use gpui_kit::*; |
| 15 | |
| 16 | use super::reaction_picker::{DoneHandler, PickHandler, ReactionPicker}; |
| 17 | use super::widgets::icon; |
| 18 | use crate::rows::reaction_glyph; |
| 19 | use crate::theme; |
| 20 | |
| 21 | const BAR_BUTTON: f32 = 28.; |
| 22 | pub const QUICK_REACTION_COUNT: usize = 4; |
| 23 | |
| 24 | pub type Action = Rc<dyn Fn(&mut Window, &mut App)>; |
| 25 | pub type OpenChange = Rc<dyn Fn(bool, &mut Window, &mut App)>; |
| 26 | pub type HoverChange = Rc<dyn Fn(bool, &mut App)>; |
| 27 | |
| 28 | pub struct MessageMenu { |
| 29 | pub key: String, |
| 30 | pub react: PickHandler, |
| 31 | pub reply: Option<Action>, |
| 32 | pub copy: Action, |
| 33 | pub edit: Option<Action>, |
| 34 | pub delete: Option<Action>, |
| 35 | pub pin: OpenChange, |
| 36 | pub hover: HoverChange, |
| 37 | pub picker: Entity<ReactionPicker>, |
| 38 | pub quick: Vec<String>, |
| 39 | pub mine: HashSet<String>, |
| 40 | pub done: DoneHandler, |
| 41 | } |
| 42 | |
| 43 | fn element_id(prefix: &str, key: &str) -> ElementId { |
| 44 | ElementId::Name(format!("{prefix}-{key}").into()) |
| 45 | } |
| 46 | |
| 47 | fn bar_button(id: ElementId) -> Stateful<Div> { |
| 48 | div() |
| 49 | .id(id) |
| 50 | .size(px(BAR_BUTTON)) |
| 51 | .flex() |
| 52 | .items_center() |
| 53 | .justify_center() |
| 54 | .rounded(px(6.)) |
| 55 | .cursor_pointer() |
| 56 | .hover(|button| button.bg(theme::border_strong())) |
| 57 | } |
| 58 | |
| 59 | fn reaction_button(menu: &MessageMenu, glyph: &str, index: usize) -> AnyElement { |
| 60 | let (react, done, picked) = (menu.react.clone(), menu.done.clone(), glyph.to_owned()); |
| 61 | let selected = menu.mine.contains(&reaction_glyph(glyph)); |
| 62 | bar_button(element_id(&format!("quick-reaction-{index}"), &menu.key)) |
| 63 | .text_size(px(17.)) |
| 64 | .when(selected, |button| button.bg(theme::reaction_on_own())) |
| 65 | .child(glyph.to_owned()) |
| 66 | .on_click(move |_, window, cx| { |
| 67 | cx.stop_propagation(); |
| 68 | react(&picked, window, cx); |
| 69 | done(window, cx); |
| 70 | }) |
| 71 | .into_any_element() |
| 72 | } |
| 73 | |
| 74 | fn picker_button(menu: &MessageMenu) -> AnyElement { |
| 75 | let (picker, picker_reset, react, done, mine, pin) = ( |
| 76 | menu.picker.clone(), |
| 77 | menu.picker.clone(), |
| 78 | menu.react.clone(), |
| 79 | menu.done.clone(), |
| 80 | menu.mine.clone(), |
| 81 | menu.pin.clone(), |
| 82 | ); |
| 83 | Popover::new(element_id("reaction-picker", &menu.key)) |
| 84 | .anchor(Anchor::TopRight) |
| 85 | .trigger( |
| 86 | Button::new(element_id("add-reaction", &menu.key)) |
| 87 | .icon(IconName::Plus) |
| 88 | .ghost() |
| 89 | .xsmall() |
| 90 | .size(px(BAR_BUTTON)), |
| 91 | ) |
| 92 | .on_open_change(move |open, window, cx| { |
| 93 | pin(*open, window, cx); |
| 94 | if *open { |
| 95 | picker_reset.update(cx, |picker, cx| picker.reset(window, cx)); |
| 96 | } |
| 97 | }) |
| 98 | .content(move |_, _, cx| { |
| 99 | let popover = cx.entity().downgrade(); |
| 100 | picker.update(cx, |picker, _| { |
| 101 | picker.set_target(react.clone(), done.clone(), popover, mine.clone()) |
| 102 | }); |
| 103 | picker.clone() |
| 104 | }) |
| 105 | .into_any_element() |
| 106 | } |
| 107 | |
| 108 | fn more_button(menu: &MessageMenu) -> AnyElement { |
| 109 | let (reply, copy, edit, delete, pin) = ( |
| 110 | menu.reply.clone(), |
| 111 | menu.copy.clone(), |
| 112 | menu.edit.clone(), |
| 113 | menu.delete.clone(), |
| 114 | menu.pin.clone(), |
| 115 | ); |
| 116 | Button::new(element_id("message-more", &menu.key)) |
| 117 | .icon(IconName::Ellipsis) |
| 118 | .ghost() |
| 119 | .xsmall() |
| 120 | .size(px(BAR_BUTTON)) |
| 121 | .dropdown_menu_with_anchor(Anchor::TopRight, move |popup, _, _| { |
| 122 | let item = |label: &'static str, glyph: IconName, action: Action| { |
| 123 | PopupMenuItem::new(label) |
| 124 | .icon(glyph) |
| 125 | .on_click(move |_, window, cx| action(window, cx)) |
| 126 | }; |
| 127 | let mut popup = popup; |
| 128 | if let Some(reply) = reply.clone() { |
| 129 | popup = popup.item(item("Reply with quote", IconName::Reply, reply)); |
| 130 | } |
| 131 | popup = popup.item(item("Copy text", IconName::Copy, copy.clone())); |
| 132 | if let Some(edit) = edit.clone() { |
| 133 | popup = popup.item(item("Edit", IconName::Pencil, edit)); |
| 134 | } |
| 135 | if let Some(delete) = delete.clone() { |
| 136 | popup = popup |
| 137 | .separator() |
| 138 | .item(item("Delete", IconName::Trash, delete)); |
| 139 | } |
| 140 | popup |
| 141 | }) |
| 142 | .on_open_change(move |open, window, cx| pin(*open, window, cx)) |
| 143 | .into_any_element() |
| 144 | } |
| 145 | |
| 146 | pub fn message_toolbar(menu: MessageMenu) -> AnyElement { |
| 147 | let hover = menu.hover.clone(); |
| 148 | let mut children: Vec<AnyElement> = menu |
| 149 | .quick |
| 150 | .iter() |
| 151 | .enumerate() |
| 152 | .map(|(index, glyph)| reaction_button(&menu, glyph, index)) |
| 153 | .collect(); |
| 154 | children.push(picker_button(&menu)); |
| 155 | children.push( |
| 156 | div() |
| 157 | .w(px(1.)) |
| 158 | .h(px(18.)) |
| 159 | .mx(px(2.)) |
| 160 | .bg(theme::border_strong()) |
| 161 | .into_any_element(), |
| 162 | ); |
| 163 | if let Some(reply) = menu.reply.clone() { |
| 164 | children.push( |
| 165 | bar_button(element_id("toolbar-reply", &menu.key)) |
| 166 | .child(icon(IconName::Reply, 16., theme::text_soft())) |
| 167 | .on_click(move |_, window, cx| { |
| 168 | cx.stop_propagation(); |
| 169 | reply(window, cx); |
| 170 | }) |
| 171 | .into_any_element(), |
| 172 | ); |
| 173 | } |
| 174 | children.push(more_button(&menu)); |
| 175 | h_flex() |
| 176 | .id(element_id("message-toolbar", &menu.key)) |
| 177 | .p(px(2.)) |
| 178 | .gap(px(2.)) |
| 179 | .items_center() |
| 180 | .rounded(px(8.)) |
| 181 | .bg(theme::surface()) |
| 182 | .border_1() |
| 183 | .border_color(theme::border_strong()) |
| 184 | .occlude() |
| 185 | .on_hover(move |hovered, _, cx| hover(*hovered, cx)) |
| 186 | .on_mouse_down(MouseButton::Left, |_, _, cx| { |
| 187 | GlobalState::suppress_text_selection(cx) |
| 188 | }) |
| 189 | .children(children) |
| 190 | .into_any_element() |
| 191 | } |