PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Copy: text selected in an error notice is copied, not the layer (#7807)
Brian Torres8b083b6e5b77
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex 5948bd4ed..8825a8c1d 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -2775,6 +2775,8 @@ mod transform_fx_tests;#[cfg(test)]mod layer_props_ui_tests;#[cfg(test)]+mod notice_text_tests;+#[cfg(test)]mod transform_switch_tests;#[cfg(test)]mod transform_undo_tests;diff --git a/crates/ui-egui/src/notice_text_tests.rs b/crates/ui-egui/src/notice_text_tests.rsnew file mode 100644index 000000000..2a266c8b4--- /dev/null+++ b/crates/ui-egui/src/notice_text_tests.rs@@ -0,0 +1,104 @@+//! An error notice's message can be copied (#7807): text dragged across in it is copied by ⌘C,+//! as egui-winit sends it (`Event::Copy`) and as the Mac menu's key equivalent sends it (a ⌘C+//! press), instead of Edit › Copy copying the layer; the drag never reaches the canvas. A plain+//! click on the text leaves ⌘C to the document. The whole app (eframe harness) with the app's own+//! `raw_input_hook`, which kittest doesn't run, so the clipboard keys are mapped as on the desktop.++use egui::{Modifiers, PointerButton, Pos2, vec2};+use egui_kittest::Harness;+use egui_kittest::kittest::Queryable;+use serde_json::json;++use crate::PhotocraftApp;+use crate::state::Tool;++/// The refusal the report quotes, as the notice shows it.+const MESSAGE: &str = "Free Transform: not enough memory for transforming pixels (the transformed layer \"Layer 1\" would be 37708 x 35259 px, more than the 1073741824 px limit); the document was not changed";++fn app_harness() -> Harness<'static, PhotocraftApp> {+ let mut h = Harness::builder().with_size(vec2(1280.0, 800.0)).with_max_steps(8).build_eframe(|cc| {+ PhotocraftApp::setup_context(&cc.egui_ctx, Default::default());+ let mut s = photocraft_engine::Session::new();+ s.execute("file.new", json!({"width": 2000, "height": 1500})).unwrap();+ s.execute("layer.new.layer", json!({})).unwrap();+ s.execute("edit.fill", json!({"color": "#3366cc"})).unwrap();+ let mut app = PhotocraftApp::new(s, crate::Services::default());+ app.ui.tool = Tool::Move;+ app+ });+ h.run_steps(4);+ crate::notices::error(h.state_mut(), MESSAGE.to_string());+ h.run_steps(3);+ h+}++/// The notice's message (the status bar shows it too, on one line: the notice wraps it).+fn notice_text(h: &Harness<'static, PhotocraftApp>) -> egui::Rect {+ let rects: Vec<egui::Rect> = h.get_all_by_label(MESSAGE).map(|n| n.rect()).collect();+ rects.into_iter().max_by(|a, b| a.height().total_cmp(&b.height())).unwrap()+}++/// The document's layers (bounds) and History length.+fn document(h: &Harness<'static, PhotocraftApp>) -> (Vec<Option<photocraft_geom::Rect>>, usize) {+ let st = h.state().session.active().unwrap();+ (st.doc.walk().iter().map(|(_, _, l)| l.surface().map(|s| s.content_bounds())).collect(), st.history.past_len())+}++/// One frame with `events`, through the app's `raw_input_hook` as on the desktop; the text it+/// copied.+fn frame(h: &mut Harness<'static, PhotocraftApp>, events: Vec<egui::Event>) -> Vec<String> {+ let ctx = h.ctx.clone();+ let mut raw = h.input_mut().clone();+ raw.events.extend(events);+ eframe::App::raw_input_hook(h.state_mut(), &ctx, &mut raw);+ *h.input_mut() = raw;+ h.step();+ h.output().platform_output.commands.iter().filter_map(|c| if let egui::OutputCommand::CopyText(t) = c { Some(t.clone()) } else { None }).collect()+}++fn button(h: &mut Harness<'static, PhotocraftApp>, p: Pos2, pressed: bool) {+ frame(h, vec![egui::Event::PointerButton { pos: p, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE }]);+ frame(h, vec![]);+}++fn drag(h: &mut Harness<'static, PhotocraftApp>, from: Pos2, to: Pos2) {+ frame(h, vec![egui::Event::PointerMoved(from)]);+ button(h, from, true);+ for i in 1..=10 {+ frame(h, vec![egui::Event::PointerMoved(from + (to - from) * (i as f32 / 10.0))]);+ }+ button(h, to, false);+}++fn command_c() -> egui::Event {+ egui::Event::Key { key: egui::Key::C, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::COMMAND }+}++#[test]+fn copy_takes_the_text_dragged_across_in_an_error_notice_not_the_layer() {+ for (how, copy) in [("Event::Copy", egui::Event::Copy), ("a ⌘C press", command_c())] {+ let mut h = app_harness();+ let before = document(&h);+ let r = notice_text(&h);+ drag(&mut h, r.left_top() + vec2(2.0, 6.0), r.right_bottom() - vec2(2.0, 6.0));+ assert_eq!(document(&h), before, "{how}: the drag across the notice stays off the canvas");+ let copied = frame(&mut h, vec![copy]);+ frame(&mut h, vec![]);+ assert!(copied.iter().any(|t| t.contains("1073741824 px limit")), "{how}: the selected text is copied: {copied:?}");+ assert!(h.state().session.clipboard.is_none(), "{how}: Edit › Copy didn't copy the layer");+ assert_eq!(document(&h), before, "{how}");+ }+}++#[test]+fn a_plain_click_on_a_notice_leaves_copy_to_the_document() {+ let mut h = app_harness();+ let p = notice_text(&h).center();+ frame(&mut h, vec![egui::Event::PointerMoved(p)]);+ button(&mut h, p, true);+ button(&mut h, p, false);+ let copied = frame(&mut h, vec![egui::Event::Copy]);+ frame(&mut h, vec![]);+ assert!(copied.is_empty(), "{copied:?}");+ assert!(h.state().session.clipboard.is_some(), "Edit › Copy copies the layer");+}diff --git a/crates/ui-egui/src/shortcuts.rs b/crates/ui-egui/src/shortcuts.rsindex 1ad8d6d92..a091ee82c 100644--- a/crates/ui-egui/src/shortcuts.rs+++ b/crates/ui-egui/src/shortcuts.rs@@ -223,6 +223,37 @@ fn clipboard_press(e: &egui::Event, mods: Modifiers, is_windows: bool) -> Option}}+/// Text is selected in a label (an error notice's message, say): dragged across, double- or+/// triple-clicked or shift-clicked, as egui selects it. ⌘C then copies that text, as in a text+/// field, rather than running Edit › Copy on the layer (#7807). A plain click on a label leaves an+/// empty selection behind, which must not take ⌘C from the document. Read once a frame, from the+/// frame before.+fn label_text_selected(ctx: &egui::Context) -> bool {+ let id = egui::Id::new("pc-label-text-selected");+ let any = ctx.plugin_opt::<egui::text_selection::LabelSelectionState>().is_some_and(|p| p.lock().has_selection());+ let (selects, pressed) = ctx.input(|i| {+ let p = &i.pointer;+ let selects = (p.primary_down() && p.is_decidedly_dragging())+ || p.button_double_clicked(egui::PointerButton::Primary)+ || p.button_triple_clicked(egui::PointerButton::Primary)+ || (p.primary_pressed() && i.modifiers.shift);+ (selects, p.primary_pressed())+ });+ let selected = any && (selects || (!pressed && ctx.data(|d| d.get_temp::<bool>(id)).unwrap_or(false)));+ ctx.data_mut(|d| d.insert_temp(id, selected));+ selected+}++/// ⌘C, as egui-winit's `Event::Copy` or as a key press (the web, the Mac menu's key equivalent),+/// without ⇧ (Copy Merged) or ⌥.+fn plain_copy(e: &egui::Event, mods: Modifiers) -> bool {+ match e {+ egui::Event::Copy => !mods.shift && !mods.alt,+ egui::Event::Key { key: Key::C, pressed: true, modifiers: m, .. } => m.command && !m.shift && !m.alt,+ _ => false,+ }+}+/// egui-winit turns ⌘C / ⌘X / ⌘V into `Event::Copy` / `Cut` / `Paste` and drops the key press, and/// drops ⌘V entirely when the clipboard holds no text (an image). Outside text fields, give the/// pixel commands their key presses back: Copy/Cut/Paste become ⌘C/⌘X/⌘V presses (with ⇧/⌥ as@@ -239,7 +270,11 @@ fn clipboard_press(e: &egui::Event, mods: Modifiers, is_windows: bool) -> Option////// Ctrl+Insert copies and Shift+Insert pastes: egui-winit maps them on Windows (a Shift+Insert/// release without a paste event pastes the image, like ⌘V), and we map them here on Linux.+///+/// With text selected in a label ([`label_text_selected`]) ⌘C copies the text; ⌘X and ⌘V stay+/// the document's.pub fn clipboard_keys(ctx: &egui::Context, typing: bool, raw: &mut egui::RawInput) {+ let text_selected = label_text_selected(ctx);let press = |key, modifiers| egui::Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers };if cfg!(target_os = "linux") {raw.events.retain_mut(|e| {@@ -302,6 +337,10 @@ pub fn clipboard_keys(ctx: &egui::Context, typing: bool, raw: &mut egui::RawInpuctx.data_mut(|d| d.insert_temp(command_lately, true));}}+ if text_selected && plain_copy(&e, mods) {+ out.push(egui::Event::Copy);+ continue;+ }if let Some((key, held)) = clipboard_press(&e, mods, cfg!(target_os = "windows")) {if key == Key::V {ctx.data_mut(|d| {