| 1 | use gpui_kit::component::fluent::{ |
| 2 | MENU_ICON_LABEL_GAP, MENU_ICON_SLOT, MENU_ITEM_GAP, MENU_ITEM_HEIGHT, MENU_ITEM_PADDING_X, |
| 3 | MENU_LIST_PADDING, |
| 4 | }; |
| 5 | use gpui_kit::component::tooltip::PopupTriggerTooltip as _; |
| 6 | use gpui_kit::component::{h_flex, v_flex}; |
| 7 | use gpui_kit::prelude::FluentBuilder as _; |
| 8 | use gpui_kit::*; |
| 9 | |
| 10 | use super::{COMPOSER_BUTTON_SIZE, COMPOSER_ICON_SIZE, Composer}; |
| 11 | use crate::app_state::AppState; |
| 12 | use crate::delivery_options::DELIVERY_BUTTON_TITLE; |
| 13 | use crate::theme::{self, ElevatedSurface as _}; |
| 14 | use crate::views::new_chat::Pick; |
| 15 | use crate::views::new_meeting_dialog::open_new_meeting_with; |
| 16 | use crate::views::widgets::symbol; |
| 17 | |
| 18 | pub const COMPOSER_PLUS_TAG: &str = "composer/actions/plus"; |
| 19 | pub const PLUS_MENU_TAG: &str = "composer-plus-menu"; |
| 20 | const PLUS_TITLE: &str = "Actions and apps"; |
| 21 | const MENU_WIDTH: f32 = 260.; |
| 22 | const ITEM_ICON_SIZE: f32 = 20.; |
| 23 | |
| 24 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 25 | pub enum PlusAction { |
| 26 | ScheduleMessage, |
| 27 | DeliveryOptions, |
| 28 | ScheduleMeeting, |
| 29 | } |
| 30 | |
| 31 | impl PlusAction { |
| 32 | pub const ALL: [PlusAction; 3] = [ |
| 33 | PlusAction::ScheduleMessage, |
| 34 | PlusAction::DeliveryOptions, |
| 35 | PlusAction::ScheduleMeeting, |
| 36 | ]; |
| 37 | |
| 38 | pub fn label(self) -> &'static str { |
| 39 | match self { |
| 40 | PlusAction::ScheduleMessage => "Schedule message", |
| 41 | PlusAction::DeliveryOptions => DELIVERY_BUTTON_TITLE, |
| 42 | PlusAction::ScheduleMeeting => "Schedule meeting", |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | pub fn tag(self) -> &'static str { |
| 47 | match self { |
| 48 | PlusAction::ScheduleMessage => "composer-plus/schedule-message", |
| 49 | PlusAction::DeliveryOptions => "composer-plus/delivery-options", |
| 50 | PlusAction::ScheduleMeeting => "composer-plus/schedule-meeting", |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | fn symbol(self) -> &'static str { |
| 55 | match self { |
| 56 | PlusAction::ScheduleMessage => "schedule_send", |
| 57 | PlusAction::DeliveryOptions => "priority_high", |
| 58 | PlusAction::ScheduleMeeting => "calendar_add_on", |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | impl Composer { |
| 64 | pub(super) fn render_plus_button(&self, cx: &mut Context<Self>) -> Option<Stateful<Div>> { |
| 65 | if self.editing.is_some() { |
| 66 | return None; |
| 67 | } |
| 68 | let open = self.plus_menu; |
| 69 | let marked = self.importance.is_marked(); |
| 70 | Some( |
| 71 | div() |
| 72 | .id("composer-plus") |
| 73 | .debug_selector(|| COMPOSER_PLUS_TAG.into()) |
| 74 | .size(px(COMPOSER_BUTTON_SIZE)) |
| 75 | .flex_none() |
| 76 | .flex() |
| 77 | .items_center() |
| 78 | .justify_center() |
| 79 | .rounded(px(8.)) |
| 80 | .cursor_pointer() |
| 81 | .when(open, |button| button.bg(theme::popup_hover())) |
| 82 | .hover(|button| button.bg(theme::popup_hover())) |
| 83 | .popup_trigger_tooltip_text(open, PLUS_TITLE) |
| 84 | .child(symbol( |
| 85 | "add", |
| 86 | COMPOSER_ICON_SIZE, |
| 87 | if marked { |
| 88 | theme::red() |
| 89 | } else { |
| 90 | theme::text_muted() |
| 91 | }, |
| 92 | )) |
| 93 | .on_mouse_down(MouseButton::Left, |_, window, cx| { |
| 94 | window.prevent_default(); |
| 95 | cx.stop_propagation(); |
| 96 | }) |
| 97 | .on_click(cx.listener(|this, _, _, cx| { |
| 98 | this.plus_menu = !this.plus_menu; |
| 99 | this.delivery_menu = false; |
| 100 | cx.notify(); |
| 101 | })), |
| 102 | ) |
| 103 | } |
| 104 | |
| 105 | pub(super) fn render_plus_menu(&self, cx: &mut Context<Self>) -> Option<AnyElement> { |
| 106 | if !self.plus_menu || self.editing.is_some() { |
| 107 | return None; |
| 108 | } |
| 109 | let rows = PlusAction::ALL |
| 110 | .into_iter() |
| 111 | .map(|action| self.render_plus_item(action, cx)) |
| 112 | .collect::<Vec<_>>(); |
| 113 | Some( |
| 114 | v_flex() |
| 115 | .id(PLUS_MENU_TAG) |
| 116 | .debug_selector(|| PLUS_MENU_TAG.into()) |
| 117 | .absolute() |
| 118 | .bottom(relative(1.)) |
| 119 | .right_0() |
| 120 | .mb(px(6.)) |
| 121 | .w(px(MENU_WIDTH)) |
| 122 | .max_w(relative(1.)) |
| 123 | .p(MENU_LIST_PADDING) |
| 124 | .gap(MENU_ITEM_GAP) |
| 125 | .rounded(px(10.)) |
| 126 | .flyout_surface() |
| 127 | .occlude() |
| 128 | .on_mouse_down_out(cx.listener(|this, _, _, cx| { |
| 129 | this.plus_menu = false; |
| 130 | cx.notify(); |
| 131 | })) |
| 132 | .children(rows) |
| 133 | .into_any_element(), |
| 134 | ) |
| 135 | } |
| 136 | |
| 137 | fn render_plus_item(&self, action: PlusAction, cx: &mut Context<Self>) -> Stateful<Div> { |
| 138 | h_flex() |
| 139 | .id(action.label()) |
| 140 | .debug_selector(move || action.tag().into()) |
| 141 | .w_full() |
| 142 | .h(MENU_ITEM_HEIGHT) |
| 143 | .px(MENU_ITEM_PADDING_X) |
| 144 | .gap(MENU_ICON_LABEL_GAP) |
| 145 | .items_center() |
| 146 | .rounded(px(6.)) |
| 147 | .cursor_pointer() |
| 148 | .hover(|row| row.bg(theme::popup_hover())) |
| 149 | .child( |
| 150 | div() |
| 151 | .size(MENU_ICON_SLOT) |
| 152 | .flex_none() |
| 153 | .flex() |
| 154 | .items_center() |
| 155 | .justify_center() |
| 156 | .child(symbol(action.symbol(), ITEM_ICON_SIZE, theme::text_soft())), |
| 157 | ) |
| 158 | .child( |
| 159 | div() |
| 160 | .text_size(px(13.)) |
| 161 | .text_color(theme::text()) |
| 162 | .child(action.label()), |
| 163 | ) |
| 164 | .on_click( |
| 165 | cx.listener(move |this, _, window, cx| this.run_plus_action(action, window, cx)), |
| 166 | ) |
| 167 | } |
| 168 | |
| 169 | fn run_plus_action(&mut self, action: PlusAction, window: &mut Window, cx: &mut Context<Self>) { |
| 170 | self.plus_menu = false; |
| 171 | match action { |
| 172 | PlusAction::ScheduleMessage => self.open_schedule_picker(cx), |
| 173 | PlusAction::DeliveryOptions => self.delivery_menu = true, |
| 174 | PlusAction::ScheduleMeeting => { |
| 175 | let invitees = self |
| 176 | .conversation_id |
| 177 | .as_deref() |
| 178 | .map(|chat_id| chat_invitees(self.app.read(cx), chat_id)) |
| 179 | .unwrap_or_default(); |
| 180 | open_new_meeting_with(self.app.clone(), invitees, window, cx); |
| 181 | } |
| 182 | } |
| 183 | cx.notify(); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | pub(crate) fn chat_invitees(state: &AppState, chat_id: &str) -> Vec<Pick> { |
| 188 | let me = state.directory.me.as_ref().map(|me| me.user_id.as_str()); |
| 189 | let Some(chat) = state.sidebar.chats.iter().find(|chat| chat.id == chat_id) else { |
| 190 | return Vec::new(); |
| 191 | }; |
| 192 | chat.members |
| 193 | .iter() |
| 194 | .filter(|member| !member.is_bot()) |
| 195 | .filter_map(|member| { |
| 196 | let user_id = member.user_id.clone()?; |
| 197 | (Some(user_id.as_str()) != me).then(|| Pick { |
| 198 | user_id, |
| 199 | name: member.display_name.clone(), |
| 200 | mail: None, |
| 201 | }) |
| 202 | }) |
| 203 | .collect() |
| 204 | } |