Skip to content
Merged
9 changes: 5 additions & 4 deletions crates/ui-egui/src/mapview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4823,12 +4823,13 @@ fn view_tabs(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) {
let area = egui::ScrollArea::horizontal().id_salt("view_tabs").max_width(room);
let tips = [tl!("Scroll the map tabs left"), tl!("Scroll the map tabs right")];
let mut close = None;
crate::ribbon::arrow_scroll_area(&mut tabs_ui, t, "view_tabs", area, tips, |ui| {
let strip_area = crate::ribbon::arrow_scroll_area(&mut tabs_ui, t, "view_tabs", area, tips, |ui| {
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing = vec2(2.0, 0.0);
close = view_tab_row(app, ui, t, follow);
});
});
crate::widgets::yield_touch_drag(ui.ctx(), strip_area);
// Right after the tabs, or at the strip's right edge once they scroll.
let x = tabs_ui.min_rect().max.x + 2.0;
let pr = Rect::from_min_size(pos2(x + 2.0, r.min.y + 6.0), vec2(NEW_TAB_W, 20.0));
Expand Down Expand Up @@ -4856,7 +4857,7 @@ fn view_tab_row(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens, follow: bool) ->
let mut map_tabs = Vec::with_capacity(names.len());
for (i, n) in names.iter().enumerate() {
let tr = next_view_tab(ui, view_tab_w(ui, n));
let resp = ui.interact(tr, egui::Id::unique(("maptab", i)), Sense::click_and_drag());
let resp = ui.interact(tr, egui::Id::unique(("maptab", i)), crate::widgets::reorder_sense(ui, true));
// Painted, so named for screen readers (and tests).
resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, n));
let dragging = drag_tab(ui, &resp, false, n);
Expand Down Expand Up @@ -4907,7 +4908,7 @@ fn view_tab_row(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens, follow: bool) ->
let mut layout_tabs = Vec::with_capacity(layouts.len());
for (i, n) in layouts.iter().enumerate() {
let tr = next_view_tab(ui, view_tab_w(ui, n));
let resp = ui.interact(tr, egui::Id::unique(("layouttab", i)), Sense::click_and_drag());
let resp = ui.interact(tr, egui::Id::unique(("layouttab", i)), crate::widgets::reorder_sense(ui, true));
resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, n));
let dragging = drag_tab(ui, &resp, true, n);
layout_tabs.push(tr);
Expand Down Expand Up @@ -5010,7 +5011,7 @@ struct DraggedTab {
/// 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) {
if crate::widgets::reorder_drag(ui, resp, true) {
egui::DragAndDrop::set_payload(ui.ctx(), DraggedTab { layout, name: name.to_string() });
}
egui::DragAndDrop::payload::<DraggedTab>(ui.ctx()).is_some_and(|d| d.layout == layout && d.name == name)
Expand Down
217 changes: 207 additions & 10 deletions crates/ui-egui/src/panes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ fn contents(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) {
});
}
ui.add_space(2.0);
egui::ScrollArea::vertical().auto_shrink([false, false]).show(ui, |ui| {
let area = egui::ScrollArea::vertical().auto_shrink([false, false]).show(ui, |ui| {
let header = ui
.horizontal(|ui| {
ui.add_space(8.0);
Expand Down Expand Up @@ -937,6 +937,7 @@ fn contents(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) {
}
}
});
crate::widgets::yield_touch_drag(ui.ctx(), area.id);
}

/// What pasting the copy adds (#491), translated: the copied group(s), or else the layer(s).
Expand Down Expand Up @@ -1337,10 +1338,10 @@ fn layer_group_row(
let filtering = !q.is_empty();
ui.push_id(("layer_group", group.id), |ui| {
let name = row_name(ui, group.name.clone(), semibold(12.5), ui.available_width() - NAME_X - 8.0);
let sense = if reorder { Sense::click_and_drag() } else { Sense::click() };
let sense = crate::widgets::reorder_sense(ui, reorder);
let (r, resp) = ui.allocate_exact_size(vec2(ui.available_width(), name.row_h), sense);
resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, &group.name));
if reorder && resp.drag_started_by(egui::PointerButton::Primary) {
if crate::widgets::reorder_drag(ui, &resp, reorder) {
egui::DragAndDrop::set_payload(ui.ctx(), DraggedLayer(group.id));
}
let dragging = egui::DragAndDrop::payload::<DraggedLayer>(ui.ctx()).is_some_and(|d| d.0 == group.id);
Expand Down Expand Up @@ -1658,12 +1659,12 @@ fn tile_row(app: &mut RustGisApp, ui: &mut Ui, i: usize, moves: [bool; 2], reord
shown = format!("{shown} {note}");
}
let name = row_name(ui, shown.clone(), regular(12.5), ui.available_width() - NAME_X - 8.0);
let sense = if reorder { Sense::click_and_drag() } else { Sense::click() };
let sense = crate::widgets::reorder_sense(ui, reorder);
let (r, resp) = ui.allocate_exact_size(vec2(ui.available_width(), name.row_h), sense);
// Named for screen readers as a layer's row is.
resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, &tile.name));
// Dragged to another place in the drawing order, as a layer's row is.
if reorder && resp.drag_started_by(egui::PointerButton::Primary) {
if crate::widgets::reorder_drag(ui, &resp, reorder) {
egui::DragAndDrop::set_payload(ui.ctx(), DraggedTile(i));
}
let dragging = egui::DragAndDrop::payload::<DraggedTile>(ui.ctx()).is_some_and(|d| d.0 == i);
Expand Down Expand Up @@ -2052,12 +2053,12 @@ fn layer_row(app: &mut RustGisApp, ui: &mut Ui, l: &Layer, scale: f64, reorder:
let font = regular(12.5);
let room = ui.available_width() - NAME_X - badge.as_ref().map_or(8.0, |(_, w)| w + 18.0);
let name = row_name(ui, shown.clone(), font, room);
let sense = if reorder { Sense::click_and_drag() } else { Sense::click() };
let sense = crate::widgets::reorder_sense(ui, reorder);
let (r, resp) = ui.allocate_exact_size(vec2(ui.available_width(), name.row_h), sense);
// The icons and the badge line up with the name's first line.
let cy = r.min.y + 13.0;
resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, &l.name));
if reorder && resp.drag_started_by(egui::PointerButton::Primary) {
if crate::widgets::reorder_drag(ui, &resp, reorder) {
egui::DragAndDrop::set_payload(ui.ctx(), DraggedLayer(l.id));
}
let dragging = egui::DragAndDrop::payload::<DraggedLayer>(ui.ctx()).is_some_and(|d| d.0 == l.id);
Expand Down Expand Up @@ -2648,7 +2649,7 @@ fn fill_dragged_row(ui: &Ui, fill: egui::layers::ShapeIdx, dragging: bool, row:

/// Draws the Browser; toolbox identities use source names so translations can share labels.
fn catalog(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) {
egui::ScrollArea::vertical().auto_shrink([false, false]).show(ui, |ui| {
let area = egui::ScrollArea::vertical().auto_shrink([false, false]).show(ui, |ui| {
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<String> = app.session.project.maps.iter().map(|m| m.name.clone()).collect();
Expand All @@ -2663,7 +2664,7 @@ fn catalog(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) {
icons::paint(ui.painter(), r, "map", t.icon, t.accent);
let active = i == app.session.project.active_map;
// Dragged to another place among the maps, as its tab is (#2829).
let resp = ui.selectable_label(active, m).interact(Sense::drag());
let resp = ui.selectable_label(active, m).interact(crate::widgets::reorder_sense(ui, true));
let dragging = crate::mapview::drag_tab(ui, &resp, false, m);
if resp.clicked() {
let _ = app.run("map.activate", json!({"name": m}));
Expand Down Expand Up @@ -2726,7 +2727,7 @@ fn catalog(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) {
.is_some_and(|l| l.name == *n),
n,
)
.interact(Sense::drag());
.interact(crate::widgets::reorder_sense(ui, true));
let dragging = crate::mapview::drag_tab(ui, &resp, true, n);
if resp.clicked() {
let _ = app.run("layout.activate", json!({"name": n}));
Expand Down Expand Up @@ -2863,6 +2864,7 @@ fn catalog(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) {
}
});
});
crate::widgets::yield_touch_drag(ui.ctx(), area.id);
}

fn folder_browser(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) {
Expand Down Expand Up @@ -8512,6 +8514,201 @@ mod tests {
h.step();
}

/// A finger on `from` that moves through `to` in steps, as winit reports a touch (touch event
/// and pointer events together), `held` frames before it starts moving and let go at the end.
fn touch_drag(h: &mut egui_kittest::Harness<'_, RustGisApp>, from: egui::Pos2, to: egui::Pos2, held: usize) {
use egui::{Event, Modifiers, PointerButton, TouchDeviceId, TouchId, TouchPhase};
let touch = |phase, pos| Event::Touch { device_id: TouchDeviceId(1), id: TouchId(1), phase, pos, force: None };
let button = |pos, pressed| Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE };
h.event(Event::PointerMoved(from));
h.step();
// As winit sends a first contact: the touch and its press in one frame.
h.input_mut().events.extend([touch(TouchPhase::Start, from), button(from, true)]);
h.step();
for _ in 0..held {
h.step();
}
for step in 1..=8 {
let pos = from + (to - from) * (step as f32 / 8.0);
h.event(touch(TouchPhase::Move, pos));
h.event(Event::PointerMoved(pos));
h.step();
}
h.event(touch(TouchPhase::End, to));
h.event(button(to, false));
h.event(Event::PointerGone);
h.step();
}

/// #2958: a swipe of a finger over the web tile rows scrolls the list; it does not take the row
/// under the finger to another place, as the mouse does.
#[test]
fn a_swipe_over_the_tile_rows_scrolls_and_does_not_reorder_them() {
use egui_kittest::kittest::Queryable;
let _turn = crate::fetching::turn();
let mut app = RustGisApp::with_sample();
for i in 0..30 {
app.run("map.addTiles", json!({"url": format!("https://wms.example.org/{i}/{{z}}/{{x}}/{{y}}.png"), "name": format!("Depths {i}"), "overlay": true}))
.unwrap();
}
let original: Vec<_> = app.session.map().layers.iter().map(|l| l.id).collect();
let tiles: Vec<_> = app.session.map().tiles.iter().map(|t| t.name.clone()).collect();
let mut h = egui_kittest::Harness::builder().with_size(vec2(1200.0, 700.0)).with_max_steps(64).build_ui_state(
|ui, app: &mut RustGisApp| {
let ctx = ui.ctx().clone();
app.logic(&ctx);
app.ui(ui);
},
app,
);
h.run_steps(6);
let row = h.get_by_label("Depths 29").rect();
assert!(row.max.y < 650.0, "the row is on screen: {row:?}");
let from = row.center();
touch_drag(&mut h, from, from - vec2(0.0, 3.0 * row.height()), 0);
h.run_steps(3);
let state = h.state().session.map();
assert_eq!(state.layers.iter().map(|l| l.id).collect::<Vec<_>>(), original);
assert_eq!(state.tiles.iter().map(|t| t.name.clone()).collect::<Vec<_>>(), tiles);
assert_eq!(h.state().session.undo_depth(), 30);
assert!(h.get_by_label("Depths 29").rect().center().y < from.y - 1.0, "the swipe did not scroll the list");
}

/// #2958: a finger held on a layer row and then moved takes the row to another place, as the
/// mouse does at once, and the menu the long press opened is gone.
#[test]
fn a_long_press_then_a_move_reorders_a_layer() {
use egui_kittest::kittest::Queryable;
let app = RustGisApp::with_sample();
let original: Vec<_> = app.session.map().layers.iter().map(|l| l.id).collect();
let moved = layer(&app, "Countries");
let mut h = egui_kittest::Harness::builder().with_size(vec2(1200.0, 900.0)).with_max_steps(64).build_ui_state(
|ui, app: &mut RustGisApp| {
let ctx = ui.ctx().clone();
app.logic(&ctx);
app.ui(ui);
},
app,
);
h.run_steps(6);
let from = h.get_by_label("Countries").rect().center();
let first = h.get_by_label("Cities").rect();
touch_drag(&mut h, from, pos2(first.center().x, first.min.y + first.height() * 0.125), 6);
h.run_steps(3);
let order: Vec<_> = h.state().session.map().layers.iter().map(|l| l.id).collect();
assert_eq!(order.first(), Some(&moved), "{order:?} from {original:?}");
assert_eq!(h.state().session.undo_depth(), 1);
assert!(h.query_by_label("Labeling Properties…").is_none(), "the menu of the long press is still open");
}

/// #2958: a tap of a finger on a layer row still picks the layer.
#[test]
fn a_tap_on_a_layer_still_picks_it() {
use egui_kittest::kittest::Queryable;
let app = RustGisApp::with_sample();
let picked = layer(&app, "Rivers");
let mut h = egui_kittest::Harness::builder().with_size(vec2(1200.0, 900.0)).with_max_steps(64).build_ui_state(
|ui, app: &mut RustGisApp| {
let ctx = ui.ctx().clone();
app.logic(&ctx);
app.ui(ui);
},
app,
);
h.run_steps(6);
assert_ne!(h.state().session.active_layer, Some(picked));
let at = h.get_by_label("Rivers").rect().center();
// Down and up at once: no long press, whose menu would pick the row itself.
use egui::{Event, Modifiers, PointerButton, TouchDeviceId, TouchId, TouchPhase};
let touch = |phase| Event::Touch { device_id: TouchDeviceId(1), id: TouchId(1), phase, pos: at, force: None };
let button = |pressed| Event::PointerButton { pos: at, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE };
h.input_mut().events.extend([Event::PointerMoved(at), touch(TouchPhase::Start), button(true)]);
h.step();
h.input_mut().events.extend([touch(TouchPhase::End), button(false), Event::PointerGone]);
h.step();
h.run_steps(3);
assert!(h.query_by_label("Labeling Properties…").is_none(), "a tap opened the menu");
assert_eq!(h.state().session.active_layer, Some(picked));
}

/// #2958: a finger held on a row of a long list and then moved drags the row; the list under
/// it stays where it was.
#[test]
fn a_long_press_then_a_move_does_not_scroll_the_list() {
use egui_kittest::kittest::Queryable;
let _turn = crate::fetching::turn();
let mut app = RustGisApp::with_sample();
for i in 0..30 {
app.run("map.addTiles", json!({"url": format!("https://wms.example.org/{i}/{{z}}/{{x}}/{{y}}.png"), "name": format!("Depths {i}"), "overlay": true}))
.unwrap();
}
let mut h = egui_kittest::Harness::builder().with_size(vec2(1200.0, 700.0)).with_max_steps(64).build_ui_state(
|ui, app: &mut RustGisApp| {
let ctx = ui.ctx().clone();
app.logic(&ctx);
app.ui(ui);
},
app,
);
h.run_steps(6);
let from = h.get_by_label("Depths 25").rect().center();
// A row the drag does not pass, so only a scroll would move it.
let still = h.get_by_label("Depths 15").rect().center();
touch_drag(&mut h, from, from - vec2(0.0, 80.0), 6);
h.run_steps(10);
let now = h.get_by_label("Depths 15").rect().center();
assert!((now.y - still.y).abs() < 0.5, "the list scrolled: {still:?} to {now:?}");
}

/// #2958: a swipe over the view tabs scrolls the strip and leaves the maps in their order.
#[test]
fn a_swipe_over_the_view_tabs_scrolls_and_does_not_reorder_them() {
use egui_kittest::kittest::Queryable;
let mut app = RustGisApp::with_sample();
for i in 0..20 {
app.run("map.new", json!({"name": format!("Sheet {i}")})).unwrap();
}
app.run("map.activate", json!({"name": "Sheet 0"})).unwrap();
let names = |app: &RustGisApp| app.session.project.maps.iter().map(|m| m.name.clone()).collect::<Vec<_>>();
let original = names(&app);
// The Browser, which lists the maps by the same names, closed.
app.ui.left = None;
let mut h = egui_kittest::Harness::builder().with_size(vec2(900.0, 700.0)).with_max_steps(64).build_ui_state(
|ui, app: &mut RustGisApp| {
let ctx = ui.ctx().clone();
app.logic(&ctx);
app.ui(ui);
},
app,
);
h.run_steps(8);
let from = h.get_by_label("Sheet 1").rect().center();
touch_drag(&mut h, from, from - vec2(120.0, 0.0), 0);
h.run_steps(3);
assert_eq!(names(h.state()), original);
assert!(h.get_by_label("Sheet 1").rect().center().x < from.x - 1.0, "the swipe did not scroll the tabs");
}

/// #2958: a finger held on a layer row without moving opens the row's menu, as today.
#[test]
fn a_long_press_on_a_layer_still_opens_its_menu() {
use egui_kittest::kittest::Queryable;
let app = RustGisApp::with_sample();
let mut h = egui_kittest::Harness::builder().with_size(vec2(1200.0, 900.0)).with_max_steps(64).build_ui_state(
|ui, app: &mut RustGisApp| {
let ctx = ui.ctx().clone();
app.logic(&ctx);
app.ui(ui);
},
app,
);
h.run_steps(6);
let from = h.get_by_label("Countries").rect().center();
touch_drag(&mut h, from, from, 6);
h.run_steps(2);
assert!(h.query_by_label("Labeling Properties…").is_some(), "no menu after the long press");
}

/// #1321: double-clicking a layer opens its Symbology pane even at 4 frames a second (CPU
/// rendering of a large map), where egui's default 0.3 s missed the second click.
#[test]
Expand Down
3 changes: 2 additions & 1 deletion crates/ui-egui/src/ribbon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ pub(crate) fn arrow_scroll_area<R>(
area: egui::ScrollArea,
tips: [&str; 2],
add: impl FnOnce(&mut Ui) -> R,
) {
) -> egui::Id {
// egui sets a scroll area's offset only as it is built, so a click moves it on the next frame.
// It is asked for once, on a frame egui keeps: a discarded one doesn't move the area, and asking
// on every frame would pin the area there against the wheel.
Expand All @@ -250,6 +250,7 @@ pub(crate) fn arrow_scroll_area<R>(
ui.data_mut(|d| d.insert_temp(key, x));
ui.ctx().request_repaint();
}
scroll.id
}

/// The collapse button at the right of the tab strip.
Expand Down
Loading
Loading