BlueGit

ict rusty-teams

feat(composer): format bar overflow into More options, one Font size button

- Bar never wraps: groups that do not fit leave from the end into a More options (...) menu, dividers only between visible groups
- fit_groups(width, groups) is a pure function with tests; the bar re-fits when the composer width changes
- Font size is one button with Teams' menu Large / Medium / Small (current one bold), as in the Teams bundle
- Deviation: whole groups move into the menu, Teams moves single items; menu rows are icon + label without submenu arrows

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

metis Daniel Däschle · pushed by metis

Authored 2026-11-04 · 3 weeks ago

Hash
9ffef475747892143db39fb83a7ccd55ada43330
Parent
699f1302
5 files changed +388 −36
crates/app-base/src/assets.rs +2 −1
@@ -8,7 +8,7 @@ macro_rules! symbols {
8 8 };
9 9 }
10 10
11 const SYMBOLS: [(&str, &[u8]); 72] = symbols![
11 const SYMBOLS: [(&str, &[u8]); 73] = symbols![
12 12 "bot",
13 13 "done",
14 14 "table",
@@ -81,6 +81,7 @@ const SYMBOLS: [(&str, &[u8]); 72] = symbols![
81 81 "fluent_text_clear_formatting",
82 82 "fluent_code_block",
83 83 "fluent_text_header_1",
84 "fluent_text_font_size",
84 85 ];
85 86
86 87 pub struct AppAssets;
@@ -8,7 +8,7 @@ macro_rules! symbols {
8 };
8 };
9}
9}
10
10
11const SYMBOLS: [(&str, &[u8]); 72] = symbols![
11const SYMBOLS: [(&str, &[u8]); 73] = symbols![
12 "bot",
12 "bot",
13 "done",
13 "done",
14 "table",
14 "table",
@@ -81,6 +81,7 @@ const SYMBOLS: [(&str, &[u8]); 72] = symbols![
81 "fluent_text_clear_formatting",
81 "fluent_text_clear_formatting",
82 "fluent_code_block",
82 "fluent_code_block",
83 "fluent_text_header_1",
83 "fluent_text_header_1",
84 "fluent_text_font_size",
84];
85];
85
86
86pub struct AppAssets;
87pub struct AppAssets;
crates/app/assets/symbols/fluent_text_font_size.svg added +1 −0
@@ -0,0 +1 @@
1 <svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M13 3.5c.2 0 .4.13.47.32l3 8v.02l1.5 3.98a.5.5 0 1 1-.94.36l-1.38-3.68h-5.3l-1.38 3.68a.5.5 0 0 1-.93 0L6.96 13.5H4.04l-1.08 2.69a.5.5 0 1 1-.92-.38l1.2-2.98v-.03l1.8-4.49a.5.5 0 0 1 .92 0l1.8 4.5v.02l.73 1.79 1.04-2.78v-.03l3-7.99A.5.5 0 0 1 13 3.5Zm-2.28 8h4.56L13 5.42l-2.28 6.08Zm-4.16 1L5.5 9.85 4.44 12.5h2.12Z"/></svg>
@@ -0,0 +1 @@
1<svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M13 3.5c.2 0 .4.13.47.32l3 8v.02l1.5 3.98a.5.5 0 1 1-.94.36l-1.38-3.68h-5.3l-1.38 3.68a.5.5 0 0 1-.93 0L6.96 13.5H4.04l-1.08 2.69a.5.5 0 1 1-.92-.38l1.2-2.98v-.03l1.8-4.49a.5.5 0 0 1 .92 0l1.8 4.5v.02l.73 1.79 1.04-2.78v-.03l3-7.99A.5.5 0 0 1 13 3.5Zm-2.28 8h4.56L13 5.42l-2.28 6.08Zm-4.16 1L5.5 9.85 4.44 12.5h2.12Z"/></svg>
crates/app/src/views/composer.rs +70 −10
@@ -230,6 +230,7 @@ pub struct Composer {
230 230 table_picker: Option<TablePicker>,
231 231 table_menu: Option<TableMenu>,
232 232 format_popup: Option<FormatPopup>,
233 format_overflow: bool,
233 234 format_bar: HashSet<String>,
234 235 active_cell: Option<(String, teams_core::Cell)>,
235 236 caret_before_table: usize,
@@ -457,6 +458,7 @@ impl Composer {
457 458 table_picker: None,
458 459 table_menu: None,
459 460 format_popup: None,
461 format_overflow: false,
460 462 format_bar: HashSet::new(),
461 463 active_cell: None,
462 464 caret_before_table: 0,
@@ -857,6 +859,7 @@ impl Composer {
857 859 self.active_cell = None;
858 860 self.table_picker = None;
859 861 self.format_popup = None;
862 self.format_overflow = false;
860 863 self.sync_tables(window, cx);
861 864 self.history.clear();
862 865 self.history_index = 0;
@@ -971,6 +974,9 @@ impl Composer {
971 974 }
972 975
973 976 fn press_format(&mut self, button: FormatButton, window: &mut Window, cx: &mut Context<Self>) {
977 if !button.opens_popup() {
978 self.format_overflow = false;
979 }
974 980 match button {
975 981 FormatButton::Bold => self.toggle_mark(MarkKind::Bold, cx),
976 982 FormatButton::Italic => self.toggle_mark(MarkKind::Italic, cx),
@@ -1007,6 +1013,8 @@ impl Composer {
1007 1013 FormatButton::Indent => self.indent_blocks(false, window, cx),
1008 1014 FormatButton::CodeBlock => self.toggle_code_block(window, cx),
1009 1015 FormatButton::Rule => self.insert_rule(window, cx),
1016 FormatButton::FontSize => self.toggle_format_popup(FormatPopup::FontSize, cx),
1017 FormatButton::More => self.toggle_format_overflow(cx),
1010 1018 }
1011 1019 }
1012 1020
@@ -1097,9 +1105,15 @@ impl Composer {
1097 1105 {
1098 1106 FormatState::On
1099 1107 }
1108 FormatButton::FontSize if self.format_popup == Some(FormatPopup::FontSize) => {
1109 FormatState::On
1110 }
1111 FormatButton::More if self.format_overflow => FormatState::On,
1100 1112 FormatButton::CodeBlock => self.draft.line_state(range, &LineKind::Code(None)),
1101 1113 FormatButton::Highlight
1102 1114 | FormatButton::FontColor
1115 | FormatButton::FontSize
1116 | FormatButton::More
1103 1117 | FormatButton::ParagraphStyle
1104 1118 | FormatButton::ClearFormatting
1105 1119 | FormatButton::Outdent
@@ -1117,7 +1131,7 @@ impl Composer {
1117 1131 ) -> ButtonState {
1118 1132 let enabled = match button {
1119 1133 FormatButton::TableInsertLine | FormatButton::TableRemoveLine => in_table,
1120 FormatButton::Table => true,
1134 FormatButton::Table | FormatButton::More => true,
1121 1135 FormatButton::Outdent => !in_table && self.draft.can_outdent(selection.clone()),
1122 1136 _ => !in_table,
1123 1137 };
@@ -1148,6 +1162,7 @@ impl Composer {
1148 1162 self.table_picker = None;
1149 1163 self.table_menu = None;
1150 1164 self.format_popup = None;
1165 self.format_overflow = false;
1151 1166 }
1152 1167 cx.notify();
1153 1168 }
@@ -1706,6 +1721,7 @@ impl Composer {
1706 1721 self.table_picker = None;
1707 1722 self.table_menu = None;
1708 1723 self.format_popup = None;
1724 self.format_overflow = false;
1709 1725 self.reset_importance();
1710 1726 if !carry {
1711 1727 self.load_stored_draft(conversation_id, window, cx);
@@ -2563,28 +2579,65 @@ impl Composer {
2563 2579 Some(element)
2564 2580 }
2565 2581
2566 fn render_format_bar(&self, window: &Window, cx: &mut Context<Self>) -> AnyElement {
2567 let selection = self.selection(cx);
2568 let in_table = self.in_table(window, cx);
2569 let state_of = |button| self.button_state(button, &selection, in_table);
2582 fn format_press_handler(&self, cx: &mut Context<Self>) -> format_toolbar::PressHandler {
2570 2583 let composer = cx.weak_entity();
2571 let on_press: format_toolbar::PressHandler = Rc::new(move |button, window, cx| {
2584 Rc::new(move |button, window, cx| {
2572 2585 composer
2573 2586 .update(cx, |this, cx| this.press_format(button, window, cx))
2574 2587 .ok();
2588 })
2589 }
2590
2591 fn format_bar_fit(&self) -> format_toolbar::BarFit<'static> {
2592 let available = self.composer_bounds.get().map_or(f32::INFINITY, |bounds| {
2593 f32::from(bounds.size.width) - 2. * (COMPOSER_BUTTON_INSET + 1.)
2575 2594 });
2576 let mut overlay = |button| match button {
2595 format_toolbar::fit_groups(available, &format_toolbar::BAR_GROUPS)
2596 }
2597
2598 fn render_format_overlay(
2599 &self,
2600 button: FormatButton,
2601 overflow: &[&[FormatButton]],
2602 window: &Window,
2603 cx: &mut Context<Self>,
2604 ) -> Option<AnyElement> {
2605 match button {
2577 2606 FormatButton::Table => self.render_table_picker(cx),
2578 2607 FormatButton::TableInsertLine => self.render_table_menu(TableLineMenu::Insert, cx),
2579 2608 FormatButton::TableRemoveLine => self.render_table_menu(TableLineMenu::Remove, cx),
2580 2609 FormatButton::Highlight => self.render_format_popup(FormatPopup::Highlight, cx),
2581 2610 FormatButton::FontColor => self.render_format_popup(FormatPopup::FontColor, cx),
2611 FormatButton::FontSize => self.render_format_popup(FormatPopup::FontSize, cx),
2582 2612 FormatButton::ParagraphStyle => {
2583 2613 self.render_format_popup(FormatPopup::ParagraphStyle, cx)
2584 2614 }
2615 FormatButton::More if self.format_overflow => {
2616 let selection = self.selection(cx);
2617 let in_table = self.in_table(window, cx);
2618 let state_of = |button| self.button_state(button, &selection, in_table);
2619 let on_press = self.format_press_handler(cx);
2620 let mut overlay = |button| self.render_format_overlay(button, &[], window, cx);
2621 let menu = format_toolbar::render_overflow_menu(
2622 overflow,
2623 &state_of,
2624 on_press,
2625 &mut overlay,
2626 );
2627 Some(self.render_overflow_surface(menu, cx))
2628 }
2585 2629 _ => None,
2586 };
2587 let row = format_toolbar::render_format_bar(&state_of, on_press, &mut overlay);
2630 }
2631 }
2632
2633 fn render_format_bar(&self, window: &Window, cx: &mut Context<Self>) -> AnyElement {
2634 let selection = self.selection(cx);
2635 let in_table = self.in_table(window, cx);
2636 let state_of = |button| self.button_state(button, &selection, in_table);
2637 let on_press = self.format_press_handler(cx);
2638 let fit = self.format_bar_fit();
2639 let mut overlay = |button| self.render_format_overlay(button, fit.overflow, window, cx);
2640 let row = format_toolbar::render_format_bar(fit, &state_of, on_press, &mut overlay);
2588 2641 v_flex()
2589 2642 .debug_selector(|| COMPOSER_FORMAT_BAR_TAG.into())
2590 2643 .w_full()
@@ -3261,7 +3314,14 @@ impl Render for Composer {
3261 3314 })
3262 3315 .child(
3263 3316 canvas(
3264 move |bounds, _, _| composer_bounds.set(Some(bounds)),
3317 move |bounds, window, _| {
3318 let previous = composer_bounds.replace(Some(bounds));
3319 if previous.map(|previous| previous.size.width)
3320 != Some(bounds.size.width)
3321 {
3322 window.refresh();
3323 }
3324 },
3265 3325 |_, _, _, _| {},
3266 3326 )
3267 3327 .absolute()
@@ -230,6 +230,7 @@ pub struct Composer {
230 table_picker: Option<TablePicker>,
230 table_picker: Option<TablePicker>,
231 table_menu: Option<TableMenu>,
231 table_menu: Option<TableMenu>,
232 format_popup: Option<FormatPopup>,
232 format_popup: Option<FormatPopup>,
233 format_overflow: bool,
233 format_bar: HashSet<String>,
234 format_bar: HashSet<String>,
234 active_cell: Option<(String, teams_core::Cell)>,
235 active_cell: Option<(String, teams_core::Cell)>,
235 caret_before_table: usize,
236 caret_before_table: usize,
@@ -457,6 +458,7 @@ impl Composer {
457 table_picker: None,
458 table_picker: None,
458 table_menu: None,
459 table_menu: None,
459 format_popup: None,
460 format_popup: None,
461 format_overflow: false,
460 format_bar: HashSet::new(),
462 format_bar: HashSet::new(),
461 active_cell: None,
463 active_cell: None,
462 caret_before_table: 0,
464 caret_before_table: 0,
@@ -857,6 +859,7 @@ impl Composer {
857 self.active_cell = None;
859 self.active_cell = None;
858 self.table_picker = None;
860 self.table_picker = None;
859 self.format_popup = None;
861 self.format_popup = None;
862 self.format_overflow = false;
860 self.sync_tables(window, cx);
863 self.sync_tables(window, cx);
861 self.history.clear();
864 self.history.clear();
862 self.history_index = 0;
865 self.history_index = 0;
@@ -971,6 +974,9 @@ impl Composer {
971 }
974 }
972
975
973 fn press_format(&mut self, button: FormatButton, window: &mut Window, cx: &mut Context<Self>) {
976 fn press_format(&mut self, button: FormatButton, window: &mut Window, cx: &mut Context<Self>) {
977 if !button.opens_popup() {
978 self.format_overflow = false;
979 }
974 match button {
980 match button {
975 FormatButton::Bold => self.toggle_mark(MarkKind::Bold, cx),
981 FormatButton::Bold => self.toggle_mark(MarkKind::Bold, cx),
976 FormatButton::Italic => self.toggle_mark(MarkKind::Italic, cx),
982 FormatButton::Italic => self.toggle_mark(MarkKind::Italic, cx),
@@ -1007,6 +1013,8 @@ impl Composer {
1007 FormatButton::Indent => self.indent_blocks(false, window, cx),
1013 FormatButton::Indent => self.indent_blocks(false, window, cx),
1008 FormatButton::CodeBlock => self.toggle_code_block(window, cx),
1014 FormatButton::CodeBlock => self.toggle_code_block(window, cx),
1009 FormatButton::Rule => self.insert_rule(window, cx),
1015 FormatButton::Rule => self.insert_rule(window, cx),
1016 FormatButton::FontSize => self.toggle_format_popup(FormatPopup::FontSize, cx),
1017 FormatButton::More => self.toggle_format_overflow(cx),
1010 }
1018 }
1011 }
1019 }
1012
1020
@@ -1097,9 +1105,15 @@ impl Composer {
1097 {
1105 {
1098 FormatState::On
1106 FormatState::On
1099 }
1107 }
1108 FormatButton::FontSize if self.format_popup == Some(FormatPopup::FontSize) => {
1109 FormatState::On
1110 }
1111 FormatButton::More if self.format_overflow => FormatState::On,
1100 FormatButton::CodeBlock => self.draft.line_state(range, &LineKind::Code(None)),
1112 FormatButton::CodeBlock => self.draft.line_state(range, &LineKind::Code(None)),
1101 FormatButton::Highlight
1113 FormatButton::Highlight
1102 | FormatButton::FontColor
1114 | FormatButton::FontColor
1115 | FormatButton::FontSize
1116 | FormatButton::More
1103 | FormatButton::ParagraphStyle
1117 | FormatButton::ParagraphStyle
1104 | FormatButton::ClearFormatting
1118 | FormatButton::ClearFormatting
1105 | FormatButton::Outdent
1119 | FormatButton::Outdent
@@ -1117,7 +1131,7 @@ impl Composer {
1117 ) -> ButtonState {
1131 ) -> ButtonState {
1118 let enabled = match button {
1132 let enabled = match button {
1119 FormatButton::TableInsertLine | FormatButton::TableRemoveLine => in_table,
1133 FormatButton::TableInsertLine | FormatButton::TableRemoveLine => in_table,
1120 FormatButton::Table => true,
1134 FormatButton::Table | FormatButton::More => true,
1121 FormatButton::Outdent => !in_table && self.draft.can_outdent(selection.clone()),
1135 FormatButton::Outdent => !in_table && self.draft.can_outdent(selection.clone()),
1122 _ => !in_table,
1136 _ => !in_table,
1123 };
1137 };
@@ -1148,6 +1162,7 @@ impl Composer {
1148 self.table_picker = None;
1162 self.table_picker = None;
1149 self.table_menu = None;
1163 self.table_menu = None;
1150 self.format_popup = None;
1164 self.format_popup = None;
1165 self.format_overflow = false;
1151 }
1166 }
1152 cx.notify();
1167 cx.notify();
1153 }
1168 }
@@ -1706,6 +1721,7 @@ impl Composer {
1706 self.table_picker = None;
1721 self.table_picker = None;
1707 self.table_menu = None;
1722 self.table_menu = None;
1708 self.format_popup = None;
1723 self.format_popup = None;
1724 self.format_overflow = false;
1709 self.reset_importance();
1725 self.reset_importance();
1710 if !carry {
1726 if !carry {
1711 self.load_stored_draft(conversation_id, window, cx);
1727 self.load_stored_draft(conversation_id, window, cx);
@@ -2563,28 +2579,65 @@ impl Composer {
2563 Some(element)
2579 Some(element)
2564 }
2580 }
2565
2581
2566 fn render_format_bar(&self, window: &Window, cx: &mut Context<Self>) -> AnyElement {
2582 fn format_press_handler(&self, cx: &mut Context<Self>) -> format_toolbar::PressHandler {
2567 let selection = self.selection(cx);
2568 let in_table = self.in_table(window, cx);
2569 let state_of = |button| self.button_state(button, &selection, in_table);
2570 let composer = cx.weak_entity();
2583 let composer = cx.weak_entity();
2571 let on_press: format_toolbar::PressHandler = Rc::new(move |button, window, cx| {
2584 Rc::new(move |button, window, cx| {
2572 composer
2585 composer
2573 .update(cx, |this, cx| this.press_format(button, window, cx))
2586 .update(cx, |this, cx| this.press_format(button, window, cx))
2574 .ok();
2587 .ok();
2588 })
2589 }
2590
2591 fn format_bar_fit(&self) -> format_toolbar::BarFit<'static> {
2592 let available = self.composer_bounds.get().map_or(f32::INFINITY, |bounds| {
2593 f32::from(bounds.size.width) - 2. * (COMPOSER_BUTTON_INSET + 1.)
2575 });
2594 });
2576 let mut overlay = |button| match button {
2595 format_toolbar::fit_groups(available, &format_toolbar::BAR_GROUPS)
2596 }
2597
2598 fn render_format_overlay(
2599 &self,
2600 button: FormatButton,
2601 overflow: &[&[FormatButton]],
2602 window: &Window,
2603 cx: &mut Context<Self>,
2604 ) -> Option<AnyElement> {
2605 match button {
2577 FormatButton::Table => self.render_table_picker(cx),
2606 FormatButton::Table => self.render_table_picker(cx),
2578 FormatButton::TableInsertLine => self.render_table_menu(TableLineMenu::Insert, cx),
2607 FormatButton::TableInsertLine => self.render_table_menu(TableLineMenu::Insert, cx),
2579 FormatButton::TableRemoveLine => self.render_table_menu(TableLineMenu::Remove, cx),
2608 FormatButton::TableRemoveLine => self.render_table_menu(TableLineMenu::Remove, cx),
2580 FormatButton::Highlight => self.render_format_popup(FormatPopup::Highlight, cx),
2609 FormatButton::Highlight => self.render_format_popup(FormatPopup::Highlight, cx),
2581 FormatButton::FontColor => self.render_format_popup(FormatPopup::FontColor, cx),
2610 FormatButton::FontColor => self.render_format_popup(FormatPopup::FontColor, cx),
2611 FormatButton::FontSize => self.render_format_popup(FormatPopup::FontSize, cx),
2582 FormatButton::ParagraphStyle => {
2612 FormatButton::ParagraphStyle => {
2583 self.render_format_popup(FormatPopup::ParagraphStyle, cx)
2613 self.render_format_popup(FormatPopup::ParagraphStyle, cx)
2584 }
2614 }
2615 FormatButton::More if self.format_overflow => {
2616 let selection = self.selection(cx);
2617 let in_table = self.in_table(window, cx);
2618 let state_of = |button| self.button_state(button, &selection, in_table);
2619 let on_press = self.format_press_handler(cx);
2620 let mut overlay = |button| self.render_format_overlay(button, &[], window, cx);
2621 let menu = format_toolbar::render_overflow_menu(
2622 overflow,
2623 &state_of,
2624 on_press,
2625 &mut overlay,
2626 );
2627 Some(self.render_overflow_surface(menu, cx))
2628 }
2585 _ => None,
2629 _ => None,
2586 };
2630 }
2587 let row = format_toolbar::render_format_bar(&state_of, on_press, &mut overlay);
2631 }
2632
2633 fn render_format_bar(&self, window: &Window, cx: &mut Context<Self>) -> AnyElement {
2634 let selection = self.selection(cx);
2635 let in_table = self.in_table(window, cx);
2636 let state_of = |button| self.button_state(button, &selection, in_table);
2637 let on_press = self.format_press_handler(cx);
2638 let fit = self.format_bar_fit();
2639 let mut overlay = |button| self.render_format_overlay(button, fit.overflow, window, cx);
2640 let row = format_toolbar::render_format_bar(fit, &state_of, on_press, &mut overlay);
2588 v_flex()
2641 v_flex()
2589 .debug_selector(|| COMPOSER_FORMAT_BAR_TAG.into())
2642 .debug_selector(|| COMPOSER_FORMAT_BAR_TAG.into())
2590 .w_full()
2643 .w_full()
@@ -3261,7 +3314,14 @@ impl Render for Composer {
3261 })
3314 })
3262 .child(
3315 .child(
3263 canvas(
3316 canvas(
3264 move |bounds, _, _| composer_bounds.set(Some(bounds)),
3317 move |bounds, window, _| {
3318 let previous = composer_bounds.replace(Some(bounds));
3319 if previous.map(|previous| previous.size.width)
3320 != Some(bounds.size.width)
3321 {
3322 window.refresh();
3323 }
3324 },
3265 |_, _, _, _| {},
3325 |_, _, _, _| {},
3266 )
3326 )
3267 .absolute()
3327 .absolute()
crates/app/src/views/composer/format_menus.rs +88 −4
@@ -3,9 +3,10 @@ use std::ops::Range;
3 3 use gpui_kit::component::{h_flex, tooltip::Tooltip, v_flex};
4 4 use gpui_kit::prelude::FluentBuilder as _;
5 5 use gpui_kit::*;
6 use teams_core::{Draft, LineKind, MarkKind, TypingStyle};
6 use teams_core::{Draft, FormatState, LineKind, MarkKind, SizeStep, TypingStyle};
7 7
8 8 use super::Composer;
9 use super::FormatButton;
9 10 use crate::theme::{self, ElevatedSurface as _};
10 11
11 12 const SWATCH: f32 = 24.;
@@ -13,6 +14,7 @@ const SWATCH_GAP: f32 = 6.;
13 14 const SWATCH_COLUMNS: usize = 4;
14 15 const POPUP_PADDING: f32 = 8.;
15 16 const MENU_WIDTH: f32 = 180.;
17 const FONT_SIZE_MENU_WIDTH: f32 = 120.;
16 18 const MENU_PADDING: f32 = 4.;
17 19 const MENU_ROW_PADDING_X: f32 = 10.;
18 20 const MENU_ROW_PADDING_Y: f32 = 6.;
@@ -23,6 +25,7 @@ pub(super) enum FormatPopup {
23 25 Highlight,
24 26 FontColor,
25 27 ParagraphStyle,
28 FontSize,
26 29 }
27 30
28 31 pub(super) struct Swatch {
@@ -60,6 +63,14 @@ pub(super) const PARAGRAPH_STYLES: [(Option<u8>, &str); 4] = [
60 63 (Some(3), "Heading 3"),
61 64 ];
62 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
63 74 pub(super) fn paragraph_label(level: Option<u8>) -> &'static str {
64 75 PARAGRAPH_STYLES
65 76 .iter()
@@ -106,6 +117,41 @@ impl Composer {
106 117 cx.notify();
107 118 }
108 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
109 155 /// Text typed at a collapsed cursor takes the change, a selection gets it at once.
110 156 fn change_style(
111 157 &mut self,
@@ -141,7 +187,7 @@ impl Composer {
141 187 window: &mut Window,
142 188 cx: &mut Context<Self>,
143 189 ) {
144 self.format_popup = None;
190 self.close_format_popups();
145 191 match popup {
146 192 FormatPopup::FontColor => self.change_style(
147 193 |pending, active| pending.set_color(color, active),
@@ -178,7 +224,7 @@ impl Composer {
178 224 window: &mut Window,
179 225 cx: &mut Context<Self>,
180 226 ) {
181 self.format_popup = None;
227 self.close_format_popups();
182 228 if self.cell_has_focus(window, cx) {
183 229 return;
184 230 }
@@ -244,6 +290,7 @@ impl Composer {
244 290 }
245 291 let content = match popup {
246 292 FormatPopup::ParagraphStyle => self.render_paragraph_menu(cx),
293 FormatPopup::FontSize => self.render_font_size_menu(cx),
247 294 _ => self.render_color_picker(popup, cx),
248 295 };
249 296 Some(
@@ -312,6 +359,32 @@ impl Composer {
312 359 .child(clear)
313 360 }
314 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
315 388 fn render_paragraph_menu(&self, cx: &mut Context<Self>) -> Div {
316 389 let current = self.draft.heading_at(self.selection(cx).start);
317 390 let rows = PARAGRAPH_STYLES
@@ -346,7 +419,18 @@ impl Composer {
346 419
347 420 #[cfg(test)]
348 421 mod tests {
349 use super::{FONT_COLORS, HIGHLIGHT_COLORS, PARAGRAPH_STYLES, paragraph_label};
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 }
350 434
351 435 #[test]
352 436 fn palettes_hold_the_eight_teams_colours_each() {
@@ -3,9 +3,10 @@ use std::ops::Range;
3use gpui_kit::component::{h_flex, tooltip::Tooltip, v_flex};
3use gpui_kit::component::{h_flex, tooltip::Tooltip, v_flex};
4use gpui_kit::prelude::FluentBuilder as _;
4use gpui_kit::prelude::FluentBuilder as _;
5use gpui_kit::*;
5use gpui_kit::*;
6use teams_core::{Draft, LineKind, MarkKind, TypingStyle};
6use teams_core::{Draft, FormatState, LineKind, MarkKind, SizeStep, TypingStyle};
7
7
8use super::Composer;
8use super::Composer;
9use super::FormatButton;
9use crate::theme::{self, ElevatedSurface as _};
10use crate::theme::{self, ElevatedSurface as _};
10
11
11const SWATCH: f32 = 24.;
12const SWATCH: f32 = 24.;
@@ -13,6 +14,7 @@ const SWATCH_GAP: f32 = 6.;
13const SWATCH_COLUMNS: usize = 4;
14const SWATCH_COLUMNS: usize = 4;
14const POPUP_PADDING: f32 = 8.;
15const POPUP_PADDING: f32 = 8.;
15const MENU_WIDTH: f32 = 180.;
16const MENU_WIDTH: f32 = 180.;
17const FONT_SIZE_MENU_WIDTH: f32 = 120.;
16const MENU_PADDING: f32 = 4.;
18const MENU_PADDING: f32 = 4.;
17const MENU_ROW_PADDING_X: f32 = 10.;
19const MENU_ROW_PADDING_X: f32 = 10.;
18const MENU_ROW_PADDING_Y: f32 = 6.;
20const MENU_ROW_PADDING_Y: f32 = 6.;
@@ -23,6 +25,7 @@ pub(super) enum FormatPopup {
23 Highlight,
25 Highlight,
24 FontColor,
26 FontColor,
25 ParagraphStyle,
27 ParagraphStyle,
28 FontSize,
26}
29}
27
30
28pub(super) struct Swatch {
31pub(super) struct Swatch {
@@ -60,6 +63,14 @@ pub(super) const PARAGRAPH_STYLES: [(Option<u8>, &str); 4] = [
60 (Some(3), "Heading 3"),
63 (Some(3), "Heading 3"),
61];
64];
62
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
63pub(super) fn paragraph_label(level: Option<u8>) -> &'static str {
74pub(super) fn paragraph_label(level: Option<u8>) -> &'static str {
64 PARAGRAPH_STYLES
75 PARAGRAPH_STYLES
65 .iter()
76 .iter()
@@ -106,6 +117,41 @@ impl Composer {
106 cx.notify();
117 cx.notify();
107 }
118 }
108
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
109 /// Text typed at a collapsed cursor takes the change, a selection gets it at once.
155 /// Text typed at a collapsed cursor takes the change, a selection gets it at once.
110 fn change_style(
156 fn change_style(
111 &mut self,
157 &mut self,
@@ -141,7 +187,7 @@ impl Composer {
141 window: &mut Window,
187 window: &mut Window,
142 cx: &mut Context<Self>,
188 cx: &mut Context<Self>,
143 ) {
189 ) {
144 self.format_popup = None;
190 self.close_format_popups();
145 match popup {
191 match popup {
146 FormatPopup::FontColor => self.change_style(
192 FormatPopup::FontColor => self.change_style(
147 |pending, active| pending.set_color(color, active),
193 |pending, active| pending.set_color(color, active),
@@ -178,7 +224,7 @@ impl Composer {
178 window: &mut Window,
224 window: &mut Window,
179 cx: &mut Context<Self>,
225 cx: &mut Context<Self>,
180 ) {
226 ) {
181 self.format_popup = None;
227 self.close_format_popups();
182 if self.cell_has_focus(window, cx) {
228 if self.cell_has_focus(window, cx) {
183 return;
229 return;
184 }
230 }
@@ -244,6 +290,7 @@ impl Composer {
244 }
290 }
245 let content = match popup {
291 let content = match popup {
246 FormatPopup::ParagraphStyle => self.render_paragraph_menu(cx),
292 FormatPopup::ParagraphStyle => self.render_paragraph_menu(cx),
293 FormatPopup::FontSize => self.render_font_size_menu(cx),
247 _ => self.render_color_picker(popup, cx),
294 _ => self.render_color_picker(popup, cx),
248 };
295 };
249 Some(
296 Some(
@@ -312,6 +359,32 @@ impl Composer {
312 .child(clear)
359 .child(clear)
313 }
360 }
314
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
315 fn render_paragraph_menu(&self, cx: &mut Context<Self>) -> Div {
388 fn render_paragraph_menu(&self, cx: &mut Context<Self>) -> Div {
316 let current = self.draft.heading_at(self.selection(cx).start);
389 let current = self.draft.heading_at(self.selection(cx).start);
317 let rows = PARAGRAPH_STYLES
390 let rows = PARAGRAPH_STYLES
@@ -346,7 +419,18 @@ impl Composer {
346
419
347#[cfg(test)]
420#[cfg(test)]
348mod tests {
421mod tests {
349 use super::{FONT_COLORS, HIGHLIGHT_COLORS, PARAGRAPH_STYLES, paragraph_label};
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 }
350
434
351 #[test]
435 #[test]
352 fn palettes_hold_the_eight_teams_colours_each() {
436 fn palettes_hold_the_eight_teams_colours_each() {
crates/app/src/views/format_toolbar.rs +227 −21
@@ -55,6 +55,8 @@ pub enum FormatButton {
55 55 Indent,
56 56 CodeBlock,
57 57 Rule,
58 FontSize,
59 More,
58 60 }
59 61
60 62 const STYLE_GROUP: &[FormatButton] = &[
@@ -84,9 +86,7 @@ const TABLE_GROUP: &[FormatButton] = &[
84 86 const FONT_GROUP: &[FormatButton] = &[
85 87 FormatButton::Highlight,
86 88 FormatButton::FontColor,
87 FormatButton::SizeSmall,
88 FormatButton::SizeNormal,
89 FormatButton::SizeLarge,
89 FormatButton::FontSize,
90 90 FormatButton::ParagraphStyle,
91 91 FormatButton::ClearFormatting,
92 92 ];
@@ -125,6 +125,44 @@ pub const BAR_GROUPS: [&[FormatButton]; 6] = [
125 125 ];
126 126
127 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 }
128 166
129 167 pub const TOGGLE_SHORTCUT: &str = "Ctrl+Shift+X";
130 168
@@ -205,9 +243,31 @@ impl FormatButton {
205 243 FormatButton::Indent => "Increase indent",
206 244 FormatButton::CodeBlock => "Code block (Ctrl+Shift+B)",
207 245 FormatButton::Rule => "Insert horizontal rule",
246 FormatButton::FontSize => "Font size",
247 FormatButton::More => "More options",
208 248 }
209 249 }
210 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
211 271 fn content(self, color: Hsla, label: Option<&'static str>) -> AnyElement {
212 272 let letter = |text: &'static str| div().text_size(px(15.)).text_color(color).child(text);
213 273 match self {
@@ -256,6 +316,10 @@ impl FormatButton {
256 316 }
257 317 FormatButton::CodeBlock => symbol("fluent_code_block", 20., color).into_any_element(),
258 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(),
259 323 }
260 324 }
261 325
@@ -390,16 +454,18 @@ fn button(
390 454 })
391 455 .on_click(move |_, window, cx| on_press(format, window, cx))
392 456 })
393 .children(overlay.map(|overlay| {
394 div().absolute().top(px(BUTTON + GAP / 2.)).left_0().child(
395 deferred(
396 anchored()
397 .snap_to_window_with_margin(px(GAP))
398 .child(overlay),
399 )
400 .with_priority(2),
401 )
402 }))
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 )
403 469 }
404 470
405 471 fn separator() -> Div {
@@ -416,7 +482,7 @@ fn groups_row(
416 482 on_press: PressHandler,
417 483 overlay: &mut dyn FnMut(FormatButton) -> Option<AnyElement>,
418 484 ) -> Div {
419 let mut row = h_flex().flex_wrap().gap(px(BUTTON_GAP)).items_center();
485 let mut row = h_flex().gap(px(BUTTON_GAP)).items_center();
420 486 for (index, group) in groups.iter().enumerate() {
421 487 if index > 0 {
422 488 row = row.child(separator());
@@ -437,13 +503,103 @@ fn bar(state: &dyn Fn(FormatButton) -> ButtonState, on_press: PressHandler) -> D
437 503 groups_row(&SELECTION_GROUPS, state, on_press, &mut |_| None)
438 504 }
439 505
440 /// The format bar inside the composer box. `overlay` hangs a popup under a button.
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`.
441 508 pub fn render_format_bar(
509 fit: BarFit<'_>,
442 510 state: &dyn Fn(FormatButton) -> ButtonState,
443 511 on_press: PressHandler,
444 512 overlay: &mut dyn FnMut(FormatButton) -> Option<AnyElement>,
445 513 ) -> Div {
446 groups_row(&BAR_GROUPS, state, on_press, overlay)
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)))
447 603 }
448 604
449 605 fn link_editor(field: &Entity<InputState>, refused: bool, on_apply: ApplyHandler) -> Div {
@@ -559,10 +715,16 @@ mod tests {
559 715 use gpui_kit::px;
560 716
561 717 use super::{
562 ARROW_MARGIN, BAR_GROUPS, BUTTON, FormatButton, SELECTION_GROUPS, bar_width, place,
563 toggle_symbol, toggle_tooltip,
718 ARROW_MARGIN, BAR_GROUPS, BUTTON, FormatButton, SELECTION_GROUPS, bar_width, fit_groups,
719 groups_width, place, toggle_symbol, toggle_tooltip,
564 720 };
565 721
722 const SIZES: [FormatButton; 3] = [
723 FormatButton::SizeSmall,
724 FormatButton::SizeNormal,
725 FormatButton::SizeLarge,
726 ];
727
566 728 #[test]
567 729 fn the_bar_centers_over_the_selection() {
568 730 let placement = place(px(400.), px(300.), (px(100.), px(900.)), 200.);
@@ -616,8 +778,12 @@ mod tests {
616 778 ][..]
617 779 );
618 780 assert_eq!(
619 BAR_GROUPS[2][..2],
620 [FormatButton::Highlight, FormatButton::FontColor]
781 BAR_GROUPS[2][..3],
782 [
783 FormatButton::Highlight,
784 FormatButton::FontColor,
785 FormatButton::FontSize
786 ]
621 787 );
622 788 assert_eq!(
623 789 BAR_GROUPS[3],
@@ -630,10 +796,50 @@ mod tests {
630 796 );
631 797 assert_eq!(BAR_GROUPS[4].last(), Some(&FormatButton::Rule));
632 798 for button in SELECTION_GROUPS.iter().flat_map(|group| group.iter()) {
633 assert!(BAR_GROUPS.iter().any(|group| group.contains(button)));
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)));
634 805 }
635 806 }
636 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
637 843 #[test]
638 844 fn the_format_toggle_reads_like_teams() {
639 845 assert_eq!(
@@ -55,6 +55,8 @@ pub enum FormatButton {
55 Indent,
55 Indent,
56 CodeBlock,
56 CodeBlock,
57 Rule,
57 Rule,
58 FontSize,
59 More,
58}
60}
59
61
60const STYLE_GROUP: &[FormatButton] = &[
62const STYLE_GROUP: &[FormatButton] = &[
@@ -84,9 +86,7 @@ const TABLE_GROUP: &[FormatButton] = &[
84const FONT_GROUP: &[FormatButton] = &[
86const FONT_GROUP: &[FormatButton] = &[
85 FormatButton::Highlight,
87 FormatButton::Highlight,
86 FormatButton::FontColor,
88 FormatButton::FontColor,
87 FormatButton::SizeSmall,
89 FormatButton::FontSize,
88 FormatButton::SizeNormal,
89 FormatButton::SizeLarge,
90 FormatButton::ParagraphStyle,
90 FormatButton::ParagraphStyle,
91 FormatButton::ClearFormatting,
91 FormatButton::ClearFormatting,
92];
92];
@@ -125,6 +125,44 @@ pub const BAR_GROUPS: [&[FormatButton]; 6] = [
125];
125];
126
126
127pub const PARAGRAPH_STYLE_WIDTH: f32 = 104.;
127pub const PARAGRAPH_STYLE_WIDTH: f32 = 104.;
128const SEPARATOR_WIDTH: f32 = 1. + 2. * SEPARATOR_MARGIN;
129const MENU_ROW_HEIGHT: f32 = 32.;
130const MENU_ROW_PADDING_X: f32 = 10.;
131const MENU_PADDING: f32 = 4.;
132const 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)]
136pub struct BarFit<'a> {
137 pub visible: &'a [&'a [FormatButton]],
138 pub overflow: &'a [&'a [FormatButton]],
139}
140
141fn 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
146fn 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.
153pub 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}
128
166
129pub const TOGGLE_SHORTCUT: &str = "Ctrl+Shift+X";
167pub const TOGGLE_SHORTCUT: &str = "Ctrl+Shift+X";
130
168
@@ -205,9 +243,31 @@ impl FormatButton {
205 FormatButton::Indent => "Increase indent",
243 FormatButton::Indent => "Increase indent",
206 FormatButton::CodeBlock => "Code block (Ctrl+Shift+B)",
244 FormatButton::CodeBlock => "Code block (Ctrl+Shift+B)",
207 FormatButton::Rule => "Insert horizontal rule",
245 FormatButton::Rule => "Insert horizontal rule",
246 FormatButton::FontSize => "Font size",
247 FormatButton::More => "More options",
208 }
248 }
209 }
249 }
210
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
211 fn content(self, color: Hsla, label: Option<&'static str>) -> AnyElement {
271 fn content(self, color: Hsla, label: Option<&'static str>) -> AnyElement {
212 let letter = |text: &'static str| div().text_size(px(15.)).text_color(color).child(text);
272 let letter = |text: &'static str| div().text_size(px(15.)).text_color(color).child(text);
213 match self {
273 match self {
@@ -256,6 +316,10 @@ impl FormatButton {
256 }
316 }
257 FormatButton::CodeBlock => symbol("fluent_code_block", 20., color).into_any_element(),
317 FormatButton::CodeBlock => symbol("fluent_code_block", 20., color).into_any_element(),
258 FormatButton::Rule => symbol("fluent_line_horizontal_1", 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(),
259 }
323 }
260 }
324 }
261
325
@@ -390,16 +454,18 @@ fn button(
390 })
454 })
391 .on_click(move |_, window, cx| on_press(format, window, cx))
455 .on_click(move |_, window, cx| on_press(format, window, cx))
392 })
456 })
393 .children(overlay.map(|overlay| {
457 .children(overlay.map(|overlay| overlay_below(overlay, BUTTON + GAP / 2.)))
394 div().absolute().top(px(BUTTON + GAP / 2.)).left_0().child(
458}
395 deferred(
459
396 anchored()
460fn overlay_below(overlay: AnyElement, offset: f32) -> Div {
397 .snap_to_window_with_margin(px(GAP))
461 div().absolute().top(px(offset)).left_0().child(
398 .child(overlay),
462 deferred(
399 )
463 anchored()
400 .with_priority(2),
464 .snap_to_window_with_margin(px(GAP))
401 )
465 .child(overlay),
402 }))
466 )
467 .with_priority(2),
468 )
403}
469}
404
470
405fn separator() -> Div {
471fn separator() -> Div {
@@ -416,7 +482,7 @@ fn groups_row(
416 on_press: PressHandler,
482 on_press: PressHandler,
417 overlay: &mut dyn FnMut(FormatButton) -> Option<AnyElement>,
483 overlay: &mut dyn FnMut(FormatButton) -> Option<AnyElement>,
418) -> Div {
484) -> Div {
419 let mut row = h_flex().flex_wrap().gap(px(BUTTON_GAP)).items_center();
485 let mut row = h_flex().gap(px(BUTTON_GAP)).items_center();
420 for (index, group) in groups.iter().enumerate() {
486 for (index, group) in groups.iter().enumerate() {
421 if index > 0 {
487 if index > 0 {
422 row = row.child(separator());
488 row = row.child(separator());
@@ -437,13 +503,103 @@ fn bar(state: &dyn Fn(FormatButton) -> ButtonState, on_press: PressHandler) -> D
437 groups_row(&SELECTION_GROUPS, state, on_press, &mut |_| None)
503 groups_row(&SELECTION_GROUPS, state, on_press, &mut |_| None)
438}
504}
439
505
440/// The format bar inside the composer box. `overlay` hangs a popup under a button.
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`.
441pub fn render_format_bar(
508pub fn render_format_bar(
509 fit: BarFit<'_>,
442 state: &dyn Fn(FormatButton) -> ButtonState,
510 state: &dyn Fn(FormatButton) -> ButtonState,
443 on_press: PressHandler,
511 on_press: PressHandler,
444 overlay: &mut dyn FnMut(FormatButton) -> Option<AnyElement>,
512 overlay: &mut dyn FnMut(FormatButton) -> Option<AnyElement>,
445) -> Div {
513) -> Div {
446 groups_row(&BAR_GROUPS, state, on_press, overlay)
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.
527pub 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
556fn 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)))
447}
603}
448
604
449fn link_editor(field: &Entity<InputState>, refused: bool, on_apply: ApplyHandler) -> Div {
605fn link_editor(field: &Entity<InputState>, refused: bool, on_apply: ApplyHandler) -> Div {
@@ -559,10 +715,16 @@ mod tests {
559 use gpui_kit::px;
715 use gpui_kit::px;
560
716
561 use super::{
717 use super::{
562 ARROW_MARGIN, BAR_GROUPS, BUTTON, FormatButton, SELECTION_GROUPS, bar_width, place,
718 ARROW_MARGIN, BAR_GROUPS, BUTTON, FormatButton, SELECTION_GROUPS, bar_width, fit_groups,
563 toggle_symbol, toggle_tooltip,
719 groups_width, place, toggle_symbol, toggle_tooltip,
564 };
720 };
565
721
722 const SIZES: [FormatButton; 3] = [
723 FormatButton::SizeSmall,
724 FormatButton::SizeNormal,
725 FormatButton::SizeLarge,
726 ];
727
566 #[test]
728 #[test]
567 fn the_bar_centers_over_the_selection() {
729 fn the_bar_centers_over_the_selection() {
568 let placement = place(px(400.), px(300.), (px(100.), px(900.)), 200.);
730 let placement = place(px(400.), px(300.), (px(100.), px(900.)), 200.);
@@ -616,8 +778,12 @@ mod tests {
616 ][..]
778 ][..]
617 );
779 );
618 assert_eq!(
780 assert_eq!(
619 BAR_GROUPS[2][..2],
781 BAR_GROUPS[2][..3],
620 [FormatButton::Highlight, FormatButton::FontColor]
782 [
783 FormatButton::Highlight,
784 FormatButton::FontColor,
785 FormatButton::FontSize
786 ]
621 );
787 );
622 assert_eq!(
788 assert_eq!(
623 BAR_GROUPS[3],
789 BAR_GROUPS[3],
@@ -630,10 +796,50 @@ mod tests {
630 );
796 );
631 assert_eq!(BAR_GROUPS[4].last(), Some(&FormatButton::Rule));
797 assert_eq!(BAR_GROUPS[4].last(), Some(&FormatButton::Rule));
632 for button in SELECTION_GROUPS.iter().flat_map(|group| group.iter()) {
798 for button in SELECTION_GROUPS.iter().flat_map(|group| group.iter()) {
633 assert!(BAR_GROUPS.iter().any(|group| group.contains(button)));
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)));
634 }
805 }
635 }
806 }
636
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
637 #[test]
843 #[test]
638 fn the_format_toggle_reads_like_teams() {
844 fn the_format_toggle_reads_like_teams() {
639 assert_eq!(
845 assert_eq!(