BlueGit

ict rusty-teams

Files

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

delivery.rs

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