From 6de3954e6b6e3df4b1cbd2f86cb71d5b7098c3cb Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 18 Nov 2026 06:26:24 -0800 Subject: [PATCH] Part of #5941: drop-down lists keep their current option in view in a short window A drop-down list measures itself against the whole window on the pass it opens and takes the room beside its field on the next pass. The current option was scrolled into view only on the first pass, so where the room beside the field was less than half the list, the option could end up below the rows shown. It now scrolls into view on both passes. The new test opens a 20-option list in windows 260 and 180 points tall with the current option at its end, in its middle and near its start, and checks that the option shows whole inside the list's clip. --- crates/ui-egui/src/widgets/dropdowns.rs | 17 ++++-- crates/ui-egui/src/widgets/look_tests.rs | 67 ++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 4 deletions(-) diff --git a/crates/ui-egui/src/widgets/dropdowns.rs b/crates/ui-egui/src/widgets/dropdowns.rs index 42abae54b8..d0efa245fc 100644 --- a/crates/ui-egui/src/widgets/dropdowns.rs +++ b/crates/ui-egui/src/widgets/dropdowns.rs @@ -117,13 +117,14 @@ pub fn dropdown_with_named( }) } -/// The flag that [`combo_atoms_named`] sets while it draws a list on the pass the list opens. +/// 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") } -/// One option of a dropdown list, `selected` for the current one. On the pass the list opens, the -/// current option scrolls to the middle of the list, or as near to it as the list's ends allow, so +/// 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). 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)); @@ -178,6 +179,7 @@ fn combo_atoms_named<'a, R>( // 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). let shown_key = button_id.with("list-shown-pass"); + let opened_key = button_id.with("list-opened-pass"); let list = move |ui: &mut Ui| { let pass = ui.ctx().cumulative_pass_nr(); let last = ui.data_mut(|d| d.get_temp::(shown_key)); @@ -185,10 +187,17 @@ fn combo_atoms_named<'a, R>( 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::(opened_key)).is_some_and(|p| pass <= p + 1); + if settling { ui.data_mut(|d| d.insert_temp(opening_list_id(), true)); } let chosen = list(ui); - if opening { + if settling { ui.data_mut(|d| d.remove::(opening_list_id())); } chosen diff --git a/crates/ui-egui/src/widgets/look_tests.rs b/crates/ui-egui/src/widgets/look_tests.rs index c66b7ea8a5..502602e53a 100644 --- a/crates/ui-egui/src/widgets/look_tests.rs +++ b/crates/ui-egui/src/widgets/look_tests.rs @@ -120,6 +120,73 @@ fn a_scroll_bar_shows_while_the_pointer_is_elsewhere() { } } +// Regression test for #5941: a drop-down list opens with its current option in view. With its field +// in the middle of a short window, neither side has room for the list's 200 pt, and the menu is cut +// to the room beside the field; the current option still shows whole, at the end of the list and in +// its middle, also when the room beside the field is less than half the list. +#[test] +fn a_dropdown_in_a_short_window_opens_with_its_current_option_in_view() { + let items: Vec = (0..20).map(|i| format!("Option {i:02}")).collect(); + let labels: Vec<&str> = items.iter().map(String::as_str).collect(); + // (window height, space above the field, current option): about 105 pt and 65 pt on either side + // of the field. + for (height, above, current) in [(260.0, 117.0, 19), (260.0, 117.0, 10), (180.0, 77.0, 10), (180.0, 77.0, 3)] { + let ctx = themed(Default::default()); + let field = std::cell::Cell::new(Rect::NOTHING); + let mut add = |ui: &mut Ui| { + ui.add_space(above); + field.set(ui.horizontal(|ui| dropdown_names(ui, "short-list", labels[current], &labels, 160.0)).response.rect); + }; + let mut run = |time: f64, events: Vec| { + 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 + }; + run(0.0, vec![]); + let at = field.get().center(); + let press = |pressed| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() }; + run(0.1, vec![egui::Event::PointerMoved(at), press(true)]); + run(0.2, vec![press(false)]); + let mut clipped = vec![]; + for i in 3..10 { + clipped = run(f64::from(i) * 0.1, vec![]); + } + // Each shape with the clip rect it is painted in. + 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)); + 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"); + // The options that show whole: inside the list and inside the clip they are painted in. + let rows: Vec<(String, Rect)> = 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())) + } + _ => None, + }) + .collect(); + let case = format!("{height} pt window, {} current", labels[current]); + assert!(rows.len() < labels.len(), "{case}: the list does not show every option in {list:?}: {rows:?}"); + assert!(rows.iter().any(|(s, _)| s == labels[current]), "{case}: the current option shows whole in {list:?}: {rows:?}"); + } +} + // 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).