BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / composer / plus_menu.rs

plus_menu.rs

plus_menu.rs 7.3 KiB Download Raw
1use 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};
5use gpui_kit::component::tooltip::PopupTriggerTooltip as _;
6use gpui_kit::component::{h_flex, v_flex};
7use gpui_kit::prelude::FluentBuilder as _;
8use gpui_kit::*;
9
10use super::{COMPOSER_BUTTON_SIZE, COMPOSER_ICON_SIZE, Composer};
11use crate::app_state::AppState;
12use crate::delivery_options::DELIVERY_BUTTON_TITLE;
13use crate::theme::{self, ElevatedSurface as _};
14use crate::views::new_chat::Pick;
15use crate::views::new_meeting_dialog::open_new_meeting_with;
16use crate::views::widgets::symbol;
17
18pub const COMPOSER_PLUS_TAG: &str = "composer/actions/plus";
19pub const PLUS_MENU_TAG: &str = "composer-plus-menu";
20const PLUS_TITLE: &str = "Actions and apps";
21const MENU_WIDTH: f32 = 260.;
22const ITEM_ICON_SIZE: f32 = 20.;
23
24#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25pub enum PlusAction {
26 ScheduleMessage,
27 DeliveryOptions,
28 ScheduleMeeting,
29}
30
31impl 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
63impl 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 .children(crate::kvt::plus_item(
134 cx.listener(|this, _, window, cx| {
135 this.plus_menu = false;
136 crate::kvt::open_picker(cx.entity().downgrade(), window, cx);
137 cx.notify();
138 }),
139 cx,
140 ))
141 .into_any_element(),
142 )
143 }
144
145 fn render_plus_item(&self, action: PlusAction, cx: &mut Context<Self>) -> Stateful<Div> {
146 h_flex()
147 .id(action.label())
148 .debug_selector(move || action.tag().into())
149 .w_full()
150 .h(MENU_ITEM_HEIGHT)
151 .px(MENU_ITEM_PADDING_X)
152 .gap(MENU_ICON_LABEL_GAP)
153 .items_center()
154 .rounded(px(6.))
155 .cursor_pointer()
156 .hover(|row| row.bg(theme::popup_hover()))
157 .child(
158 div()
159 .size(MENU_ICON_SLOT)
160 .flex_none()
161 .flex()
162 .items_center()
163 .justify_center()
164 .child(symbol(action.symbol(), ITEM_ICON_SIZE, theme::text_soft())),
165 )
166 .child(
167 div()
168 .text_size(px(13.))
169 .text_color(theme::text())
170 .child(action.label()),
171 )
172 .on_click(
173 cx.listener(move |this, _, window, cx| this.run_plus_action(action, window, cx)),
174 )
175 }
176
177 fn run_plus_action(&mut self, action: PlusAction, window: &mut Window, cx: &mut Context<Self>) {
178 self.plus_menu = false;
179 match action {
180 PlusAction::ScheduleMessage => self.open_schedule_picker(cx),
181 PlusAction::DeliveryOptions => self.delivery_menu = true,
182 PlusAction::ScheduleMeeting => {
183 let invitees = self
184 .conversation_id
185 .as_deref()
186 .map(|chat_id| chat_invitees(self.app.read(cx), chat_id))
187 .unwrap_or_default();
188 open_new_meeting_with(self.app.clone(), invitees, window, cx);
189 }
190 }
191 cx.notify();
192 }
193}
194
195pub(crate) fn chat_invitees(state: &AppState, chat_id: &str) -> Vec<Pick> {
196 let me = state.directory.me.as_ref().map(|me| me.user_id.as_str());
197 let Some(chat) = state.sidebar.chats.iter().find(|chat| chat.id == chat_id) else {
198 return Vec::new();
199 };
200 chat.members
201 .iter()
202 .filter(|member| !member.is_bot())
203 .filter_map(|member| {
204 let user_id = member.user_id.clone()?;
205 (Some(user_id.as_str()) != me).then(|| Pick {
206 user_id,
207 name: member.display_name.clone(),
208 mail: None,
209 organization: None,
210 })
211 })
212 .collect()
213}