From 29dc3dda3f231a3fb81f743b57faef783afae73a Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 17 Nov 2026 22:31:04 -0800 Subject: [PATCH] Fix #5941: drop-down lists open at their first option A drop-down list kept the scroll position it had when it closed, so the Export As Format list could open again with SVG, its first format, above the top of the list. Each list built on the shared drop-down widget now starts at its first option when it opens. The Export As test now reads the list's own rect instead of assuming the field's width, checks the bar only while the list does not show every format, and opens the list again after scrolling it. --- crates/ui-egui/src/dialogs/tests_export.rs | 76 ++++++++++++++++------ crates/ui-egui/src/widgets/dropdowns.rs | 11 ++++ 2 files changed, 67 insertions(+), 20 deletions(-) diff --git a/crates/ui-egui/src/dialogs/tests_export.rs b/crates/ui-egui/src/dialogs/tests_export.rs index a512162a2e..3d9ba05cc4 100644 --- a/crates/ui-egui/src/dialogs/tests_export.rs +++ b/crates/ui-egui/src/dialogs/tests_export.rs @@ -245,10 +245,11 @@ fn an_empty_export_as_range_shows_an_example() { } /// #5941: the Format list showed no scroll bar until the pointer came over it, so nothing told -/// the user that more formats lay below the last one shown. Opened with the pointer elsewhere, the -/// list shows its bar beside the formats. +/// the user that more formats lay below the last one shown, and it opened again where it was last +/// scrolled, with its first formats out of view. Opened with the pointer elsewhere, a list that +/// does not show every format shows its bar, and a list opened again starts at its first format. #[test] -fn the_format_list_shows_its_scroll_bar_while_the_pointer_is_elsewhere() { +fn the_format_list_shows_its_scroll_bar_at_rest_and_opens_at_its_first_format() { fn walk(shape: &egui::Shape, clip: egui::Rect, texts: &mut Vec<(String, egui::Rect, egui::Rect)>, bars: &mut Vec) { match shape { egui::Shape::Text(t) => texts.push((t.galley.text().into(), t.visual_bounding_rect(), clip)), @@ -276,25 +277,60 @@ fn the_format_list_shows_its_scroll_bar_while_the_pointer_is_elsewhere() { run(&mut app, vec![away.clone()]); let (texts, _) = run(&mut app, vec![]); let field = texts.iter().find(|(s, ..)| s == "PNG").map(|(_, r, _)| r.center()).expect("the Format field shows PNG"); - let click = |pressed| egui::Event::PointerButton { pos: field, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() }; - run(&mut app, vec![egui::Event::PointerMoved(field), click(true)]); - run(&mut app, vec![click(false)]); - run(&mut app, vec![away]); - let (mut texts, mut bars) = (vec![], vec![]); - for _ in 0..4 { - (texts, bars) = run(&mut app, vec![]); - } + let click_at = |pos, pressed| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() }; let labels: Vec<&str> = export_as::formats().iter().map(|f| f.label).collect(); - // The list's rows: the format names drawn below the field. - let rows: Vec<_> = texts.iter().filter(|(s, r, _)| labels.contains(&s.as_str()) && r.top() > field.y + 8.0).collect(); + // Opens the list, moves the pointer away and returns the list's rect, the format names drawn in + // it with their clip rects, and the bars drawn. + type Run<'r> = dyn FnMut(&mut VectorcraftApp, Vec) -> (Vec<(String, egui::Rect, egui::Rect)>, Vec) + 'r; + let open = |run: &mut Run<'_>, app: &mut VectorcraftApp| { + run(app, vec![egui::Event::PointerMoved(field), click_at(field, true)]); + run(app, vec![click_at(field, false)]); + run(app, vec![away.clone()]); + let (mut texts, mut bars) = (vec![], vec![]); + for _ in 0..4 { + (texts, bars) = run(app, vec![]); + } + 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 Format list is open"); + let rows: Vec<_> = texts.into_iter().filter(|(s, r, _)| labels.contains(&s.as_str()) && list.contains_rect(*r)).collect(); + (list, rows, bars) + }; + let (list, rows, bars) = open(&mut run, &mut app); assert!(!rows.is_empty(), "the Format list is open"); - assert!(rows.len() < labels.len(), "the list holds more formats than it shows: {} of {}", rows.len(), labels.len()); - let left = rows.iter().map(|(_, r, _)| r.left()).fold(f32::INFINITY, f32::min); - let top = rows.iter().map(|(_, r, _)| r.top()).fold(f32::INFINITY, f32::min); - let bottom = rows.iter().map(|(_, r, _)| r.bottom()).fold(0.0, f32::max); - // The list is no wider than the 160 pt field. - let beside = egui::Rect::from_min_max(egui::pos2(left, top - 12.0), egui::pos2(left + 160.0, bottom + 12.0)); - assert!(bars.iter().any(|b| beside.contains_rect(*b)), "the list's scroll bar is drawn at rest beside the formats in {beside:?}: {bars:?}"); + // A list that shows every format needs no bar; one that does not shows its bar. + if rows.len() < labels.len() { + assert!( + bars.iter().any(|b| list.contains_rect(*b) && b.left() > list.center().x), + "the list's scroll bar is drawn at rest in {list:?}: {bars:?}" + ); + } + + // Scrolled, closed and opened again, the list starts at its first format, not cut off at the top. + run(&mut app, vec![egui::Event::PointerMoved(list.center())]); + let wheel = egui::Event::MouseWheel { + unit: egui::MouseWheelUnit::Point, + delta: egui::vec2(0.0, -30.0), + modifiers: Default::default(), + phase: egui::TouchPhase::Move, + source: egui::MouseWheelSource::Unknown, + }; + run(&mut app, vec![wheel]); + for _ in 0..4 { + run(&mut app, vec![]); + } + run(&mut app, vec![egui::Event::PointerMoved(field), click_at(field, true)]); + run(&mut app, vec![click_at(field, false)]); + let (_, rows, _) = open(&mut run, &mut app); + let first = rows.iter().find(|(s, ..)| s == labels[0]); + assert!( + first.is_some_and(|(_, r, clip)| r.top() >= clip.top()), + "the reopened list shows {} whole at its top: {:?}", + labels[0], + rows.iter().map(|(s, r, c)| (s, r.top(), c.top())).take(3).collect::>() + ); } #[test] diff --git a/crates/ui-egui/src/widgets/dropdowns.rs b/crates/ui-egui/src/widgets/dropdowns.rs index 431bc4e8f9..73b80a99b4 100644 --- a/crates/ui-egui/src/widgets/dropdowns.rs +++ b/crates/ui-egui/src/widgets/dropdowns.rs @@ -147,6 +147,17 @@ 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, not where it was scrolled when it last closed (#5941). + let shown_key = button_id.with("list-shown-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)); + ui.data_mut(|d| d.insert_temp(shown_key, pass)); + if last.is_none_or(|p| p + 1 < pass) { + ui.scroll_to_cursor_animation(Some(egui::Align::TOP), egui::style::ScrollAnimation::none()); + } + list(ui) + }; let enter_for_combo = ui.memory(|m| m.has_focus(button_id)) || craft_ui::ComboBox::is_open(ui.ctx(), button_id); let response = craft_ui::ComboBox::from_id_salt(HashOnly(&id)) .selected_text(current)