| 1 | use std::rc::Rc; |
| 2 | |
| 3 | use gpui_kit::component::input::{Input, InputState}; |
| 4 | use gpui_kit::component::{h_flex, tooltip::Tooltip, v_flex}; |
| 5 | use gpui_kit::prelude::FluentBuilder as _; |
| 6 | use gpui_kit::assets::IconName; |
| 7 | use gpui_kit::*; |
| 8 | use teams_core::FormatState; |
| 9 | |
| 10 | use super::widgets::{icon, symbol}; |
| 11 | use crate::theme::{self, ElevatedSurface as _}; |
| 12 | |
| 13 | const PADDING: f32 = 4.; |
| 14 | const BUTTON: f32 = 28.; |
| 15 | const BUTTON_GAP: f32 = 2.; |
| 16 | const SEPARATOR_MARGIN: f32 = 4.; |
| 17 | const GAP: f32 = 8.; |
| 18 | const ARROW_WIDTH: f32 = 14.; |
| 19 | const ARROW_HEIGHT: f32 = 7.; |
| 20 | const ARROW_MARGIN: f32 = 12.; |
| 21 | const LINK_FIELD_WIDTH: f32 = 240.; |
| 22 | const APPLY_WIDTH: f32 = 64.; |
| 23 | const ACTIVE_ALPHA: f32 = 0.22; |
| 24 | const DISABLED_OPACITY: f32 = 0.4; |
| 25 | const SMALL_LETTER: f32 = 11.; |
| 26 | const NORMAL_LETTER: f32 = 15.; |
| 27 | const LARGE_LETTER: f32 = 19.; |
| 28 | const SCRIPT_LETTER: f32 = 10.; |
| 29 | const SCRIPT_SHIFT: f32 = 4.; |
| 30 | |
| 31 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 32 | pub enum FormatButton { |
| 33 | Bold, |
| 34 | Italic, |
| 35 | Underline, |
| 36 | Strike, |
| 37 | Superscript, |
| 38 | Subscript, |
| 39 | SizeSmall, |
| 40 | SizeNormal, |
| 41 | SizeLarge, |
| 42 | Code, |
| 43 | Link, |
| 44 | Bulleted, |
| 45 | Numbered, |
| 46 | Quote, |
| 47 | Table, |
| 48 | TableInsertLine, |
| 49 | TableRemoveLine, |
| 50 | Highlight, |
| 51 | FontColor, |
| 52 | ParagraphStyle, |
| 53 | ClearFormatting, |
| 54 | Outdent, |
| 55 | Indent, |
| 56 | CodeBlock, |
| 57 | Rule, |
| 58 | FontSize, |
| 59 | More, |
| 60 | } |
| 61 | |
| 62 | const STYLE_GROUP: &[FormatButton] = &[ |
| 63 | FormatButton::Bold, |
| 64 | FormatButton::Italic, |
| 65 | FormatButton::Underline, |
| 66 | FormatButton::Strike, |
| 67 | ]; |
| 68 | const SCRIPT_GROUP: &[FormatButton] = &[FormatButton::Superscript, FormatButton::Subscript]; |
| 69 | const SIZE_GROUP: &[FormatButton] = &[ |
| 70 | FormatButton::SizeSmall, |
| 71 | FormatButton::SizeNormal, |
| 72 | FormatButton::SizeLarge, |
| 73 | ]; |
| 74 | const INLINE_GROUP: &[FormatButton] = &[FormatButton::Code, FormatButton::Link]; |
| 75 | const LINE_GROUP: &[FormatButton] = &[ |
| 76 | FormatButton::Bulleted, |
| 77 | FormatButton::Numbered, |
| 78 | FormatButton::Quote, |
| 79 | ]; |
| 80 | const TABLE_GROUP: &[FormatButton] = &[ |
| 81 | FormatButton::Table, |
| 82 | FormatButton::TableInsertLine, |
| 83 | FormatButton::TableRemoveLine, |
| 84 | ]; |
| 85 | |
| 86 | const FONT_GROUP: &[FormatButton] = &[ |
| 87 | FormatButton::Highlight, |
| 88 | FormatButton::FontColor, |
| 89 | FormatButton::FontSize, |
| 90 | FormatButton::ParagraphStyle, |
| 91 | FormatButton::ClearFormatting, |
| 92 | ]; |
| 93 | const PARAGRAPH_GROUP: &[FormatButton] = &[ |
| 94 | FormatButton::Outdent, |
| 95 | FormatButton::Indent, |
| 96 | FormatButton::Bulleted, |
| 97 | FormatButton::Numbered, |
| 98 | ]; |
| 99 | const INSERT_GROUP: &[FormatButton] = &[ |
| 100 | FormatButton::Quote, |
| 101 | FormatButton::Link, |
| 102 | FormatButton::Code, |
| 103 | FormatButton::CodeBlock, |
| 104 | FormatButton::Rule, |
| 105 | ]; |
| 106 | |
| 107 | /// The floating bar over a selection: text formats only, tables live in the format bar. |
| 108 | pub const SELECTION_GROUPS: [&[FormatButton]; 5] = [ |
| 109 | STYLE_GROUP, |
| 110 | SCRIPT_GROUP, |
| 111 | SIZE_GROUP, |
| 112 | INLINE_GROUP, |
| 113 | LINE_GROUP, |
| 114 | ]; |
| 115 | |
| 116 | /// The format bar the Format button opens, in Teams' group order; Teams' last group (undo, |
| 117 | /// redo) is left out, so the table group ends the bar. |
| 118 | pub const BAR_GROUPS: [&[FormatButton]; 6] = [ |
| 119 | STYLE_GROUP, |
| 120 | SCRIPT_GROUP, |
| 121 | FONT_GROUP, |
| 122 | PARAGRAPH_GROUP, |
| 123 | INSERT_GROUP, |
| 124 | TABLE_GROUP, |
| 125 | ]; |
| 126 | |
| 127 | pub const PARAGRAPH_STYLE_WIDTH: f32 = 104.; |
| 128 | const SEPARATOR_WIDTH: f32 = 1. + 2. * SEPARATOR_MARGIN; |
| 129 | const MENU_ROW_HEIGHT: f32 = 32.; |
| 130 | const MENU_ROW_PADDING_X: f32 = 10.; |
| 131 | const MENU_PADDING: f32 = 4.; |
| 132 | const MENU_WIDTH: f32 = 220.; |
| 133 | |
| 134 | /// The groups of the format bar that fit, and those that go into More options. |
| 135 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 136 | pub struct BarFit<'a> { |
| 137 | pub visible: &'a [&'a [FormatButton]], |
| 138 | pub overflow: &'a [&'a [FormatButton]], |
| 139 | } |
| 140 | |
| 141 | fn group_width(group: &[FormatButton]) -> f32 { |
| 142 | let buttons: f32 = group.iter().map(|button| button.width()).sum(); |
| 143 | buttons + group.len().saturating_sub(1) as f32 * BUTTON_GAP |
| 144 | } |
| 145 | |
| 146 | fn groups_width(groups: &[&[FormatButton]]) -> f32 { |
| 147 | let buttons: f32 = groups.iter().map(|group| group_width(group)).sum(); |
| 148 | let separators = groups.len().saturating_sub(1) as f32 * (SEPARATOR_WIDTH + 2. * BUTTON_GAP); |
| 149 | buttons + separators |
| 150 | } |
| 151 | |
| 152 | /// Like Teams' overflow: groups leave from the end until the rest and the More button fit. |
| 153 | pub fn fit_groups<'a>(available: f32, groups: &'a [&'a [FormatButton]]) -> BarFit<'a> { |
| 154 | let more = BUTTON_GAP + FormatButton::More.width(); |
| 155 | let visible = if groups_width(groups) <= available { |
| 156 | groups.len() |
| 157 | } else { |
| 158 | (0..groups.len()) |
| 159 | .rev() |
| 160 | .find(|count| groups_width(&groups[..*count]) + more <= available) |
| 161 | .unwrap_or(0) |
| 162 | }; |
| 163 | let (visible, overflow) = groups.split_at(visible); |
| 164 | BarFit { visible, overflow } |
| 165 | } |
| 166 | |
| 167 | pub const TOGGLE_SHORTCUT: &str = "Ctrl+Shift+X"; |
| 168 | |
| 169 | pub fn toggle_tooltip(open: bool) -> String { |
| 170 | let label = if open { |
| 171 | "Hide Formatting options" |
| 172 | } else { |
| 173 | "Show Formatting options" |
| 174 | }; |
| 175 | format!("{label} ({TOGGLE_SHORTCUT})") |
| 176 | } |
| 177 | |
| 178 | pub fn toggle_symbol(open: bool) -> &'static str { |
| 179 | if open { |
| 180 | "fluent_text_edit_style_filled" |
| 181 | } else { |
| 182 | "fluent_text_edit_style" |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 187 | pub struct ButtonState { |
| 188 | pub format: FormatState, |
| 189 | pub enabled: bool, |
| 190 | pub label: Option<&'static str>, |
| 191 | } |
| 192 | |
| 193 | impl ButtonState { |
| 194 | pub fn enabled(format: FormatState) -> Self { |
| 195 | ButtonState { |
| 196 | format, |
| 197 | enabled: true, |
| 198 | label: None, |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | pub fn disabled() -> Self { |
| 203 | ButtonState { |
| 204 | format: FormatState::Off, |
| 205 | enabled: false, |
| 206 | label: None, |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | pub fn with_label(self, label: &'static str) -> Self { |
| 211 | ButtonState { |
| 212 | label: Some(label), |
| 213 | ..self |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | impl FormatButton { |
| 219 | fn tooltip(self) -> &'static str { |
| 220 | match self { |
| 221 | FormatButton::Bold => "Bold (Ctrl+B)", |
| 222 | FormatButton::Italic => "Italic (Ctrl+I)", |
| 223 | FormatButton::Underline => "Underline (Ctrl+U)", |
| 224 | FormatButton::Strike => "Strikethrough (Ctrl+Alt+X)", |
| 225 | FormatButton::Superscript => "Superscript (Ctrl+Shift+=)", |
| 226 | FormatButton::Subscript => "Subscript (Ctrl+=)", |
| 227 | FormatButton::SizeSmall => "Small text", |
| 228 | FormatButton::SizeNormal => "Normal text", |
| 229 | FormatButton::SizeLarge => "Large text", |
| 230 | FormatButton::Code => "Code (Ctrl+Shift+C)", |
| 231 | FormatButton::Link => "Link (Ctrl+K)", |
| 232 | FormatButton::Bulleted => "Bulleted list", |
| 233 | FormatButton::Numbered => "Numbered list", |
| 234 | FormatButton::Quote => "Quote (Ctrl+Alt+4)", |
| 235 | FormatButton::Table => "Insert table", |
| 236 | FormatButton::TableInsertLine => "Insert column or row (Ctrl+Alt+=)", |
| 237 | FormatButton::TableRemoveLine => "Delete column, row, or table (Ctrl+Alt+-)", |
| 238 | FormatButton::Highlight => "Text highlight colour", |
| 239 | FormatButton::FontColor => "Font colour", |
| 240 | FormatButton::ParagraphStyle => "Rich style", |
| 241 | FormatButton::ClearFormatting => "Clear all formatting", |
| 242 | FormatButton::Outdent => "Decrease indent", |
| 243 | FormatButton::Indent => "Increase indent", |
| 244 | FormatButton::CodeBlock => "Code block (Ctrl+Shift+B)", |
| 245 | FormatButton::Rule => "Insert horizontal rule", |
| 246 | FormatButton::FontSize => "Font size", |
| 247 | FormatButton::More => "More options", |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | pub fn menu_label(self) -> &'static str { |
| 252 | let tooltip = self.tooltip(); |
| 253 | tooltip.split(" (").next().unwrap_or(tooltip) |
| 254 | } |
| 255 | |
| 256 | /// Opens a picker or menu below itself instead of acting at once. |
| 257 | pub fn opens_popup(self) -> bool { |
| 258 | matches!( |
| 259 | self, |
| 260 | FormatButton::Highlight |
| 261 | | FormatButton::FontColor |
| 262 | | FormatButton::FontSize |
| 263 | | FormatButton::ParagraphStyle |
| 264 | | FormatButton::Table |
| 265 | | FormatButton::TableInsertLine |
| 266 | | FormatButton::TableRemoveLine |
| 267 | | FormatButton::More |
| 268 | ) |
| 269 | } |
| 270 | |
| 271 | fn content(self, color: Hsla, label: Option<&'static str>) -> AnyElement { |
| 272 | let letter = |text: &'static str| div().text_size(px(15.)).text_color(color).child(text); |
| 273 | match self { |
| 274 | FormatButton::Bold => letter("B").font_weight(FontWeight::BOLD).into_any_element(), |
| 275 | FormatButton::Italic => letter("I").italic().into_any_element(), |
| 276 | FormatButton::Underline => letter("U").underline().into_any_element(), |
| 277 | FormatButton::Strike => letter("S").line_through().into_any_element(), |
| 278 | FormatButton::Superscript => script("x", "2", true, color), |
| 279 | FormatButton::Subscript => script("x", "2", false, color), |
| 280 | FormatButton::SizeSmall => sized_letter(SMALL_LETTER, color), |
| 281 | FormatButton::SizeNormal => sized_letter(NORMAL_LETTER, color), |
| 282 | FormatButton::SizeLarge => sized_letter(LARGE_LETTER, color), |
| 283 | FormatButton::Code => symbol("code", 18., color).into_any_element(), |
| 284 | FormatButton::Link => symbol("link", 18., color).into_any_element(), |
| 285 | FormatButton::Bulleted => symbol("format_list_bulleted", 18., color).into_any_element(), |
| 286 | FormatButton::Numbered => symbol("format_list_numbered", 18., color).into_any_element(), |
| 287 | FormatButton::Quote => symbol("format_quote", 18., color).into_any_element(), |
| 288 | FormatButton::Table => symbol("table", 18., color).into_any_element(), |
| 289 | FormatButton::TableInsertLine => { |
| 290 | symbol("fluent_table_add", 20., color).into_any_element() |
| 291 | } |
| 292 | FormatButton::TableRemoveLine => { |
| 293 | symbol("fluent_table_dismiss", 20., color).into_any_element() |
| 294 | } |
| 295 | FormatButton::Highlight => symbol("fluent_highlight", 20., color).into_any_element(), |
| 296 | FormatButton::FontColor => symbol("fluent_text_color", 20., color).into_any_element(), |
| 297 | FormatButton::ParagraphStyle => h_flex() |
| 298 | .w_full() |
| 299 | .px(px(6.)) |
| 300 | .gap(px(4.)) |
| 301 | .items_center() |
| 302 | .justify_between() |
| 303 | .text_size(px(13.)) |
| 304 | .text_color(color) |
| 305 | .child(label.unwrap_or_default()) |
| 306 | .child(icon(IconName::ChevronDown, 12., color)) |
| 307 | .into_any_element(), |
| 308 | FormatButton::ClearFormatting => { |
| 309 | symbol("fluent_text_clear_formatting", 20., color).into_any_element() |
| 310 | } |
| 311 | FormatButton::Outdent => { |
| 312 | symbol("fluent_text_indent_decrease", 20., color).into_any_element() |
| 313 | } |
| 314 | FormatButton::Indent => { |
| 315 | symbol("fluent_text_indent_increase", 20., color).into_any_element() |
| 316 | } |
| 317 | FormatButton::CodeBlock => symbol("fluent_code_block", 20., color).into_any_element(), |
| 318 | FormatButton::Rule => symbol("fluent_line_horizontal_1", 20., color).into_any_element(), |
| 319 | FormatButton::FontSize => { |
| 320 | symbol("fluent_text_font_size", 20., color).into_any_element() |
| 321 | } |
| 322 | FormatButton::More => icon(IconName::Ellipsis, 18., color).into_any_element(), |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | fn width(self) -> f32 { |
| 327 | match self { |
| 328 | FormatButton::ParagraphStyle => PARAGRAPH_STYLE_WIDTH, |
| 329 | _ => BUTTON, |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | fn script(base: &'static str, mark: &'static str, raised: bool, color: Hsla) -> AnyElement { |
| 335 | h_flex() |
| 336 | .items_baseline() |
| 337 | .text_color(color) |
| 338 | .child(div().text_size(px(15.)).child(base)) |
| 339 | .child( |
| 340 | div() |
| 341 | .text_size(px(SCRIPT_LETTER)) |
| 342 | .when(raised, |mark| mark.mb(px(SCRIPT_SHIFT))) |
| 343 | .when(!raised, |mark| mark.mt(px(SCRIPT_SHIFT))) |
| 344 | .child(mark), |
| 345 | ) |
| 346 | .into_any_element() |
| 347 | } |
| 348 | |
| 349 | fn sized_letter(size: f32, color: Hsla) -> AnyElement { |
| 350 | div() |
| 351 | .text_size(px(size)) |
| 352 | .text_color(color) |
| 353 | .child("A") |
| 354 | .into_any_element() |
| 355 | } |
| 356 | |
| 357 | pub fn bar_width() -> f32 { |
| 358 | let groups = SELECTION_GROUPS; |
| 359 | let buttons: usize = groups.iter().map(|group| group.len()).sum(); |
| 360 | let gaps: usize = groups.iter().map(|group| group.len() - 1).sum(); |
| 361 | let separators = (groups.len() - 1) as f32 * (1. + 2. * SEPARATOR_MARGIN); |
| 362 | 2. * PADDING + buttons as f32 * BUTTON + gaps as f32 * BUTTON_GAP + separators |
| 363 | } |
| 364 | |
| 365 | pub fn link_width() -> f32 { |
| 366 | 2. * PADDING + LINK_FIELD_WIDTH + BUTTON_GAP * 2. + APPLY_WIDTH |
| 367 | } |
| 368 | |
| 369 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 370 | pub struct Placement { |
| 371 | pub left: Pixels, |
| 372 | pub bottom: Pixels, |
| 373 | pub arrow_x: Pixels, |
| 374 | } |
| 375 | |
| 376 | /// Centered over `center_x`, `GAP` above `top`, kept between the composer edges. |
| 377 | pub fn place(center_x: Pixels, top: Pixels, edges: (Pixels, Pixels), width: f32) -> Placement { |
| 378 | let width = px(width); |
| 379 | let (left_edge, right_edge) = edges; |
| 380 | let left = (center_x - width / 2.) |
| 381 | .max(left_edge) |
| 382 | .min(right_edge - width); |
| 383 | Placement { |
| 384 | left, |
| 385 | bottom: top - px(GAP), |
| 386 | arrow_x: (center_x - left) |
| 387 | .max(px(ARROW_MARGIN)) |
| 388 | .min(width - px(ARROW_MARGIN)), |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | pub type PressHandler = Rc<dyn Fn(FormatButton, &mut Window, &mut App)>; |
| 393 | pub type ApplyHandler = Rc<dyn Fn(&mut Window, &mut App)>; |
| 394 | |
| 395 | pub enum Mode<'a> { |
| 396 | Bar { |
| 397 | state: &'a dyn Fn(FormatButton) -> ButtonState, |
| 398 | on_press: PressHandler, |
| 399 | }, |
| 400 | Link { |
| 401 | field: &'a Entity<InputState>, |
| 402 | refused: bool, |
| 403 | on_apply: ApplyHandler, |
| 404 | }, |
| 405 | } |
| 406 | |
| 407 | fn button( |
| 408 | format: FormatButton, |
| 409 | state: ButtonState, |
| 410 | on_press: PressHandler, |
| 411 | overlay: Option<AnyElement>, |
| 412 | ) -> impl IntoElement { |
| 413 | let on = state.enabled && state.format == FormatState::On; |
| 414 | let color = if on { |
| 415 | theme::accent_soft() |
| 416 | } else { |
| 417 | theme::text_soft() |
| 418 | }; |
| 419 | div() |
| 420 | .id(ElementId::Name(format!("format-{format:?}").into())) |
| 421 | .relative() |
| 422 | .w(px(format.width())) |
| 423 | .h(px(BUTTON)) |
| 424 | .flex() |
| 425 | .flex_none() |
| 426 | .items_center() |
| 427 | .justify_center() |
| 428 | .rounded(px(6.)) |
| 429 | .tooltip(move |window, cx| Tooltip::new(format.tooltip()).build(window, cx)) |
| 430 | .child(format.content(color, state.label)) |
| 431 | .on_mouse_down(MouseButton::Left, |_, window, cx| { |
| 432 | window.prevent_default(); |
| 433 | cx.stop_propagation(); |
| 434 | }) |
| 435 | .when(!state.enabled, |button| button.opacity(DISABLED_OPACITY)) |
| 436 | .when(state.enabled, |button| { |
| 437 | button |
| 438 | .cursor_pointer() |
| 439 | .when(on, |button| { |
| 440 | button.bg(theme::accent().opacity(ACTIVE_ALPHA)) |
| 441 | }) |
| 442 | .hover(|button| button.bg(theme::popup_hover())) |
| 443 | .when(state.format == FormatState::Mixed, |button| { |
| 444 | button.child( |
| 445 | div() |
| 446 | .absolute() |
| 447 | .bottom(px(2.)) |
| 448 | .left(px(7.)) |
| 449 | .w(px(BUTTON - 16.)) |
| 450 | .h(px(2.)) |
| 451 | .rounded(px(1.)) |
| 452 | .bg(theme::accent()), |
| 453 | ) |
| 454 | }) |
| 455 | .on_click(move |_, window, cx| on_press(format, window, cx)) |
| 456 | }) |
| 457 | .children(overlay.map(|overlay| overlay_below(overlay, BUTTON + GAP / 2.))) |
| 458 | } |
| 459 | |
| 460 | fn overlay_below(overlay: AnyElement, offset: f32) -> Div { |
| 461 | div().absolute().top(px(offset)).left_0().child( |
| 462 | deferred( |
| 463 | anchored() |
| 464 | .snap_to_window_with_margin(px(GAP)) |
| 465 | .child(overlay), |
| 466 | ) |
| 467 | .with_priority(2), |
| 468 | ) |
| 469 | } |
| 470 | |
| 471 | fn separator() -> Div { |
| 472 | div() |
| 473 | .w(px(1.)) |
| 474 | .h(px(BUTTON - 8.)) |
| 475 | .mx(px(SEPARATOR_MARGIN)) |
| 476 | .bg(theme::popup_hover()) |
| 477 | } |
| 478 | |
| 479 | fn groups_row( |
| 480 | groups: &[&[FormatButton]], |
| 481 | state: &dyn Fn(FormatButton) -> ButtonState, |
| 482 | on_press: PressHandler, |
| 483 | overlay: &mut dyn FnMut(FormatButton) -> Option<AnyElement>, |
| 484 | ) -> Div { |
| 485 | let mut row = h_flex().gap(px(BUTTON_GAP)).items_center(); |
| 486 | for (index, group) in groups.iter().enumerate() { |
| 487 | if index > 0 { |
| 488 | row = row.child(separator()); |
| 489 | } |
| 490 | for format in group.iter().copied() { |
| 491 | row = row.child(button( |
| 492 | format, |
| 493 | state(format), |
| 494 | on_press.clone(), |
| 495 | overlay(format), |
| 496 | )); |
| 497 | } |
| 498 | } |
| 499 | row |
| 500 | } |
| 501 | |
| 502 | fn bar(state: &dyn Fn(FormatButton) -> ButtonState, on_press: PressHandler) -> Div { |
| 503 | groups_row(&SELECTION_GROUPS, state, on_press, &mut |_| None) |
| 504 | } |
| 505 | |
| 506 | /// The format bar inside the composer box. `overlay` hangs a popup under a button; the |
| 507 | /// More button's popup is the overflow menu, built with `render_overflow_menu`. |
| 508 | pub fn render_format_bar( |
| 509 | fit: BarFit<'_>, |
| 510 | state: &dyn Fn(FormatButton) -> ButtonState, |
| 511 | on_press: PressHandler, |
| 512 | overlay: &mut dyn FnMut(FormatButton) -> Option<AnyElement>, |
| 513 | ) -> Div { |
| 514 | let row = groups_row(fit.visible, state, on_press.clone(), overlay); |
| 515 | if fit.overflow.is_empty() { |
| 516 | return row; |
| 517 | } |
| 518 | row.child(button( |
| 519 | FormatButton::More, |
| 520 | state(FormatButton::More), |
| 521 | on_press, |
| 522 | overlay(FormatButton::More), |
| 523 | )) |
| 524 | } |
| 525 | |
| 526 | /// The groups that do not fit, as menu rows with a divider between groups. |
| 527 | pub fn render_overflow_menu( |
| 528 | groups: &[&[FormatButton]], |
| 529 | state: &dyn Fn(FormatButton) -> ButtonState, |
| 530 | on_press: PressHandler, |
| 531 | overlay: &mut dyn FnMut(FormatButton) -> Option<AnyElement>, |
| 532 | ) -> Div { |
| 533 | let mut menu = v_flex().w(px(MENU_WIDTH)).py(px(MENU_PADDING)); |
| 534 | for (index, group) in groups.iter().enumerate() { |
| 535 | if index > 0 { |
| 536 | menu = menu.child( |
| 537 | div() |
| 538 | .w_full() |
| 539 | .h(px(1.)) |
| 540 | .my(px(MENU_PADDING)) |
| 541 | .bg(theme::popup_hover()), |
| 542 | ); |
| 543 | } |
| 544 | for format in group.iter().copied() { |
| 545 | menu = menu.child(menu_row( |
| 546 | format, |
| 547 | state(format), |
| 548 | on_press.clone(), |
| 549 | overlay(format), |
| 550 | )); |
| 551 | } |
| 552 | } |
| 553 | menu |
| 554 | } |
| 555 | |
| 556 | fn menu_row( |
| 557 | format: FormatButton, |
| 558 | state: ButtonState, |
| 559 | on_press: PressHandler, |
| 560 | overlay: Option<AnyElement>, |
| 561 | ) -> impl IntoElement { |
| 562 | let on = state.enabled && state.format == FormatState::On; |
| 563 | let color = if on { |
| 564 | theme::accent_soft() |
| 565 | } else { |
| 566 | theme::text_soft() |
| 567 | }; |
| 568 | let content = match format { |
| 569 | FormatButton::ParagraphStyle => symbol("fluent_text_header_1", 20., color).into_any_element(), |
| 570 | _ => format.content(color, None), |
| 571 | }; |
| 572 | div() |
| 573 | .id(ElementId::Name(format!("format-overflow-{format:?}").into())) |
| 574 | .relative() |
| 575 | .mx(px(MENU_PADDING)) |
| 576 | .h(px(MENU_ROW_HEIGHT)) |
| 577 | .flex_none() |
| 578 | .px(px(MENU_ROW_PADDING_X)) |
| 579 | .flex() |
| 580 | .items_center() |
| 581 | .gap(px(GAP)) |
| 582 | .rounded(px(6.)) |
| 583 | .child(div().w(px(BUTTON)).flex().justify_center().child(content)) |
| 584 | .child( |
| 585 | div() |
| 586 | .flex_1() |
| 587 | .text_size(px(13.)) |
| 588 | .text_color(theme::text()) |
| 589 | .child(format.menu_label()), |
| 590 | ) |
| 591 | .on_mouse_down(MouseButton::Left, |_, window, cx| { |
| 592 | window.prevent_default(); |
| 593 | cx.stop_propagation(); |
| 594 | }) |
| 595 | .when(!state.enabled, |row| row.opacity(DISABLED_OPACITY)) |
| 596 | .when(state.enabled, |row| { |
| 597 | row.cursor_pointer() |
| 598 | .when(on, |row| row.bg(theme::accent().opacity(ACTIVE_ALPHA))) |
| 599 | .hover(|row| row.bg(theme::popup_hover())) |
| 600 | .on_click(move |_, window, cx| on_press(format, window, cx)) |
| 601 | }) |
| 602 | .children(overlay.map(|overlay| overlay_below(overlay, MENU_ROW_HEIGHT))) |
| 603 | } |
| 604 | |
| 605 | fn link_editor(field: &Entity<InputState>, refused: bool, on_apply: ApplyHandler) -> Div { |
| 606 | let row = h_flex() |
| 607 | .gap(px(BUTTON_GAP * 2.)) |
| 608 | .items_center() |
| 609 | .child( |
| 610 | div() |
| 611 | .w(px(LINK_FIELD_WIDTH)) |
| 612 | .h(px(BUTTON)) |
| 613 | .rounded(px(6.)) |
| 614 | .border_1() |
| 615 | .border_color(if refused { |
| 616 | theme::red() |
| 617 | } else { |
| 618 | theme::accent() |
| 619 | }) |
| 620 | .bg(theme::popup_hover()) |
| 621 | .child(Input::new(field).appearance(false).bordered(false)), |
| 622 | ) |
| 623 | .child( |
| 624 | div() |
| 625 | .id("format-link-apply") |
| 626 | .w(px(APPLY_WIDTH)) |
| 627 | .h(px(BUTTON)) |
| 628 | .flex() |
| 629 | .items_center() |
| 630 | .justify_center() |
| 631 | .rounded(px(6.)) |
| 632 | .bg(theme::accent()) |
| 633 | .text_color(theme::on_accent()) |
| 634 | .text_size(px(13.)) |
| 635 | .font_weight(FontWeight::SEMIBOLD) |
| 636 | .cursor_pointer() |
| 637 | .hover(|button| button.opacity(0.9)) |
| 638 | .child("Apply") |
| 639 | .on_mouse_down(MouseButton::Left, |_, window, cx| { |
| 640 | window.prevent_default(); |
| 641 | cx.stop_propagation(); |
| 642 | }) |
| 643 | .on_click(move |_, window, cx| on_apply(window, cx)), |
| 644 | ); |
| 645 | v_flex().child(row).when(refused, |editor| { |
| 646 | editor.child( |
| 647 | div() |
| 648 | .pt(px(4.)) |
| 649 | .px(px(2.)) |
| 650 | .text_size(px(11.5)) |
| 651 | .text_color(theme::red_soft()) |
| 652 | .child("Only http, https and mailto links"), |
| 653 | ) |
| 654 | }) |
| 655 | } |
| 656 | |
| 657 | fn arrow(offset: Pixels) -> impl IntoElement { |
| 658 | canvas( |
| 659 | |_, _, _| {}, |
| 660 | move |bounds, _, window, _| { |
| 661 | let tip = point(bounds.origin.x + offset, bounds.origin.y + px(ARROW_HEIGHT)); |
| 662 | let mut builder = PathBuilder::fill(); |
| 663 | builder.move_to(point( |
| 664 | tip.x - px(ARROW_WIDTH / 2.), |
| 665 | bounds.origin.y - px(1.), |
| 666 | )); |
| 667 | builder.line_to(point( |
| 668 | tip.x + px(ARROW_WIDTH / 2.), |
| 669 | bounds.origin.y - px(1.), |
| 670 | )); |
| 671 | builder.line_to(point(tip.x, tip.y)); |
| 672 | builder.close(); |
| 673 | if let Ok(path) = builder.build() { |
| 674 | window.paint_path(path, theme::popup()); |
| 675 | } |
| 676 | }, |
| 677 | ) |
| 678 | .w_full() |
| 679 | .h(px(ARROW_HEIGHT)) |
| 680 | } |
| 681 | |
| 682 | pub fn render(placement: Placement, mode: Mode<'_>) -> AnyElement { |
| 683 | let content = match mode { |
| 684 | Mode::Bar { state, on_press } => bar(state, on_press), |
| 685 | Mode::Link { |
| 686 | field, |
| 687 | refused, |
| 688 | on_apply, |
| 689 | } => link_editor(field, refused, on_apply), |
| 690 | }; |
| 691 | deferred( |
| 692 | anchored() |
| 693 | .position(point(placement.left, placement.bottom)) |
| 694 | .anchor(Anchor::BottomLeft) |
| 695 | .child( |
| 696 | v_flex() |
| 697 | .id("format-toolbar") |
| 698 | .occlude() |
| 699 | .child( |
| 700 | div() |
| 701 | .p(px(PADDING)) |
| 702 | .rounded(px(10.)) |
| 703 | .flyout_surface() |
| 704 | .child(content), |
| 705 | ) |
| 706 | .child(arrow(placement.arrow_x)), |
| 707 | ), |
| 708 | ) |
| 709 | .with_priority(1) |
| 710 | .into_any_element() |
| 711 | } |
| 712 | |
| 713 | #[cfg(test)] |
| 714 | mod tests { |
| 715 | use gpui_kit::px; |
| 716 | |
| 717 | use super::{ |
| 718 | ARROW_MARGIN, BAR_GROUPS, BUTTON, FormatButton, SELECTION_GROUPS, bar_width, fit_groups, |
| 719 | groups_width, place, toggle_symbol, toggle_tooltip, |
| 720 | }; |
| 721 | |
| 722 | const SIZES: [FormatButton; 3] = [ |
| 723 | FormatButton::SizeSmall, |
| 724 | FormatButton::SizeNormal, |
| 725 | FormatButton::SizeLarge, |
| 726 | ]; |
| 727 | |
| 728 | #[test] |
| 729 | fn the_bar_centers_over_the_selection() { |
| 730 | let placement = place(px(400.), px(300.), (px(100.), px(900.)), 200.); |
| 731 | assert_eq!(placement.left, px(300.)); |
| 732 | assert_eq!(placement.bottom, px(292.)); |
| 733 | assert_eq!(placement.arrow_x, px(100.)); |
| 734 | } |
| 735 | |
| 736 | #[test] |
| 737 | fn the_bar_stays_inside_the_composer_and_the_arrow_follows_the_selection() { |
| 738 | let near_left = place(px(120.), px(300.), (px(100.), px(900.)), 200.); |
| 739 | assert_eq!(near_left.left, px(100.)); |
| 740 | assert_eq!(near_left.arrow_x, px(20.)); |
| 741 | let near_right = place(px(899.), px(300.), (px(100.), px(900.)), 200.); |
| 742 | assert_eq!(near_right.left, px(700.)); |
| 743 | assert_eq!(near_right.arrow_x, px(200. - ARROW_MARGIN)); |
| 744 | } |
| 745 | |
| 746 | #[test] |
| 747 | fn a_bar_wider_than_the_composer_ends_at_its_right_edge() { |
| 748 | let placement = place(px(560.), px(300.), (px(500.), px(800.)), 400.); |
| 749 | assert_eq!(placement.left, px(400.)); |
| 750 | assert_eq!(placement.arrow_x, px(160.)); |
| 751 | } |
| 752 | |
| 753 | #[test] |
| 754 | fn the_selection_bar_holds_the_text_formats_without_the_table() { |
| 755 | assert_eq!( |
| 756 | SELECTION_GROUPS |
| 757 | .iter() |
| 758 | .map(|group| group.len()) |
| 759 | .sum::<usize>(), |
| 760 | 14 |
| 761 | ); |
| 762 | assert!( |
| 763 | SELECTION_GROUPS |
| 764 | .iter() |
| 765 | .all(|group| !group.contains(&FormatButton::Table)) |
| 766 | ); |
| 767 | assert!(bar_width() > 14. * BUTTON); |
| 768 | } |
| 769 | |
| 770 | #[test] |
| 771 | fn the_format_bar_follows_the_teams_groups_and_keeps_every_selection_format() { |
| 772 | assert_eq!( |
| 773 | BAR_GROUPS[BAR_GROUPS.len() - 1], |
| 774 | &[ |
| 775 | FormatButton::Table, |
| 776 | FormatButton::TableInsertLine, |
| 777 | FormatButton::TableRemoveLine |
| 778 | ][..] |
| 779 | ); |
| 780 | assert_eq!( |
| 781 | BAR_GROUPS[2][..3], |
| 782 | [ |
| 783 | FormatButton::Highlight, |
| 784 | FormatButton::FontColor, |
| 785 | FormatButton::FontSize |
| 786 | ] |
| 787 | ); |
| 788 | assert_eq!( |
| 789 | BAR_GROUPS[3], |
| 790 | &[ |
| 791 | FormatButton::Outdent, |
| 792 | FormatButton::Indent, |
| 793 | FormatButton::Bulleted, |
| 794 | FormatButton::Numbered |
| 795 | ][..] |
| 796 | ); |
| 797 | assert_eq!(BAR_GROUPS[4].last(), Some(&FormatButton::Rule)); |
| 798 | for button in SELECTION_GROUPS.iter().flat_map(|group| group.iter()) { |
| 799 | let covered = if SIZES.contains(button) { |
| 800 | &FormatButton::FontSize |
| 801 | } else { |
| 802 | button |
| 803 | }; |
| 804 | assert!(BAR_GROUPS.iter().any(|group| group.contains(covered))); |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | #[test] |
| 809 | fn a_wide_bar_shows_every_group_without_more_options() { |
| 810 | let fit = fit_groups(groups_width(&BAR_GROUPS), &BAR_GROUPS); |
| 811 | assert_eq!(fit.visible.len(), BAR_GROUPS.len()); |
| 812 | assert!(fit.overflow.is_empty()); |
| 813 | } |
| 814 | |
| 815 | #[test] |
| 816 | fn groups_leave_from_the_end_and_more_options_fits_beside_the_rest() { |
| 817 | let fit = fit_groups(groups_width(&BAR_GROUPS) - 1., &BAR_GROUPS); |
| 818 | assert_eq!(fit.visible, &BAR_GROUPS[..BAR_GROUPS.len() - 1]); |
| 819 | assert_eq!(fit.overflow, &BAR_GROUPS[BAR_GROUPS.len() - 1..]); |
| 820 | let three = groups_width(&BAR_GROUPS[..3]); |
| 821 | let fit = fit_groups(three + 2. + BUTTON, &BAR_GROUPS); |
| 822 | assert_eq!(fit.visible.len(), 3); |
| 823 | let fit = fit_groups(three + 2. + BUTTON - 1., &BAR_GROUPS); |
| 824 | assert_eq!(fit.visible.len(), 2); |
| 825 | assert_eq!(fit.overflow.len(), BAR_GROUPS.len() - 2); |
| 826 | } |
| 827 | |
| 828 | #[test] |
| 829 | fn a_tiny_bar_keeps_only_more_options() { |
| 830 | let fit = fit_groups(BUTTON, &BAR_GROUPS); |
| 831 | assert!(fit.visible.is_empty()); |
| 832 | assert_eq!(fit.overflow.len(), BAR_GROUPS.len()); |
| 833 | } |
| 834 | |
| 835 | #[test] |
| 836 | fn overflow_rows_read_without_the_shortcut() { |
| 837 | assert_eq!(FormatButton::CodeBlock.menu_label(), "Code block"); |
| 838 | assert_eq!(FormatButton::Rule.menu_label(), "Insert horizontal rule"); |
| 839 | assert_eq!(FormatButton::More.tooltip(), "More options"); |
| 840 | assert_eq!(FormatButton::FontSize.tooltip(), "Font size"); |
| 841 | } |
| 842 | |
| 843 | #[test] |
| 844 | fn the_format_toggle_reads_like_teams() { |
| 845 | assert_eq!( |
| 846 | toggle_tooltip(false), |
| 847 | "Show Formatting options (Ctrl+Shift+X)" |
| 848 | ); |
| 849 | assert_eq!( |
| 850 | toggle_tooltip(true), |
| 851 | "Hide Formatting options (Ctrl+Shift+X)" |
| 852 | ); |
| 853 | assert_ne!(toggle_symbol(false), toggle_symbol(true)); |
| 854 | assert_eq!(FormatButton::Strike.tooltip(), "Strikethrough (Ctrl+Alt+X)"); |
| 855 | } |
| 856 | } |