Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(pen): an unclosed path stays with its document (#5373)

vngocvan4e131ecf2d37
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 3a981c41f..152328656 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -1191,6 +1191,8 @@ impl PhotocraftApp {
// ... and to the document it was started on.
crate::canvas::polygon_cancel_stale(self);
crate::crop_ui::cancel_stale(self);
+ // A pen path under construction belongs to the document it was started on too.
+ vector_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.
let now = |i: usize| match self.view_docs.get(i) {
diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs
index 5cbda434e..5d690beba 100644
--- a/crates/ui-egui/src/state.rs
+++ b/crates/ui-egui/src/state.rs
@@ -1150,6 +1150,10 @@ pub struct UiState {
/// Pen path under construction.
#[serde(default)]
pub pen: Option<crate::vector_ui::PenPath>,
+ /// The document the pen path is drawn on (set at its first anchor): another document becoming
+ /// active drops it (`vector_ui::cancel_stale`).
+ #[serde(default)]
+ pub pen_doc: Option<photocraft_doc::DocId>,
#[serde(default)]
pub stroke_editor: Option<crate::shape_stroke_ui::StrokeEditor>,
/// Direct Selection tool: selected anchors and the drag in progress (#790).
@@ -1346,6 +1350,7 @@ impl Default for UiState {
layer_filter_name: String::new(),
layer_filter_mode: default_layer_filter_mode(),
pen: None,
+ pen_doc: None,
stroke_editor: None,
direct_selection: Default::default(),
corner_radius: None,
diff --git a/crates/ui-egui/src/vector_ui.rs b/crates/ui-egui/src/vector_ui.rs
index 8b5e914af..e89a9c7fa 100644
--- a/crates/ui-egui/src/vector_ui.rs
+++ b/crates/ui-egui/src/vector_ui.rs
@@ -223,6 +223,9 @@ pub fn closes_path_at(app: &PhotocraftApp, tool: Tool, x: f64, y: f64) -> bool {
/// lands on the nearest 45° ray from the previous one, at the pointer's projection onto it.
pub fn pen_down(app: &mut PhotocraftApp, x: f64, y: f64, shift: bool) {
let tol = 6.0 / app.point_zoom().max(0.01) as f64;
+ if app.ui.pen.is_none() {
+ app.ui.pen_doc = app.session.active().map(|st| st.doc.id);
+ }
let pen = app.ui.pen.get_or_insert_with(PenPath::default);
if let Some(first) = pen.knots.first().map(|k| k[0])
&& on_closing_anchor(pen, x, y, tol)
@@ -423,8 +426,26 @@ pub(crate) fn pen_replace(app: &mut PhotocraftApp, path: &Path) {
}
}
+/// The path in progress belongs to the document it was started on (`UiState::pen_doc`): once
+/// another document becomes active it is dropped, so it doesn't show on (or get committed to) the
+/// other document. Switching tools is handled in `PhotocraftApp::logic`, which commits the path
+/// to its own document. Like the Polygonal Lasso's ([`crate::canvas::polygon_cancel_stale`]) and
+/// the Crop tool's ([`crate::crop_ui::cancel_stale`]).
+pub fn cancel_stale(app: &mut PhotocraftApp) {
+ let active = app.session.active().map(|st| st.doc.id);
+ if app.ui.pen.is_none() {
+ app.ui.pen_doc = None;
+ } else if app.ui.pen_doc.is_some_and(|d| Some(d) != active) {
+ app.ui.pen = None;
+ app.ui.pen_doc = None;
+ }
+}
+
/// Finish the pen path: a work path (Path mode) or a new shape layer (Shape mode).
pub fn pen_commit(app: &mut PhotocraftApp, closed: bool) {
+ // The control channel is drained after `sync_views`, so a document switch in this frame has
+ // not been seen yet: never commit the path into the document it was not drawn on.
+ cancel_stale(app);
let Some(mut pen) = app.ui.pen.take() else { return };
if app.ui.direct_selection.target == Some(crate::direct_select::PathRef::Pen) {
// Its anchors are no longer the Pen's: the committed path is another path.
@@ -600,6 +621,9 @@ pub fn curvature_down(app: &mut PhotocraftApp, x: f64, y: f64) {
pen_commit(app, true);
return;
}
+ if app.ui.pen.is_none() {
+ app.ui.pen_doc = app.session.active().map(|st| st.doc.id);
+ }
let pen = app.ui.pen.get_or_insert_with(|| PenPath { curvature: true, ..Default::default() });
if hit.is_none() {
pen.knots.push([[x, y]; 3]);
@@ -2236,6 +2260,39 @@ mod tests {
let k = app.ui.pen.as_ref().unwrap().knots[2];
assert!(k.iter().all(|p| *p == [200.0, 100.0]), "{k:?}");
pen_up(&mut app);
+
+ /// #5373: an unclosed path belongs to the document it was drawn on. File › New opened a blank
+ /// document that showed the first document's path; a document switch between `sync_views` and
+ /// the commit also committed it into the wrong document. Like the Polygonal Lasso's #5015.
+ #[test]
+ fn an_unclosed_path_stays_with_its_document() {
+ let open_path = |app: &mut PhotocraftApp| {
+ app.ui.tool = Tool::Pen;
+ for (x, y) in [(20.0, 20.0), (120.0, 60.0)] {
+ pen_down(app, x, y, false);
+ pen_up(app);
+ }
+ };
+ let work = |app: &PhotocraftApp, i: usize| app.session.documents()[i].doc.work_path.clone();
+ // File › New drops the first document's path instead of carrying it over.
+ let mut app = app();
+ open_path(&mut app);
+ assert!(app.ui.pen.is_some() && app.ui.pen_doc.is_some(), "the path is claimed by its document");
+ app.run("file.new", json!({"width": 300, "height": 150})).unwrap();
+ assert!(app.ui.pen.is_none() && app.ui.pen_doc.is_none(), "the first document's path is gone");
+ assert_eq!(work(&app, 1), None, "the new document is blank");
+ // The control channel is drained after `sync_views`, so a switch in the same frame reaches
+ // the commit unseen: it must not write the path into the other document.
+ open_path(&mut app);
+ assert!(app.session.set_active(0));
+ pen_commit(&mut app, false);
+ assert!(app.ui.pen.is_none());
+ assert_eq!((work(&app, 0), work(&app, 1)), (None, None), "neither document took it");
+ // Staying on its own document, the path still commits there.
+ open_path(&mut app);
+ pen_commit(&mut app, false);
+ assert!(work(&app, 0).is_some(), "it commits to the document it was drawn on");
+ assert_eq!(work(&app, 1), None);
}
/// #4721: a second Pen path joins the Work Path it was drawn after (still selected), as a new