From 3750a77cb5766e79abfbf13fbcfcd74c9f42040d Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 12 Nov 2026 09:21:11 +0000 Subject: [PATCH] Columns keep their least width: fill columns share the room down to the header's least column width, columns wider than the room scroll sideways with the header, and a row's label stays in its column --- crates/behavior/src/item_layout.rs | 4 +- .../ui-contract/src/contract/items/header.rs | 52 +++++++++++++++---- .../ui-contract/src/contract/items/hooks.rs | 7 ++- crates/ui-widgets/src/list/row/placement.rs | 5 +- crates/ui-widgets/tests/it/table.rs | 50 ++++++++++++++++++ 5 files changed, 104 insertions(+), 14 deletions(-) diff --git a/crates/behavior/src/item_layout.rs b/crates/behavior/src/item_layout.rs index 2085d5d6e..0332d57e8 100644 --- a/crates/behavior/src/item_layout.rs +++ b/crates/behavior/src/item_layout.rs @@ -378,7 +378,7 @@ impl SpanLayout { /// The columns with their widths, from the start edge. #[must_use] pub fn columns(mut self, columns: Vec<(ColumnKey, f32)>) -> Self { - self.columns = columns; + ItemLayout::set_columns(&mut self, &columns); self } @@ -458,8 +458,10 @@ impl ItemLayout for SpanLayout { if self.tree { NavShape::Tree } else { NavShape::Flat } } + /// Rows are as wide as their columns where those together are wider than the rows: the collection scrolls sideways. fn set_columns(&mut self, columns: &[(ColumnKey, f32)]) { self.columns = columns.to_vec(); + self.width = self.width.max(columns.iter().map(|(_, width)| width).sum()); } fn cells(&self, at: usize, offset: ScrollPos, out: &mut Vec<(ColumnKey, Rect)>) { diff --git a/crates/ui-contract/src/contract/items/header.rs b/crates/ui-contract/src/contract/items/header.rs index 73e7cba6b..ad0195aa3 100644 --- a/crates/ui-contract/src/contract/items/header.rs +++ b/crates/ui-contract/src/contract/items/header.rs @@ -168,6 +168,8 @@ pub(super) struct Header { hideable: Vec, /// The empty header after the last column. room: Option, + /// The header row in view: cells that scroll past it are clipped to it. + view: Option, /// The narrowest a resized column gets, and the keyboard resize step. min: f32, step: f32, @@ -216,10 +218,12 @@ impl Header { } /// The width of each column, in display order, in a row `room` wide: a `Content` column its widest cell (capped by - /// its `max_share` of `room`), and the `Fill` columns share what is left. + /// its `max_share` of `room`), and the `Fill` columns share what is left, none narrower than the least column + /// width: a fill column whose share would be less takes the least and the others share the rest. Columns wider + /// together than `room` run past it, and the collection scrolls sideways. fn widths(&self, room: f32) -> Vec { let content: Vec = self.content.iter().map(|wide| wide.max(self.min)).collect(); - let kinds: Vec = self + let mut kinds: Vec = self .specs .iter() .zip(&content) @@ -228,7 +232,19 @@ impl Header { (kind, _) => kind, }) .collect(); - resolve_columns(&kinds, &content, room, 0.0) + loop { + let widths = resolve_columns(&kinds, &content, room, 0.0); + let mut floored = false; + for (kind, width) in kinds.iter_mut().zip(&widths) { + if matches!(kind, ColumnWidth::Fill { .. }) && *width < self.min { + *kind = ColumnWidth::Fixed(self.min); + floored = true; + } + } + if !floored { + return widths; + } + } } /// Reads the widest cell of each `Content` column the items' measure recorded in this pass under the collection `id`. @@ -340,28 +356,37 @@ impl Header { self.specs.iter().map(|spec| spec.key.clone()).zip(self.widths(room)).collect() } - /// Step 7: one focusable part per column, a resize grip at the end edge of each resizable one, and the group. - pub(super) fn parts(&mut self, pcx: &mut PartsCx<'_, C>) { + /// Step 7: one focusable part per column, a resize grip at the end edge of each resizable one, and the group. The + /// cells scroll sideways with the rows, by `offset` from the reading start, and take the pointer only in view. + pub(super) fn parts(&mut self, pcx: &mut PartsCx<'_, C>, offset: f32) { self.cells.clear(); if self.height <= 0.0 { return; } let area = pcx.content_rect; - let row = Rect::from_min_size(area.min, Vec2::new(area.width(), self.height)); + let view = Rect::from_min_size(area.min, Vec2::new(area.width(), self.height)); + self.view = Some(view); let grip = scroll_bar_look(pcx.tokens, ScrollBarKind::Overlay).thickness; let rtl = pcx.flow.is_rtl(); - let mut flow = Flow::new(row, rtl); let widths = self.widths(area.width()); let help = pcx.words::().tip.clone(); for (at, (spec, width)) in self.specs.iter().zip(widths).enumerate() { let (rect, rest) = flow.take_start(width); flow = rest; let hash = hash_of(&spec.key); + let wide = widths.iter().sum::().max(area.width()); + let left = if rtl { area.max.x + offset - wide } else { area.min.x - offset }; + let mut flow = Flow::new(Rect::from_min_size(Pos2::new(left, area.min.y), Vec2::new(wide, self.height)), rtl); + // The part of `rect` in view: a cell scrolled out keeps its place in the header's roving group with no area. + let shown = |rect: Rect| { + let x = rect.min.x.clamp(view.min.x, view.max.x); + Rect::from_x_y_ranges(x..=rect.max.x.clamp(x, view.max.x), rect.y_range()) + }; let mut sense = Sense::CLICK | Sense::FOCUS; if spec.movable { sense |= Sense::DRAG; } - pcx.part(HEADER.at(hash), rect) + pcx.part(HEADER.at(hash), shown(rect)) .sense(sense) .focusable() .priority(HitPriority::new(HitLevel::Body, 0)) @@ -373,7 +398,7 @@ impl Header { } else { Rect::from_min_max(Pos2::new(rect.max.x - grip, rect.min.y), rect.max) }; - pcx.part(GRIP.at(hash), edge) + pcx.part(GRIP.at(hash), shown(edge)) .sense(Sense::DRAG | Sense::CLICK) .priority(HitPriority::new(HitLevel::Body, 1)) .cursor(CursorIcon::ResizeHorizontal) @@ -381,7 +406,7 @@ impl Header { } self.cells.push(Cell { spec: at, rect, hash }); } - let room = flow.rect(); + let room = shown(flow.rect()); self.room = (room.width() > 0.0).then_some(room); if let Some(room) = self.room { pcx.part(ROOM, room) @@ -396,6 +421,11 @@ impl Header { } } + /// The header row in view, while the header shows. + pub(super) fn view(&self) -> Option { + self.view.filter(|_| self.height > 0.0) + } + /// Whether `key` is the sorted column, and if so whether it sorts descending. fn sort_of(&self, key: &ColumnKey) -> Option { self.sort.as_ref().filter(|sort| sort.key == *key).map(|sort| sort.descending) @@ -692,7 +722,7 @@ impl Header { let sort = self.sort_of(&spec.key); let builder = UiBuilder::new().max_rect(cell.rect).layout(child::column_layout(rtl)).scope_id(sub(id, ("header_cell", cell.hash))); let mut cell_ui = alloc::child(ui, builder); - cell_ui.set_clip_rect(cell.rect.intersect(ui.clip_rect())); + cell_ui.set_clip_rect(self.view.map_or(cell.rect, |view| cell.rect.intersect(view)).intersect(ui.clip_rect())); let installed = Env::install_for(&cell_ui, env.derive_local(|scoped| scoped.arena = None)); let edge = spec .resizable diff --git a/crates/ui-contract/src/contract/items/hooks.rs b/crates/ui-contract/src/contract/items/hooks.rs index b8edb695d..347359d83 100644 --- a/crates/ui-contract/src/contract/items/hooks.rs +++ b/crates/ui-contract/src/contract/items/hooks.rs @@ -288,7 +288,7 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { } fn parts(&mut self, comp: &mut C, _: Rect, pcx: &mut PartsCx<'_, C>) { - self.header.parts(pcx); + self.header.parts(pcx, self.offset_x as f32); self.notch = comp.items().wheel_notch(pcx.tokens.typography.body.line_height); if !self.scrolls { return; @@ -419,6 +419,11 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { let left = if rtl { rect.max.x + self.offset_x as f32 - size.x } else { rect.min.x - self.offset_x as f32 }; let content = Rect::from_min_size(Pos2::new(left, rect.min.y - self.offset as f32), size); crate::runtime::egui::widgets::note_scroll_region(&ctx, rect.intersect(ui.clip_rect()), content); + // The header row scrolls sideways with the rows. + if let Some(header) = self.header.view() { + let row = Rect::from_x_y_ranges(content.x_range(), header.y_range()); + crate::runtime::egui::widgets::note_scroll_region(&ctx, header.intersect(ui.clip_rect()), row); + } } let sheet = Self::selects_cells(comp); let columns: Vec = match layout.grid() { diff --git a/crates/ui-widgets/src/list/row/placement.rs b/crates/ui-widgets/src/list/row/placement.rs index 90c0a0071..c0e3da401 100644 --- a/crates/ui-widgets/src/list/row/placement.rs +++ b/crates/ui-widgets/src/list/row/placement.rs @@ -412,7 +412,10 @@ impl ListRow<'_> { let indent_given = given.pop().unwrap_or(0.0); let used = gaps + given.iter().sum::(); let label_end = end - used; - let overflow = (base + column + leading_width + sizes.min_label - label_end).max(0.0); + // A row without columns keeps its label's least width and runs past its end, where the collection clips it; a + // label column's cell is the label's hard end, so it elides there instead of running into the next column. + let in_cell = self.label_span(cells_of).is_some(); + let overflow = if in_cell { 0.0 } else { (base + column + leading_width + sizes.min_label - label_end).max(0.0) }; let label_end = label_end + overflow; let indent = indent_given.min((label_end - base - column - leading_width - sizes.min_label).max(0.0)); let mut from_end = 0.0; diff --git a/crates/ui-widgets/tests/it/table.rs b/crates/ui-widgets/tests/it/table.rs index 60d3a8a04..e439e6e1c 100644 --- a/crates/ui-widgets/tests/it/table.rs +++ b/crates/ui-widgets/tests/it/table.rs @@ -212,3 +212,53 @@ fn ctrl_or_cmd_with_an_arrow_moves_the_focused_column() { assert_eq!(events.borrow().iter().filter(|event| matches!(event, ColumnEvent::Moved { .. })).count(), 1, "a plain arrow moves no column"); } } + +/// A table wider than its room: a fill name column and four numeric columns. +fn wide_table() -> Fixture { + let mut fixture = Fixture::new(|ui| { + let numeric = |key: &'static str, header: &'static str| ColumnSpec::new(ColumnKey::new(key), header, ColumnWidth::Controls(3.0)).numeric(); + let columns = vec![ + ColumnSpec::new(NAME, "Name", ColumnWidth::fill(1.0)), + numeric("pages", "Pages"), + numeric("size", "Size"), + numeric("age", "Age"), + numeric("count", "Count"), + ]; + let keys = [0_usize]; + let source = keyed(&keys, |key: &usize| *key); + Table::::new("Files", columns).show_items(ui, &source, &mut |_, icx| { + ListRow::new(LONG).cells(icx.cells()).cell(ColumnKey::new("pages"), "12").cell(ColumnKey::new("count"), "7") + }); + }); + fixture.room(egui::Vec2::new(300.0, 200.0)).settle(); + fixture +} + +fn painted(fixture: &Fixture, text: &str) -> Rect { + fixture.texts().iter().find(|painted| painted.text == text).map(|painted| painted.rect).expect("the text was painted") +} + +#[trial] +fn columns_wider_than_the_table_keep_their_least_width_scroll_sideways_with_the_header_and_keep_their_text_in_their_cells() { + let mut fixture = wide_table(); + let header = |fixture: &Fixture, label: &str| fixture.by_label(label).expect("the header cell's node").rect; + let table = fixture.by_role(Role::TABLE).first().expect("the table's node").rect; + let (name, pages, count) = (header(&fixture, "Name"), header(&fixture, "Pages"), header(&fixture, "Count")); + assert!(name.width() >= pages.width() * 2.0 / 3.0 - 0.5, "the fill column keeps the least column width: {name:?} beside {pages:?}"); + assert!(count.right() > table.right(), "the columns run past the table: {count:?} in {table:?}"); + let label = fixture.texts().iter().find(|painted| painted.text == LONG).cloned().expect("the label was painted"); + assert!(label.elided && label.rect.right() <= name.right() + 0.5, "the label stays in its column: {:?} in {name:?}", label.rect); + let before = painted(&fixture, "7"); + fixture.frame(vec![pointer::moved(table.center())]); + fixture.frame(vec![egui::Event::MouseWheel { + unit: egui::MouseWheelUnit::Point, + delta: egui::Vec2::new(-40.0, 0.0), + phase: egui::TouchPhase::Move, + modifiers: Modifiers::NONE, + source: egui::MouseWheelSource::Wheel, + }]); + fixture.settle(); + let (moved_cell, moved_text) = (count.left() - header(&fixture, "Count").left(), before.left() - painted(&fixture, "7").left()); + assert!(moved_cell > 1.0, "the wheel scrolls the columns sideways: {moved_cell}"); + assert!((moved_cell - moved_text).abs() < 0.5, "the header scrolls with the rows: header {moved_cell}, row {moved_text}"); +}