BlueGit

ict rusty-teams

Files

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

format_menus.rs

format_menus.rs 15.3 KiB Download Raw
1use std::ops::Range;
2
3use gpui_kit::component::{h_flex, tooltip::Tooltip, v_flex};
4use gpui_kit::prelude::FluentBuilder as _;
5use gpui_kit::*;
6use teams_core::{Draft, FormatState, LineKind, MarkKind, SizeStep, TypingStyle};
7
8use super::Composer;
9use super::FormatButton;
10use crate::theme::{self, ElevatedSurface as _};
11
12const SWATCH: f32 = 24.;
13const SWATCH_GAP: f32 = 6.;
14const SWATCH_COLUMNS: usize = 4;
15const POPUP_PADDING: f32 = 8.;
16const MENU_WIDTH: f32 = 180.;
17const FONT_SIZE_MENU_WIDTH: f32 = 120.;
18const MENU_PADDING: f32 = 4.;
19const MENU_ROW_PADDING_X: f32 = 10.;
20const MENU_ROW_PADDING_Y: f32 = 6.;
21const STYLE_SIZES: [f32; 4] = [13., 20., 17., 15.];
22
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24pub(super) enum FormatPopup {
25 Highlight,
26 FontColor,
27 ParagraphStyle,
28 FontSize,
29}
30
31pub(super) struct Swatch {
32 pub(super) name: &'static str,
33 pub(super) color: u32,
34}
35
36/// Teams' font colour palette (`categoryColors`, shades in the compose bundle).
37pub(super) const FONT_COLORS: [Swatch; 8] = [
38 Swatch { name: "Red", color: 0xB6424C },
39 Swatch { name: "Tuscan orange", color: 0xCD5937 },
40 Swatch { name: "Sunglow", color: 0xFDC030 },
41 Swatch { name: "Pear", color: 0xBDCB4C },
42 Swatch { name: "Eucalyptus", color: 0x2B9B62 },
43 Swatch { name: "Faded jade", color: 0x37797B },
44 Swatch { name: "Fun blue", color: 0x1E53A3 },
45 Swatch { name: "Tyrian purple", color: 0xA5397A },
46];
47
48pub(super) const HIGHLIGHT_COLORS: [Swatch; 8] = [
49 Swatch { name: "Kobi red", color: 0xDF9299 },
50 Swatch { name: "Rose bud", color: 0xF4A593 },
51 Swatch { name: "Goldenrod", color: 0xFDD472 },
52 Swatch { name: "Primrose", color: 0xE5F18F },
53 Swatch { name: "Mantis", color: 0x82CDA8 },
54 Swatch { name: "Regent St. blue", color: 0x9DD9DB },
55 Swatch { name: "Periwinkle grey", color: 0xC7D4E8 },
56 Swatch { name: "Pale rose", color: 0xEBD3E1 },
57];
58
59pub(super) const PARAGRAPH_STYLES: [(Option<u8>, &str); 4] = [
60 (None, "Paragraph"),
61 (Some(1), "Heading 1"),
62 (Some(2), "Heading 2"),
63 (Some(3), "Heading 3"),
64];
65
66/// Teams' Font size menu (`compose_font_size`): Large is `x-large`, Medium removes the size,
67/// Small is `xx-small`.
68pub(super) const FONT_SIZES: [(Option<SizeStep>, FormatButton, &str); 3] = [
69 (Some(SizeStep::Large), FormatButton::SizeLarge, "Large"),
70 (None, FormatButton::SizeNormal, "Medium"),
71 (Some(SizeStep::Small), FormatButton::SizeSmall, "Small"),
72];
73
74pub(super) fn paragraph_label(level: Option<u8>) -> &'static str {
75 PARAGRAPH_STYLES
76 .iter()
77 .find(|(style, _)| *style == level)
78 .map_or(PARAGRAPH_STYLES[0].1, |(_, label)| label)
79}
80
81impl FormatPopup {
82 fn palette(self) -> &'static [Swatch] {
83 match self {
84 FormatPopup::FontColor => &FONT_COLORS,
85 _ => &HIGHLIGHT_COLORS,
86 }
87 }
88
89 fn clear_label(self) -> &'static str {
90 match self {
91 FormatPopup::FontColor => "Automatic",
92 _ => "No highlight",
93 }
94 }
95
96 fn marker(self) -> MarkKind {
97 match self {
98 FormatPopup::FontColor => MarkKind::Color(0),
99 _ => MarkKind::Highlight(0),
100 }
101 }
102}
103
104fn swallow_mouse_down<E: InteractiveElement>(element: E) -> E {
105 element.on_mouse_down(MouseButton::Left, |_, window, cx| {
106 window.prevent_default();
107 cx.stop_propagation();
108 })
109}
110
111impl Composer {
112 pub(super) fn toggle_format_popup(&mut self, popup: FormatPopup, cx: &mut Context<Self>) {
113 self.table_picker = None;
114 self.table_menu = None;
115 self.format_popup = (self.format_popup != Some(popup)).then_some(popup);
116 self.toolbar_dismissed = Some(self.selection(cx));
117 cx.notify();
118 }
119
120 pub(super) fn toggle_format_overflow(&mut self, cx: &mut Context<Self>) {
121 self.table_picker = None;
122 self.table_menu = None;
123 self.format_popup = None;
124 self.format_overflow = !self.format_overflow;
125 cx.notify();
126 }
127
128 fn close_format_popups(&mut self) {
129 self.format_popup = None;
130 self.format_overflow = false;
131 }
132
133 fn apply_font_size(&mut self, size: Option<SizeStep>, cx: &mut Context<Self>) {
134 self.close_format_popups();
135 self.set_size(size, cx);
136 }
137
138 pub(super) fn render_overflow_surface(&self, menu: Div, cx: &mut Context<Self>) -> AnyElement {
139 menu.id("format-overflow-menu")
140 .rounded(px(10.))
141 .flyout_surface()
142 .occlude()
143 .on_mouse_down_out(cx.listener(|this, _, _, cx| {
144 let nested_open = this.format_popup.is_some()
145 || this.table_picker.is_some()
146 || this.table_menu.is_some();
147 if !nested_open {
148 this.format_overflow = false;
149 cx.notify();
150 }
151 }))
152 .into_any_element()
153 }
154
155 /// Text typed at a collapsed cursor takes the change, a selection gets it at once.
156 fn change_style(
157 &mut self,
158 typing: impl FnOnce(&mut TypingStyle, &[MarkKind]),
159 apply: impl FnOnce(&mut Draft, Range<usize>),
160 window: &mut Window,
161 cx: &mut Context<Self>,
162 ) {
163 let selection = self.selection(cx);
164 if selection.is_empty() {
165 let cursor = selection.start;
166 let mut pending = self
167 .pending_style
168 .take()
169 .filter(|pending| pending.at == cursor)
170 .unwrap_or(TypingStyle {
171 at: cursor,
172 ..TypingStyle::default()
173 });
174 typing(&mut pending, &self.draft.style_at(cursor));
175 self.pending_style = (!pending.is_empty()).then_some(pending);
176 } else {
177 apply(&mut self.draft, selection);
178 self.commit(None, window, cx);
179 }
180 cx.notify();
181 }
182
183 fn apply_color(
184 &mut self,
185 popup: FormatPopup,
186 color: Option<u32>,
187 window: &mut Window,
188 cx: &mut Context<Self>,
189 ) {
190 self.close_format_popups();
191 match popup {
192 FormatPopup::FontColor => self.change_style(
193 |pending, active| pending.set_color(color, active),
194 |draft, range| draft.set_color(range, color),
195 window,
196 cx,
197 ),
198 _ => self.change_style(
199 |pending, active| pending.set_highlight(color, active),
200 |draft, range| draft.set_highlight(range, color),
201 window,
202 cx,
203 ),
204 }
205 }
206
207 pub(super) fn clear_formatting(&mut self, window: &mut Window, cx: &mut Context<Self>) {
208 let selection = self.selection(cx);
209 if selection.is_empty() {
210 self.draft.clear_formatting(selection.clone());
211 self.commit(None, window, cx);
212 }
213 self.change_style(
214 |pending, active| pending.clear(active),
215 |draft, range| draft.clear_formatting(range),
216 window,
217 cx,
218 );
219 }
220
221 pub(super) fn set_paragraph_style(
222 &mut self,
223 level: Option<u8>,
224 window: &mut Window,
225 cx: &mut Context<Self>,
226 ) {
227 self.close_format_popups();
228 if self.cell_has_focus(window, cx) {
229 return;
230 }
231 let selection = self.selection(cx);
232 self.draft.set_heading(selection, level);
233 self.commit(None, window, cx);
234 cx.notify();
235 }
236
237 pub(super) fn indent_blocks(
238 &mut self,
239 outdent: bool,
240 window: &mut Window,
241 cx: &mut Context<Self>,
242 ) {
243 let selection = self.selection(cx);
244 self.draft.indent_blocks(selection, outdent);
245 self.commit(None, window, cx);
246 cx.notify();
247 }
248
249 pub(super) fn insert_rule(&mut self, window: &mut Window, cx: &mut Context<Self>) {
250 if self.cell_has_focus(window, cx) {
251 return;
252 }
253 let cursor = self.draft.insert_rule(self.selection(cx).end);
254 self.commit(Some(cursor), window, cx);
255 cx.notify();
256 }
257
258 pub(super) fn toggle_code_block(&mut self, window: &mut Window, cx: &mut Context<Self>) {
259 if self.cell_has_focus(window, cx) {
260 return;
261 }
262 self.toggle_lines(LineKind::Code(None), window, cx);
263 }
264
265 fn current_color(&self, popup: FormatPopup, cx: &App) -> Option<u32> {
266 let selection = self.selection(cx);
267 let marker = popup.marker();
268 if !selection.is_empty() {
269 return self.draft.color_in(selection, &marker);
270 }
271 let style = self.draft.style_at(selection.start);
272 let style = match self.pending_style.as_ref() {
273 Some(pending) if pending.at == selection.start => pending.apply(style),
274 _ => style,
275 };
276 style.into_iter().find_map(|kind| match kind {
277 MarkKind::Color(color) if marker.same_format(&kind) => Some(color),
278 MarkKind::Highlight(color) if marker.same_format(&kind) => Some(color),
279 _ => None,
280 })
281 }
282
283 pub(super) fn render_format_popup(
284 &self,
285 popup: FormatPopup,
286 cx: &mut Context<Self>,
287 ) -> Option<AnyElement> {
288 if self.format_popup != Some(popup) {
289 return None;
290 }
291 let content = match popup {
292 FormatPopup::ParagraphStyle => self.render_paragraph_menu(cx),
293 FormatPopup::FontSize => self.render_font_size_menu(cx),
294 _ => self.render_color_picker(popup, cx),
295 };
296 Some(
297 content
298 .id(ElementId::Name(format!("format-popup-{popup:?}").into()))
299 .rounded(px(10.))
300 .flyout_surface()
301 .occlude()
302 .on_mouse_down_out(cx.listener(|this, _, _, cx| {
303 this.format_popup = None;
304 cx.notify();
305 }))
306 .into_any_element(),
307 )
308 }
309
310 fn render_color_picker(&self, popup: FormatPopup, cx: &mut Context<Self>) -> Div {
311 let current = self.current_color(popup, cx);
312 let rows = popup.palette().chunks(SWATCH_COLUMNS).map(|row| {
313 h_flex().gap(px(SWATCH_GAP)).children(row.iter().map(|swatch| {
314 let color = swatch.color;
315 let name = swatch.name;
316 let selected = current == Some(color);
317 swallow_mouse_down(
318 div()
319 .id(ElementId::Name(format!("swatch-{popup:?}-{color:06x}").into()))
320 .size(px(SWATCH))
321 .rounded(px(SWATCH / 2.))
322 .bg(rgb(color))
323 .cursor_pointer()
324 .border_2()
325 .border_color(if selected {
326 theme::text()
327 } else {
328 transparent_black()
329 })
330 .hover(|swatch| swatch.opacity(0.85))
331 .tooltip(move |window, cx| Tooltip::new(name).build(window, cx)),
332 )
333 .on_click(cx.listener(move |this, _, window, cx| {
334 this.apply_color(popup, Some(color), window, cx)
335 }))
336 }))
337 });
338 let clear = swallow_mouse_down(
339 div()
340 .id(ElementId::Name(format!("swatch-{popup:?}-clear").into()))
341 .w_full()
342 .py(px(MENU_ROW_PADDING_Y))
343 .rounded(px(6.))
344 .flex()
345 .justify_center()
346 .text_size(px(12.))
347 .text_color(theme::text())
348 .cursor_pointer()
349 .hover(|row| row.bg(theme::popup_hover())),
350 )
351 .on_click(cx.listener(move |this, _, window, cx| {
352 this.apply_color(popup, None, window, cx)
353 }))
354 .child(popup.clear_label());
355 v_flex()
356 .p(px(POPUP_PADDING))
357 .gap(px(SWATCH_GAP))
358 .children(rows)
359 .child(clear)
360 }
361
362 fn render_font_size_menu(&self, cx: &mut Context<Self>) -> Div {
363 let selection = self.selection(cx);
364 let rows = FONT_SIZES.iter().map(|(size, button, label)| {
365 let size = *size;
366 let current = self.format_state(*button, &selection) == FormatState::On;
367 swallow_mouse_down(
368 div()
369 .id(ElementId::Name(format!("font-size-{label}").into()))
370 .px(px(MENU_ROW_PADDING_X))
371 .py(px(MENU_ROW_PADDING_Y))
372 .rounded(px(6.))
373 .text_size(px(13.))
374 .text_color(theme::text())
375 .when(current, |row| row.font_weight(FontWeight::BOLD))
376 .cursor_pointer()
377 .hover(|row| row.bg(theme::popup_hover())),
378 )
379 .on_click(cx.listener(move |this, _, _, cx| this.apply_font_size(size, cx)))
380 .child(*label)
381 });
382 v_flex()
383 .min_w(px(FONT_SIZE_MENU_WIDTH))
384 .p(px(MENU_PADDING))
385 .children(rows)
386 }
387
388 fn render_paragraph_menu(&self, cx: &mut Context<Self>) -> Div {
389 let current = self.draft.heading_at(self.selection(cx).start);
390 let rows = PARAGRAPH_STYLES
391 .iter()
392 .zip(STYLE_SIZES)
393 .map(|((level, label), size)| {
394 let level = *level;
395 swallow_mouse_down(
396 div()
397 .id(ElementId::Name(format!("paragraph-style-{level:?}").into()))
398 .px(px(MENU_ROW_PADDING_X))
399 .py(px(MENU_ROW_PADDING_Y))
400 .rounded(px(6.))
401 .text_size(px(size))
402 .text_color(theme::text())
403 .when(level.is_some(), |row| row.font_weight(FontWeight::BOLD))
404 .cursor_pointer()
405 .when(level == current, |row| row.bg(theme::popup_hover()))
406 .hover(|row| row.bg(theme::popup_hover())),
407 )
408 .on_click(cx.listener(move |this, _, window, cx| {
409 this.set_paragraph_style(level, window, cx)
410 }))
411 .child(*label)
412 });
413 v_flex()
414 .min_w(px(MENU_WIDTH))
415 .p(px(MENU_PADDING))
416 .children(rows)
417 }
418}
419
420#[cfg(test)]
421mod tests {
422 use teams_core::SizeStep;
423
424 use super::{FONT_COLORS, FONT_SIZES, HIGHLIGHT_COLORS, PARAGRAPH_STYLES, paragraph_label};
425
426 #[test]
427 fn font_sizes_read_like_teams() {
428 assert_eq!(FONT_SIZES.map(|(_, _, label)| label), ["Large", "Medium", "Small"]);
429 assert_eq!(
430 FONT_SIZES.map(|(size, _, _)| size),
431 [Some(SizeStep::Large), None, Some(SizeStep::Small)]
432 );
433 }
434
435 #[test]
436 fn palettes_hold_the_eight_teams_colours_each() {
437 assert_eq!(FONT_COLORS.len(), 8);
438 assert_eq!(HIGHLIGHT_COLORS.len(), 8);
439 assert_eq!(FONT_COLORS[0].color, 0xB6424C);
440 assert_eq!(HIGHLIGHT_COLORS[2].name, "Goldenrod");
441 }
442
443 #[test]
444 fn paragraph_styles_read_like_teams() {
445 assert_eq!(
446 PARAGRAPH_STYLES.map(|(_, label)| label),
447 ["Paragraph", "Heading 1", "Heading 2", "Heading 3"]
448 );
449 assert_eq!(paragraph_label(Some(2)), "Heading 2");
450 assert_eq!(paragraph_label(None), "Paragraph");
451 }
452}