PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Layer Comps: double-clicking a comp's name renames it in place (fixes #6496)
Marco Arnulfoe9f9406f5689
diff --git a/crates/ui-egui/src/comps_ui.rs b/crates/ui-egui/src/comps_ui.rsindex 30304f6c8..6b612f2b7 100644--- a/crates/ui-egui/src/comps_ui.rs+++ b/crates/ui-egui/src/comps_ui.rs@@ -24,6 +24,42 @@ fn clear_warning_action(comp: u32) -> (&'static str, Value) {("layerComp.updateWarnings", json!({"clear": true, "comp": comp}))}+/// The comp whose name is being edited in place, kept in egui's temp memory under its row id.+#[derive(Clone)]+struct RenameComp {+ text: String,+ focused: bool,+}++/// The in-place name field of a comp. Enter, Tab or clicking elsewhere returns the new name to+/// commit; Escape cancels and keeps the old name.+fn rename_field(ui: &mut egui::Ui, id: egui::Id, rect: Rect) -> Option<String> {+ let mut r = ui.data(|d| d.get_temp::<RenameComp>(id))?;+ ui.data_mut(|d| d.remove::<RenameComp>(id));+ let te = ui.put(rect, egui::TextEdit::singleline(&mut r.text).id(id).font(egui::FontId::proportional(12.0)));+ if !r.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(r.text.chars().count()));+ state.cursor.set_char_range(Some(all));+ state.store(ui.ctx(), id);+ }+ r.focused = true;+ ui.data_mut(|d| d.insert_temp(id, r));+ return None;+ }+ let (enter, tab, esc) = ui.input(|i| (i.key_pressed(egui::Key::Enter), i.key_pressed(egui::Key::Tab), i.key_pressed(egui::Key::Escape)));+ if esc {+ None+ } else if enter || tab || te.lost_focus() || !te.has_focus() {+ Some(r.text)+ } else {+ ui.data_mut(|d| d.insert_temp(id, r));+ None+ }+}+fn run(app: &mut PhotocraftApp, id: &str, p: Value) {if let Err(e) = app.run(id, p) {app.ui.status = e;@@ -137,7 +173,19 @@ pub fn panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) {}let galley_w = (x - name_x).max(20.0);let name = ui.painter().layout(c.name.clone(), egui::FontId::proportional(12.0), t.text, galley_w);- ui.painter().galley(pos2(name_x, rect.center().y - name.size().y / 2.0), name, t.text);+ let name_rect = Rect::from_min_size(pos2(name_x, rect.center().y - name.size().y / 2.0), name.size());+ let rename_id = egui::Id::new(("comp-rename", c.id));+ if ui.data(|d| d.get_temp::<RenameComp>(rename_id)).is_some() {+ let field = Rect::from_min_max(pos2(name_x - 3.0, rect.top() + 3.0), pos2((x + 10.0).max(name_x + 60.0), rect.bottom() - 3.0));+ if let Some(text) = rename_field(ui, rename_id, field) {+ let text = text.trim();+ if !text.is_empty() && text != c.name {+ action = Some(("layerComp.rename", json!({"comp": c.id, "name": text})));+ }+ }+ } else {+ ui.painter().galley(name_rect.min, name, t.text);+ }if !c.comment.is_empty() && resp.hovered() {resp.clone().on_hover_text(&c.comment);}@@ -148,7 +196,12 @@ pub fn panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) {}}if resp.double_clicked() && !toggle_hit {- action = Some(("layerComp.apply", json!({"comp": c.id})));+ // Photoshop renames in place when the name itself is double-clicked.+ if resp.interact_pointer_pos().is_some_and(|p| name_rect.expand2(vec2(4.0, 13.0)).contains(p)) {+ ui.data_mut(|d| d.insert_temp(rename_id, RenameComp { text: c.name.clone(), focused: false }));+ } else {+ action = Some(("layerComp.apply", json!({"comp": c.id})));+ }}ui.painter().line_segment([rect.left_bottom(), rect.right_bottom()], Stroke::new(1.0, t.separator.gamma_multiply(0.5)));}@@ -213,4 +266,51 @@ mod tests {assert!(doc.comp(c1).unwrap().missing_layers(doc).is_empty());assert_eq!(doc.comp(c2).unwrap().missing_layers(doc).iter().map(|l| l.0).collect::<Vec<_>>(), vec![layer]);}++ #[test]+ fn double_clicking_a_comp_name_renames_it_in_place() {+ // #6496.+ use egui_kittest::{Harness, kittest::Queryable};+ let mut app = PhotocraftApp::new(Session::new(), crate::Services::default());+ app.run("file.new", json!({"width": 16, "height": 16})).unwrap();+ let first = app.run("layerComp.new", json!({"name": "Comp number one"})).unwrap()["comp"].as_u64().unwrap() as u32;+ let second = app.run("layerComp.new", json!({"name": "Two"})).unwrap()["comp"].as_u64().unwrap() as u32;+ // 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| panel(app, ui), app);+ h.run_steps(3);+ // The footer starts at the panel's left edge; the first comp is the second 26 px row.+ let left = h.get_by_label("Apply previous selected layer comp").rect().left();+ let (name, rest) = (pos2(left + 70.0, left + 39.0), pos2(left + 190.0, left + 39.0));+ let double_click = |h: &mut Harness<'_, PhotocraftApp>, pos: egui::Pos2| {+ h.event(egui::Event::PointerMoved(pos));+ // Wait out the double-click window of the previous attempt.+ h.run_steps(30);+ for pressed in [true, false, true, false] {+ h.event(egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE });+ h.step();+ }+ h.run_steps(3);+ };+ let type_name = |h: &mut Harness<'_, PhotocraftApp>, text: &str, key: egui::Key| {+ double_click(h, name);+ h.event(egui::Event::Text(text.into()));+ h.run_steps(1);+ h.key_press(key);+ h.run_steps(3);+ };+ let comp_name = |h: &Harness<'_, PhotocraftApp>| h.state().session.active().unwrap().doc.comp(first).unwrap().name.clone();+ let applied = |h: &Harness<'_, PhotocraftApp>| h.state().session.active().unwrap().doc.last_applied_comp;+ // Whitespace keeps the old name.+ type_name(&mut h, " ", egui::Key::Enter);+ assert_eq!(comp_name(&h), "Comp number one");+ type_name(&mut h, "Hero", egui::Key::Enter);+ assert_eq!(comp_name(&h), "Hero");+ assert_eq!(applied(&h), Some(second), "renaming must not apply the comp");+ type_name(&mut h, "Lost", egui::Key::Escape);+ assert_eq!(comp_name(&h), "Hero", "Escape cancels");+ // Double-clicking the rest of the row still applies the comp.+ double_click(&mut h, rest);+ assert_eq!(applied(&h), Some(first));+ assert_eq!(comp_name(&h), "Hero");+ }}