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

This commit is contained in:
aiongg
2026-11-12 14:32:49 +00:00
parent 9604d6b294
commit 3750a77cb5
5 changed files with 104 additions and 14 deletions
+3 -1
View File
@@ -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)>) {
+41 -11
View File
@@ -168,6 +168,8 @@ pub(super) struct Header {
hideable: Vec<ColumnKey>,
/// The empty header after the last column.
room: Option<Rect>,
/// The header row in view: cells that scroll past it are clipped to it.
view: Option<Rect>,
/// 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<f32> {
let content: Vec<f32> = self.content.iter().map(|wide| wide.max(self.min)).collect();
let kinds: Vec<ColumnWidth> = self
let mut kinds: Vec<ColumnWidth> = 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<C: Component>(&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<C: Component>(&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::<HeaderWords>().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::<f32>().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<Rect> {
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<bool> {
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
@@ -288,7 +288,7 @@ impl<C: Collection> ShapeHooks<C> 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<C: Collection> ShapeHooks<C> 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<u64> = match layout.grid() {
+4 -1
View File
@@ -412,7 +412,10 @@ impl ListRow<'_> {
let indent_given = given.pop().unwrap_or(0.0);
let used = gaps + given.iter().sum::<f32>();
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;
+50
View File
@@ -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::<usize>::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}");
}