BlueGit

ict rusty-teams

Files

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

delivery.rs

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