BlueGit

ict rusty-teams

Files

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

plus_menu.rs

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