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