Skip to content

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

Merge PR #6252: Paths panel: rename a path in place, and name the Work Path when saving it

aionggafc3619b5210
diff --cc crates/ui-egui/src/vector_ui.rs
index 1b363f05e,b6aff620a..54253180e
--- a/crates/ui-egui/src/vector_ui.rs
+++ b/crates/ui-egui/src/vector_ui.rs
@@@ -1935,10 -1690,35 +1935,36 @@@ pub fn paths_footer(ctx: &egui::Context
ctx.data(|d| d.get_temp(footer_id()))
}
+ /// Photoshop's default name for a saved path: the first free "Path N", counting from the number of
+ /// saved paths.
+ fn free_path_name(doc: &Document) -> String {
+ let mut n = doc.paths.len().saturating_add(1);
+ while doc.paths.iter().any(|p| p.name == format!("Path {n}")) {
+ n = n.saturating_add(1);
+ if n == usize::MAX {
+ break;
+ }
+ }
+ format!("Path {n}")
+ }
+
+ /// A Paths panel name being typed in place: the row's key ("work" or the saved path's name) and
+ /// the text so far. It lives in egui's temp data, like the Channels panel's rename field.
+ #[derive(Clone)]
+ struct PathRename {
+ key: String,
+ text: String,
+ focused: bool,
+ }
+
+ fn rename_id(doc: &Document) -> egui::Id {
+ egui::Id::new(("path-rename", doc.id.0))
+ }
+
/// Commands offered by a Paths row's context menu. The row's path is passed explicitly so a
-/// right-click acts on that row even when another path is selected in the panel.
-fn path_context_actions(entry: &PathEntry, doc: &Document) -> Vec<(&'static str, &'static str, Value)> {
+/// right-click acts on that row even when another path is selected in the panel. `layer` is the
+/// layer a `PathRow::Layer` row belongs to (the active layer).
+fn path_context_actions(entry: &PathEntry, doc: &Document, layer: Option<photocraft_doc::LayerId>) -> Vec<(&'static str, &'static str, Value)> {
let key = match entry.kind {
PathRow::Work => "work".to_string(),
PathRow::Layer => "layer".to_string(),
@@@ -1992,13 -1765,7 +2015,14 @@@ pub fn paths_panel(app: &mut Photocraft
let has_layer_path = rows.iter().any(|r| r.kind == PathRow::Layer);
let mut action: Option<(&str, Value)> = None;
let mut stroke_dialog: Option<String> = None;
+ // A row dragged onto the trash is deleted (#6553); a drag that never saw its release must not
+ // outlive it.
+ let ctx = ui.ctx().clone();
+ if !ctx.input(|i| i.pointer.primary_down() || i.pointer.any_released()) {
+ ctx.data_mut(|d| d.remove::<PathDrag>(path_drag_key()));
+ }
+ let drag = ctx.data(|d| d.get_temp::<PathDrag>(path_drag_key()));
+ let rename_id = rename_id(&doc);
// The buttons sit in a footer at the panel's bottom, like Photoshop's.
let footer = crate::widgets::footer_height(ui) + ui.spacing().item_spacing.y;
let fill = ui.available_height() > footer + 60.0;
@@@ -2304,27 -2091,61 +2378,82 @@@ mod tests
assert!(!entries.iter().any(|(_, id, _)| *id == "path.delete"));
}
+ /// #6552: a shape layer's "<Layer> Shape Path" row offers Delete Path, and it deletes the
+ /// shape layer, as Photoshop's Delete Path does (measured: one "Delete Layer" step).
+ #[test]
+ fn shape_path_row_delete_path_deletes_the_shape_layer() {
+ let mut app = app();
+ app.run("shape.create", json!({"kind": "ellipse", "rect": [20, 20, 100, 70]})).unwrap();
+ let st = app.session.active().unwrap();
+ let (doc, shape) = (st.doc.clone(), st.active_layer);
+ let layers = doc.layers.len();
+ let row = path_rows(&doc, shape).into_iter().find(|r| r.kind == PathRow::Layer).unwrap();
+ assert!(row.name.ends_with(" Shape Path"), "{}", row.name);
+ let (_, id, params) =
+ path_context_actions(&row, &doc, shape).into_iter().find(|(label, _, _)| *label == "Delete Path").expect("Delete Path is offered");
+ app.run(id, params).unwrap();
+ let doc = &app.session.active().unwrap().doc;
+ assert_eq!(doc.layers.len(), layers - 1);
+ assert!(shape.and_then(|id| doc.layer(id)).is_none(), "the shape layer is gone");
+ app.run("edit.undo", json!({})).unwrap();
+ assert!(shape.and_then(|id| app.session.active().unwrap().doc.layer(id)).is_some(), "one undo step brings it back");
+ }
+
+ /// #6238: double-clicking a saved path's name renames it in place, and double-clicking the
+ /// Work Path saves it under the name typed (it used to save it as "Path N" straight away).
+ #[test]
+ fn double_clicking_a_path_name_renames_it_in_place() {
+ use egui_kittest::{Harness, kittest::Queryable};
+ let mut app = app();
+ let path = json!({"subpaths": [{"closed": true, "knots": [
+ {"anchor": [10, 10], "in": [10, 10], "out": [10, 10]},
+ {"anchor": [80, 10], "in": [80, 10], "out": [80, 10]},
+ {"anchor": [80, 80], "in": [80, 80], "out": [80, 80]}
+ ]}]});
+ app.run("path.set", json!({"name": "First", "path": path})).unwrap();
+ app.run("path.set", json!({"name": "work", "path": path})).unwrap();
+ // 60 fps steps, so two clicks a frame apart count as a double-click.
+ let mut h = Harness::builder().with_size(vec2(320.0, 260.0)).with_step_dt(1.0 / 60.0).build_ui_state(|ui, app| paths_panel(app, ui), app);
+ h.run_steps(3);
+ let rename = |h: &mut Harness<'_, PhotocraftApp>, row: &str, text: &str| {
+ let name = h.get_by_label(row).rect().left_center() + vec2(60.0, 0.0);
+ h.event(egui::Event::PointerMoved(name));
+ h.run_steps(30);
+ for pressed in [true, false, true, false] {
+ h.event(egui::Event::PointerButton { pos: name, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE });
+ h.step();
+ }
+ h.run_steps(3);
+ h.event(egui::Event::Text(text.into()));
+ h.run_steps(1);
+ h.key_press(egui::Key::Enter);
+ h.run_steps(3);
+ };
+ let names = |h: &Harness<'_, PhotocraftApp>| h.state().session.active().unwrap().doc.paths.iter().map(|p| p.name.clone()).collect::<Vec<_>>();
+ rename(&mut h, "First", "Outline");
+ assert_eq!(names(&h), ["Outline"]);
+ assert!(h.state().session.active().unwrap().doc.work_path.is_some(), "renaming a saved path leaves the work path alone");
+ rename(&mut h, "Work Path", "Circle");
+ assert_eq!(names(&h), ["Outline", "Circle"]);
+ assert!(h.state().session.active().unwrap().doc.work_path.is_none());
+ assert_eq!(h.state().ui.selected_path.as_deref(), Some("Circle"));
+ }
+
+ /// #6238: a saved path's context menu offers Rename Path…, as the Layers panel's does.
+ #[test]
+ fn saved_path_context_menu_offers_rename() {
+ let mut app = app();
+ app.run("path.set", json!({"name": "First", "path": {"subpaths": []}})).unwrap();
+ app.run("path.set", json!({"name": "work", "path": {"subpaths": []}})).unwrap();
+ let doc = &app.session.active().unwrap().doc;
+ let rename = |kind| {
+ let entry = path_rows(doc, None).into_iter().find(|r| r.kind == kind).unwrap();
- path_context_actions(&entry, doc).into_iter().find(|(label, _, _)| *label == "Rename Path…").map(|(_, id, p)| (id, p))
++ path_context_actions(&entry, doc, None).into_iter().find(|(label, _, _)| *label == "Rename Path…").map(|(_, id, p)| (id, p))
+ };
+ assert_eq!(rename(PathRow::Saved), Some(("path.rename", json!({"name": "First"}))));
+ assert_eq!(rename(PathRow::Work), None, "the work path is saved, not renamed");
+ }
+
/// How many shapes the path overlay paints for `app` this frame.
fn overlay_shapes(app: &PhotocraftApp, ctx: &egui::Context) -> usize {
let doc = app.session.active().unwrap().doc.clone();