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