PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Type: Edit › Paste, Copy and Select › All work on the text being typed (#2893) (#2960)
Valentin Klein7b88189e2d9f
diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rsindex 73b18078b..d5e55afae 100644--- a/crates/ui-egui/src/menus.rs+++ b/crates/ui-egui/src/menus.rs@@ -467,10 +467,23 @@ pub(crate) fn invoke_unguarded(app: &mut PhotocraftApp, ctx: &egui::Context, id:"select.transformSelection" if params.as_object().is_none_or(|o| o.is_empty()) => {crate::transform_tool::begin_selection(app, ctx).map(|_| json!({"transform": app.ui.transform}))}+ // While editing type, the clipboard rows and Select > All work on the text (#1355, #2893)."edit.cut" if app.ui.text_edit.is_some() => {crate::type_tool::cut_selection(app, ctx);Ok(Value::Null)}+ "edit.copy" if app.ui.text_edit.is_some() => {+ crate::type_tool::copy_selection(app, ctx);+ Ok(Value::Null)+ }+ "edit.paste" if app.ui.text_edit.is_some() => {+ crate::type_tool::request_paste(ctx);+ Ok(Value::Null)+ }+ "select.all" if app.ui.text_edit.is_some() => {+ crate::type_tool::select_all(app);+ Ok(Value::Null)+ }"edit.paste" if params.as_object().is_none_or(|o| o.is_empty()) => {// Photoshop: paste in place when the copied area is visible, else centred in the view;// images from other apps are always centred.@@ -668,7 +681,8 @@ pub fn is_enabled(app: &PhotocraftApp, id: &str) -> bool {// "Custom…" is the full Proof Setup dialog."view.proofSetup.custom" => app.session.active().is_some(),"view.rulers" | "view.show.grid" | "view.show.guides" | "view.snap" | "view.lockGuides" => true,- "edit.cut" if app.ui.text_edit.is_some() => crate::type_tool::selected_range(app).is_some(),+ "edit.cut" | "edit.copy" if app.ui.text_edit.is_some() => crate::type_tool::selected_range(app).is_some(),+ "edit.paste" | "select.all" if app.ui.text_edit.is_some() => true,// An image copied in another app can only be seen by reading the OS clipboard, which happens// on an explicit paste: with a clipboard service these stay enabled. Paste and New from// Clipboard need no document (with none open, Paste makes one); Paste in Place does.diff --git a/crates/ui-egui/src/native_menu.rs b/crates/ui-egui/src/native_menu.rsindex 91131627d..6ee60bf3f 100644--- a/crates/ui-egui/src/native_menu.rs+++ b/crates/ui-egui/src/native_menu.rs@@ -1102,6 +1102,56 @@ mod tests {}}+ /// Select > All, Edit > Copy and Edit > Paste work on the text being typed, not on pixels (#2893).+ #[test]+ fn copy_and_paste_type_from_native_menu() {+ let synced = std::rc::Rc::new(std::cell::RefCell::new(Vec::new()));+ let mut app = app(true);+ let ctx = egui::Context::default();+ let id = photocraft_doc::LayerId(app.run("type.create", json!({"text": "Hello", "size": 12, "x": 4, "y": 24})).unwrap()["layer"].as_u64().unwrap());+ crate::menus::invoke(&mut app, &ctx, "type.editText", json!({})).unwrap();+ if let Some(e) = app.ui.text_edit.as_mut() {+ (e.anchor, e.caret) = (5, 5);+ }+ let layers = app.session.active().unwrap().doc.layers.len();+ let click = |app: &mut PhotocraftApp, id: &str| {+ app.services.native_menu = Some(NativeMenu::new(Box::new(Fake { synced: synced.clone(), events: vec![Event::Click(id.into())] })));+ let mut raw = egui::RawInput::default();+ app.services.native_menu.as_mut().unwrap().raw_input(&mut raw);+ let mut out = ctx.run_ui(raw, |ui| {+ run(app, ui.ctx());+ sync(app, ui.ctx());+ });+ out.textures_delta.clear();+ out+ };+ let text = |app: &PhotocraftApp| match &app.session.active().unwrap().doc.layer(id).unwrap().content {+ photocraft_doc::LayerContent::Text(t) => t.text.clone(),+ _ => panic!("type layer changed kind"),+ };++ click(&mut app, "select.all");+ let edit = app.ui.text_edit.as_ref().unwrap();+ assert_eq!((edit.anchor, edit.caret), (0, 5), "Select > All selects the characters");+ let bar = synced.borrow().last().cloned().unwrap();+ assert!(bar.find("edit.copy").unwrap().enabled, "Copy is enabled for selected characters");+ assert!(bar.find("edit.paste").unwrap().enabled, "Paste is enabled while typing");++ let out = click(&mut app, "edit.copy");+ assert_eq!(out.platform_output.commands, [egui::OutputCommand::CopyText("Hello".into())]);+ assert!(app.session.clipboard.is_none(), "text Copy must not copy rendered pixels");++ let out = click(&mut app, "edit.paste");+ let asked = out.viewport_output.values().any(|v| v.commands.contains(&egui::ViewportCommand::RequestPaste));+ assert!(asked, "Edit > Paste asks the window for the clipboard text");+ // The text arrives as a Paste event next frame and replaces the selection.+ let raw = egui::RawInput { events: vec![egui::Event::Paste("World".into())], ..Default::default() };+ frame(&ctx, raw, |ui| crate::shortcuts::handle(&mut app, ui.ctx()));+ assert_eq!(text(&app), "World");+ assert_eq!(app.session.active().unwrap().doc.layers.len(), layers, "no pixel layer was pasted");+ assert!(!app.ui.status_error, "{}", app.ui.status);+ }+#[test]fn native_cut_without_type_edit_keeps_pixel_clipboard_behavior() {let synced = std::rc::Rc::new(std::cell::RefCell::new(Vec::new()));diff --git a/crates/ui-egui/src/type_tool.rs b/crates/ui-egui/src/type_tool.rsindex 895a00fee..0ec11eed6 100644--- a/crates/ui-egui/src/type_tool.rs+++ b/crates/ui-egui/src/type_tool.rs@@ -323,13 +323,33 @@ pub(crate) fn selected_range(app: &PhotocraftApp) -> Option<[usize; 2]> {(a < b).then_some([a, b])}+/// Copy the selected characters to the system clipboard. Returns the copied range. Shared by the+/// clipboard Copy event and Edit > Copy during a type edit.+pub(crate) fn copy_selection(app: &PhotocraftApp, ctx: &egui::Context) -> Option<[usize; 2]> {+ let [a, b] = selected_range(app)?;+ let ed = app.ui.text_edit.as_ref()?;+ let text = text_layer(&app.session.active()?.doc, LayerId(ed.layer))?;+ ctx.copy_text(text.text.chars().skip(a).take(b - a).collect());+ Some([a, b])+}++/// Edit > Paste during a type edit: ask the window for the clipboard text, which arrives as a+/// Paste event next frame and replaces the selection there (like ⌘V).+pub(crate) fn request_paste(ctx: &egui::Context) {+ ctx.send_viewport_cmd(egui::ViewportCommand::RequestPaste);+}++/// Select > All during a type edit selects every character (like ⌘A).+pub(crate) fn select_all(app: &mut PhotocraftApp) {+ let Some(n) = app.ui.text_edit.as_ref().and_then(|ed| current_text(app, LayerId(ed.layer))).map(|t| t.chars().count()) else { return };+ if let Some(e) = app.ui.text_edit.as_mut() {+ (e.anchor, e.caret) = (0, n);+ }+}+/// Shared by the clipboard Cut event and Edit > Cut during a type edit.pub(crate) fn cut_selection(app: &mut PhotocraftApp, ctx: &egui::Context) {- let Some([a, b]) = selected_range(app) else { return };- let Some(ed) = app.ui.text_edit.as_ref() else { return };- let Some(st) = app.session.active() else { return };- let Some(text) = text_layer(&st.doc, LayerId(ed.layer)) else { return };- ctx.copy_text(text.text.chars().skip(a).take(b - a).collect());+ let Some([a, b]) = copy_selection(app, ctx) else { return };if let Some(ed) = app.ui.text_edit.as_mut() {ed.anchor = a;ed.caret = b;@@ -551,9 +571,7 @@ pub fn handle_keys(app: &mut PhotocraftApp, ctx: &egui::Context) -> bool {egui::Event::Ime(egui::ImeEvent::Commit(s)) => ime_update(app, s, true),egui::Event::Ime(_) => {}egui::Event::Copy => {- if a < b {- ctx.copy_text(text.chars().skip(a).take(b - a).collect());- }+ copy_selection(app, ctx);}egui::Event::Cut => cut_selection(app, ctx),egui::Event::Key { key, pressed: true, modifiers: m, .. } => {