diff --git a/crates/ui-egui/src/mapview.rs b/crates/ui-egui/src/mapview.rs index f84f174dbb..374e84589d 100644 --- a/crates/ui-egui/src/mapview.rs +++ b/crates/ui-egui/src/mapview.rs @@ -4996,6 +4996,25 @@ pub(crate) fn drag_tab(ui: &Ui, resp: &egui::Response, layout: bool, name: &str) egui::DragAndDrop::payload::(ui.ctx()).is_some_and(|d| d.layout == layout && d.name == name) } +/// While an item is dragged over a list that scrolls (the view tabs, the Browser or the Layers +/// pane), scrolls the list `ui` is in when the `pointer` is near its edge along the axis +/// (`vertical`, else horizontal), faster the closer it is, and asks for a frame to go on (#2957). +/// `view` is the list's visible rectangle: a `pointer` outside it scrolls nothing. +pub(crate) fn edge_scroll(ui: &Ui, view: Rect, pointer: egui::Pos2, vertical: bool) { + if !view.contains(pointer) { + return; + } + let a = usize::from(vertical); + let edge = 28.0; + let push = ((view.min[a] + edge - pointer[a]).max(0.0) - (pointer[a] - (view.max[a] - edge)).max(0.0)) / edge; + if push != 0.0 { + let dt = ui.input(|i| i.stable_dt).min(0.1); + let step = push * 600.0 * dt; + ui.scroll_with_delta_animation(if vertical { vec2(0.0, step) } else { vec2(step, 0.0) }, egui::style::ScrollAnimation::none()); + ui.ctx().request_repaint(); + } +} + /// While a map or layout tab is dragged over the strip, or its row over the Browser's list /// (`vertical`): a mark where it would land among the others of its kind (`tabs`, named `names`; /// layouts with `layout`, else maps), which `map.move` or `layout.move` takes it to when let go; @@ -5009,14 +5028,7 @@ pub(crate) fn tab_drop(app: &mut RustGisApp, ui: &Ui, t: &Tokens, tabs: &[Rect], let Some(pointer) = ui.ctx().pointer_interact_pos().filter(|p| view.contains(*p)) else { return }; // Along the row (x) or the list (y). let a = usize::from(vertical); - let edge = 28.0; - let push = ((view.min[a] + edge - pointer[a]).max(0.0) - (pointer[a] - (view.max[a] - edge)).max(0.0)) / edge; - if push != 0.0 { - let dt = ui.input(|i| i.stable_dt).min(0.1); - let step = push * 600.0 * dt; - ui.scroll_with_delta_animation(if vertical { vec2(0.0, step) } else { vec2(step, 0.0) }, egui::style::ScrollAnimation::none()); - ui.ctx().request_repaint(); - } + edge_scroll(ui, view, pointer, vertical); let (Some(from), Some(first), Some(last)) = (names.iter().position(|n| *n == dragged.name), tabs.first(), tabs.last()) else { return; }; diff --git a/crates/ui-egui/src/panes.rs b/crates/ui-egui/src/panes.rs index 4ad6177166..dce678f84d 100644 --- a/crates/ui-egui/src/panes.rs +++ b/crates/ui-egui/src/panes.rs @@ -1496,13 +1496,7 @@ fn layer_drop(app: &mut RustGisApp, ui: &Ui, rows: &[DropRow], t: &Tokens) { let view = ui.clip_rect(); let Some(pointer) = ui.ctx().pointer_interact_pos().filter(|p| view.contains(*p)) else { return }; // Near the top or bottom edge the list scrolls, faster the closer the pointer is. - let edge = 28.0; - let push = ((view.min.y + edge - pointer.y).max(0.0) - (pointer.y - (view.max.y - edge)).max(0.0)) / edge; - if push != 0.0 { - let dt = ui.input(|i| i.stable_dt).min(0.1); - ui.scroll_with_delta_animation(vec2(0.0, push * 600.0 * dt), egui::style::ScrollAnimation::none()); - ui.ctx().request_repaint(); - } + crate::mapview::edge_scroll(ui, view, pointer, true); // Only the layer rows take a drop: not the map's name above them, the basemap or a table below. let (Some(first), Some(last)) = (rows.first(), rows.last()) else { return }; if pointer.y < first.rect.min.y || pointer.y > last.rect.max.y { diff --git a/crates/ui-egui/tests/smoke.rs b/crates/ui-egui/tests/smoke.rs index 6f1837dd39..03e99d6b9c 100644 --- a/crates/ui-egui/tests/smoke.rs +++ b/crates/ui-egui/tests/smoke.rs @@ -1632,6 +1632,17 @@ fn a_browser_row_being_dragged_is_filled_and_its_drop_line_spans_the_list() { assert_eq!(layout_names(&h), ["L2", "L1"]); } +/// The bottom edge of the side pane's list around `from`, where its scroll bar ends (the map is +/// right of the pane, past 250 points). +fn list_bottom(h: &egui_kittest::Harness<'_, RustGisApp>, from: egui::Pos2) -> f32 { + h.query_all_by_role(egui::accesskit::Role::ScrollBar) + .map(|n| n.rect()) + .find(|r| r.max.x < from.x + 250.0) + .expect("the pane's scroll bar") + .max + .y +} + /// Held near the Browser's bottom edge, a dragged map row scrolls the list down past the rows in /// view, and lands there when let go (#2829). #[test] @@ -1644,14 +1655,7 @@ fn a_row_dragged_to_the_edge_of_a_long_browser_scrolls_it() { h.state_mut().run("ui.pane.catalog", json!(null)).unwrap(); activate_world(&mut h); let from = browser_row(&h, "World").center(); - // The list's bottom edge, where its scroll bar ends (the map is right of the Browser). - let bottom = h - .query_all_by_role(egui::accesskit::Role::ScrollBar) - .map(|n| n.rect()) - .find(|r| r.max.x < from.x + 250.0) - .expect("the Browser's scroll bar") - .max - .y; + let bottom = list_bottom(&h, from); let in_view = map_names(&h).iter().position(|n| browser_row(&h, n).max.y > bottom).expect("more maps than fit"); // A few frames: each scrolls up to 60 points here (the harness steps a quarter second), and // held longer the list reaches its end and the pointer is past the maps, over Toolboxes. @@ -1660,6 +1664,28 @@ fn a_row_dragged_to_the_edge_of_a_long_browser_scrolls_it() { assert!(landed > in_view, "World landed at {landed}, past the {in_view} rows first in view"); } +/// Held near the Layers pane's bottom edge, a dragged layer scrolls the list down: the rows move +/// up (#2957). +#[test] +fn a_layer_dragged_to_the_edge_of_a_long_layers_pane_scrolls_it() { + use rustgis_ui_egui::rustgis_engine::data::{Dataset, GeometryType}; + let mut h = harness(); + h.set_size(egui::vec2(900.0, 600.0)); + for i in 1..=40 { + h.state_mut().session.map_mut().add_dataset(Dataset::new(format!("Row Layer {i}"), GeometryType::Point, vec![])); + } + settle(&mut h); + let row = |h: &egui_kittest::Harness<'_, RustGisApp>, name: &str| h.get_by_label(name).rect(); + let from = row(&h, "Row Layer 40").center(); + let bottom = list_bottom(&h, from); + let before = row(&h, "Row Layer 30").min.y; + // A few frames, held near the bottom: each scrolls up to 60 points here (the harness steps a + // quarter second). + drag(&mut h, from, egui::pos2(from.x, bottom - 6.0), 5); + let after = row(&h, "Row Layer 30").min.y; + assert!(after < before - 20.0, "the rows moved up from {before} to {after}"); +} + #[test] fn plugins_install_from_a_file_after_a_prompt() { let _turn = PLUGINS.lock().unwrap_or_else(|e| e.into_inner());