PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(ui): cancel unfinished polygon on lasso tool switch (#4021)
Nigel Ihza Firdausyfd4ba52f794a
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex a144477cd..c9847d863 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -1056,6 +1056,11 @@ impl PhotocraftApp {type_transform::cancel_stale(self);crate::cam_transform::cancel_stale(self);crate::lasso_ui::cancel_stale(self);+ // A polygon under construction belongs to the Polygonal Lasso tool, not the next tool.+ if self.ui.tool != Tool::PolygonLasso {+ self.ui.polygon.clear();+ self.ui.polygon_mode.clear();+ }crate::crop_ui::cancel_stale(self);let ids: Vec<DocId> = self.session.documents().iter().map(|d| d.doc.id).collect();// Where the document of view `i` is now. Views not tracked yet keep their index.diff --git a/crates/ui-egui/src/polygon_lasso_tests.rs b/crates/ui-egui/src/polygon_lasso_tests.rsindex ed46a7e50..1f4de7877 100644--- a/crates/ui-egui/src/polygon_lasso_tests.rs+++ b/crates/ui-egui/src/polygon_lasso_tests.rs@@ -82,6 +82,32 @@ fn near(r: Rect, x0: i32, y0: i32, x1: i32, y1: i32) -> bool {r.x0.abs_diff(x0) <= 1 && r.y0.abs_diff(y0) <= 1 && r.x1.abs_diff(x1) <= 1 && r.y1.abs_diff(y1) <= 1}+#[test]+fn switching_tools_discards_unfinished_polygon_without_changing_selection() {+ let mut app = polygon_app();+ app.run("select.rect", json!({"x": 200, "y": 150, "width": 50, "height": 50})).unwrap();+ let before = app.session.active().unwrap().doc.selection.as_ref().unwrap().content_bounds();+ let history = app.session.active().unwrap().history.past_len();++ ev(&mut app, "down", 20.0, 20.0, Modifiers::NONE);+ ev(&mut app, "up", 20.0, 20.0, Modifiers::NONE);+ assert_eq!(app.ui.polygon.len(), 1);+ assert_eq!(app.ui.polygon_mode, "replace");+ assert!(crate::canvas::polygon_replaces_selection(&app));++ app.ui.tool = Tool::QuickSelection;+ app.sync_views();+ assert!(app.ui.polygon.is_empty());+ assert!(app.ui.polygon_mode.is_empty());+ assert!(!crate::canvas::polygon_replaces_selection(&app));+ assert_eq!(app.session.active().unwrap().doc.selection.as_ref().unwrap().content_bounds(), before);+ assert_eq!(app.session.active().unwrap().history.past_len(), history);++ app.ui.tool = Tool::PolygonLasso;+ app.sync_views();+ assert!(app.ui.polygon.is_empty(), "returning to the tool must start a fresh outline");+}+#[test]fn double_click_closes_the_polygon_at_the_last_point() {let mut h = harness();