Part of #5941: lists with their own rows reopen where they were; picker and glyph bars fit

- A drop-down list that draws its own rows (blend modes, warp styles,
  tracing presets) has no current option for the list to show. It
  opened at its first row since the change that opens option lists at
  their current option. It now opens where it was scrolled when it
  closed, as before that change, and the scroll is put back after the
  list takes its height beside the field: the list is taller on the pass
  it opens, and the scroll was clamped to that height.
- When several options have the current name, the list scrolls to the
  first of them.
- The Color Picker's heading set the dialog to the width of its content
  without the scroll bar's column, and the body then left out that
  column a second time. In a window 448 pt wide and 380 pt tall the
  heading measured the columns side by side while the body stacked
  them. The heading now keeps the column in the width.
- The Glyphs panel counts a row of its two smallest cell sizes at the
  height it is drawn, a button's height, so a grid whose rows run past
  the list leaves room for the bar.
This commit is contained in:
dotCooCoo
2026-11-18 19:53:40 -08:00
parent 59012217f6
commit d78e5759be
5 changed files with 249 additions and 31 deletions
+14 -12
View File
@@ -346,22 +346,23 @@ fn button_width(ui: &Ui) -> f32 {
})
}
/// A compact width shared by the heading and body, bounded by the available window width. While
/// the body scrolls, the width leaves out the column of its scroll bar, so the body and its bar fit
/// in the window side by side.
fn layout_size(ui: &Ui) -> (f32, f32, f32, bool) {
/// A compact width shared by the heading and body, bounded by the available window width, and the
/// width of the body's scroll bar column. While the body scrolls, the width leaves out that column,
/// so the body and its bar fit in the window side by side.
fn layout_size(ui: &Ui) -> (f32, f32, f32, bool, f32) {
let button_w = button_width(ui);
let values_w = PREVIEW_W + COLUMN_GAP + button_w;
let fixed_w = SLIDER_GAP + SLIDER_W + COLUMN_GAP + values_w;
let fit = |room: f32| ((room - fixed_w).clamp(MIN_FIELD, FIELD), room < MIN_FIELD + fixed_w);
let (side, stacked) = fit(ui.available_width());
// The margin keeps the bar's column whenever the body may scroll, as its height is estimated.
let (side, stacked) = if body_height(ui, side, stacked) + 40.0 > scroll_height(ui) {
fit(ui.available_width() - ui.spacing().scroll.allocated_width())
if body_height(ui, side, stacked) + 40.0 > scroll_height(ui) {
let bar = ui.spacing().scroll.allocated_width();
let (side, stacked) = fit(ui.available_width() - bar);
(side, values_w, button_w, stacked, bar)
} else {
(side, stacked)
};
(side, values_w, button_w, stacked)
(side, values_w, button_w, stacked, 0.0)
}
}
/// The tallest the body's scrolling content is drawn.
@@ -380,10 +381,11 @@ fn body_height(ui: &Ui, side: f32, stacked: bool) -> f32 {
/// Full-width, draggable title band using the app's existing theme colours.
fn header(ui: &mut Ui, title: &str) {
let (side, values_w, _, stacked) = layout_size(ui);
let (side, values_w, _, stacked, bar) = layout_size(ui);
let field_w = side + SLIDER_GAP + SLIDER_W;
let width = if stacked { field_w.max(values_w) } else { field_w + COLUMN_GAP + values_w };
ui.set_width(width.min(ui.available_width()));
// The width includes the bar's column: the body measures from it and leaves the column out.
ui.set_width((width + bar).min(ui.available_width()));
let margin = f32::from(super::MARGIN);
let (r, _) = ui.allocate_exact_size(vec2(ui.available_width(), HEADER_H - margin), Sense::hover());
let band = Rect::from_min_max(r.min - vec2(margin, margin), r.max + vec2(margin, 0.0));
@@ -407,7 +409,7 @@ fn body(app: &mut VectorcraftApp, ui: &mut Ui, d: &mut Dialog) -> DialogAction {
let original = d.fields.get("original").and_then(color_value).unwrap_or(p.color);
let gamut = gamut_fix(app, ui.ctx(), "picker-gamut", &p.color);
super::modal::fits_itself(ui);
let (side, values_w, button_w, stacked) = layout_size(ui);
let (side, values_w, button_w, stacked, _) = layout_size(ui);
let field_w = side + SLIDER_GAP + SLIDER_W;
let content_w = if stacked { field_w.max(values_w) } else { field_w + COLUMN_GAP + values_w };
let mut pick: Option<Picked> = None;
@@ -823,6 +823,26 @@ fn a_scrolling_picker_shows_its_bar_and_fits_its_content_beside_it() {
}
}
/// #5941: while the body scrolls, the heading sets the dialog's width to the one the body lays its
/// field and columns out in, with the scroll bar's column, so the body measures the same layout from
/// it and does not leave out the bar's column a second time.
#[test]
fn the_heading_leaves_the_body_the_width_of_the_layout_it_measured() {
let ctx = context();
for width in (400..=800).step_by(4) {
let size = vec2(width as f32, 380.0);
let raw = egui::RawInput { screen_rect: Some(Rect::from_min_size(egui::Pos2::ZERO, size)), ..Default::default() };
let mut out = ctx.run_ui(raw, |ui| {
let (side, _, _, stacked, bar) = layout_size(ui);
assert!(bar > 0.0, "{size:?}: the body scrolls");
header(ui, "Color Picker");
let (body_side, _, _, body_stacked, _) = layout_size(ui);
assert_eq!((body_side, body_stacked), (side, stacked), "{size:?}: the body lays out the field and columns the heading measured");
});
out.textures_delta.clear();
}
}
/// Type all three Lab components through the numeric fields, then apply the picked color.
#[test]
fn native_lab_entry_in_color_picker() {
+34 -4
View File
@@ -69,10 +69,16 @@ fn grid_cols(ui: &Ui, w: f32, cell: f32, count: usize, list_h: f32) -> usize {
let gap = ui.spacing().item_spacing.y;
let cols = fit(w);
// The height `ScrollArea::show_rows` gives the rows: the gaps between them, none after the last.
let content_h = count.div_ceil(cols) as f32 * (cell + gap) - gap;
let content_h = count.div_ceil(cols) as f32 * (row_height(ui, cell) + gap) - gap;
if list_h.min(ui.available_height()).ceil() < content_h { fit(w - ui.spacing().scroll.allocated_width()) } else { cols }
}
/// The height of a row of cells of side `cell`: a row laid out with `Ui::horizontal` is at least as
/// tall as a button, so the two smallest cell sizes give rows taller than their cells.
fn row_height(ui: &Ui, cell: f32) -> f32 {
cell.max(ui.spacing().interact_size.y)
}
/// Character subsets offered by the Show menu.
const SUBSETS: [(&str, u32, u32); 7] = [
("Entire Font", 0, 0x10FFFF),
@@ -402,14 +408,14 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
&& let Ok(i) = chars.binary_search_by_key(&c, |x| x.0)
&& !shown.contains(&(i / cols))
{
let row_h = cell + ui.spacing().item_spacing.y;
let row_h = row_height(ui, cell) + ui.spacing().item_spacing.y;
area = area.vertical_scroll_offset((((i / cols) as f32 + 0.5) * row_h - list_h * 0.5).max(0.0));
}
// A pick in a cell's pop-up of alternates: the character (none for a glyph no character maps),
// the glyph of its cell, the glyph picked.
let mut popup_pick: Option<(Option<char>, u32, u32)> = None;
let shown =
area.show_rows(ui, cell, rows, |ui, range| {
area.show_rows(ui, row_height(ui, cell), rows, |ui, range| {
for r in range.clone() {
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing = vec2(0.0, 0.0);
@@ -626,7 +632,7 @@ fn alternates(app: &mut VectorcraftApp, ui: &mut Ui, w: f32) {
.max_height(list_h)
.min_scrolled_height(list_h.min(64.0))
.auto_shrink([false, false]);
area.show_rows(ui, cell, rows, |ui, range| {
area.show_rows(ui, row_height(ui, cell), rows, |ui, range| {
for r in range {
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing = vec2(0.0, 0.0);
@@ -2599,6 +2605,30 @@ mod tests {
assert!(scrolled > 0 && fitted > 0, "both layouts are drawn: {scrolled} scrolled, {fitted} fitted");
}
/// #5941: at the smallest cell sizes a row of the grid is as tall as a button, taller than its
/// cells. A list tall enough for four rows of cells but not for four rows as drawn scrolls, so
/// its cells go beside the bar, at a width that holds a whole number of cells too.
#[test]
fn small_cells_leave_room_for_the_bar_when_their_rows_as_drawn_run_past_the_list() {
let ctx = egui::Context::default();
crate::theme::apply(&ctx, Default::default());
let raw = egui::RawInput { screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(800.0, 800.0))), ..Default::default() };
let mut out = ctx.run_ui(raw, |ui| {
let gap = ui.spacing().item_spacing.y;
let bar = ui.spacing().scroll.allocated_width();
for cell in [CELL_STEPS[0], CELL_STEPS[1]] {
let drawn = ui.spacing().interact_size.y;
assert!(drawn > cell, "{cell} pt cells: rows are {drawn} pt as drawn");
let w = 6.0 * cell;
// Four rows of six: they fit at the cells' own height, not at the rows' drawn height.
let list_h = 4.0 * (cell + drawn) / 2.0 + 3.0 * gap;
let beside = ((w - bar) / cell).floor() as usize;
assert_eq!(grid_cols(ui, w, cell, 24, list_h), beside, "{cell} pt cells in a list {list_h} pt tall");
}
});
out.textures_delta.clear();
}
/// Show › Entire Font also lists the glyphs no character maps.
#[test]
fn entire_font_lists_the_unencoded_glyphs() {
+50 -15
View File
@@ -117,19 +117,34 @@ pub fn dropdown_with_named(
})
}
/// The flag that [`combo_atoms_named`] sets while it draws a list on the pass the list opens and
/// the pass after it.
fn opening_list_id() -> egui::Id {
egui::Id::unique("dropdown-list-opening")
/// What [`option_row`] found in a list that [`combo_atoms_named`] draws on the pass the list opens
/// and the pass after it.
#[derive(Clone, Copy, Default)]
struct Opening {
/// The list draws its options with [`option_row`].
rows: bool,
/// A current option was scrolled into view.
scrolled: bool,
}
thread_local! {
/// The [`Opening`] that [`combo_atoms_named`] keeps while it draws a list on the pass the list
/// opens and the pass after it; `None` otherwise. A row reads it without a lookup in the
/// context's data.
static OPENING: std::cell::Cell<Option<Opening>> = const { std::cell::Cell::new(None) };
}
/// One option of a dropdown list, `selected` for the current one. On the pass the list opens and
/// the next one, the current option scrolls to the middle of the list, or as near to it as the
/// list's ends allow, so that it shows whole (#5941).
/// the next one, the first current option scrolls to the middle of the list, or as near to it as
/// the list's ends allow, so that it shows whole (#5941).
fn option_row<'a>(ui: &mut Ui, enabled: bool, selected: bool, atoms: impl egui::IntoAtoms<'a>) -> Response {
let response = ui.add_enabled(enabled, egui::Button::selectable(selected, atoms));
if selected && ui.data(|d| d.get_temp::<bool>(opening_list_id())).unwrap_or(false) {
ui.scroll_to_rect_animation(response.rect, Some(egui::Align::Center), egui::style::ScrollAnimation::none());
if let Some(opening) = OPENING.get() {
let scroll = selected && !opening.scrolled;
if scroll {
ui.scroll_to_rect_animation(response.rect, Some(egui::Align::Center), egui::style::ScrollAnimation::none());
}
OPENING.set(Some(Opening { rows: true, scrolled: opening.scrolled || scroll }));
}
response
}
@@ -176,29 +191,49 @@ fn combo_atoms_named<'a, R>(
.corner_radius(CornerRadius::same(3))
.show(ui, |ui| {
let button_id = combo_button_id(ui, &id);
// A list opens at its first option, or with its current option in view (`option_row`),
// not where it was scrolled when it last closed (#5941).
// A list whose options are `option_row`s opens with its current option in view, or at
// its first option, not where it was scrolled when it last closed (#5941). A list that
// draws its own rows has no current option to show, and opens where it was scrolled
// when it last closed.
let shown_key = button_id.with("list-shown-pass");
let opened_key = button_id.with("list-opened-pass");
let offset_key = button_id.with("list-offset");
let list = move |ui: &mut Ui| {
let pass = ui.ctx().cumulative_pass_nr();
let last = ui.data_mut(|d| d.get_temp::<u64>(shown_key));
ui.data_mut(|d| d.insert_temp(shown_key, pass));
let opening = last.is_none_or(|p| p + 1 < pass);
if opening {
ui.scroll_to_cursor_animation(Some(egui::Align::TOP), egui::style::ScrollAnimation::none());
ui.data_mut(|d| d.insert_temp(opened_key, pass));
}
// The menu measures itself against the whole screen on the pass it opens and takes
// the room beside its field on the next one, so the current option is scrolled into
// view on both.
let settling = ui.data(|d| d.get_temp::<u64>(opened_key)).is_some_and(|p| pass <= p + 1);
if settling {
ui.data_mut(|d| d.insert_temp(opening_list_id(), true));
}
// A list drawn inside this one's rows finds its own record, and this one's comes back after it.
let outer = if settling { OPENING.replace(Some(Opening::default())) } else { OPENING.take() };
let (content_left, content_top) = (ui.max_rect().left(), ui.max_rect().top());
let at = move |y: f32| Rect::from_min_size(egui::pos2(content_left, content_top + y), Vec2::ZERO);
let chosen = list(ui);
let found = OPENING.replace(outer).unwrap_or_default();
let none = egui::style::ScrollAnimation::none();
if settling {
ui.data_mut(|d| d.remove::<bool>(opening_list_id()));
if found.rows {
if opening && !found.scrolled {
ui.scroll_to_rect_animation(at(0.0), Some(egui::Align::TOP), none);
}
} else {
// The list is taller on the pass it opens than beside its field, and egui
// clamps the scroll to the taller list, so the scroll it had when it closed
// is put back on both passes. A scroll area leaves an item spacing above a
// target aligned to its top, so the target is that much below the scroll.
let saved = ui.data(|d| d.get_temp::<f32>(offset_key)).unwrap_or(0.0);
ui.scroll_to_rect_animation(at(saved + ui.spacing().item_spacing.y), Some(egui::Align::TOP), none);
}
}
if !settling || chosen.is_some() {
// The scroll while the list is open: the content's top is above the clip's by it.
ui.data_mut(|d| d.insert_temp(offset_key, (ui.clip_rect().top() - content_top).max(0.0)));
}
chosen
};
+131
View File
@@ -187,6 +187,137 @@ fn a_dropdown_in_a_short_window_opens_with_its_current_option_in_view() {
}
}
// #5941: a drop-down list that draws its own rows (warp styles, tracing presets) has no current
// option for the drop-down to scroll to, so it opens where it was scrolled when it last closed, and
// a row picked below the first ones is still in view when the list opens again. That holds for a
// field in the middle of a short window too, where the list is taller on the pass it opens than
// beside the field, and for a list scrolled to its end.
#[test]
fn a_dropdown_that_draws_its_own_rows_opens_where_it_was_scrolled() {
// (window height, space above the field, wheel, a row the wheel brings into view)
for (height, above, wheel, row) in [(300.0, 20.0, -300.0, "Row 15"), (260.0, 117.0, -2000.0, "Row 19")] {
own_rows_reopen(height, above, wheel, row);
}
}
fn own_rows_reopen(height: f32, above: f32, wheel: f32, row: &str) {
let case = format!("{height} pt window, {above} pt above the field");
let items: Vec<String> = (0..20).map(|i| format!("Row {i:02}")).collect();
let ctx = themed(Default::default());
let field = std::cell::Cell::new(Rect::NOTHING);
let current = std::cell::RefCell::new(String::from("Row 00"));
let mut add = |ui: &mut Ui| {
ui.add_space(above);
let shown = current.borrow().clone();
let r = ui.horizontal(|ui| {
combo(ui, "own-rows", &shown, 160.0, |ui| {
let mut chosen = None;
for (i, s) in items.iter().enumerate() {
if ui.selectable_label(*s == shown, s.as_str()).clicked() {
chosen = Some(i);
}
}
chosen
})
});
if let Some(i) = r.inner {
current.replace(items[i].clone());
}
field.set(r.response.rect);
};
let mut time = 0.0;
let mut run = |events: Vec<egui::Event>| {
time += 0.1;
let raw = egui::RawInput {
time: Some(time),
screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(600.0, height))),
events,
..Default::default()
};
let mut out = ctx.run_ui(raw, &mut add);
out.textures_delta.clear();
out.shapes
};
// The rows that show whole, with their tops: inside the open list and the clip they are painted
// in.
let rows = |clipped: Vec<egui::epaint::ClippedShape>| -> Vec<(String, f32)> {
let list = ctx
.memory(|m| {
m.layer_ids().filter(|l| l.order == egui::Order::Foreground && m.areas().is_visible(l)).last().and_then(|l| m.area_rect(l.id))
})
.expect("the list is open");
let mut shapes = vec![];
fn flat(s: egui::Shape, clip: Rect, out: &mut Vec<(egui::Shape, Rect)>) {
match s {
egui::Shape::Vec(v) => v.into_iter().for_each(|s| flat(s, clip, out)),
s => out.push((s, clip)),
}
}
clipped.into_iter().for_each(|c| flat(c.shape, c.clip_rect, &mut shapes));
shapes
.iter()
.filter_map(|(s, clip)| match s {
egui::Shape::Text(t) if list.contains_rect(t.visual_bounding_rect()) && clip.contains_rect(t.visual_bounding_rect()) => {
Some((t.galley.text().to_owned(), t.visual_bounding_rect().top().round()))
}
_ => None,
})
.collect()
};
let has = |rows: &[(String, f32)], name: &str| rows.iter().any(|(s, _)| s == name);
run(vec![]);
let at = field.get().center();
let press = |pressed| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() };
let open = |run: &mut dyn FnMut(Vec<egui::Event>) -> Vec<egui::epaint::ClippedShape>| {
run(vec![egui::Event::PointerMoved(at), press(true)]);
run(vec![press(false)]);
let mut clipped = vec![];
for _ in 0..4 {
clipped = run(vec![]);
}
clipped
};
let first = rows(open(&mut run));
assert!(has(&first, "Row 00") && !has(&first, row), "{case}: the list opens at its top: {first:?}");
// Scrolled down to `row` and closed.
let list = ctx
.memory(|m| m.layer_ids().filter(|l| l.order == egui::Order::Foreground && m.areas().is_visible(l)).last().and_then(|l| m.area_rect(l.id)))
.expect("the list is open");
run(vec![egui::Event::PointerMoved(list.center())]);
let wheel = egui::Event::MouseWheel {
unit: egui::MouseWheelUnit::Point,
delta: vec2(0.0, wheel),
modifiers: Default::default(),
phase: egui::TouchPhase::Move,
source: egui::MouseWheelSource::Unknown,
};
run(vec![wheel]);
let mut clipped = vec![];
for _ in 0..4 {
clipped = run(vec![]);
}
let scrolled = rows(clipped);
assert!(has(&scrolled, row), "{case}: the wheel scrolls {row} into view: {scrolled:?}");
// Closed and opened again twice: each time its rows are where they were when it closed.
for time in ["first", "second"] {
run(vec![egui::Event::PointerMoved(at), press(true)]);
run(vec![press(false)]);
run(vec![]);
let reopened = rows(open(&mut run));
assert_eq!(reopened, scrolled, "{case}: the list opens again where it was scrolled, the {time} time");
}
// `row` picked: the field shows it, and the list opens again where it was when it was picked.
let top = scrolled.iter().find(|(s, _)| s == row).map(|(_, top)| *top).expect("the row shows");
let on_row = pos2(list.left() + 30.0, top + 6.0);
let click = |pressed| egui::Event::PointerButton { pos: on_row, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() };
run(vec![egui::Event::PointerMoved(on_row), click(true)]);
run(vec![click(false)]);
run(vec![]);
assert_eq!(*current.borrow(), row, "{case}: the click picks {row}");
let reopened = rows(open(&mut run));
assert_eq!(reopened, scrolled, "{case}: the list opens again where {row} was picked");
}
// Regression test for #5941: the toolbar's and the dock icon column's scroll bar showed nothing
// until the pointer came over the column. Its slim line shows at rest, still at most 4 points
// wide (#808).