BlueGit

ict rusty-teams

feat(composer): single buttons overflow into More options, Font size in the selection bar

- fit_groups moves single buttons from the end into More options, like Teams; bar and menu each keep a divider only where a group starts between two buttons, never at an edge
- Tooltips of popup buttons (More options, pickers, Font size) stay off while their popup is open (popup_trigger_tooltip)
- Selection bar: one Font size button with the Large / Medium / Small menu; with no size set nothing is shown as selected
- Deviation: pickers opened from the More options menu hang below their row instead of opening as a submenu

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
3f5273aee20af7d096ddbc3d3b281d16ce947949
Parent
dbd00aad
3 files changed +102 −73
crates/app/src/views/composer.rs +9 −4
@@ -2563,6 +2563,10 @@ impl Composer {
2563 2563 ),
2564 2564 None => {
2565 2565 let state_of = |button| ButtonState::enabled(self.format_state(button, &range));
2566 let mut overlay = |button| match button {
2567 FormatButton::FontSize => self.render_format_popup(FormatPopup::FontSize, cx),
2568 _ => None,
2569 };
2566 2570 format_toolbar::render(
2567 2571 placement,
2568 2572 format_toolbar::Mode::Bar {
@@ -2572,6 +2576,7 @@ impl Composer {
2572 2576 .update(cx, |this, cx| this.press_format(button, window, cx))
2573 2577 .ok();
2574 2578 }),
2579 overlay: &mut overlay,
2575 2580 },
2576 2581 )
2577 2582 }
@@ -2588,7 +2593,7 @@ impl Composer {
2588 2593 })
2589 2594 }
2590 2595
2591 fn format_bar_fit(&self) -> format_toolbar::BarFit<'static> {
2596 fn format_bar_fit(&self) -> format_toolbar::BarFit {
2592 2597 let available = self.composer_bounds.get().map_or(f32::INFINITY, |bounds| {
2593 2598 f32::from(bounds.size.width) - 2. * (COMPOSER_BUTTON_INSET + 1.)
2594 2599 });
@@ -2598,7 +2603,7 @@ impl Composer {
2598 2603 fn render_format_overlay(
2599 2604 &self,
2600 2605 button: FormatButton,
2601 overflow: &[&[FormatButton]],
2606 overflow: &[Vec<FormatButton>],
2602 2607 window: &Window,
2603 2608 cx: &mut Context<Self>,
2604 2609 ) -> Option<AnyElement> {
@@ -2636,8 +2641,8 @@ impl Composer {
2636 2641 let state_of = |button| self.button_state(button, &selection, in_table);
2637 2642 let on_press = self.format_press_handler(cx);
2638 2643 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);
2644 let mut overlay = |button| self.render_format_overlay(button, &fit.overflow, window, cx);
2645 let row = format_toolbar::render_format_bar(&fit, &state_of, on_press, &mut overlay);
2641 2646 v_flex()
2642 2647 .debug_selector(|| COMPOSER_FORMAT_BAR_TAG.into())
2643 2648 .w_full()
@@ -2563,6 +2563,10 @@ impl Composer {
2563 ),
2563 ),
2564 None => {
2564 None => {
2565 let state_of = |button| ButtonState::enabled(self.format_state(button, &range));
2565 let state_of = |button| ButtonState::enabled(self.format_state(button, &range));
2566 let mut overlay = |button| match button {
2567 FormatButton::FontSize => self.render_format_popup(FormatPopup::FontSize, cx),
2568 _ => None,
2569 };
2566 format_toolbar::render(
2570 format_toolbar::render(
2567 placement,
2571 placement,
2568 format_toolbar::Mode::Bar {
2572 format_toolbar::Mode::Bar {
@@ -2572,6 +2576,7 @@ impl Composer {
2572 .update(cx, |this, cx| this.press_format(button, window, cx))
2576 .update(cx, |this, cx| this.press_format(button, window, cx))
2573 .ok();
2577 .ok();
2574 }),
2578 }),
2579 overlay: &mut overlay,
2575 },
2580 },
2576 )
2581 )
2577 }
2582 }
@@ -2588,7 +2593,7 @@ impl Composer {
2588 })
2593 })
2589 }
2594 }
2590
2595
2591 fn format_bar_fit(&self) -> format_toolbar::BarFit<'static> {
2596 fn format_bar_fit(&self) -> format_toolbar::BarFit {
2592 let available = self.composer_bounds.get().map_or(f32::INFINITY, |bounds| {
2597 let available = self.composer_bounds.get().map_or(f32::INFINITY, |bounds| {
2593 f32::from(bounds.size.width) - 2. * (COMPOSER_BUTTON_INSET + 1.)
2598 f32::from(bounds.size.width) - 2. * (COMPOSER_BUTTON_INSET + 1.)
2594 });
2599 });
@@ -2598,7 +2603,7 @@ impl Composer {
2598 fn render_format_overlay(
2603 fn render_format_overlay(
2599 &self,
2604 &self,
2600 button: FormatButton,
2605 button: FormatButton,
2601 overflow: &[&[FormatButton]],
2606 overflow: &[Vec<FormatButton>],
2602 window: &Window,
2607 window: &Window,
2603 cx: &mut Context<Self>,
2608 cx: &mut Context<Self>,
2604 ) -> Option<AnyElement> {
2609 ) -> Option<AnyElement> {
@@ -2636,8 +2641,8 @@ impl Composer {
2636 let state_of = |button| self.button_state(button, &selection, in_table);
2641 let state_of = |button| self.button_state(button, &selection, in_table);
2637 let on_press = self.format_press_handler(cx);
2642 let on_press = self.format_press_handler(cx);
2638 let fit = self.format_bar_fit();
2643 let fit = self.format_bar_fit();
2639 let mut overlay = |button| self.render_format_overlay(button, fit.overflow, window, cx);
2644 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);
2645 let row = format_toolbar::render_format_bar(&fit, &state_of, on_press, &mut overlay);
2641 v_flex()
2646 v_flex()
2642 .debug_selector(|| COMPOSER_FORMAT_BAR_TAG.into())
2647 .debug_selector(|| COMPOSER_FORMAT_BAR_TAG.into())
2643 .w_full()
2648 .w_full()
crates/app/src/views/composer/format_menus.rs +4 −2
@@ -113,7 +113,9 @@ impl Composer {
113 113 self.table_picker = None;
114 114 self.table_menu = None;
115 115 self.format_popup = (self.format_popup != Some(popup)).then_some(popup);
116 self.toolbar_dismissed = Some(self.selection(cx));
116 if self.format_bar_open() {
117 self.toolbar_dismissed = Some(self.selection(cx));
118 }
117 119 cx.notify();
118 120 }
119 121
@@ -363,7 +365,7 @@ impl Composer {
363 365 let selection = self.selection(cx);
364 366 let rows = FONT_SIZES.iter().map(|(size, button, label)| {
365 367 let size = *size;
366 let current = self.format_state(*button, &selection) == FormatState::On;
368 let current = size.is_some() && self.format_state(*button, &selection) == FormatState::On;
367 369 swallow_mouse_down(
368 370 div()
369 371 .id(ElementId::Name(format!("font-size-{label}").into()))
@@ -113,7 +113,9 @@ impl Composer {
113 self.table_picker = None;
113 self.table_picker = None;
114 self.table_menu = None;
114 self.table_menu = None;
115 self.format_popup = (self.format_popup != Some(popup)).then_some(popup);
115 self.format_popup = (self.format_popup != Some(popup)).then_some(popup);
116 self.toolbar_dismissed = Some(self.selection(cx));
116 if self.format_bar_open() {
117 self.toolbar_dismissed = Some(self.selection(cx));
118 }
117 cx.notify();
119 cx.notify();
118 }
120 }
119
121
@@ -363,7 +365,7 @@ impl Composer {
363 let selection = self.selection(cx);
365 let selection = self.selection(cx);
364 let rows = FONT_SIZES.iter().map(|(size, button, label)| {
366 let rows = FONT_SIZES.iter().map(|(size, button, label)| {
365 let size = *size;
367 let size = *size;
366 let current = self.format_state(*button, &selection) == FormatState::On;
368 let current = size.is_some() && self.format_state(*button, &selection) == FormatState::On;
367 swallow_mouse_down(
369 swallow_mouse_down(
368 div()
370 div()
369 .id(ElementId::Name(format!("font-size-{label}").into()))
371 .id(ElementId::Name(format!("font-size-{label}").into()))
crates/app/src/views/format_toolbar.rs +89 −67
@@ -1,7 +1,8 @@
1 1 use std::rc::Rc;
2 2
3 3 use gpui_kit::component::input::{Input, InputState};
4 use gpui_kit::component::{h_flex, tooltip::Tooltip, v_flex};
4 use gpui_kit::component::tooltip::PopupTriggerTooltip as _;
5 use gpui_kit::component::{h_flex, v_flex};
5 6 use gpui_kit::prelude::FluentBuilder as _;
6 7 use gpui_kit::assets::IconName;
7 8 use gpui_kit::*;
@@ -66,11 +67,7 @@ const STYLE_GROUP: &[FormatButton] = &[
66 67 FormatButton::Strike,
67 68 ];
68 69 const SCRIPT_GROUP: &[FormatButton] = &[FormatButton::Superscript, FormatButton::Subscript];
69 const SIZE_GROUP: &[FormatButton] = &[
70 FormatButton::SizeSmall,
71 FormatButton::SizeNormal,
72 FormatButton::SizeLarge,
73 ];
70 const SIZE_GROUP: &[FormatButton] = &[FormatButton::FontSize];
74 71 const INLINE_GROUP: &[FormatButton] = &[FormatButton::Code, FormatButton::Link];
75 72 const LINE_GROUP: &[FormatButton] = &[
76 73 FormatButton::Bulleted,
@@ -131,37 +128,50 @@ const MENU_ROW_PADDING_X: f32 = 10.;
131 128 const MENU_PADDING: f32 = 4.;
132 129 const MENU_WIDTH: f32 = 220.;
133 130
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]],
131 /// The format bar split like Teams' overflow: buttons leave from the end into More options,
132 /// each side keeps the group boundaries of its own buttons.
133 #[derive(Debug, Clone, PartialEq, Eq)]
134 pub struct BarFit {
135 pub visible: Vec<Vec<FormatButton>>,
136 pub overflow: Vec<Vec<FormatButton>>,
139 137 }
140 138
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
139 fn groups_width<G: AsRef<[FormatButton]>>(groups: &[G]) -> f32 {
140 let buttons = groups.iter().flat_map(|group| group.as_ref().iter());
141 let widths: f32 = buttons.clone().map(|button| button.width()).sum();
142 let gaps = buttons.count().saturating_sub(1) as f32 * BUTTON_GAP;
143 let separators = groups.len().saturating_sub(1) as f32 * (SEPARATOR_WIDTH + BUTTON_GAP);
144 widths + gaps + separators
144 145 }
145 146
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
147 fn split_groups(groups: &[&[FormatButton]], count: usize) -> BarFit {
148 let mut visible = Vec::new();
149 let mut overflow = Vec::new();
150 let mut taken = 0;
151 for group in groups {
152 let shown = group.len().min(count - taken.min(count));
153 taken += group.len();
154 if shown > 0 {
155 visible.push(group[..shown].to_vec());
156 }
157 if shown < group.len() {
158 overflow.push(group[shown..].to_vec());
159 }
160 }
161 BarFit { visible, overflow }
150 162 }
151 163
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> {
164 pub fn fit_groups(available: f32, groups: &[&[FormatButton]]) -> BarFit {
165 let total: usize = groups.iter().map(|group| group.len()).sum();
166 if groups_width(groups) <= available {
167 return split_groups(groups, total);
168 }
154 169 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 }
170 let count = (0..total)
171 .rev()
172 .find(|count| groups_width(&split_groups(groups, *count).visible) + more <= available)
173 .unwrap_or(0);
174 split_groups(groups, count)
165 175 }
166 176
167 177 pub const TOGGLE_SHORTCUT: &str = "Ctrl+Shift+X";
@@ -355,11 +365,7 @@ fn sized_letter(size: f32, color: Hsla) -> AnyElement {
355 365 }
356 366
357 367 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
368 2. * PADDING + groups_width(&SELECTION_GROUPS)
363 369 }
364 370
365 371 pub fn link_width() -> f32 {
@@ -396,6 +402,7 @@ pub enum Mode<'a> {
396 402 Bar {
397 403 state: &'a dyn Fn(FormatButton) -> ButtonState,
398 404 on_press: PressHandler,
405 overlay: &'a mut dyn FnMut(FormatButton) -> Option<AnyElement>,
399 406 },
400 407 Link {
401 408 field: &'a Entity<InputState>,
@@ -426,7 +433,7 @@ fn button(
426 433 .items_center()
427 434 .justify_center()
428 435 .rounded(px(6.))
429 .tooltip(move |window, cx| Tooltip::new(format.tooltip()).build(window, cx))
436 .popup_trigger_tooltip_text(format.opens_popup() && on, format.tooltip())
430 437 .child(format.content(color, state.label))
431 438 .on_mouse_down(MouseButton::Left, |_, window, cx| {
432 439 window.prevent_default();
@@ -476,8 +483,8 @@ fn separator() -> Div {
476 483 .bg(theme::popup_hover())
477 484 }
478 485
479 fn groups_row(
480 groups: &[&[FormatButton]],
486 fn groups_row<G: AsRef<[FormatButton]>>(
487 groups: &[G],
481 488 state: &dyn Fn(FormatButton) -> ButtonState,
482 489 on_press: PressHandler,
483 490 overlay: &mut dyn FnMut(FormatButton) -> Option<AnyElement>,
@@ -487,7 +494,7 @@ fn groups_row(
487 494 if index > 0 {
488 495 row = row.child(separator());
489 496 }
490 for format in group.iter().copied() {
497 for format in group.as_ref().iter().copied() {
491 498 row = row.child(button(
492 499 format,
493 500 state(format),
@@ -499,19 +506,23 @@ fn groups_row(
499 506 row
500 507 }
501 508
502 fn bar(state: &dyn Fn(FormatButton) -> ButtonState, on_press: PressHandler) -> Div {
503 groups_row(&SELECTION_GROUPS, state, on_press, &mut |_| None)
509 fn bar(
510 state: &dyn Fn(FormatButton) -> ButtonState,
511 on_press: PressHandler,
512 overlay: &mut dyn FnMut(FormatButton) -> Option<AnyElement>,
513 ) -> Div {
514 groups_row(&SELECTION_GROUPS, state, on_press, overlay)
504 515 }
505 516
506 517 /// The format bar inside the composer box. `overlay` hangs a popup under a button; the
507 518 /// More button's popup is the overflow menu, built with `render_overflow_menu`.
508 519 pub fn render_format_bar(
509 fit: BarFit<'_>,
520 fit: &BarFit,
510 521 state: &dyn Fn(FormatButton) -> ButtonState,
511 522 on_press: PressHandler,
512 523 overlay: &mut dyn FnMut(FormatButton) -> Option<AnyElement>,
513 524 ) -> Div {
514 let row = groups_row(fit.visible, state, on_press.clone(), overlay);
525 let row = groups_row(&fit.visible, state, on_press.clone(), overlay);
515 526 if fit.overflow.is_empty() {
516 527 return row;
517 528 }
@@ -525,7 +536,7 @@ pub fn render_format_bar(
525 536
526 537 /// The groups that do not fit, as menu rows with a divider between groups.
527 538 pub fn render_overflow_menu(
528 groups: &[&[FormatButton]],
539 groups: &[Vec<FormatButton>],
529 540 state: &dyn Fn(FormatButton) -> ButtonState,
530 541 on_press: PressHandler,
531 542 overlay: &mut dyn FnMut(FormatButton) -> Option<AnyElement>,
@@ -681,7 +692,11 @@ fn arrow(offset: Pixels) -> impl IntoElement {
681 692
682 693 pub fn render(placement: Placement, mode: Mode<'_>) -> AnyElement {
683 694 let content = match mode {
684 Mode::Bar { state, on_press } => bar(state, on_press),
695 Mode::Bar {
696 state,
697 on_press,
698 overlay,
699 } => bar(state, on_press, overlay),
685 700 Mode::Link {
686 701 field,
687 702 refused,
@@ -719,11 +734,6 @@ mod tests {
719 734 groups_width, place, toggle_symbol, toggle_tooltip,
720 735 };
721 736
722 const SIZES: [FormatButton; 3] = [
723 FormatButton::SizeSmall,
724 FormatButton::SizeNormal,
725 FormatButton::SizeLarge,
726 ];
727 737
728 738 #[test]
729 739 fn the_bar_centers_over_the_selection() {
@@ -757,14 +767,15 @@ mod tests {
757 767 .iter()
758 768 .map(|group| group.len())
759 769 .sum::<usize>(),
760 14
770 12
761 771 );
762 772 assert!(
763 773 SELECTION_GROUPS
764 774 .iter()
765 775 .all(|group| !group.contains(&FormatButton::Table))
766 776 );
767 assert!(bar_width() > 14. * BUTTON);
777 assert!(SELECTION_GROUPS.iter().any(|group| group == &[FormatButton::FontSize]));
778 assert!(bar_width() > 12. * BUTTON);
768 779 }
769 780
770 781 #[test]
@@ -796,33 +807,44 @@ mod tests {
796 807 );
797 808 assert_eq!(BAR_GROUPS[4].last(), Some(&FormatButton::Rule));
798 809 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)));
810 assert!(BAR_GROUPS.iter().any(|group| group.contains(button)));
805 811 }
806 812 }
807 813
814 fn flat(groups: &[Vec<FormatButton>]) -> Vec<FormatButton> {
815 groups.iter().flatten().copied().collect()
816 }
817
808 818 #[test]
809 fn a_wide_bar_shows_every_group_without_more_options() {
819 fn a_wide_bar_shows_every_button_without_more_options() {
810 820 let fit = fit_groups(groups_width(&BAR_GROUPS), &BAR_GROUPS);
811 821 assert_eq!(fit.visible.len(), BAR_GROUPS.len());
812 822 assert!(fit.overflow.is_empty());
813 823 }
814 824
815 825 #[test]
816 fn groups_leave_from_the_end_and_more_options_fits_beside_the_rest() {
826 fn single_buttons_leave_from_the_end_and_keep_their_groups() {
827 let all: Vec<FormatButton> = BAR_GROUPS.iter().flat_map(|group| group.iter()).copied().collect();
817 828 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);
829 assert_eq!(flat(&fit.overflow), all[all.len() - 2..]);
830 assert_eq!(fit.overflow, vec![vec![FormatButton::TableInsertLine, FormatButton::TableRemoveLine]]);
831 assert_eq!(fit.visible.last(), Some(&vec![FormatButton::Table]));
832 let mut both = flat(&fit.visible);
833 both.extend(flat(&fit.overflow));
834 assert_eq!(both, all);
835 }
836
837 #[test]
838 fn a_cut_between_groups_leaves_no_empty_group_on_either_side() {
839 let two = groups_width(&BAR_GROUPS[..2]);
840 let fit = fit_groups(two + 2. + BUTTON, &BAR_GROUPS);
841 assert_eq!(fit.visible, vec![BAR_GROUPS[0].to_vec(), BAR_GROUPS[1].to_vec()]);
825 842 assert_eq!(fit.overflow.len(), BAR_GROUPS.len() - 2);
843 assert!(fit.overflow.iter().chain(&fit.visible).all(|group| !group.is_empty()));
844 let fit = fit_groups(two + 2. + BUTTON - 1., &BAR_GROUPS);
845 assert_eq!(fit.visible.len(), 2);
846 assert_eq!(fit.visible[1], vec![FormatButton::Superscript]);
847 assert_eq!(fit.overflow[0], vec![FormatButton::Subscript]);
826 848 }
827 849
828 850 #[test]
@@ -1,7 +1,8 @@
1use std::rc::Rc;
1use std::rc::Rc;
2
2
3use gpui_kit::component::input::{Input, InputState};
3use gpui_kit::component::input::{Input, InputState};
4use gpui_kit::component::{h_flex, tooltip::Tooltip, v_flex};
4use gpui_kit::component::tooltip::PopupTriggerTooltip as _;
5use gpui_kit::component::{h_flex, v_flex};
5use gpui_kit::prelude::FluentBuilder as _;
6use gpui_kit::prelude::FluentBuilder as _;
6use gpui_kit::assets::IconName;
7use gpui_kit::assets::IconName;
7use gpui_kit::*;
8use gpui_kit::*;
@@ -66,11 +67,7 @@ const STYLE_GROUP: &[FormatButton] = &[
66 FormatButton::Strike,
67 FormatButton::Strike,
67];
68];
68const SCRIPT_GROUP: &[FormatButton] = &[FormatButton::Superscript, FormatButton::Subscript];
69const SCRIPT_GROUP: &[FormatButton] = &[FormatButton::Superscript, FormatButton::Subscript];
69const SIZE_GROUP: &[FormatButton] = &[
70const SIZE_GROUP: &[FormatButton] = &[FormatButton::FontSize];
70 FormatButton::SizeSmall,
71 FormatButton::SizeNormal,
72 FormatButton::SizeLarge,
73];
74const INLINE_GROUP: &[FormatButton] = &[FormatButton::Code, FormatButton::Link];
71const INLINE_GROUP: &[FormatButton] = &[FormatButton::Code, FormatButton::Link];
75const LINE_GROUP: &[FormatButton] = &[
72const LINE_GROUP: &[FormatButton] = &[
76 FormatButton::Bulleted,
73 FormatButton::Bulleted,
@@ -131,37 +128,50 @@ const MENU_ROW_PADDING_X: f32 = 10.;
131const MENU_PADDING: f32 = 4.;
128const MENU_PADDING: f32 = 4.;
132const MENU_WIDTH: f32 = 220.;
129const MENU_WIDTH: f32 = 220.;
133
130
134/// The groups of the format bar that fit, and those that go into More options.
131/// The format bar split like Teams' overflow: buttons leave from the end into More options,
135#[derive(Debug, Clone, Copy, PartialEq, Eq)]
132/// each side keeps the group boundaries of its own buttons.
136pub struct BarFit<'a> {
133#[derive(Debug, Clone, PartialEq, Eq)]
137 pub visible: &'a [&'a [FormatButton]],
134pub struct BarFit {
138 pub overflow: &'a [&'a [FormatButton]],
135 pub visible: Vec<Vec<FormatButton>>,
136 pub overflow: Vec<Vec<FormatButton>>,
139}
137}
140
138
141fn group_width(group: &[FormatButton]) -> f32 {
139fn groups_width<G: AsRef<[FormatButton]>>(groups: &[G]) -> f32 {
142 let buttons: f32 = group.iter().map(|button| button.width()).sum();
140 let buttons = groups.iter().flat_map(|group| group.as_ref().iter());
143 buttons + group.len().saturating_sub(1) as f32 * BUTTON_GAP
141 let widths: f32 = buttons.clone().map(|button| button.width()).sum();
142 let gaps = buttons.count().saturating_sub(1) as f32 * BUTTON_GAP;
143 let separators = groups.len().saturating_sub(1) as f32 * (SEPARATOR_WIDTH + BUTTON_GAP);
144 widths + gaps + separators
144}
145}
145
146
146fn groups_width(groups: &[&[FormatButton]]) -> f32 {
147fn split_groups(groups: &[&[FormatButton]], count: usize) -> BarFit {
147 let buttons: f32 = groups.iter().map(|group| group_width(group)).sum();
148 let mut visible = Vec::new();
148 let separators = groups.len().saturating_sub(1) as f32 * (SEPARATOR_WIDTH + 2. * BUTTON_GAP);
149 let mut overflow = Vec::new();
149 buttons + separators
150 let mut taken = 0;
151 for group in groups {
152 let shown = group.len().min(count - taken.min(count));
153 taken += group.len();
154 if shown > 0 {
155 visible.push(group[..shown].to_vec());
156 }
157 if shown < group.len() {
158 overflow.push(group[shown..].to_vec());
159 }
160 }
161 BarFit { visible, overflow }
150}
162}
151
163
152/// Like Teams' overflow: groups leave from the end until the rest and the More button fit.
164pub fn fit_groups(available: f32, groups: &[&[FormatButton]]) -> BarFit {
153pub fn fit_groups<'a>(available: f32, groups: &'a [&'a [FormatButton]]) -> BarFit<'a> {
165 let total: usize = groups.iter().map(|group| group.len()).sum();
166 if groups_width(groups) <= available {
167 return split_groups(groups, total);
168 }
154 let more = BUTTON_GAP + FormatButton::More.width();
169 let more = BUTTON_GAP + FormatButton::More.width();
155 let visible = if groups_width(groups) <= available {
170 let count = (0..total)
156 groups.len()
171 .rev()
157 } else {
172 .find(|count| groups_width(&split_groups(groups, *count).visible) + more <= available)
158 (0..groups.len())
173 .unwrap_or(0);
159 .rev()
174 split_groups(groups, count)
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}
175}
166
176
167pub const TOGGLE_SHORTCUT: &str = "Ctrl+Shift+X";
177pub const TOGGLE_SHORTCUT: &str = "Ctrl+Shift+X";
@@ -355,11 +365,7 @@ fn sized_letter(size: f32, color: Hsla) -> AnyElement {
355}
365}
356
366
357pub fn bar_width() -> f32 {
367pub fn bar_width() -> f32 {
358 let groups = SELECTION_GROUPS;
368 2. * PADDING + groups_width(&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}
369}
364
370
365pub fn link_width() -> f32 {
371pub fn link_width() -> f32 {
@@ -396,6 +402,7 @@ pub enum Mode<'a> {
396 Bar {
402 Bar {
397 state: &'a dyn Fn(FormatButton) -> ButtonState,
403 state: &'a dyn Fn(FormatButton) -> ButtonState,
398 on_press: PressHandler,
404 on_press: PressHandler,
405 overlay: &'a mut dyn FnMut(FormatButton) -> Option<AnyElement>,
399 },
406 },
400 Link {
407 Link {
401 field: &'a Entity<InputState>,
408 field: &'a Entity<InputState>,
@@ -426,7 +433,7 @@ fn button(
426 .items_center()
433 .items_center()
427 .justify_center()
434 .justify_center()
428 .rounded(px(6.))
435 .rounded(px(6.))
429 .tooltip(move |window, cx| Tooltip::new(format.tooltip()).build(window, cx))
436 .popup_trigger_tooltip_text(format.opens_popup() && on, format.tooltip())
430 .child(format.content(color, state.label))
437 .child(format.content(color, state.label))
431 .on_mouse_down(MouseButton::Left, |_, window, cx| {
438 .on_mouse_down(MouseButton::Left, |_, window, cx| {
432 window.prevent_default();
439 window.prevent_default();
@@ -476,8 +483,8 @@ fn separator() -> Div {
476 .bg(theme::popup_hover())
483 .bg(theme::popup_hover())
477}
484}
478
485
479fn groups_row(
486fn groups_row<G: AsRef<[FormatButton]>>(
480 groups: &[&[FormatButton]],
487 groups: &[G],
481 state: &dyn Fn(FormatButton) -> ButtonState,
488 state: &dyn Fn(FormatButton) -> ButtonState,
482 on_press: PressHandler,
489 on_press: PressHandler,
483 overlay: &mut dyn FnMut(FormatButton) -> Option<AnyElement>,
490 overlay: &mut dyn FnMut(FormatButton) -> Option<AnyElement>,
@@ -487,7 +494,7 @@ fn groups_row(
487 if index > 0 {
494 if index > 0 {
488 row = row.child(separator());
495 row = row.child(separator());
489 }
496 }
490 for format in group.iter().copied() {
497 for format in group.as_ref().iter().copied() {
491 row = row.child(button(
498 row = row.child(button(
492 format,
499 format,
493 state(format),
500 state(format),
@@ -499,19 +506,23 @@ fn groups_row(
499 row
506 row
500}
507}
501
508
502fn bar(state: &dyn Fn(FormatButton) -> ButtonState, on_press: PressHandler) -> Div {
509fn bar(
503 groups_row(&SELECTION_GROUPS, state, on_press, &mut |_| None)
510 state: &dyn Fn(FormatButton) -> ButtonState,
511 on_press: PressHandler,
512 overlay: &mut dyn FnMut(FormatButton) -> Option<AnyElement>,
513) -> Div {
514 groups_row(&SELECTION_GROUPS, state, on_press, overlay)
504}
515}
505
516
506/// The format bar inside the composer box. `overlay` hangs a popup under a button; the
517/// 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`.
518/// More button's popup is the overflow menu, built with `render_overflow_menu`.
508pub fn render_format_bar(
519pub fn render_format_bar(
509 fit: BarFit<'_>,
520 fit: &BarFit,
510 state: &dyn Fn(FormatButton) -> ButtonState,
521 state: &dyn Fn(FormatButton) -> ButtonState,
511 on_press: PressHandler,
522 on_press: PressHandler,
512 overlay: &mut dyn FnMut(FormatButton) -> Option<AnyElement>,
523 overlay: &mut dyn FnMut(FormatButton) -> Option<AnyElement>,
513) -> Div {
524) -> Div {
514 let row = groups_row(fit.visible, state, on_press.clone(), overlay);
525 let row = groups_row(&fit.visible, state, on_press.clone(), overlay);
515 if fit.overflow.is_empty() {
526 if fit.overflow.is_empty() {
516 return row;
527 return row;
517 }
528 }
@@ -525,7 +536,7 @@ pub fn render_format_bar(
525
536
526/// The groups that do not fit, as menu rows with a divider between groups.
537/// The groups that do not fit, as menu rows with a divider between groups.
527pub fn render_overflow_menu(
538pub fn render_overflow_menu(
528 groups: &[&[FormatButton]],
539 groups: &[Vec<FormatButton>],
529 state: &dyn Fn(FormatButton) -> ButtonState,
540 state: &dyn Fn(FormatButton) -> ButtonState,
530 on_press: PressHandler,
541 on_press: PressHandler,
531 overlay: &mut dyn FnMut(FormatButton) -> Option<AnyElement>,
542 overlay: &mut dyn FnMut(FormatButton) -> Option<AnyElement>,
@@ -681,7 +692,11 @@ fn arrow(offset: Pixels) -> impl IntoElement {
681
692
682pub fn render(placement: Placement, mode: Mode<'_>) -> AnyElement {
693pub fn render(placement: Placement, mode: Mode<'_>) -> AnyElement {
683 let content = match mode {
694 let content = match mode {
684 Mode::Bar { state, on_press } => bar(state, on_press),
695 Mode::Bar {
696 state,
697 on_press,
698 overlay,
699 } => bar(state, on_press, overlay),
685 Mode::Link {
700 Mode::Link {
686 field,
701 field,
687 refused,
702 refused,
@@ -719,11 +734,6 @@ mod tests {
719 groups_width, place, toggle_symbol, toggle_tooltip,
734 groups_width, place, toggle_symbol, toggle_tooltip,
720 };
735 };
721
736
722 const SIZES: [FormatButton; 3] = [
723 FormatButton::SizeSmall,
724 FormatButton::SizeNormal,
725 FormatButton::SizeLarge,
726 ];
727
737
728 #[test]
738 #[test]
729 fn the_bar_centers_over_the_selection() {
739 fn the_bar_centers_over_the_selection() {
@@ -757,14 +767,15 @@ mod tests {
757 .iter()
767 .iter()
758 .map(|group| group.len())
768 .map(|group| group.len())
759 .sum::<usize>(),
769 .sum::<usize>(),
760 14
770 12
761 );
771 );
762 assert!(
772 assert!(
763 SELECTION_GROUPS
773 SELECTION_GROUPS
764 .iter()
774 .iter()
765 .all(|group| !group.contains(&FormatButton::Table))
775 .all(|group| !group.contains(&FormatButton::Table))
766 );
776 );
767 assert!(bar_width() > 14. * BUTTON);
777 assert!(SELECTION_GROUPS.iter().any(|group| group == &[FormatButton::FontSize]));
778 assert!(bar_width() > 12. * BUTTON);
768 }
779 }
769
780
770 #[test]
781 #[test]
@@ -796,33 +807,44 @@ mod tests {
796 );
807 );
797 assert_eq!(BAR_GROUPS[4].last(), Some(&FormatButton::Rule));
808 assert_eq!(BAR_GROUPS[4].last(), Some(&FormatButton::Rule));
798 for button in SELECTION_GROUPS.iter().flat_map(|group| group.iter()) {
809 for button in SELECTION_GROUPS.iter().flat_map(|group| group.iter()) {
799 let covered = if SIZES.contains(button) {
810 assert!(BAR_GROUPS.iter().any(|group| group.contains(button)));
800 &FormatButton::FontSize
801 } else {
802 button
803 };
804 assert!(BAR_GROUPS.iter().any(|group| group.contains(covered)));
805 }
811 }
806 }
812 }
807
813
814 fn flat(groups: &[Vec<FormatButton>]) -> Vec<FormatButton> {
815 groups.iter().flatten().copied().collect()
816 }
817
808 #[test]
818 #[test]
809 fn a_wide_bar_shows_every_group_without_more_options() {
819 fn a_wide_bar_shows_every_button_without_more_options() {
810 let fit = fit_groups(groups_width(&BAR_GROUPS), &BAR_GROUPS);
820 let fit = fit_groups(groups_width(&BAR_GROUPS), &BAR_GROUPS);
811 assert_eq!(fit.visible.len(), BAR_GROUPS.len());
821 assert_eq!(fit.visible.len(), BAR_GROUPS.len());
812 assert!(fit.overflow.is_empty());
822 assert!(fit.overflow.is_empty());
813 }
823 }
814
824
815 #[test]
825 #[test]
816 fn groups_leave_from_the_end_and_more_options_fits_beside_the_rest() {
826 fn single_buttons_leave_from_the_end_and_keep_their_groups() {
827 let all: Vec<FormatButton> = BAR_GROUPS.iter().flat_map(|group| group.iter()).copied().collect();
817 let fit = fit_groups(groups_width(&BAR_GROUPS) - 1., &BAR_GROUPS);
828 let fit = fit_groups(groups_width(&BAR_GROUPS) - 1., &BAR_GROUPS);
818 assert_eq!(fit.visible, &BAR_GROUPS[..BAR_GROUPS.len() - 1]);
829 assert_eq!(flat(&fit.overflow), all[all.len() - 2..]);
819 assert_eq!(fit.overflow, &BAR_GROUPS[BAR_GROUPS.len() - 1..]);
830 assert_eq!(fit.overflow, vec![vec![FormatButton::TableInsertLine, FormatButton::TableRemoveLine]]);
820 let three = groups_width(&BAR_GROUPS[..3]);
831 assert_eq!(fit.visible.last(), Some(&vec![FormatButton::Table]));
821 let fit = fit_groups(three + 2. + BUTTON, &BAR_GROUPS);
832 let mut both = flat(&fit.visible);
822 assert_eq!(fit.visible.len(), 3);
833 both.extend(flat(&fit.overflow));
823 let fit = fit_groups(three + 2. + BUTTON - 1., &BAR_GROUPS);
834 assert_eq!(both, all);
824 assert_eq!(fit.visible.len(), 2);
835 }
836
837 #[test]
838 fn a_cut_between_groups_leaves_no_empty_group_on_either_side() {
839 let two = groups_width(&BAR_GROUPS[..2]);
840 let fit = fit_groups(two + 2. + BUTTON, &BAR_GROUPS);
841 assert_eq!(fit.visible, vec![BAR_GROUPS[0].to_vec(), BAR_GROUPS[1].to_vec()]);
825 assert_eq!(fit.overflow.len(), BAR_GROUPS.len() - 2);
842 assert_eq!(fit.overflow.len(), BAR_GROUPS.len() - 2);
843 assert!(fit.overflow.iter().chain(&fit.visible).all(|group| !group.is_empty()));
844 let fit = fit_groups(two + 2. + BUTTON - 1., &BAR_GROUPS);
845 assert_eq!(fit.visible.len(), 2);
846 assert_eq!(fit.visible[1], vec![FormatButton::Superscript]);
847 assert_eq!(fit.overflow[0], vec![FormatButton::Subscript]);
826 }
848 }
827
849
828 #[test]
850 #[test]