diff --git a/crates/ui-egui/src/mapview.rs b/crates/ui-egui/src/mapview.rs index 20de4ca687..e98373cee4 100644 --- a/crates/ui-egui/src/mapview.rs +++ b/crates/ui-egui/src/mapview.rs @@ -4944,55 +4944,71 @@ fn view_tab_row(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens, follow: bool) -> let _ = app.run("ui.modelBuilder", Value::Null); } } - let (tabs, names, cmd) = match egui::DragAndDrop::payload::(ui.ctx()) { - Some(d) if d.layout => (layout_tabs, layouts, "layout.move"), - _ => (map_tabs, names, "map.move"), - }; - tab_drop(app, ui, t, &tabs, &names, cmd); + let layout = egui::DragAndDrop::payload::(ui.ctx()).is_some_and(|d| d.layout); + let (tabs, names) = if layout { (layout_tabs, layouts) } else { (map_tabs, names) }; + tab_drop(app, ui, t, &tabs, &names, layout, false); close } -/// A map or layout tab being dragged to another place in the row (#2802): which of the two, and -/// its name, so a list that changes during the drag (an undo, a script) moves the same one. +/// A map or layout tab, or its row in the Browser (#2829), being dragged to another place among +/// its kind (#2802): which of the two, and its name, so a list that changes during the drag (an +/// undo, a script) moves the same one. struct DraggedTab { layout: bool, name: String, } -/// Starts dragging the map or layout tab `name` when `resp` starts a drag; whether that tab is -/// the one being dragged. -fn drag_tab(ui: &Ui, resp: &egui::Response, layout: bool, name: &str) -> bool { +/// Starts dragging the map or layout tab or row `name` when `resp` starts a drag; whether that one +/// is being dragged. +pub(crate) fn drag_tab(ui: &Ui, resp: &egui::Response, layout: bool, name: &str) -> bool { if resp.drag_started_by(egui::PointerButton::Primary) { egui::DragAndDrop::set_payload(ui.ctx(), DraggedTab { layout, name: name.to_string() }); } egui::DragAndDrop::payload::(ui.ctx()).is_some_and(|d| d.layout == layout && d.name == name) } -/// While a map or layout tab is dragged over the strip: a mark where it would land among the -/// others of its kind (`tabs`, named `names`), which `cmd` takes it to when let go. A map dragged -/// past the maps lands last among them, a layout dragged before the layouts first. Near the -/// strip's ends the tabs scroll, faster the closer the pointer is, as the Layers pane does. -fn tab_drop(app: &mut RustGisApp, ui: &Ui, t: &Tokens, tabs: &[Rect], names: &[String], cmd: &str) { - let Some(dragged) = egui::DragAndDrop::payload::(ui.ctx()) else { return }; +/// 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; +/// one of the other kind lands nowhere. On the strip a map dragged past the maps lands last among +/// them, a layout dragged before the layouts first; in the list, let go off its kind's rows it +/// lands nowhere. Near the strip's or list's ends they scroll, faster the closer the pointer is, +/// as the Layers pane does. +pub(crate) fn tab_drop(app: &mut RustGisApp, ui: &Ui, t: &Tokens, tabs: &[Rect], names: &[String], layout: bool, vertical: bool) { + let Some(dragged) = egui::DragAndDrop::payload::(ui.ctx()).filter(|d| d.layout == layout) else { return }; let view = ui.clip_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.x + edge - pointer.x).max(0.0) - (pointer.x - (view.max.x - edge)).max(0.0)) / edge; + 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); - ui.scroll_with_delta_animation(vec2(push * 600.0 * dt, 0.0), egui::style::ScrollAnimation::none()); + 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(); } - let (Some(from), Some(last)) = (names.iter().position(|n| *n == dragged.name), tabs.last()) else { return }; - // The tabs whose middle is left of the pointer go before it. - let slot = tabs.iter().filter(|r| r.center().x < pointer.x).count(); + let (Some(from), Some(first), Some(last)) = (names.iter().position(|n| *n == dragged.name), tabs.first(), tabs.last()) else { + return; + }; + if vertical && (pointer.y < first.min.y || pointer.y > last.max.y) { + return; + } + // The tabs or rows whose middle is before the pointer go before it. + let slot = tabs.iter().filter(|r| r.center()[a] < pointer[a]).count(); let to = if slot > from { slot - 1 } else { slot }; if to == from { return; } - let x = tabs.get(slot).map_or(last.max.x + 1.0, |r| r.min.x - 1.0); - ui.painter().line_segment([pos2(x, last.min.y + 2.0), pos2(x, last.max.y - 2.0)], Stroke::new(2.0, t.accent)); + let at = tabs.get(slot).map_or(last.max[a] + 1.0, |r| r.min[a] - 1.0); + let mark = if vertical { + [pos2(last.min.x + 2.0, at), pos2(last.max.x - 2.0, at)] + } else { + [pos2(at, last.min.y + 2.0), pos2(at, last.max.y - 2.0)] + }; + ui.painter().line_segment(mark, Stroke::new(2.0, t.accent)); if ui.input(|i| i.pointer.primary_released()) { + let cmd = if layout { "layout.move" } else { "map.move" }; let _ = app.run(cmd, json!({"name": dragged.name, "to": to})); } } diff --git a/crates/ui-egui/src/panes.rs b/crates/ui-egui/src/panes.rs index 05b87dc3d3..f78b4e4bd1 100644 --- a/crates/ui-egui/src/panes.rs +++ b/crates/ui-egui/src/panes.rs @@ -2634,18 +2634,23 @@ fn catalog(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { egui::Frame::NONE.inner_margin(egui::Margin::symmetric(10, 8)).show(ui, |ui| { egui::CollapsingHeader::new(egui::RichText::new(tl!("Maps")).font(semibold(12.5))).default_open(true).show(ui, |ui| { let maps: Vec = app.session.project.maps.iter().map(|m| m.name.clone()).collect(); + let mut rows = Vec::with_capacity(maps.len()); for (i, m) in maps.iter().enumerate() { - ui.horizontal(|ui| { + let row = ui.horizontal(|ui| { let (r, _) = ui.allocate_exact_size(vec2(16.0, 16.0), Sense::hover()); icons::paint(ui.painter(), r, "map", t.icon, t.accent); let active = i == app.session.project.active_map; - let resp = ui.selectable_label(active, m); + // Dragged to another place among the maps, as its tab is (#2829). + let resp = ui.selectable_label(active, m).interact(Sense::drag()); + crate::mapview::drag_tab(ui, &resp, false, m); if resp.clicked() { let _ = app.run("map.activate", json!({"name": m})); } resp.context_menu(|ui| map_menu(app, ui, m)); }); + rows.push(row.response.rect); } + crate::mapview::tab_drop(app, ui, t, &rows, &maps, false, true); }); let toolboxes = egui::CollapsingHeader::new(egui::RichText::new(tl!("Toolboxes")).font(semibold(12.5))).id_salt("browser_toolboxes"); @@ -2681,25 +2686,31 @@ fn catalog(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { let layouts: Vec = app.session.project.layouts.iter().map(|l| l.name.clone()).collect(); if !layouts.is_empty() { egui::CollapsingHeader::new(egui::RichText::new(tl!("Layouts")).font(semibold(12.5))).default_open(true).show(ui, |ui| { - for n in layouts { - ui.horizontal(|ui| { + let mut rows = Vec::with_capacity(layouts.len()); + for n in &layouts { + let row = ui.horizontal(|ui| { let (r, _) = ui.allocate_exact_size(vec2(16.0, 16.0), Sense::hover()); icons::paint(ui.painter(), r, "new_layout", t.icon, t.accent); - let resp = ui.selectable_label( - app.session - .project - .layouts - .get(app.session.active_layout.unwrap_or(usize::MAX)) - .is_some_and(|l| l.name == n), - &n, - ); + let resp = ui + .selectable_label( + app.session + .project + .layouts + .get(app.session.active_layout.unwrap_or(usize::MAX)) + .is_some_and(|l| l.name == *n), + n, + ) + .interact(Sense::drag()); + crate::mapview::drag_tab(ui, &resp, true, n); if resp.clicked() { let _ = app.run("layout.activate", json!({"name": n})); app.ui.tab = "Publish".into(); } - resp.context_menu(|ui| layout_menu(app, ui, &n)); + resp.context_menu(|ui| layout_menu(app, ui, n)); }); + rows.push(row.response.rect); } + crate::mapview::tab_drop(app, ui, t, &rows, &layouts, true, true); }); } let styles: Vec = app.session.project.styles.iter().map(|st| st.name.clone()).collect(); diff --git a/crates/ui-egui/tests/smoke.rs b/crates/ui-egui/tests/smoke.rs index 2c692237b0..3c759749b1 100644 --- a/crates/ui-egui/tests/smoke.rs +++ b/crates/ui-egui/tests/smoke.rs @@ -1404,6 +1404,111 @@ fn move_up_and_down_in_the_map_and_layout_menus_reorder_them() { assert_eq!(layout_names(&h), ["L1", "L2"]); } +/// The Browser's row for the map or layout `name` (the strip has a tab with the same name). +fn browser_row(h: &egui_kittest::Harness<'_, RustGisApp>, name: &str) -> egui::Rect { + let row = strip_row(h); + h.query_all_by_label(name) + .map(|n| n.rect()) + .find(|r| (r.center().y - row).abs() > 8.0) + .unwrap_or_else(|| panic!("no Browser row {name}")) +} + +/// Dragging a map or layout row in the Browser to another place among its kind changes the +/// project's order in one undo step, as its tab does; let go where it was, over the other kind or +/// with another button, it moves nothing, and a click still opens it (#2829). +#[test] +fn dragging_browser_rows_reorders_maps_or_layouts_in_one_undo_step() { + let mut h = harness(); + h.state_mut().run("ui.pane.catalog", json!(null)).unwrap(); + activate_world(&mut h); + let depth = h.state().session.undo_depth(); + // The only map: nowhere to go. + let at = browser_row(&h, "World").center(); + drag(&mut h, at, at + egui::vec2(0.0, 40.0), 0); + assert_eq!((map_names(&h), h.state().session.undo_depth()), (vec!["World".to_string()], depth)); + for n in ["M2", "M3"] { + h.state_mut().run("map.new", json!({"name": n})).unwrap(); + } + for n in ["L1", "L2"] { + h.state_mut().run("layout.new", json!({"name": n})).unwrap(); + } + activate_world(&mut h); + let depth = h.state().session.undo_depth(); + // M3 onto the upper half of World's row: first. + let (from, onto) = (browser_row(&h, "M3").center(), browser_row(&h, "World")); + drag(&mut h, from, egui::pos2(onto.center().x, onto.min.y + 3.0), 0); + assert_eq!(map_names(&h), ["M3", "World", "M2"]); + assert_eq!(h.state().session.undo_depth(), depth + 1); + assert_eq!(active_view(&h), "World", "the map in view stays in view"); + // Let go within its own row (a drag, past egui's click distance), over the layouts, or dragged + // with the secondary button: nothing. + let at = browser_row(&h, "M2").center(); + drag(&mut h, at, at + egui::vec2(0.0, 8.0), 0); + assert_eq!(active_view(&h), "World", "a drag, not a click"); + let layout = browser_row(&h, "L1").center(); + drag(&mut h, at, layout, 0); + let secondary = |pos, pressed| egui::Event::PointerButton { + pos, + button: egui::PointerButton::Secondary, + pressed, + modifiers: egui::Modifiers::NONE, + }; + let top = browser_row(&h, "M3"); + let to = egui::pos2(top.center().x, top.min.y + 3.0); + h.event(egui::Event::PointerMoved(at)); + h.event(secondary(at, true)); + h.step(); + for i in 1..=6 { + h.event(egui::Event::PointerMoved(at + (to - at) * (i as f32 / 6.0))); + h.step(); + } + h.event(secondary(to, false)); + settle(&mut h); + h.key_press(egui::Key::Escape); + settle(&mut h); + assert_eq!((map_names(&h), h.state().session.undo_depth()), (vec!["M3".to_string(), "World".into(), "M2".into()], depth + 1)); + // L1 onto the lower half of L2's row: last. + let (from, onto) = (browser_row(&h, "L1").center(), browser_row(&h, "L2")); + drag(&mut h, from, egui::pos2(onto.center().x, onto.max.y - 3.0), 0); + assert_eq!((layout_names(&h), h.state().session.undo_depth()), (vec!["L2".to_string(), "L1".into()], depth + 2)); + h.state_mut().run("edit.undo", json!(null)).unwrap(); + settle(&mut h); + assert_eq!(layout_names(&h), ["L1", "L2"]); + // A click still opens it. + let row = strip_row(&h); + h.query_all_by_label("M2").find(|n| (n.rect().center().y - row).abs() > 8.0).expect("M2 in the Browser").click(); + settle(&mut h); + assert_eq!(active_view(&h), "M2"); +} + +/// 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] +fn a_row_dragged_to_the_edge_of_a_long_browser_scrolls_it() { + let mut h = harness(); + h.set_size(egui::vec2(900.0, 600.0)); + for i in 1..=40 { + h.state_mut().run("map.new", json!({"name": format!("Row Map {i}")})).unwrap(); + } + 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 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. + drag(&mut h, from, egui::pos2(from.x, bottom - 6.0), 5); + let landed = map_names(&h).iter().position(|n| n == "World").unwrap(); + assert!(landed > in_view, "World landed at {landed}, past the {in_view} rows first in view"); +} + #[test] fn plugins_install_from_a_file_after_a_prompt() { let _turn = PLUGINS.lock().unwrap_or_else(|e| e.into_inner()); diff --git a/docs/user-guide/workspace.md b/docs/user-guide/workspace.md index 3d04248a38..06f4bab70d 100644 --- a/docs/user-guide/workspace.md +++ b/docs/user-guide/workspace.md @@ -478,9 +478,10 @@ Right-click a map or layout tab (or its entry in Browser) to **Rename**, **Dupli **Move Up**, **Move Down** or **Delete** it. Deletion asks for confirmation; a project must keep at least one map. Drag a map or layout tab to another place to change the order of the maps or the layouts (maps stay before layouts); near the ends of a strip whose tabs don't all fit, the -tabs scroll. The map or layout in view stays in view, each move is one undo step, and the order is -saved with the project. From scripts: `map.move` and `layout.move`, with the name and the new -place (`"to"`: 0 for the first). +tabs scroll. In Browser, drag a map or layout up or down its list the same way; near the +Browser's top or bottom, the list scrolls. The map or layout in view stays in view, each move is +one undo step, and the order is saved with the project. From scripts: `map.move` and +`layout.move`, with the name and the new place (`"to"`: 0 for the first). Click **Command Search** at the right of the title bar or on the Help tab, or press ++ctrl+k++ (++cmd+k++ on macOS). Search by command name, ID or ribbon location; analysis tools, workflow