PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Paths panel: rename a path in place, and name the Work Path when saving it (part of #6238)
Marco Arnulfoa1502432e525
diff --git a/crates/ui-egui/src/i18n/cs.tsv b/crates/ui-egui/src/i18n/cs.tsvindex 6c8f21418..6bb36f9b7 100644--- a/crates/ui-egui/src/i18n/cs.tsv+++ b/crates/ui-egui/src/i18n/cs.tsv@@ -2862,6 +2862,7 @@ cameraData Lens ObjektivDuplicate Layer Comp Duplikovat kompozici vrstevDuplicate Layers Duplikovat vrstvyDuplicate Path Duplikovat cestu+ Rename Path… Přejmenovat cestu…Duplication DuplikaceDuration Doba trváníDust & Scratches Prach a škrábancediff --git a/crates/ui-egui/src/i18n/de.tsv b/crates/ui-egui/src/i18n/de.tsvindex 2bb1c57f1..25c5b8ff6 100644--- a/crates/ui-egui/src/i18n/de.tsv+++ b/crates/ui-egui/src/i18n/de.tsv@@ -2245,6 +2245,7 @@Fill Path Pfad füllenStroke Path Pfad nachziehenDelete Path Pfad löschen+ Rename Path… Pfad umbenennen…W BH HRuler Linealdiff --git a/crates/ui-egui/src/i18n/el.tsv b/crates/ui-egui/src/i18n/el.tsvindex 48d23d13b..33f2f4f02 100644--- a/crates/ui-egui/src/i18n/el.tsv+++ b/crates/ui-egui/src/i18n/el.tsv@@ -2348,6 +2348,7 @@Drag to move the active shape's path or the Work Path Σύρετε για μετακίνηση της διαδρομής του ενεργού σχήματος ή της διαδρομής εργασίαςDual Brush Διπλό πινέλοDuplicate Path Διπλασιασμός διαδρομής+ Rename Path… Μετονομασία διαδρομής…Duplicate style Διπλασιασμός στυλDust & Scratches Σκόνη και γρατζουνιέςDust & Scratches… Σκόνη και γρατζουνιές…diff --git a/crates/ui-egui/src/i18n/es.tsv b/crates/ui-egui/src/i18n/es.tsvindex a7338795a..97d051088 100644--- a/crates/ui-egui/src/i18n/es.tsv+++ b/crates/ui-egui/src/i18n/es.tsv@@ -2936,6 +2936,7 @@ curves Smooth SuavizarClipping Path Trazado de recorteNew Path Nuevo trazadoDuplicate Path Duplicar trazado+ Rename Path… Cambiar nombre del trazado…Auto-Hide Ocultar automáticamenteX XY Ydiff --git a/crates/ui-egui/src/i18n/fr.tsv b/crates/ui-egui/src/i18n/fr.tsvindex ba1de7b70..918c6dea3 100644--- a/crates/ui-egui/src/i18n/fr.tsv+++ b/crates/ui-egui/src/i18n/fr.tsv@@ -2366,6 +2366,7 @@Clipping Path Tracé de détourageNew Path Nouveau tracéDuplicate Path Dupliquer le tracé+ Rename Path… Renommer le tracé…Delete Path Supprimer le tracéAuto-Hide Masquage automatiqueW Ldiff --git a/crates/ui-egui/src/i18n/hu.tsv b/crates/ui-egui/src/i18n/hu.tsvindex e26f3ec3b..620702ac8 100644--- a/crates/ui-egui/src/i18n/hu.tsv+++ b/crates/ui-egui/src/i18n/hu.tsv@@ -3193,6 +3193,7 @@ cameraRaw Yellow SárgaDrop an image or PSD anywhere to open it. Húzz ide bárhová egy képet vagy PSD-fájlt a megnyitásához.Duplicate Layer Comp Rétegkompozíció kettőzéseDuplicate Path Útvonal kettőzése+ Rename Path… Útvonal átnevezése…Duplication KettőzésEast asian Kelet-ázsiaiEdge Extension Szélek kiterjesztésediff --git a/crates/ui-egui/src/i18n/id.tsv b/crates/ui-egui/src/i18n/id.tsvindex d13db9f0c..86e920709 100644--- a/crates/ui-egui/src/i18n/id.tsv+++ b/crates/ui-egui/src/i18n/id.tsv@@ -2357,6 +2357,7 @@Clipping Path Clipping PathNew Path Path BaruDuplicate Path Duplikat Path+ Rename Path… Ganti Nama Path…Delete Path Hapus PathAuto-Hide Sembunyi OtomatisW Wdiff --git a/crates/ui-egui/src/i18n/it.tsv b/crates/ui-egui/src/i18n/it.tsvindex 1779c1259..6e89a8465 100644--- a/crates/ui-egui/src/i18n/it.tsv+++ b/crates/ui-egui/src/i18n/it.tsv@@ -2928,6 +2928,7 @@ curves Smooth LevigaClipping Path Tracciato di ritaglioNew Path Nuovo tracciatoDuplicate Path Duplica tracciato+ Rename Path… Rinomina tracciato…Auto-Hide Nascondi automaticamenteX XY Ydiff --git a/crates/ui-egui/src/i18n/ja.tsv b/crates/ui-egui/src/i18n/ja.tsvindex dd0cca727..33e2ea9a2 100644--- a/crates/ui-egui/src/i18n/ja.tsv+++ b/crates/ui-egui/src/i18n/ja.tsv@@ -2872,6 +2872,7 @@ fontWeight Italic 斜体Duplicate Layer Comp レイヤーカンプを複製Duplicate Layers レイヤーを複製Duplicate Path パスを複製+ Rename Path… パス名を変更…Dust & Scratches ダスト&スクラッチEXIF EXIFExport As 書き出し形式を指定diff --git a/crates/ui-egui/src/i18n/ko.tsv b/crates/ui-egui/src/i18n/ko.tsvindex f5c243d1e..5eea8a298 100644--- a/crates/ui-egui/src/i18n/ko.tsv+++ b/crates/ui-egui/src/i18n/ko.tsv@@ -1390,6 +1390,7 @@Rename 이름 바꾸기Rename Group… 그룹 이름 바꾸기…Rename Layer… 레이어 이름 바꾸기…+ Rename Path… 패스 이름 바꾸기…Rename Tool Preset… 도구 사전 설정 이름 바꾸기…Rename… 이름 바꾸기…Resample 리샘플링diff --git a/crates/ui-egui/src/i18n/nl.tsv b/crates/ui-egui/src/i18n/nl.tsvindex aea44757c..161ba124f 100644--- a/crates/ui-egui/src/i18n/nl.tsv+++ b/crates/ui-egui/src/i18n/nl.tsv@@ -3347,6 +3347,7 @@ cameraRaw Color KleurDo you want to save you made to “{name}” before quitting? Wil je de wijzigingen in “{name}” opslaan voordat je afsluit?Dodge/Burn Tegenhouden/doordrukkenDuplicate Path Pad dupliceren+ Rename Path… Pad hernoemen…Dust & Scratches Stof en krassenDynamics DynamiekEdit Plane Vlak bewerkendiff --git a/crates/ui-egui/src/i18n/pl.tsv b/crates/ui-egui/src/i18n/pl.tsvindex 17feb459a..c7891648f 100644--- a/crates/ui-egui/src/i18n/pl.tsv+++ b/crates/ui-egui/src/i18n/pl.tsv@@ -2389,6 +2389,7 @@Clipping Path Ścieżka przycinającaNew Path Nowa ścieżkaDuplicate Path Duplikuj ścieżkę+ Rename Path… Zmień nazwę ścieżki…Delete Path Usuń ścieżkęAuto-Hide Ukrywanie automatyczneW Sz.diff --git a/crates/ui-egui/src/i18n/pt-br.tsv b/crates/ui-egui/src/i18n/pt-br.tsvindex 0e9559c80..b42fc3710 100644--- a/crates/ui-egui/src/i18n/pt-br.tsv+++ b/crates/ui-egui/src/i18n/pt-br.tsv@@ -2361,6 +2361,7 @@Clipping Path Demarcador de corteNew Path Novo demarcadorDuplicate Path Duplicar demarcador+ Rename Path… Renomear demarcador…Delete Path Excluir demarcadorAuto-Hide Ocultar automaticamenteW Ldiff --git a/crates/ui-egui/src/i18n/ru.tsv b/crates/ui-egui/src/i18n/ru.tsvindex 78e10a10c..b602fa9c2 100644--- a/crates/ui-egui/src/i18n/ru.tsv+++ b/crates/ui-egui/src/i18n/ru.tsv@@ -1190,6 +1190,7 @@Clipping Path Обтравочный контурNew Path Новый контурDuplicate Path Дублировать контур+ Rename Path… Переименовать контур…Delete Path Удалить контурCurrent Path Текущий контурVector Mask from Current Path Векторная маска из текущего контураdiff --git a/crates/ui-egui/src/i18n/sr.tsv b/crates/ui-egui/src/i18n/sr.tsvindex f4a8552fb..ecfc33a54 100644--- a/crates/ui-egui/src/i18n/sr.tsv+++ b/crates/ui-egui/src/i18n/sr.tsv@@ -2343,6 +2343,7 @@Clipping Path Putanja isecanjaNew Path Nova putanjaDuplicate Path Dupliraj putanju+ Rename Path… Preimenuj putanju…Delete Path Obriši putanjuAuto-Hide Automatski sakrijW Šdiff --git a/crates/ui-egui/src/i18n/tr.tsv b/crates/ui-egui/src/i18n/tr.tsvindex b11f51a80..bdd8a2f8e 100644--- a/crates/ui-egui/src/i18n/tr.tsv+++ b/crates/ui-egui/src/i18n/tr.tsv@@ -3319,6 +3319,7 @@ cameraRaw Color RenkDo you want to save you made to “{name}” before quitting? Çıkmadan önce yaptığınız işi “{name}” konumuna kaydetmek istiyor musunuz?Dodge/Burn Soldurma/YakmaDuplicate Path Yinelenen Yol+ Rename Path… Yolu Yeniden Adlandır…Dust & Scratches Toz ve ÇiziklerDynamics DinamikEdit Plane Düzlemi Düzenlediff --git a/crates/ui-egui/src/i18n/uk.tsv b/crates/ui-egui/src/i18n/uk.tsvindex e563354fd..79daa2209 100644--- a/crates/ui-egui/src/i18n/uk.tsv+++ b/crates/ui-egui/src/i18n/uk.tsv@@ -1165,6 +1165,7 @@Clipping Path Відсічний контурNew Path Новий контурDuplicate Path Дублювати контур+ Rename Path… Перейменувати контур…Delete Path Видалити контурCurrent Path Поточний контурVector Mask from Current Path Векторна маска з поточного контуруdiff --git a/crates/ui-egui/src/i18n/vi.tsv b/crates/ui-egui/src/i18n/vi.tsvindex 6907be96a..97ea1d458 100644--- a/crates/ui-egui/src/i18n/vi.tsv+++ b/crates/ui-egui/src/i18n/vi.tsv@@ -1272,6 +1272,7 @@Stroke Path Đi nét đường dẫnSave Path Lưu đường dẫnDuplicate Path Nhân bản đường dẫn+ Rename Path… Đổi tên đường dẫn…Delete Path Xóa đường dẫnDelete Vector Mask Xóa mặt nạ vectorDraw with the Pen tool (P) or make a work path from a selection. Vẽ bằng công cụ Bút (P) hoặc tạo đường dẫn làm việc từ vùng chọn.diff --git a/crates/ui-egui/src/i18n/zh-hans.tsv b/crates/ui-egui/src/i18n/zh-hans.tsvindex 3962f46d6..3b3b5098f 100644--- a/crates/ui-egui/src/i18n/zh-hans.tsv+++ b/crates/ui-egui/src/i18n/zh-hans.tsv@@ -1430,6 +1430,7 @@Rename 重命名Rename Group… 重命名组…Rename Layer… 重命名图层…+ Rename Path… 重命名路径…Rename Tool Preset… 重命名工具预设…Rename… 重命名…Resample 重新采样diff --git a/crates/ui-egui/src/i18n/zh-hant.tsv b/crates/ui-egui/src/i18n/zh-hant.tsvindex 005c992e2..5d96e2034 100644--- a/crates/ui-egui/src/i18n/zh-hant.tsv+++ b/crates/ui-egui/src/i18n/zh-hant.tsv@@ -1427,6 +1427,7 @@Rename 重新命名Rename Group… 重新命名群組…Rename Layer… 重新命名圖層…+ Rename Path… 重新命名路徑…Rename Tool Preset… 重新命名工具預設集…Rename… 重新命名…Resample 重新取樣diff --git a/crates/ui-egui/src/vector_ui.rs b/crates/ui-egui/src/vector_ui.rsindex 01bb3c133..b6aff620a 100644--- a/crates/ui-egui/src/vector_ui.rs+++ b/crates/ui-egui/src/vector_ui.rs@@ -1690,6 +1690,32 @@ pub fn paths_footer(ctx: &egui::Context) -> Option<Rect> {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)> {@@ -1704,14 +1730,7 @@ fn path_context_actions(entry: &PathEntry, doc: &Document) -> Vec<(&'static str,("Stroke Path…", "path.stroke", json!({"name": key})),];if entry.kind == PathRow::Work {- 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;- }- }- actions.push(("Save Path", "path.rename", json!({"name": "work", "to": format!("Path {n}")})));+ actions.push(("Save Path", "path.rename", json!({"name": "work", "to": free_path_name(doc)})));} else {let mut n = 1usize;while doc.paths.iter().any(|p| p.name == format!("{} copy {n}", entry.name)) {@@ -1723,6 +1742,10 @@ fn path_context_actions(entry: &PathEntry, doc: &Document) -> Vec<(&'static str,let copy_name = format!("{} copy {n}", entry.name);actions.push(("Duplicate Path", "path.set", json!({"name": copy_name, "path": photocraft_engine::vector_cmds::path_json(&entry.path)})));}+ if entry.kind == PathRow::Saved {+ // No `to`: the panel asks for the new name in place.+ actions.push(("Rename Path…", "path.rename", json!({"name": key})));+ }if entry.kind != PathRow::Layer {actions.push(("Delete Path", "path.delete", json!({"name": key})));} else if entry.name.ends_with(" Vector Mask") {@@ -1742,6 +1765,7 @@ pub fn paths_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) {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;+ 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;@@ -1777,8 +1801,38 @@ pub fn paths_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) {0.0,egui::TextFormat { font_id: egui::FontId::proportional(12.0), color: t.text, italics, ..Default::default() },);- let g = ui.painter().layout_job(job);- ui.painter().galley(pos2(r.left() + 46.0, r.center().y - g.size().y / 2.0), g, t.text);+ let renaming = ui.ctx().data(|d| d.get_temp::<PathRename>(rename_id)).filter(|e| e.key == key);+ if let Some(mut edit) = renaming {+ let field = Rect::from_min_max(pos2(r.left() + 43.0, r.center().y - 11.0), pos2(r.right() - 8.0, r.center().y + 11.0));+ let id = rename_id.with("field");+ let te = ui.put(field, egui::TextEdit::singleline(&mut edit.text).id(id).font(egui::FontId::proportional(12.0)));+ let (enter, esc) = ui.input(|i| (i.key_pressed(egui::Key::Enter), i.key_pressed(egui::Key::Escape)));+ if !edit.focused {+ te.request_focus();+ // Like Photoshop, the whole name starts selected so typing replaces it.+ if let Some(mut state) = egui::TextEdit::load_state(ui.ctx(), id) {+ let all = egui::text::CCursorRange::two(egui::text::CCursor::new(0), egui::text::CCursor::new(edit.text.chars().count()));+ state.cursor.set_char_range(Some(all));+ state.store(ui.ctx(), id);+ }+ edit.focused = true;+ ui.ctx().data_mut(|d| d.insert_temp(rename_id, edit));+ } else if esc {+ ui.ctx().data_mut(|d| d.remove::<PathRename>(rename_id));+ } else if enter || te.lost_focus() || !te.has_focus() {+ ui.ctx().data_mut(|d| d.remove::<PathRename>(rename_id));+ let to = edit.text.trim();+ // The Work Path is saved even under its proposed name; an unchanged saved name is left alone.+ if !to.is_empty() && (*kind == PathRow::Work || to != name) {+ action = Some(("path.rename", json!({"name": key, "to": to})));+ }+ } else {+ ui.ctx().data_mut(|d| d.insert_temp(rename_id, edit));+ }+ } else {+ let g = ui.painter().layout_job(job);+ ui.painter().galley(pos2(r.left() + 46.0, r.center().y - g.size().y / 2.0), g, t.text);+ }// Ctrl/Cmd-click loads only the thumbnail's path, without changing the panel target.let modifiers = ui.input(|i| i.modifiers);let load = resp.clicked() && modifiers.command && resp.interact_pointer_pos().is_some_and(|p| thumbnail.contains(p));@@ -1791,10 +1845,18 @@ pub fn paths_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) {} else if resp.clicked() {app.ui.selected_path = Some(key.clone());}- // Double-clicking the Work Path saves it (Photoshop's "Save Path").- if !load && resp.double_clicked() && *kind == PathRow::Work {- let n = doc.paths.len() + 1;- action = Some(("path.rename", json!({"name": "work", "to": format!("Path {n}")})));+ // As in Photoshop, double-clicking a saved path's name renames it, and double-clicking+ // the Work Path asks for the name to save it under (#6238).+ let proposed = match kind {+ PathRow::Work => Some(free_path_name(&doc)),+ PathRow::Saved => Some(name.clone()),+ PathRow::Layer => None,+ };+ if !load+ && resp.double_clicked()+ && let Some(text) = proposed+ {+ ui.ctx().data_mut(|d| d.insert_temp(rename_id, PathRename { key: key.clone(), text, focused: false }));}resp.context_menu(|ui| {crate::widgets::menu_scroll(ui, |ui| {@@ -1807,6 +1869,9 @@ pub fn paths_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) {if cmd == "path.stroke" {// Stroke Path… asks for the tool first, as in Photoshop (#2978).stroke_dialog = Some(params.get("name").and_then(Value::as_str).unwrap_or("work").to_string());+ } else if cmd == "path.rename" && params.get("to").is_none() {+ let edit = PathRename { key: key.clone(), text: name.clone(), focused: false };+ ui.ctx().data_mut(|d| d.insert_temp(rename_id, edit));} else {action = Some((cmd, params));}@@ -1869,11 +1934,20 @@ pub fn paths_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) {if let Some(name) = stroke_dialog {crate::canvas_tool_menu::stroke_path_dialog(app, &name);}- if let Some((cmd, p)) = action- && let Err(e) = app.run(cmd, p)- {- app.ui.status = e;- app.ui.status_error = true;+ if let Some((cmd, p)) = action {+ let renamed = if cmd == "path.rename" { p.get("to").and_then(Value::as_str).map(str::to_string) } else { None };+ match app.run(cmd, p) {+ // The renamed (or just saved) path stays the panel's target.+ Ok(_) => {+ if renamed.is_some() {+ app.ui.selected_path = renamed;+ }+ }+ Err(e) => {+ app.ui.status = e;+ app.ui.status_error = true;+ }+ }}}@@ -2017,6 +2091,61 @@ mod tests {assert!(!entries.iter().any(|(_, id, _)| *id == "path.delete"));}+ /// #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))+ };+ 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();