diff --git a/crates/ui-egui/src/dialogs/tests_export.rs b/crates/ui-egui/src/dialogs/tests_export.rs index 8825f65a5e..548db64350 100644 --- a/crates/ui-egui/src/dialogs/tests_export.rs +++ b/crates/ui-egui/src/dialogs/tests_export.rs @@ -246,10 +246,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, 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. +/// scrolled, with its first formats out of view. Opened with the pointer elsewhere and the last +/// format chosen, a list that does not show every format shows its bar, the chosen format shows +/// whole, and a list opened again after a scroll shows the same rows as when it first opened. #[test] -fn the_format_list_shows_its_scroll_bar_at_rest_and_opens_at_its_first_format() { +fn the_format_list_shows_its_scroll_bar_at_rest_and_opens_with_the_chosen_format_in_view() { 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)), @@ -258,11 +259,15 @@ fn the_format_list_shows_its_scroll_bar_at_rest_and_opens_at_its_first_format() _ => {} } } + let labels: Vec<&str> = export_as::formats().iter().map(|f| f.label).collect(); + let last = export_as::formats().last().copied().expect("Export As lists formats"); let (mut app, _) = app(1); - app.run("file.exportAs", json!({"format": "png"})).unwrap(); + app.run("file.exportAs", json!({"format": last.id})).unwrap(); let ctx = egui::Context::default(); theme::install_fonts(&ctx); theme::apply(&ctx, Default::default()); + // Every scroll area takes the bar at rest from the theme, whatever style the list sets itself. + assert_eq!(ctx.global_style().spacing.scroll, crate::widgets::list_scroll_style()); let screen_rect = Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1024.0, 480.0))); let mut time = 0.0; let mut run = |app: &mut VectorcraftApp, events: Vec| { @@ -276,9 +281,8 @@ fn the_format_list_shows_its_scroll_bar_at_rest_and_opens_at_its_first_format() let away = egui::Event::PointerMoved(egui::pos2(1.0, 1.0)); 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 field = texts.iter().find(|(s, ..)| s == last.label).map(|(_, r, _)| r.center()).expect("the Format field shows the last format"); 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(); // 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; @@ -298,8 +302,13 @@ fn the_format_list_shows_its_scroll_bar_at_rest_and_opens_at_its_first_format() let rows: Vec<_> = texts.into_iter().filter(|(s, r, _)| labels.contains(&s.as_str()) && list.contains_rect(*r)).collect(); (list, rows, bars) }; + let whole = |rows: &[(String, egui::Rect, egui::Rect)], name: &str| { + rows.iter().any(|(s, r, clip)| s == name && r.top() >= clip.top() && r.bottom() <= clip.bottom()) + }; + let tops = |rows: &[(String, egui::Rect, egui::Rect)]| rows.iter().map(|(s, r, _)| (s.clone(), r.top().round())).collect::>(); let (list, rows, bars) = open(&mut run, &mut app); assert!(!rows.is_empty(), "the Format list is open"); + assert!(whole(&rows, last.label), "the chosen format shows whole: {:?}", tops(&rows)); // A list that shows every format needs no bar; one that does not shows its bar. if rows.len() < labels.len() { assert!( @@ -308,40 +317,30 @@ fn the_format_list_shows_its_scroll_bar_at_rest_and_opens_at_its_first_format() ); } - // Scrolled, closed and opened again, the list starts at its first format, not cut off at the top. + // Scrolled, closed and opened again, the list shows the rows it first opened with, not the + // offset it was scrolled to. 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), + delta: egui::vec2(0.0, 30.0), modifiers: Default::default(), phase: egui::TouchPhase::Move, source: egui::MouseWheelSource::Unknown, }; - let top_before = rows.iter().find(|(s, ..)| s == labels[0]).map(|(_, r, _)| r.top()); run(&mut app, vec![wheel]); - let mut texts = vec![]; + let mut scrolled = vec![]; for _ in 0..4 { - (texts, _) = run(&mut app, vec![]); - } - // The wheel scrolled the list: its first format moved up or out of view. - if rows.len() < labels.len() { - let top_after = texts.iter().find(|(s, r, _)| s == labels[0] && list.contains_rect(*r)).map(|(_, r, _)| r.top()); - assert!( - top_before.is_some_and(|before| top_after.is_none_or(|after| after < before - 1.0)), - "the wheel scrolls the list: {} at {top_before:?}, then {top_after:?}", - labels[0] - ); + (scrolled, _) = run(&mut app, vec![]); } + let scrolled: Vec<_> = scrolled.into_iter().filter(|(s, r, _)| labels.contains(&s.as_str()) && list.contains_rect(*r)).collect(); 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::>() - ); + let (_, reopened, _) = open(&mut run, &mut app); + if rows.len() < labels.len() { + assert_ne!(tops(&scrolled), tops(&rows), "the wheel scrolled the list"); + } + assert_eq!(tops(&reopened), tops(&rows), "the reopened list shows the rows it first opened with"); + assert!(whole(&reopened, last.label), "the chosen format shows whole: {:?}", tops(&reopened)); } #[test] diff --git a/crates/ui-egui/src/dialogs/tests_screens.rs b/crates/ui-egui/src/dialogs/tests_screens.rs index 496d73aa68..9745abe9a9 100644 --- a/crates/ui-egui/src/dialogs/tests_screens.rs +++ b/crates/ui-egui/src/dialogs/tests_screens.rs @@ -1029,8 +1029,9 @@ fn tiff_dropdown_exports_scaled_artboards_with_options_and_reopens() { // Existing vector and raster choices remain selectable. h.choose(&mut app, "TIFF", "SVG"); assert_eq!(field(&app, "formats")[0]["format"], "svg"); - h.choose(&mut app, "SVG", "PNG"); - assert_eq!(field(&app, "formats")[0]["format"], "png"); + // The list opens with SVG in its middle, so a raster format near it is in view. + h.choose(&mut app, "SVG", "GIF"); + assert_eq!(field(&app, "formats")[0]["format"], "gif"); } /// The Example line names the first file the export writes for the same settings: the item's file diff --git a/crates/ui-egui/src/widgets/dropdowns.rs b/crates/ui-egui/src/widgets/dropdowns.rs index 1bbe7ae496..42abae54b8 100644 --- a/crates/ui-egui/src/widgets/dropdowns.rs +++ b/crates/ui-egui/src/widgets/dropdowns.rs @@ -27,7 +27,7 @@ pub fn dropdown_names_named(ui: &mut Ui, id: impl std::hash::Hash, name: Option< combo_named(ui, id, name, current, width, |ui| { let mut chosen = None; for (i, o) in options.iter().enumerate() { - if ui.add(egui::Button::selectable(*o == current, *o)).clicked() { + if option_row(ui, true, *o == current, *o).clicked() { chosen = Some(i); } } @@ -75,7 +75,7 @@ pub fn dropdown_icon_names_sized( let mut chosen = None; for (i, o) in options.iter().enumerate() { let on = current == Some(i); - if ui.add(egui::Button::selectable(on, (atom(i, on), *o))).clicked() { + if option_row(ui, true, on, (atom(i, on), *o)).clicked() { chosen = Some(i); } } @@ -109,7 +109,7 @@ pub fn dropdown_with_named( combo_named(ui, id, name, tl!(current), width, |ui| { let mut chosen = None; for (i, o) in options.iter().enumerate() { - if ui.add_enabled(enabled(i), egui::Button::selectable(*o == current, tl!(o))).clicked() { + if option_row(ui, enabled(i), *o == current, tl!(o)).clicked() { chosen = Some(i); } } @@ -117,6 +117,22 @@ pub fn dropdown_with_named( }) } +/// The flag that [`combo_atoms_named`] sets while it draws a list on the pass the list opens. +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 +/// 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::(opening_list_id())).unwrap_or(false) { + ui.scroll_to_rect_animation(response.rect, Some(egui::Align::Center), egui::style::ScrollAnimation::none()); + } + response +} + /// The id [`craft_ui::ComboBox::from_id_salt`] gives its button in this `Ui`: `from_id_salt` /// stores `IdSalt::new(salt)` and hashes it once more with the scope, so the app must hash `id` /// the same way to read the button's focus and open state before it is drawn. @@ -159,16 +175,23 @@ 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). + // 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 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) { + 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(opening_list_id(), true)); } - list(ui) + let chosen = list(ui); + if opening { + ui.data_mut(|d| d.remove::(opening_list_id())); + } + chosen }; 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))