Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 133 additions & 9 deletions crates/ui-egui/src/modelbuilder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,8 @@ fn palette_ui(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens, canvas: Rect) {
(Add::Output, tl!("+ Output"), tl!("A result the model keeps, under a name")),
] {
let resp = ui.add(egui::Button::new(label).sense(Sense::click_and_drag()));
drag_source(ui, &resp, &what, label);
// Outside the list, so a finger drags these at once: nothing here scrolls.
drag_source(ui, resp.drag_started(), &what, label);
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
if resp.on_hover_text(tip).clicked() {
add = Some(what);
}
Expand Down Expand Up @@ -800,7 +801,7 @@ fn palette_ui(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens, canvas: Rect) {
let top = tool.toolbox.split(" › ").nth(1).unwrap_or("Other");
push(format!("GeoLibre › {}", crate::i18n::location(top)), !q.is_empty(), tool);
}
egui::ScrollArea::vertical().id_salt("mb_palette").auto_shrink([false, false]).show(ui, |ui| {
let area = egui::ScrollArea::vertical().id_salt("mb_palette").auto_shrink([false, false]).show(ui, |ui| {
if groups.is_empty() {
ui.label(egui::RichText::new(tl!("No tools match your search.")).size(11.5).color(t.text_dim));
}
Expand All @@ -811,7 +812,8 @@ fn palette_ui(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens, canvas: Rect) {
.show(ui, |ui| {
for tool in tools {
let resp = {
let (r, resp) = ui.allocate_exact_size(vec2(ui.available_width(), 26.0), Sense::click_and_drag());
let (r, resp) =
ui.allocate_exact_size(vec2(ui.available_width(), 26.0), crate::widgets::reorder_sense(ui, true));
resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, tl!(tool.name)));
if resp.hovered() {
ui.painter().rect_filled(r, CornerRadius::same(5), t.hover);
Expand All @@ -827,7 +829,10 @@ fn palette_ui(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens, canvas: Rect) {
ui.painter().text(pos2(r.min.x + 26.0, r.center().y), Align2::LEFT_CENTER, name, regular(12.0), t.text);
resp
};
drag_source(ui, &resp, &Add::Tool(tool.id), tl!(tool.name));
// The rule of the rows that reorder (#2958), here to drag onto the canvas:
// a finger swipes the list, and drags a tool only after a long press.
let start = crate::widgets::reorder_drag(ui, &resp, true);
drag_source(ui, start, &Add::Tool(tool.id), tl!(tool.name));
let resp = resp.on_hover_text(format!("{}\n{}", tl!(tool.name), tl!(tool.summary)));
if resp.clicked() {
add = Some(Add::Tool(tool.id));
Expand All @@ -836,6 +841,7 @@ fn palette_ui(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens, canvas: Rect) {
});
}
});
crate::widgets::yield_touch_drag(ui.ctx(), area.id);
if let Some(what) = add {
// Clicked: to the right of the last node, or at the top left of the view.
let zoom = app.mb.zoom.max(0.1);
Expand All @@ -848,13 +854,14 @@ fn palette_ui(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens, canvas: Rect) {
}
}

/// Makes a palette item draggable onto the canvas: the payload set when its drag starts, and
/// its name following the pointer meanwhile.
fn drag_source(ui: &Ui, resp: &egui::Response, what: &Add, label: &str) {
if resp.drag_started() {
/// Makes a palette item draggable onto the canvas: the payload set when its drag `start`s, and
/// its name following the pointer while it is carried. The payload says so, not `resp.dragged()`:
/// after a long press egui has let go of the row's own drag.
fn drag_source(ui: &Ui, start: bool, what: &Add, label: &str) {
if start {
egui::DragAndDrop::set_payload(ui.ctx(), what.clone());
}
if resp.dragged()
if egui::DragAndDrop::payload::<Add>(ui.ctx()).is_some_and(|d| *d == *what)
&& let Some(p) = ui.ctx().pointer_latest_pos()
{
let t = Tokens::get(ui.ctx());
Expand Down Expand Up @@ -1941,6 +1948,123 @@ mod tests {
assert_eq!(m.steps.len(), 2);
}

/// ModelBuilder over the sample project, settled, with its palette on the left.
fn palette_harness() -> egui_kittest::Harness<'static, RustGisApp> {
let mut app = RustGisApp::with_sample();
app.run("ui.modelBuilder", Value::Null).unwrap();
let mut h = harness(app);
h.run_steps(6);
h
}

fn tools(h: &egui_kittest::Harness<'_, RustGisApp>) -> Vec<(String, f32, f32)> {
h.state().mb.graph.nodes.iter().map(|n| (n.tool.clone().unwrap_or_default(), n.x, n.y)).collect()
}

/// #3034: a swipe of a finger over the tool rows scrolls the palette; it drags no tool.
#[test]
fn a_swipe_over_the_palette_scrolls_and_adds_no_tool() {
use egui_kittest::kittest::Queryable;
let mut h = palette_harness();
let row = h.get_by_label("Buffer").rect();
let from = row.center();
// Onto the canvas, so a row that took the swipe would drop its tool there.
crate::panes::tests::touch_drag(&mut h, from, pos2(700.0, from.y - 3.0 * row.height()), 0);
h.run_steps(3);
assert!(tools(&h).is_empty(), "{:?}", tools(&h));
assert!(h.get_by_label("Buffer").rect().center().y < from.y - 1.0, "the swipe did not scroll the palette");
}

/// #3034: a tap of a finger on a tool row still adds the tool.
#[test]
fn a_tap_on_a_palette_tool_adds_it() {
use egui::{PointerButton, TouchDeviceId, TouchId, TouchPhase};
use egui_kittest::kittest::Queryable;
let mut h = palette_harness();
let at = h.get_by_label("Buffer").rect().center();
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_eq!(tools(&h).iter().map(|t| t.0.as_str()).collect::<Vec<_>>(), ["buffer"]);
}

/// #3034: a finger held on a tool row adds nothing until it moves; moved to the canvas and
/// let go, it drops the tool where a mouse drag drops it.
#[test]
fn a_long_press_then_a_move_drops_a_tool_on_the_canvas() {
use egui_kittest::kittest::Queryable;
let to = pos2(700.0, 400.0);
let mut mouse = palette_harness();
let from = mouse.get_by_label("Buffer").rect().center();
crate::panes::tests::drag_layer(&mut mouse, from, to);
mouse.run_steps(3);
assert_eq!(tools(&mouse).len(), 1, "the mouse drag drops the tool");

let mut held = palette_harness();
crate::panes::tests::touch_drag(&mut held, from, from, 6);
held.run_steps(3);
assert!(tools(&held).is_empty(), "a long press alone added {:?}", tools(&held));

// Held and then moved up, the tool is carried: the list stays where it was.
let mut up = palette_harness();
crate::panes::tests::touch_drag(&mut up, from, from - vec2(0.0, 80.0), 6);
up.run_steps(10);
assert!((up.get_by_label("Buffer").rect().center().y - from.y).abs() < 0.5, "the palette scrolled");

let mut secondary = palette_harness();
let button =
|pos, pressed| Event::PointerButton { pos, button: egui::PointerButton::Secondary, pressed, modifiers: Modifiers::NONE };
for event in [Event::PointerMoved(from), button(from, true), Event::PointerMoved(to), button(to, false)] {
secondary.event(event);
secondary.step();
}
secondary.run_steps(3);
assert!(tools(&secondary).is_empty(), "the secondary button added {:?}", tools(&secondary));

let mut h = palette_harness();
crate::panes::tests::touch_drag(&mut h, from, to, 6);
h.run_steps(3);
assert_eq!(tools(&h), tools(&mouse));
}

/// #3034: `+ Input` and `+ Output`, above the list, are dragged onto the canvas at once by
/// the mouse and by a finger, their name following the pointer meanwhile.
#[test]
fn the_input_and_output_buttons_drag_onto_the_canvas() {
use egui_kittest::kittest::Queryable;
let to = pos2(700.0, 400.0);
// How many times `label` is drawn: the button, and the ghost while it is carried.
let drawn = |h: &egui_kittest::Harness<'_, RustGisApp>, label: &str| {
h.output().shapes.iter().filter(|s| matches!(&s.shape, egui::Shape::Text(t) if t.galley.text() == label)).count()
};
for (label, kind) in [("+ Input", ModelNodeKind::Input), ("+ Output", ModelNodeKind::Output)] {
let kinds = |h: &egui_kittest::Harness<'_, RustGisApp>| h.state().mb.graph.nodes.iter().map(|n| n.kind).collect::<Vec<_>>();
let mut h = palette_harness();
let from = h.get_by_label(label).rect().center();
assert_eq!(drawn(&h, label), 1);
let button =
|pos, pressed| Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: Modifiers::NONE };
for event in [Event::PointerMoved(from), button(from, true), Event::PointerMoved(to)] {
h.event(event);
h.step();
}
assert_eq!(drawn(&h, label), 2, "no ghost of {label} mid-drag");
h.event(button(to, false));
h.run_steps(3);
assert_eq!(kinds(&h), [kind]);
assert_eq!(drawn(&h, label), 1, "the ghost of {label} stayed");

let mut finger = palette_harness();
crate::panes::tests::touch_drag(&mut finger, from, to, 0);
finger.run_steps(3);
assert_eq!(kinds(&finger), [kind], "a finger on {label}");
}
}

/// Delete removes the selected node with its wires, Ctrl+Z brings both back, Ctrl+D copies
/// it; a map tab clicked (or a map activated) hides ModelBuilder, its tab shows it again.
#[test]
Expand Down
6 changes: 3 additions & 3 deletions crates/ui-egui/src/panes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7915,7 +7915,7 @@ pub(crate) fn layout_menu(app: &mut RustGisApp, ui: &mut Ui, n: &str) {

#[expect(clippy::disallowed_methods, reason = "a thread the system refuses fails the test, as it should")]
#[cfg(test)]
mod tests {
pub(crate) mod tests {
use super::*;

/// A layer linked to its file says so after its name, with the file and what linking means
Expand Down Expand Up @@ -8499,7 +8499,7 @@ mod tests {
app.session.map().layer_by_name(name).map(|l| l.id).unwrap_or_default()
}

fn drag_layer(h: &mut egui_kittest::Harness<'_, RustGisApp>, from: egui::Pos2, to: egui::Pos2) {
pub(crate) fn drag_layer(h: &mut egui_kittest::Harness<'_, RustGisApp>, from: egui::Pos2, to: egui::Pos2) {
use egui::{Event, Modifiers, PointerButton};
let button = |pos, pressed| Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE };
h.event(Event::PointerMoved(from));
Expand All @@ -8516,7 +8516,7 @@ mod tests {

/// 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) {
pub(crate) 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 };
Expand Down
Loading