Skip to content

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

fix(ui): restore previous tool when committing or canceling text edit (#5527) (#5528)

safvanedgecc9967654c06
diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs
index aeefb3ecb..5cbda434e 100644
--- a/crates/ui-egui/src/state.rs
+++ b/crates/ui-egui/src/state.rs
@@ -1073,6 +1073,9 @@ pub struct UiState {
/// Inline type editing session, if any.
#[serde(default)]
pub text_edit: Option<TextEdit>,
+ /// Tool to restore when an inline type edit session commits or cancels (e.g. Move tool, #1132).
+ #[serde(skip)]
+ pub text_edit_return_tool: Option<Tool>,
/// Ctrl/Cmd Type-tool gesture, exposed to automation but never restored as a held pointer.
#[serde(default, skip_deserializing)]
pub type_transform: Option<TypeTransform>,
@@ -1304,6 +1307,7 @@ impl Default for UiState {
spring: None,
recent_files: Vec::new(),
text_edit: None,
+ text_edit_return_tool: None,
type_transform: None,
type_transform_pivot: None,
cam_transform: None,
diff --git a/crates/ui-egui/src/type_tool.rs b/crates/ui-egui/src/type_tool.rs
index 564112748..d73bb2e2c 100644
--- a/crates/ui-egui/src/type_tool.rs
+++ b/crates/ui-egui/src/type_tool.rs
@@ -121,6 +121,7 @@ pub fn edit_active(app: &mut PhotocraftApp) -> Result<(), String> {
ed.caret = n;
}
let vertical = app.session.active().and_then(|st| text_layer(&st.doc, id)).is_some_and(|t| t.orientation == photocraft_doc::text::Orientation::Vertical);
+ app.ui.text_edit_return_tool = None;
app.ui.tool = if vertical { crate::state::Tool::VerticalType } else { crate::state::Tool::Type };
app.ui.mask_target = false;
app.ui.vector_mask_target = false;
@@ -132,6 +133,7 @@ pub fn edit_active(app: &mut PhotocraftApp) -> Result<(), String> {
/// type is there.
pub fn edit_at(app: &mut PhotocraftApp, x: f64, y: f64) -> bool {
let Some(id) = hit_layer(app, x, y) else { return false };
+ let previous_tool = app.ui.tool;
commit(app);
if app.session.select_layer(id).is_err() {
return false;
@@ -144,6 +146,9 @@ pub fn edit_at(app: &mut PhotocraftApp, x: f64, y: f64) -> bool {
app.ui.text_edit =
Some(TextEdit { layer: id.0, caret: off, anchor: off, session: key, created: false, mask: false, dragging: false, resize: None, preedit: None });
let vertical = app.session.active().and_then(|st| text_layer(&st.doc, id)).is_some_and(|t| t.orientation == photocraft_doc::text::Orientation::Vertical);
+ if !previous_tool.is_type() {
+ app.ui.text_edit_return_tool = Some(previous_tool);
+ }
app.ui.tool = if vertical { crate::state::Tool::VerticalType } else { crate::state::Tool::Type };
app.ui.mask_target = false;
app.ui.vector_mask_target = false;
@@ -271,6 +276,7 @@ pub fn pointer_down(app: &mut PhotocraftApp, x: f64, y: f64, mods: egui::Modifie
let off = hit_offset(app, id, x, y);
app.ui.text_edit =
Some(TextEdit { layer: id.0, caret: off, anchor: off, session: key, created: false, mask: false, dragging: true, resize: None, preedit: None });
+ app.ui.text_edit_return_tool = None;
return true;
}
false
@@ -335,6 +341,7 @@ pub fn pointer_up(app: &mut PhotocraftApp, start: [f64; 2], end: [f64; 2]) {
// Like Photoshop: the placeholder is selected, so typing replaces it.
let n = text.chars().count();
app.ui.text_edit = Some(TextEdit { layer: id, caret: n, anchor: 0, session: key, created: true, mask, dragging: false, resize: None, preedit: None });
+ app.ui.text_edit_return_tool = None;
}
}
@@ -773,6 +780,7 @@ pub fn handle_keys(app: &mut PhotocraftApp, ctx: &egui::Context) -> bool {
let id = LayerId(ed.layer);
let Some(text) = current_text(app, id) else {
app.ui.text_edit = None;
+ app.ui.text_edit_return_tool = None;
return false;
};
// Undo/redo can shorten the text under us.
@@ -960,6 +968,11 @@ pub fn commit_with(app: &mut PhotocraftApp, mods: egui::Modifiers) {
}
crate::type_transform::reset(app);
let Some(ed) = app.ui.text_edit.take() else { return };
+ if let Some(tool) = app.ui.text_edit_return_tool.take()
+ && app.ui.tool.is_type()
+ {
+ app.ui.tool = tool;
+ }
let Some(text) = current_text(app, LayerId(ed.layer)) else { return };
if ed.mask {
// Type Mask: the text's glyphs become (or combine with) the selection and the working
@@ -2392,6 +2405,11 @@ fn type_options(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
pub fn cancel(app: &mut PhotocraftApp) {
crate::type_transform::reset(app);
let Some(ed) = app.ui.text_edit.take() else { return };
+ if let Some(tool) = app.ui.text_edit_return_tool.take()
+ && app.ui.tool.is_type()
+ {
+ app.ui.tool = tool;
+ }
undo_session(app, &ed);
}
@@ -2682,4 +2700,41 @@ mod tests {
assert_eq!(word_boundary("hello big world", 7, false), 6);
assert_eq!(word_boundary("hello big world", 15, false), 10);
}
+ #[test]
+ fn double_click_from_move_tool_restores_move_tool_on_commit_and_cancel() {
+ let (mut app, _) = variants();
+ app.ui.tool = crate::state::Tool::Move;
+ assert!(edit_at(&mut app, ON_TEXT.0, ON_TEXT.1));
+ assert_eq!(app.ui.tool, crate::state::Tool::Type);
+ assert_eq!(app.ui.text_edit_return_tool, Some(crate::state::Tool::Move));
+ commit(&mut app);
+ assert_eq!(app.ui.tool, crate::state::Tool::Move, "commit restores the Move tool");
+ assert_eq!(app.ui.text_edit_return_tool, None);
+
+ // Also test cancel
+ app.ui.tool = crate::state::Tool::Move;
+ assert!(edit_at(&mut app, ON_TEXT.0, ON_TEXT.1));
+ assert_eq!(app.ui.tool, crate::state::Tool::Type);
+ cancel(&mut app);
+ assert_eq!(app.ui.tool, crate::state::Tool::Move, "cancel restores the Move tool");
+ assert_eq!(app.ui.text_edit_return_tool, None);
+
+ // Switching tools mid-edit commits without overriding the newly chosen tool
+ app.ui.tool = crate::state::Tool::Move;
+ assert!(edit_at(&mut app, ON_TEXT.0, ON_TEXT.1));
+ assert_eq!(app.ui.tool, crate::state::Tool::Type);
+ assert_eq!(app.ui.text_edit_return_tool, Some(crate::state::Tool::Move));
+ app.ui.tool = crate::state::Tool::Brush;
+ commit(&mut app);
+ assert_eq!(app.ui.tool, crate::state::Tool::Brush, "switching tools keeps the chosen tool");
+ assert_eq!(app.ui.text_edit_return_tool, None);
+
+ // Layers-thumbnail edit stays on Type tool (matches Photoshop)
+ app.ui.tool = crate::state::Tool::Move;
+ edit_active(&mut app).unwrap();
+ assert_eq!(app.ui.tool, crate::state::Tool::Type);
+ assert_eq!(app.ui.text_edit_return_tool, None);
+ commit(&mut app);
+ assert_eq!(app.ui.tool, crate::state::Tool::Type, "Layers-thumbnail edit stays on Type tool");
+ }
}
diff --git a/crates/ui-egui/src/type_transform.rs b/crates/ui-egui/src/type_transform.rs
index b724cfe32..0c1b4837e 100644
--- a/crates/ui-egui/src/type_transform.rs
+++ b/crates/ui-egui/src/type_transform.rs
@@ -52,6 +52,7 @@ pub(crate) fn cancel_stale(app: &mut PhotocraftApp) {
cancel_drag(app);
if lost_layer {
app.ui.text_edit = None;
+ app.ui.text_edit_return_tool = None;
}
}
}