| 1 | use std::ops::Range; |
| 2 | |
| 3 | use gpui_kit::component::{h_flex, tooltip::Tooltip, v_flex}; |
| 4 | use gpui_kit::prelude::FluentBuilder as _; |
| 5 | use gpui_kit::*; |
| 6 | use teams_core::{Draft, FormatState, LineKind, MarkKind, SizeStep, TypingStyle}; |
| 7 | |
| 8 | use super::Composer; |
| 9 | use super::FormatButton; |
| 10 | use crate::theme::{self, ElevatedSurface as _}; |
| 11 | |
| 12 | const SWATCH: f32 = 24.; |
| 13 | const SWATCH_GAP: f32 = 6.; |
| 14 | const SWATCH_COLUMNS: usize = 4; |
| 15 | const POPUP_PADDING: f32 = 8.; |
| 16 | const MENU_WIDTH: f32 = 180.; |
| 17 | const FONT_SIZE_MENU_WIDTH: f32 = 120.; |
| 18 | const MENU_PADDING: f32 = 4.; |
| 19 | const MENU_ROW_PADDING_X: f32 = 10.; |
| 20 | const MENU_ROW_PADDING_Y: f32 = 6.; |
| 21 | const STYLE_SIZES: [f32; 4] = [13., 20., 17., 15.]; |
| 22 | |
| 23 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 24 | pub(super) enum FormatPopup { |
| 25 | Highlight, |
| 26 | FontColor, |
| 27 | ParagraphStyle, |
| 28 | FontSize, |
| 29 | } |
| 30 | |
| 31 | pub(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). |
| 37 | pub(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 | |
| 48 | pub(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 | |
| 59 | pub(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`. |
| 68 | pub(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 | |
| 74 | pub(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 | |
| 81 | impl 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 | |
| 104 | fn 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 | |
| 111 | impl 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 | if self.format_bar_open() { |
| 117 | self.toolbar_dismissed = Some(self.selection(cx)); |
| 118 | } |
| 119 | cx.notify(); |
| 120 | } |
| 121 | |
| 122 | pub(super) fn toggle_format_overflow(&mut self, cx: &mut Context<Self>) { |
| 123 | self.table_picker = None; |
| 124 | self.table_menu = None; |
| 125 | self.format_popup = None; |
| 126 | self.format_overflow = !self.format_overflow; |
| 127 | cx.notify(); |
| 128 | } |
| 129 | |
| 130 | fn close_format_popups(&mut self) { |
| 131 | self.format_popup = None; |
| 132 | self.format_overflow = false; |
| 133 | } |
| 134 | |
| 135 | fn apply_font_size(&mut self, size: Option<SizeStep>, cx: &mut Context<Self>) { |
| 136 | self.close_format_popups(); |
| 137 | self.set_size(size, cx); |
| 138 | } |
| 139 | |
| 140 | pub(super) fn render_overflow_surface(&self, menu: Div, cx: &mut Context<Self>) -> AnyElement { |
| 141 | menu.id("format-overflow-menu") |
| 142 | .rounded(px(10.)) |
| 143 | .flyout_surface() |
| 144 | .occlude() |
| 145 | .on_mouse_down_out(cx.listener(|this, _, _, cx| { |
| 146 | let nested_open = this.format_popup.is_some() |
| 147 | || this.table_picker.is_some() |
| 148 | || this.table_menu.is_some(); |
| 149 | if !nested_open { |
| 150 | this.format_overflow = false; |
| 151 | cx.notify(); |
| 152 | } |
| 153 | })) |
| 154 | .into_any_element() |
| 155 | } |
| 156 | |
| 157 | /// Text typed at a collapsed cursor takes the change, a selection gets it at once. |
| 158 | fn change_style( |
| 159 | &mut self, |
| 160 | typing: impl FnOnce(&mut TypingStyle, &[MarkKind]), |
| 161 | apply: impl FnOnce(&mut Draft, Range<usize>), |
| 162 | window: &mut Window, |
| 163 | cx: &mut Context<Self>, |
| 164 | ) { |
| 165 | let selection = self.selection(cx); |
| 166 | if selection.is_empty() { |
| 167 | let cursor = selection.start; |
| 168 | let mut pending = self |
| 169 | .pending_style |
| 170 | .take() |
| 171 | .filter(|pending| pending.at == cursor) |
| 172 | .unwrap_or(TypingStyle { |
| 173 | at: cursor, |
| 174 | ..TypingStyle::default() |
| 175 | }); |
| 176 | typing(&mut pending, &self.draft.style_at(cursor)); |
| 177 | self.pending_style = (!pending.is_empty()).then_some(pending); |
| 178 | } else { |
| 179 | apply(&mut self.draft, selection); |
| 180 | self.commit(None, window, cx); |
| 181 | } |
| 182 | cx.notify(); |
| 183 | } |
| 184 | |
| 185 | fn apply_color( |
| 186 | &mut self, |
| 187 | popup: FormatPopup, |
| 188 | color: Option<u32>, |
| 189 | window: &mut Window, |
| 190 | cx: &mut Context<Self>, |
| 191 | ) { |
| 192 | self.close_format_popups(); |
| 193 | match popup { |
| 194 | FormatPopup::FontColor => self.change_style( |
| 195 | |pending, active| pending.set_color(color, active), |
| 196 | |draft, range| draft.set_color(range, color), |
| 197 | window, |
| 198 | cx, |
| 199 | ), |
| 200 | _ => self.change_style( |
| 201 | |pending, active| pending.set_highlight(color, active), |
| 202 | |draft, range| draft.set_highlight(range, color), |
| 203 | window, |
| 204 | cx, |
| 205 | ), |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | pub(super) fn clear_formatting(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 210 | let selection = self.selection(cx); |
| 211 | if selection.is_empty() { |
| 212 | self.draft.clear_formatting(selection.clone()); |
| 213 | self.commit(None, window, cx); |
| 214 | } |
| 215 | self.change_style( |
| 216 | |pending, active| pending.clear(active), |
| 217 | |draft, range| draft.clear_formatting(range), |
| 218 | window, |
| 219 | cx, |
| 220 | ); |
| 221 | } |
| 222 | |
| 223 | pub(super) fn set_paragraph_style( |
| 224 | &mut self, |
| 225 | level: Option<u8>, |
| 226 | window: &mut Window, |
| 227 | cx: &mut Context<Self>, |
| 228 | ) { |
| 229 | self.close_format_popups(); |
| 230 | if self.cell_has_focus(window, cx) { |
| 231 | return; |
| 232 | } |
| 233 | let selection = self.selection(cx); |
| 234 | self.draft.set_heading(selection, level); |
| 235 | self.commit(None, window, cx); |
| 236 | cx.notify(); |
| 237 | } |
| 238 | |
| 239 | pub(super) fn indent_blocks( |
| 240 | &mut self, |
| 241 | outdent: bool, |
| 242 | window: &mut Window, |
| 243 | cx: &mut Context<Self>, |
| 244 | ) { |
| 245 | let selection = self.selection(cx); |
| 246 | self.draft.indent_blocks(selection, outdent); |
| 247 | self.commit(None, window, cx); |
| 248 | cx.notify(); |
| 249 | } |
| 250 | |
| 251 | pub(super) fn insert_rule(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 252 | if self.cell_has_focus(window, cx) { |
| 253 | return; |
| 254 | } |
| 255 | let cursor = self.draft.insert_rule(self.selection(cx).end); |
| 256 | self.commit(Some(cursor), window, cx); |
| 257 | cx.notify(); |
| 258 | } |
| 259 | |
| 260 | pub(super) fn toggle_code_block(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 261 | if self.cell_has_focus(window, cx) { |
| 262 | return; |
| 263 | } |
| 264 | self.toggle_lines(LineKind::Code(None), window, cx); |
| 265 | } |
| 266 | |
| 267 | fn current_color(&self, popup: FormatPopup, cx: &App) -> Option<u32> { |
| 268 | let selection = self.selection(cx); |
| 269 | let marker = popup.marker(); |
| 270 | if !selection.is_empty() { |
| 271 | return self.draft.color_in(selection, &marker); |
| 272 | } |
| 273 | let style = self.draft.style_at(selection.start); |
| 274 | let style = match self.pending_style.as_ref() { |
| 275 | Some(pending) if pending.at == selection.start => pending.apply(style), |
| 276 | _ => style, |
| 277 | }; |
| 278 | style.into_iter().find_map(|kind| match kind { |
| 279 | MarkKind::Color(color) if marker.same_format(&kind) => Some(color), |
| 280 | MarkKind::Highlight(color) if marker.same_format(&kind) => Some(color), |
| 281 | _ => None, |
| 282 | }) |
| 283 | } |
| 284 | |
| 285 | pub(super) fn render_format_popup( |
| 286 | &self, |
| 287 | popup: FormatPopup, |
| 288 | cx: &mut Context<Self>, |
| 289 | ) -> Option<AnyElement> { |
| 290 | if self.format_popup != Some(popup) { |
| 291 | return None; |
| 292 | } |
| 293 | let content = match popup { |
| 294 | FormatPopup::ParagraphStyle => self.render_paragraph_menu(cx), |
| 295 | FormatPopup::FontSize => self.render_font_size_menu(cx), |
| 296 | _ => self.render_color_picker(popup, cx), |
| 297 | }; |
| 298 | Some( |
| 299 | content |
| 300 | .id(ElementId::Name(format!("format-popup-{popup:?}").into())) |
| 301 | .rounded(px(10.)) |
| 302 | .flyout_surface() |
| 303 | .occlude() |
| 304 | .on_mouse_down_out(cx.listener(|this, _, _, cx| { |
| 305 | this.format_popup = None; |
| 306 | cx.notify(); |
| 307 | })) |
| 308 | .into_any_element(), |
| 309 | ) |
| 310 | } |
| 311 | |
| 312 | fn render_color_picker(&self, popup: FormatPopup, cx: &mut Context<Self>) -> Div { |
| 313 | let current = self.current_color(popup, cx); |
| 314 | let rows = popup.palette().chunks(SWATCH_COLUMNS).map(|row| { |
| 315 | h_flex().gap(px(SWATCH_GAP)).children(row.iter().map(|swatch| { |
| 316 | let color = swatch.color; |
| 317 | let name = swatch.name; |
| 318 | let selected = current == Some(color); |
| 319 | swallow_mouse_down( |
| 320 | div() |
| 321 | .id(ElementId::Name(format!("swatch-{popup:?}-{color:06x}").into())) |
| 322 | .size(px(SWATCH)) |
| 323 | .rounded(px(SWATCH / 2.)) |
| 324 | .bg(rgb(color)) |
| 325 | .cursor_pointer() |
| 326 | .border_2() |
| 327 | .border_color(if selected { |
| 328 | theme::text() |
| 329 | } else { |
| 330 | transparent_black() |
| 331 | }) |
| 332 | .hover(|swatch| swatch.opacity(0.85)) |
| 333 | .tooltip(move |window, cx| Tooltip::new(name).build(window, cx)), |
| 334 | ) |
| 335 | .on_click(cx.listener(move |this, _, window, cx| { |
| 336 | this.apply_color(popup, Some(color), window, cx) |
| 337 | })) |
| 338 | })) |
| 339 | }); |
| 340 | let clear = swallow_mouse_down( |
| 341 | div() |
| 342 | .id(ElementId::Name(format!("swatch-{popup:?}-clear").into())) |
| 343 | .w_full() |
| 344 | .py(px(MENU_ROW_PADDING_Y)) |
| 345 | .rounded(px(6.)) |
| 346 | .flex() |
| 347 | .justify_center() |
| 348 | .text_size(px(12.)) |
| 349 | .text_color(theme::text()) |
| 350 | .cursor_pointer() |
| 351 | .hover(|row| row.bg(theme::popup_hover())), |
| 352 | ) |
| 353 | .on_click(cx.listener(move |this, _, window, cx| { |
| 354 | this.apply_color(popup, None, window, cx) |
| 355 | })) |
| 356 | .child(popup.clear_label()); |
| 357 | v_flex() |
| 358 | .p(px(POPUP_PADDING)) |
| 359 | .gap(px(SWATCH_GAP)) |
| 360 | .children(rows) |
| 361 | .child(clear) |
| 362 | } |
| 363 | |
| 364 | fn render_font_size_menu(&self, cx: &mut Context<Self>) -> Div { |
| 365 | let selection = self.selection(cx); |
| 366 | let rows = FONT_SIZES.iter().map(|(size, button, label)| { |
| 367 | let size = *size; |
| 368 | let current = size.is_some() && self.format_state(*button, &selection) == FormatState::On; |
| 369 | swallow_mouse_down( |
| 370 | div() |
| 371 | .id(ElementId::Name(format!("font-size-{label}").into())) |
| 372 | .px(px(MENU_ROW_PADDING_X)) |
| 373 | .py(px(MENU_ROW_PADDING_Y)) |
| 374 | .rounded(px(6.)) |
| 375 | .text_size(px(13.)) |
| 376 | .text_color(theme::text()) |
| 377 | .when(current, |row| row.font_weight(FontWeight::BOLD)) |
| 378 | .cursor_pointer() |
| 379 | .hover(|row| row.bg(theme::popup_hover())), |
| 380 | ) |
| 381 | .on_click(cx.listener(move |this, _, _, cx| this.apply_font_size(size, cx))) |
| 382 | .child(*label) |
| 383 | }); |
| 384 | v_flex() |
| 385 | .min_w(px(FONT_SIZE_MENU_WIDTH)) |
| 386 | .p(px(MENU_PADDING)) |
| 387 | .children(rows) |
| 388 | } |
| 389 | |
| 390 | fn render_paragraph_menu(&self, cx: &mut Context<Self>) -> Div { |
| 391 | let current = self.draft.heading_at(self.selection(cx).start); |
| 392 | let rows = PARAGRAPH_STYLES |
| 393 | .iter() |
| 394 | .zip(STYLE_SIZES) |
| 395 | .map(|((level, label), size)| { |
| 396 | let level = *level; |
| 397 | swallow_mouse_down( |
| 398 | div() |
| 399 | .id(ElementId::Name(format!("paragraph-style-{level:?}").into())) |
| 400 | .px(px(MENU_ROW_PADDING_X)) |
| 401 | .py(px(MENU_ROW_PADDING_Y)) |
| 402 | .rounded(px(6.)) |
| 403 | .text_size(px(size)) |
| 404 | .text_color(theme::text()) |
| 405 | .when(level.is_some(), |row| row.font_weight(FontWeight::BOLD)) |
| 406 | .cursor_pointer() |
| 407 | .when(level == current, |row| row.bg(theme::popup_hover())) |
| 408 | .hover(|row| row.bg(theme::popup_hover())), |
| 409 | ) |
| 410 | .on_click(cx.listener(move |this, _, window, cx| { |
| 411 | this.set_paragraph_style(level, window, cx) |
| 412 | })) |
| 413 | .child(*label) |
| 414 | }); |
| 415 | v_flex() |
| 416 | .min_w(px(MENU_WIDTH)) |
| 417 | .p(px(MENU_PADDING)) |
| 418 | .children(rows) |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | #[cfg(test)] |
| 423 | mod tests { |
| 424 | use teams_core::SizeStep; |
| 425 | |
| 426 | use super::{FONT_COLORS, FONT_SIZES, HIGHLIGHT_COLORS, PARAGRAPH_STYLES, paragraph_label}; |
| 427 | |
| 428 | #[test] |
| 429 | fn font_sizes_read_like_teams() { |
| 430 | assert_eq!(FONT_SIZES.map(|(_, _, label)| label), ["Large", "Medium", "Small"]); |
| 431 | assert_eq!( |
| 432 | FONT_SIZES.map(|(size, _, _)| size), |
| 433 | [Some(SizeStep::Large), None, Some(SizeStep::Small)] |
| 434 | ); |
| 435 | } |
| 436 | |
| 437 | #[test] |
| 438 | fn palettes_hold_the_eight_teams_colours_each() { |
| 439 | assert_eq!(FONT_COLORS.len(), 8); |
| 440 | assert_eq!(HIGHLIGHT_COLORS.len(), 8); |
| 441 | assert_eq!(FONT_COLORS[0].color, 0xB6424C); |
| 442 | assert_eq!(HIGHLIGHT_COLORS[2].name, "Goldenrod"); |
| 443 | } |
| 444 | |
| 445 | #[test] |
| 446 | fn paragraph_styles_read_like_teams() { |
| 447 | assert_eq!( |
| 448 | PARAGRAPH_STYLES.map(|(_, label)| label), |
| 449 | ["Paragraph", "Heading 1", "Heading 2", "Heading 3"] |
| 450 | ); |
| 451 | assert_eq!(paragraph_label(Some(2)), "Heading 2"); |
| 452 | assert_eq!(paragraph_label(None), "Paragraph"); |
| 453 | } |
| 454 | } |