From b34130c6d84bd47d3dc75bcbe652da10b8b79f6a Mon Sep 17 00:00:00 2001 From: chasse20 Date: Sun, 15 Nov 2026 22:27:16 +0000 Subject: [PATCH] Auto: Preserve 2D stroke endpoints after forced completion Update the Shift-click line origin when an unavailable tool forces a 2D stroke to finish successfully. Clear pending endpoints after completion or cancellation to prevent reuse by later gestures. Add a regression test for Shift-click following forced stroke completion. --- crates/ui-egui/src/viewport.rs | 40 ++++++++++++++++++----- crates/ui-egui/tests/it/shift_lines_2d.rs | 37 +++++++++++++++++++++ 2 files changed, 68 insertions(+), 9 deletions(-) diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs index 65c82fb0..a610d07f 100644 --- a/crates/ui-egui/src/viewport.rs +++ b/crates/ui-egui/src/viewport.rs @@ -54,7 +54,18 @@ pub fn central(app: &mut TextureCraftApp, ui: &mut Ui) { crate::toolbar::paint_bar(app, ui); } - finish_unavailable_tool_gestures(app); + let stroke_end = ui.scope_id().with("paint_2d_stroke_end"); + // A cancelled stroke can leave a pending 2D endpoint; do not attach it to a later gesture. + let pending_2d_endpoint = if !app.session.stroke_active() || !tool_acts(app) { + ui.data_mut(|data| { + let endpoint = data.get_temp(stroke_end); + data.remove::(stroke_end); + endpoint + }) + } else { + None + }; + finish_unavailable_tool_gestures(app, pending_2d_endpoint); crate::id_color_pick::IdColorPick::end_when_stale(app); let tokens = Tokens::get(ui.ctx()); let aux_view = match app.session.view { @@ -1547,12 +1558,17 @@ fn tool_acts(app: &TextureCraftApp) -> bool { } /// Both views must close gestures even when the selection disables their input handlers. -fn finish_unavailable_tool_gestures(app: &mut TextureCraftApp) { +fn finish_unavailable_tool_gestures(app: &mut TextureCraftApp, pending_2d_endpoint: Option) { if !tool_acts(app) { - if app.session.stroke_active() - && let Err(error) = app.session.end_stroke() - { - app.toast(error.to_string(), true); + if app.session.stroke_active() { + match app.session.end_stroke() { + Ok(()) => { + if let Some(endpoint) = pending_2d_endpoint { + app.last_paint_uv = Some(endpoint); + } + } + Err(error) => app.toast(error.to_string(), true), + } } app.session.end_polygon_fill_box(); } @@ -1946,7 +1962,13 @@ fn paint_2d_input(app: &mut TextureCraftApp, ui: &Ui, response: &egui::Response, if response.clicked_by(PointerButton::Primary) || response.drag_stopped_by(PointerButton::Primary) { let finishing = app.session.stroke_active(); match app.session.end_stroke() { - Ok(()) if finishing => app.last_paint_uv = ui.data(|data| data.get_temp(stroke_end)), + Ok(()) if finishing => { + app.last_paint_uv = ui.data_mut(|data| { + let endpoint = data.get_temp(stroke_end); + data.remove::(stroke_end); + endpoint + }); + } Ok(()) => {} Err(error) => app.toast(error.to_string(), true), } @@ -2027,11 +2049,11 @@ mod tests { } app.session.target = StackTarget::Effect(LayerId(u64::MAX)); assert!(!tool_acts(app)); - finish_unavailable_tool_gestures(app); + finish_unavailable_tool_gestures(app, None); assert!(!app.session.stroke_active()); assert_eq!(app.session.history_labels().len(), history + 1); assert_eq!(app.session.end_polygon_fill_box(), 0, "the interrupted box is already closed"); - finish_unavailable_tool_gestures(app); + finish_unavailable_tool_gestures(app, None); assert_eq!(app.session.history_labels().len(), history + 1, "cleanup records the gesture only once"); } } diff --git a/crates/ui-egui/tests/it/shift_lines_2d.rs b/crates/ui-egui/tests/it/shift_lines_2d.rs index a6e6175d..08537f4f 100644 --- a/crates/ui-egui/tests/it/shift_lines_2d.rs +++ b/crates/ui-egui/tests/it/shift_lines_2d.rs @@ -5,6 +5,7 @@ use egui_kittest::Harness; use serde_json::json; use texturecraft_engine::core::Channel; use texturecraft_engine::geom::glam::Vec2; +use texturecraft_engine::{StackTarget, core::LayerId}; use texturecraft_ui_egui::TextureCraftApp; use texturecraft_ui_egui::state::ViewLayout; @@ -179,6 +180,42 @@ fn a_stroke_cancelled_with_escape_leaves_the_line_start_where_it_was() { assert_shift_click_draws_a_line(|harness| cancelled_drag(harness, CANCELLED_DRAG_FROM, CANCELLED_DRAG_TO)); } +#[test] +fn a_forced_2d_completion_moves_the_next_line_start() { + let mut harness = uv_view_app(); + click(&mut harness, FIRST_CLICK, false); + + harness.hover_at(CANCELLED_DRAG_FROM); + harness.run_steps(1); + harness.event(Event::PointerButton { pos: CANCELLED_DRAG_FROM, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }); + harness.run_steps(1); + harness.hover_at(CANCELLED_DRAG_TO); + harness.run_steps(2); + assert!(harness.state().session.stroke_active(), "the 2D drag is still active"); + + harness.state_mut().session.target = StackTarget::Effect(LayerId(u64::MAX)); + harness.run_steps(1); + assert!(!harness.state().session.stroke_active(), "the unavailable tool finishes the drag"); + harness.state_mut().session.target = StackTarget::Content; + harness.event(Event::PointerButton { pos: CANCELLED_DRAG_TO, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }); + harness.run_steps(2); + + let (before_line, width) = alpha(&harness); + click(&mut harness, SECOND_CLICK, true); + let (after_line, _) = alpha(&harness); + let line = added(&before_line, &after_line); + + let dab_centre = |point| { + let mut reference = uv_view_app(); + let (blank, width) = alpha(&reference); + click(&mut reference, point, false); + let (painted, _) = alpha(&reference); + centre(&added(&blank, &painted), width) + }; + let expected_middle = (dab_centre(CANCELLED_DRAG_TO) + dab_centre(SECOND_CLICK)) * 0.5; + assert!(painted_at(&line, width, expected_middle), "Shift+click starts at the forced completion endpoint"); +} + #[test] fn a_new_project_does_not_reuse_the_previous_projects_line_start() { let mut harness = uv_view_app();