BlueGit

ict rusty-teams

Files

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

delivery.rs

delivery.rs 9.6 KiB Download Raw
1use gpui_kit::component::{h_flex, tooltip::Tooltip, v_flex};
2use gpui_kit::prelude::FluentBuilder as _;
3use gpui_kit::*;
4use store::MessageImportance;
5
6use super::{COMPOSER_BUTTON_INSET, COMPOSER_BUTTON_SIZE, COMPOSER_ICON_SIZE, Composer};
7use crate::delivery_options::{
8 DELIVERY_BUTTON_TITLE, DeliveryOption, URGENT_TOO_MANY_MEMBERS, UrgentAvailability,
9 allowed_importance, delivery_options, urgent_banner,
10};
11use crate::importance::importance_label;
12use crate::theme::{self, ElevatedSurface as _};
13use crate::views::widgets::symbol;
14
15pub const COMPOSER_DELIVERY_TAG: &str = "composer/leading/delivery";
16pub const COMPOSER_IMPORTANCE_LABEL_TAG: &str = "composer/importance-label";
17pub const COMPOSER_URGENT_BANNER_TAG: &str = "composer-urgent-banner";
18pub const DELIVERY_MENU_TAG: &str = "delivery-menu";
19const MENU_WIDTH: f32 = 320.;
20const MENU_PADDING: f32 = 4.;
21const ROW_PADDING: f32 = 10.;
22const OPTION_ICON_SIZE: f32 = 20.;
23const CHECK_SIZE: f32 = 16.;
24const DISABLED_OPACITY: f32 = 0.4;
25const LABEL_SIZE: f32 = 12.;
26const LABEL_LINE_HEIGHT: f32 = 16.;
27const BANNER_HEIGHT: f32 = 36.;
28const BANNER_PADDING: f32 = 12.;
29const BANNER_TINT: f32 = 0.14;
30pub const LABEL_INSET_X: f32 =
31 COMPOSER_BUTTON_INSET + (COMPOSER_BUTTON_SIZE - COMPOSER_ICON_SIZE) / 2.;
32
33impl Composer {
34 fn urgent_availability(&self, cx: &App) -> UrgentAvailability {
35 let conversation_id = self.conversation_id.as_deref().unwrap_or_default();
36 self.app
37 .read(cx)
38 .urgent_availability(conversation_id, self.editing.is_some())
39 }
40
41 /// Teams sends edits and scheduled messages as Standard, and drops Urgent where it is no longer offered.
42 pub(super) fn sendable_importance(&self, cx: &App) -> MessageImportance {
43 if self.editing.is_some() {
44 return MessageImportance::Normal;
45 }
46 allowed_importance(self.importance, self.urgent_availability(cx))
47 }
48
49 pub(super) fn reset_importance(&mut self) {
50 self.importance = MessageImportance::Normal;
51 self.delivery_menu = false;
52 }
53
54 fn choose_importance(
55 &mut self,
56 importance: MessageImportance,
57 window: &mut Window,
58 cx: &mut Context<Self>,
59 ) {
60 self.importance = importance;
61 self.delivery_menu = false;
62 self.focus(window, cx);
63 cx.notify();
64 }
65
66 pub(super) fn render_delivery_button(&self, cx: &mut Context<Self>) -> Option<Stateful<Div>> {
67 if self.editing.is_some() {
68 return None;
69 }
70 let open = self.delivery_menu;
71 let marked = self.importance.is_marked();
72 Some(
73 div()
74 .id("composer-delivery")
75 .debug_selector(|| COMPOSER_DELIVERY_TAG.into())
76 .size(px(COMPOSER_BUTTON_SIZE))
77 .flex_none()
78 .flex()
79 .items_center()
80 .justify_center()
81 .rounded(px(8.))
82 .cursor_pointer()
83 .when(open, |button| button.bg(theme::popup_hover()))
84 .hover(|button| button.bg(theme::popup_hover()))
85 .tooltip(|window, cx| Tooltip::new(DELIVERY_BUTTON_TITLE).build(window, cx))
86 .child(symbol(
87 "fluent_important",
88 COMPOSER_ICON_SIZE,
89 if marked {
90 theme::red()
91 } else {
92 theme::text_muted()
93 },
94 ))
95 .on_mouse_down(MouseButton::Left, |_, window, cx| {
96 window.prevent_default();
97 cx.stop_propagation();
98 })
99 .on_click(cx.listener(|this, _, _, cx| {
100 this.delivery_menu = !this.delivery_menu;
101 cx.notify();
102 })),
103 )
104 }
105
106 pub(super) fn render_delivery_menu(&self, cx: &mut Context<Self>) -> Option<AnyElement> {
107 if !self.delivery_menu || self.editing.is_some() {
108 return None;
109 }
110 let selected = self.sendable_importance(cx);
111 let rows = delivery_options(self.urgent_availability(cx))
112 .into_iter()
113 .map(|(option, enabled)| {
114 self.render_option(option, enabled, option.importance == selected, cx)
115 })
116 .collect::<Vec<_>>();
117 Some(
118 v_flex()
119 .id("delivery-menu")
120 .debug_selector(|| DELIVERY_MENU_TAG.into())
121 .absolute()
122 .bottom(relative(1.))
123 .left_0()
124 .mb(px(6.))
125 .w(px(MENU_WIDTH))
126 .max_w(relative(1.))
127 .p(px(MENU_PADDING))
128 .rounded(px(10.))
129 .flyout_surface()
130 .occlude()
131 .on_mouse_down_out(cx.listener(|this, _, _, cx| {
132 this.delivery_menu = false;
133 cx.notify();
134 }))
135 .children(rows)
136 .into_any_element(),
137 )
138 }
139
140 fn render_option(
141 &self,
142 option: DeliveryOption,
143 enabled: bool,
144 selected: bool,
145 cx: &mut Context<Self>,
146 ) -> Stateful<Div> {
147 let accent = if option.importance.is_marked() {
148 theme::red()
149 } else {
150 theme::text_soft()
151 };
152 let row = h_flex()
153 .id(option.tag)
154 .debug_selector(|| option.tag.into())
155 .w_full()
156 .p(px(ROW_PADDING))
157 .gap(px(ROW_PADDING))
158 .items_center()
159 .rounded(px(6.))
160 .child(symbol(option.symbol, OPTION_ICON_SIZE, accent))
161 .child(
162 v_flex()
163 .flex_1()
164 .min_w_0()
165 .child(
166 div()
167 .text_size(px(13.))
168 .font_weight(FontWeight::SEMIBOLD)
169 .text_color(theme::text())
170 .child(option.header),
171 )
172 .child(
173 div()
174 .text_size(px(12.))
175 .text_color(theme::text_muted())
176 .child(option.content),
177 ),
178 )
179 .children(selected.then(|| symbol("done", CHECK_SIZE, theme::text())));
180 if !enabled {
181 return row
182 .opacity(DISABLED_OPACITY)
183 .tooltip(|window, cx| Tooltip::new(URGENT_TOO_MANY_MEMBERS).build(window, cx));
184 }
185 let importance = option.importance;
186 row.cursor_pointer()
187 .hover(|row| row.bg(theme::popup_hover()))
188 .on_click(cx.listener(move |this, _, window, cx| {
189 this.choose_importance(importance, window, cx)
190 }))
191 }
192
193 pub(super) fn render_importance_label(&self, cx: &App) -> Option<Div> {
194 let label = importance_label(self.sendable_importance(cx))?;
195 Some(
196 div()
197 .pt(px(COMPOSER_BUTTON_INSET * 2.))
198 .px(px(LABEL_INSET_X))
199 .text_size(px(LABEL_SIZE))
200 .line_height(px(LABEL_LINE_HEIGHT))
201 .font_weight(FontWeight::BOLD)
202 .text_color(theme::red())
203 .child(
204 div()
205 .debug_selector(|| COMPOSER_IMPORTANCE_LABEL_TAG.into())
206 .child(label),
207 ),
208 )
209 }
210
211 pub(super) fn render_urgent_banner(&self, cx: &mut Context<Self>) -> Option<Div> {
212 if self.sendable_importance(cx) != MessageImportance::Urgent {
213 return None;
214 }
215 let member_count = self
216 .conversation_id
217 .as_deref()
218 .and_then(|id| {
219 self.app
220 .read(cx)
221 .sidebar
222 .chats
223 .iter()
224 .find(|chat| chat.id == id)
225 .map(|chat| chat.members.len())
226 })
227 .unwrap_or_default();
228 Some(
229 h_flex()
230 .debug_selector(|| COMPOSER_URGENT_BANNER_TAG.into())
231 .w_full()
232 .h(px(BANNER_HEIGHT))
233 .mb(px(8.))
234 .px(px(BANNER_PADDING))
235 .gap(px(8.))
236 .items_center()
237 .rounded(px(10.))
238 .bg(theme::red().opacity(BANNER_TINT))
239 .text_size(px(12.))
240 .text_color(theme::red_tint())
241 .child(symbol("fluent_alert_urgent", CHECK_SIZE, theme::red_tint()))
242 .child(
243 div()
244 .flex_1()
245 .min_w_0()
246 .truncate()
247 .child(urgent_banner(member_count)),
248 )
249 .child(
250 div()
251 .id("composer-urgent-banner-close")
252 .size(px(24.))
253 .flex_none()
254 .flex()
255 .items_center()
256 .justify_center()
257 .rounded(px(6.))
258 .cursor_pointer()
259 .hover(|button| button.bg(theme::red().opacity(BANNER_TINT)))
260 .tooltip(|window, cx| Tooltip::new("Send as standard").build(window, cx))
261 .child(symbol("close", CHECK_SIZE, theme::red_tint()))
262 .on_click(cx.listener(|this, _, window, cx| {
263 this.choose_importance(MessageImportance::Normal, window, cx)
264 })),
265 ),
266 )
267 }
268}