diff --git a/crates/menu/src/ribbon.rs b/crates/menu/src/ribbon.rs index 4bcb2c3687..c769c926f6 100644 --- a/crates/menu/src/ribbon.rs +++ b/crates/menu/src/ribbon.rs @@ -360,7 +360,7 @@ fn show_row( // The groups start a regular pad in from the row's start edge, as the old ribbon's did. craft_ui::layout::place_in_flow(ui, Vec2::new(start, 0.0)); let mut row = - RibbonRow { ui, base, groups: Vec::new(), title_line, launcher_side, icons, launcher: launcher.clone(), chevron }; + RibbonRow { ui, base, groups: Vec::new(), title_line, launcher_side, icons, launcher: launcher.clone(), chevron, owner }; body(&mut row, key); row.groups }); @@ -458,6 +458,8 @@ pub struct RibbonRow<'a> { launcher: IconId, /// The width of an edge chevron's slot, which no group shown in a scrolling row reaches into. chevron: f32, + /// The scrolling body the groups stand in: its content's extent says whether groups run past an edge. + owner: Id, } /// What the ribbon reports for a pass. diff --git a/crates/menu/src/ribbon/group.rs b/crates/menu/src/ribbon/group.rs index d065b7593e..62f2debcce 100644 --- a/crates/menu/src/ribbon/group.rs +++ b/crates/menu/src/ribbon/group.rs @@ -6,7 +6,7 @@ use craft_behavior::IconId; use craft_keymap::CommandRef; use craft_ui::buttons::{Arrangement, Button, ControlPlace, IconButton}; use craft_ui::contract::MemoProps; -use craft_ui::contract::{Bind, Gap, Props as _, Show as _, ShowContainer as _, Sizing, Trigger}; +use craft_ui::contract::{Bind, Gap, Props as _, Show as _, ShowContainer as _, Sizing, Trigger, scroll_extent}; use craft_ui::display::{Divider, Shade, Text}; use craft_ui::layout::{RowAlign, levelled_unit}; use craft_ui::layout_components::{Row, Stack}; @@ -208,11 +208,18 @@ impl RibbonRow<'_> { _ => LEVELS.len(), }; // A scrolling row shows whole groups only: a group that does not lie wholly inside the view, short of the edge - // chevrons' slots when the row overflows on that side, is not drawn and takes no pointer. + // chevrons' slots when groups run past that edge (as the chevrons show), is not drawn and takes no pointer. The + // body's room is unbounded along the row, so the groups' run is the first group's start edge to the end the + // content's extent reaches. let (body, view, chevron) = (self.ui.max_rect(), self.ui.clip_rect(), self.chevron); - let slots = (if body.min.x < view.min.x - 0.5 { chevron } else { 0.0 }, if body.max.x > view.max.x + 0.5 { chevron } else { 0.0 }); + let rtl = Env::of(self.ui).direction.is_rtl(); + let extent = scroll_extent(self.ui.ctx(), self.owner).x; + let first = self.groups.first().copied(); levelled_unit(self.ui, id, levels, |ui, at| { let unit = ui.max_rect(); + let first = first.unwrap_or(unit); + let (start, end) = if rtl { (body.max.x - extent, first.max.x) } else { (first.min.x, body.min.x + extent) }; + let slots = (if start < view.min.x - 0.5 { chevron } else { 0.0 }, if end > view.max.x + 0.5 { chevron } else { 0.0 }); if unit.min.x < view.min.x + slots.0 - 0.5 || unit.max.x > view.max.x - slots.1 + 0.5 { ui.set_clip_rect(Rect::NOTHING); } diff --git a/crates/menu/tests/it/ribbon.rs b/crates/menu/tests/it/ribbon.rs index 5a72968b25..c87315f29a 100644 --- a/crates/menu/tests/it/ribbon.rs +++ b/crates/menu/tests/it/ribbon.rs @@ -189,6 +189,65 @@ fn an_overflowing_scrolling_row_ends_on_a_whole_group_clear_of_the_chevron() { } } +/// Five three-button groups in a scrolling row. +const FIVE: [(&str, [&str; 3]); 5] = [ + ("one", ["One1", "One2", "One3"]), + ("two", ["Two1", "Two2", "Two3"]), + ("three", ["Three1", "Three2", "Three3"]), + ("four", ["Four1", "Four2", "Four3"]), + ("five", ["Five1", "Five2", "Five3"]), +]; + +fn five_groups() -> Fixture { + Fixture::new(move |ui| { + let tabs = [RibbonTab::new(0_usize, "Home")]; + let look = RibbonLook::default().fit(RibbonFit::Scroll); + let _ = Ribbon::new("Ribbon", &tabs, Bind::given(0_usize)).look(look).show(ui, |row, _| { + for (key, labels) in &FIVE { + group(row, key, key, labels); + } + }); + }) +} + +/// Whether the label `text` paints inside its clip. +fn painted(fixture: &Fixture, text: &str) -> bool { + fixture.texts().iter().any(|run| run.text == text && run.rect.intersect(run.clip).width() > 0.0) +} + +/// Steps the row with its end chevron until the chevron goes, at most ten steps. +fn scroll_to_end(fixture: &mut Fixture) { + for _ in 0..10 { + let Some(chevron) = fixture.by_label("Scroll ribbon right") else { return }; + fixture.click(&chevron).settle(); + } +} + +/// Scrolled to its end, the row shows its last group: no end chevron is left, so none keeps its slot. +#[trial] +fn a_scrolling_row_scrolled_to_its_end_shows_its_last_group() { + let mut fixture = five_groups(); + fixture.room(vec2(420.0, 300.0)); + fixture.settle(); + scroll_to_end(&mut fixture); + assert!(fixture.by_label("Scroll ribbon right").is_none(), "the end chevron goes at the row's end"); + assert!(painted(&fixture, "Five3"), "the last group shows at the row's end"); +} + +/// A row scrolled while narrow starts at its first group again once it is wide enough for every group. +#[trial] +fn a_scrolled_row_that_widens_to_fit_shows_its_first_group() { + let mut fixture = five_groups(); + fixture.room(vec2(420.0, 300.0)); + fixture.settle(); + scroll_to_end(&mut fixture); + fixture.room(vec2(2400.0, 300.0)); + fixture.settle(); + assert!(fixture.by_label("Scroll ribbon left").is_none(), "nothing lies past the start edge of a row that fits"); + assert!(painted(&fixture, "One1"), "the first group shows once the row fits"); + assert!(painted(&fixture, "Five3"), "the last group shows once the row fits"); +} + /// A gallery's More list hangs from its More button, below the ribbon row, clear of a taller control beside the /// gallery, not from the gallery's own shorter rect. #[trial] diff --git a/crates/ui-contract/src/contract/body/scroll.rs b/crates/ui-contract/src/contract/body/scroll.rs index a142bdb4ae..525595ad12 100644 --- a/crates/ui-contract/src/contract/body/scroll.rs +++ b/crates/ui-contract/src/contract/body/scroll.rs @@ -96,6 +96,12 @@ pub fn scroll_position(ctx: &Context, owner: Id) -> ScrollPos { }) } +/// The extent of the scrolling body `owner`'s content as its measuring run took it, which its real run scrolls over: +/// read from inside the body, it is the content of this pass. A body that has not measured reads zero. +pub fn scroll_extent(ctx: &Context, owner: Id) -> Vec2 { + crate::runtime::layout_memory::measured(ctx, crate::runtime::memory::sub(owner, "extent")).map_or(Vec2::ZERO, |measured| measured.took) +} + /// Queues `offset` for step 4 of the body `id`'s next pass, in place of one queued before, and repeats the pass to show /// it. pub(super) fn queue_jump(ctx: &Context, id: Id, offset: ScrollPos) { diff --git a/crates/ui-contract/src/contract/mod.rs b/crates/ui-contract/src/contract/mod.rs index 66a98f1051..3fc7a51bb6 100644 --- a/crates/ui-contract/src/contract/mod.rs +++ b/crates/ui-contract/src/contract/mod.rs @@ -57,7 +57,7 @@ pub use body::grip_band; pub use body::keep_inside; pub use body::pad_of; // PLAN: assumed body::scroll::scroll_position(ctx: &Context, owner: Id) -> ScrollPos (widgets-w2) -pub use body::scroll::scroll_position; +pub use body::scroll::{scroll_extent, scroll_position}; #[doc(hidden)] pub use body::{STRIP_BACK, STRIP_FORWARD}; pub use collection::{