PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Route menu Cut through the active type selection (#1448)
Harrison O'Connor-Hooverca3718e7b5f1
diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rsindex 3e3b22301..ee757b257 100644--- a/crates/ui-egui/src/menus.rs+++ b/crates/ui-egui/src/menus.rs@@ -416,6 +416,10 @@ 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}))}+ "edit.cut" if app.ui.text_edit.is_some() => {+ crate::type_tool::cut_selection(app, ctx);+ 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.@@ -596,6 +600,7 @@ 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(),// 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 6b6101d9e..91131627d 100644--- a/crates/ui-egui/src/native_menu.rs+++ b/crates/ui-egui/src/native_menu.rs@@ -1063,6 +1063,72 @@ mod tests {assert!(synced.borrow().last().unwrap().find("image.imageRotation.90cw").unwrap().enabled);}+ /// Edit > Cut uses the type editor's selection, like the clipboard Cut event (#1355).+ #[test]+ fn cut_selected_type_from_native_menu_matches_keyboard_cut() {+ for native_click in [true, false] {+ 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();+ let edit = app.ui.text_edit.as_ref().unwrap();+ assert_eq!((edit.anchor, edit.caret), (0, 5));+ let events = if native_click { vec![Event::Click("edit.cut".into())] } else { Vec::new() };+ app.services.native_menu = Some(NativeMenu::new(Box::new(Fake { synced: synced.clone(), events })));+ frame(&ctx, egui::RawInput::default(), |ui| sync(&mut app, ui.ctx()));+ assert!(synced.borrow().last().unwrap().find("edit.cut").unwrap().enabled, "Cut must be enabled for selected characters");++ let mut raw = egui::RawInput::default();+ app.services.native_menu.as_mut().unwrap().raw_input(&mut raw);+ if !native_click {+ raw.events.push(egui::Event::Cut);+ }+ let mut out = ctx.run_ui(raw, |ui| {+ run(&mut app, ui.ctx());+ crate::shortcuts::handle(&mut app, ui.ctx());+ sync(&mut app, ui.ctx());+ });+ out.textures_delta.clear();+ assert_eq!(out.platform_output.commands, [egui::OutputCommand::CopyText("Hello".into())]);+ let photocraft_doc::LayerContent::Text(text) = &app.session.active().unwrap().doc.layer(id).unwrap().content else {+ panic!("type layer changed kind")+ };+ assert!(text.text.is_empty());+ let edit = app.ui.text_edit.as_ref().unwrap();+ assert_eq!((edit.anchor, edit.caret), (0, 0));+ assert!(!synced.borrow().last().unwrap().find("edit.cut").unwrap().enabled, "Cut must be disabled at an empty caret");+ assert!(app.session.clipboard.is_none(), "text Cut must not copy rendered pixels");+ }+ }++ #[test]+ fn native_cut_without_type_edit_keeps_pixel_clipboard_behavior() {+ let synced = std::rc::Rc::new(std::cell::RefCell::new(Vec::new()));+ let mut app = app(false);+ app.run("file.new", json!({"width": 4, "height": 4, "background": "transparent"})).unwrap();+ app.run("edit.fill", json!({"color": "#ff0000"})).unwrap();+ assert_eq!(app.run("document.pixel", json!({"x": 1, "y": 1})).unwrap(), json!([1.0, 0.0, 0.0, 1.0]));+ let ctx = egui::Context::default();+ app.services.native_menu = Some(NativeMenu::new(Box::new(Fake { synced: synced.clone(), events: vec![Event::Click("edit.cut".into())] })));+ frame(&ctx, egui::RawInput::default(), |ui| sync(&mut app, ui.ctx()));+ assert_eq!(synced.borrow().last().unwrap().find("edit.cut").unwrap().enabled, app.session.is_enabled("edit.cut"));+ assert!(app.session.is_enabled("edit.cut"));+ let mut raw = egui::RawInput::default();+ app.services.native_menu.as_mut().unwrap().raw_input(&mut raw);+ frame(&ctx, raw, |ui| {+ run(&mut app, ui.ctx());+ sync(&mut app, ui.ctx());+ });+ assert_eq!(app.run("document.pixel", json!({"x": 1, "y": 1})).unwrap(), json!([0.0, 0.0, 0.0, 0.0]));+ let clip = app.session.clipboard.as_ref().unwrap();+ assert_eq!((clip.bounds.width(), clip.bounds.height()), (4, 4));+ let mut px = vec![[0u8; 4]; 16];+ clip.surface.read_rgba8_into(clip.bounds, &mut px);+ assert_eq!(px, vec![[255u8, 0, 0, 255]; 16]);+ assert!(app.ui.text_edit.is_none());+ }+/// With the Mac menu bar the title bar draws no menu titles: each title shows once fewer.#[test]fn the_title_bar_hides_its_menus_with_the_mac_menu_bar() {diff --git a/crates/ui-egui/src/type_tool.rs b/crates/ui-egui/src/type_tool.rsindex 38e659ada..95feebb38 100644--- a/crates/ui-egui/src/type_tool.rs+++ b/crates/ui-egui/src/type_tool.rs@@ -295,6 +295,28 @@ fn current_text(app: &PhotocraftApp, id: LayerId) -> Option<String> {Some(text_layer(&app.session.active()?.doc, id)?.text.clone())}+/// The selected characters, clamped after undo/redo may have shortened the text.+pub(crate) fn selected_range(app: &PhotocraftApp) -> Option<[usize; 2]> {+ let ed = app.ui.text_edit.as_ref()?;+ let n = text_layer(&app.session.active()?.doc, LayerId(ed.layer))?.text.chars().count();+ let (a, b) = (ed.caret.min(ed.anchor).min(n), ed.caret.max(ed.anchor).min(n));+ (a < b).then_some([a, b])+}++/// 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());+ if let Some(ed) = app.ui.text_edit.as_mut() {+ ed.anchor = a;+ ed.caret = b;+ }+ insert(app, "");+}+/// Replace the selection with `s`.fn insert(app: &mut PhotocraftApp, s: &str) {let Some(ed) = app.ui.text_edit.clone() else { return };@@ -460,14 +482,12 @@ pub fn handle_keys(app: &mut PhotocraftApp, ctx: &egui::Context) -> bool {egui::Event::Ime(egui::ImeEvent::Preedit { text, .. }) => ime_update(app, text, false),egui::Event::Ime(egui::ImeEvent::Commit(s)) => ime_update(app, s, true),egui::Event::Ime(_) => {}- egui::Event::Copy | egui::Event::Cut => {+ egui::Event::Copy => {if a < b {ctx.copy_text(text.chars().skip(a).take(b - a).collect());- if matches!(ev, egui::Event::Cut) {- insert(app, "");- }}}+ egui::Event::Cut => cut_selection(app, ctx),egui::Event::Key { key, pressed: true, modifiers: m, .. } => {use egui::Key;let key = &flow_key(*key, vertical_flow);