From bb8f7a4f53a477a38280c78a4ab90a535f420a97 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 17 Nov 2026 18:21:21 -0800 Subject: [PATCH 1/3] Part of #8432: Copy and Cut take the text selected in a panel, not the art MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Text selected in a panel's label (a Document Info value, an Info panel readout) could not be copied: Cmd+C, Cmd+X and the menu bar's Copy and Cut always ran Edit › Copy or Cut, so the clipboard got the selected art as SVG instead of the selected text. - With label text selected, Copy and Cut copy that text and leave the art. When no label copies anything (its panel is hidden), the art is copied in the next frame. Formats an earlier Copy or Cut has not written to the system clipboard yet are dropped, so they do not replace the copied text. - A floating panel's window whose label copied its text no longer copies the art after it. A Copy sent on from another window (a floating document's) copies the art, not text still selected in the main window's panels. --- crates/ui-egui/src/menus/invoke.rs | 13 +++- crates/ui-egui/src/panel_docking.rs | 9 ++- crates/ui-egui/src/shortcuts.rs | 41 ++++++++++-- crates/ui-egui/src/shortcuts/labels.rs | 92 ++++++++++++++++++++++++++ crates/ui-egui/src/sysclip.rs | 12 +++- crates/ui-egui/src/tests/selectall.rs | 86 +++++++++++++++++++++++- crates/ui-egui/src/tests/sysclip.rs | 15 +++++ 7 files changed, 255 insertions(+), 13 deletions(-) create mode 100644 crates/ui-egui/src/shortcuts/labels.rs diff --git a/crates/ui-egui/src/menus/invoke.rs b/crates/ui-egui/src/menus/invoke.rs index 845ea7102a..ee40c7793e 100644 --- a/crates/ui-egui/src/menus/invoke.rs +++ b/crates/ui-egui/src/menus/invoke.rs @@ -108,10 +108,21 @@ pub(super) fn field_event(app: &mut VectorcraftApp, id: &str) -> Option Some(("edit.copy", egui::Event::Copy)), + "edit.cut" => Some(("edit.cut", egui::Event::Cut)), + _ => None, + }; match focused_text_field(ctx).and_then(|f| Some((f, field_event(app, id)?))) { Some((field, e)) => to_field(ctx, field, e), + None if label_event.as_ref().is_some_and(|(id, _)| crate::shortcuts::leave_to_labels(ctx, id)) => { + if let Some((_, e)) = label_event { + ctx.input_mut(|i| i.events.push(e)); + } + } None if id == "select.all" && crate::panels::artboards::selects_all(app, ctx) => crate::panels::artboards::select_all(app), None => invoke(app, id, p), } diff --git a/crates/ui-egui/src/panel_docking.rs b/crates/ui-egui/src/panel_docking.rs index 0a4f01879d..585cd8876d 100644 --- a/crates/ui-egui/src/panel_docking.rs +++ b/crates/ui-egui/src/panel_docking.rs @@ -1309,8 +1309,13 @@ pub(crate) fn modal_open(app: &VectorcraftApp, ctx: &egui::Context) -> bool { /// window's shortcuts, tool keys and clipboard on its next frame ([`VectorcraftApp::raw_input_hook`]). /// craft-ui forwards nothing while a field in that window has the keyboard. A field of the main /// window loses it, as a click elsewhere in one window would take it. A floating document's OS -/// window forwards its keys here too ([`crate::doc_windows`]). -pub(crate) fn forward_keys(app: &mut VectorcraftApp, ctx: &egui::Context, events: Vec, modifiers: Option) { +/// window forwards its keys here too ([`crate::doc_windows`]). A Copy or Cut whose text a label of +/// that window copied stays there. +pub(crate) fn forward_keys(app: &mut VectorcraftApp, ctx: &egui::Context, mut events: Vec, modifiers: Option) { + if crate::shortcuts::take_copied_in_window(ctx) { + events.retain(|e| !matches!(e, egui::Event::Copy | egui::Event::Cut)); + app.drop_unwritten_formats(); + } if events.is_empty() { return; } diff --git a/crates/ui-egui/src/shortcuts.rs b/crates/ui-egui/src/shortcuts.rs index 781bec4e74..0b93ed3a84 100644 --- a/crates/ui-egui/src/shortcuts.rs +++ b/crates/ui-egui/src/shortcuts.rs @@ -12,11 +12,16 @@ use vectorcraft_tools::{Mods, SurroundingUnit, ToolKey}; use crate::VectorcraftApp; mod held_tool; +mod labels; mod paste; mod presentation; mod typing; pub(crate) use held_tool::{HeldTool, note_tool_use}; +#[cfg(test)] +pub(crate) use labels::note_copied_in_window; +pub(crate) use labels::{leave_to_labels, take_copied_in_window}; +use labels::{left_to_labels, take_command_after_labels, watch_label_copies}; use paste::unclip_chords; pub(crate) use paste::{PasteChord, paste_command}; pub(crate) use presentation::presentation_step; @@ -193,6 +198,12 @@ pub fn handle(app: &mut VectorcraftApp, ctx: &egui::Context) { return; } ctx.input(|i| crate::shortcut_editor::note_backtick_text(&i.events)); + // A Copy or Cut the last pass left to labels that copied nothing runs now, whatever this + // frame's keys do. + watch_label_copies(ctx); + if let Some(id) = take_command_after_labels(ctx) { + crate::menus::invoke(app, id, json!({})); + } note_key_surfaces(app, ctx); forget_opening_key(ctx); let focused = ctx.input(|input| input.focused) || app.forwarded_frame && ctx.viewport_id() == egui::ViewportId::ROOT; @@ -348,7 +359,7 @@ pub fn handle(app: &mut VectorcraftApp, ctx: &egui::Context) { return; } crate::ime::controls_without_editor(app, ctx); - handle_clipboard(app, ctx, textless_paste); + let to_labels = handle_clipboard(app, ctx, textless_paste); if busy { busy_tool_keys(app, ctx, view); return; @@ -368,10 +379,12 @@ pub fn handle(app: &mut VectorcraftApp, ctx: &egui::Context) { }; // An Escape that ended a ruler-origin drag or floating Tools move runs no command bound to it. let gesture_escape = |chord: &craft_keymap::Chord| escape_ended_gesture && chord.is_plain() && chord.key == craft_keymap::Key::Escape; + // A Copy or Cut left to the labels runs neither command from the keymap. + let labels_clip = |hit: &Hit| to_labels && matches!(hit.command.id.as_str(), "edit.copy" | "edit.cut"); let hits = dispatch(app, ctx, |hit| { let chord = &hit.trigger.chord; let shifted_punctuation = chord.mods.shift && chord.key.name().chars().all(|c| !c.is_alphanumeric()); - !(chord.is_plain() && types_text(chord.key) && !shifted_punctuation || row_key(chord) || gesture_escape(chord)) + !(chord.is_plain() && types_text(chord.key) && !shifted_punctuation || row_key(chord) || gesture_escape(chord) || labels_clip(hit)) }); let ran = !hits.is_empty(); for c in hits { @@ -737,8 +750,9 @@ fn nudge_and_clear(app: &mut VectorcraftApp, ctx: &egui::Context, rows_keys: boo } } -/// Route clipboard events to the system clipboard or the app's rich-format paste path. -fn handle_clipboard(app: &mut VectorcraftApp, ctx: &egui::Context, textless_paste: Option) { +/// Route clipboard events to the system clipboard or the app's rich-format paste path. → whether +/// a Copy or Cut is left to the labels, so no shortcut runs it. +fn handle_clipboard(app: &mut VectorcraftApp, ctx: &egui::Context, textless_paste: Option) -> bool { // A clipboard image (eframe's `clipboard` feature, 0.37): the host's text paste path never // carries it. With the app's rich-format `SystemClipboard`, it is only the signal that the // clipboard holds no text — the next tick invokes Paste, whose `adopt_system_clipboard` reads @@ -787,14 +801,28 @@ fn handle_clipboard(app: &mut VectorcraftApp, ctx: &egui::Context, textless_past let held = ctx.input(|i| i.modifiers); app.clipboard_in = Some(Flavour { mime: vectorcraft_engine::cmd::clipboard::PNG, data: png }); crate::menus::invoke(app, paste_command(held), json!({})); - return; + return false; } - // Clipboard keys arrive as events, not key presses; the chord held says which paste. + // Clipboard keys arrive as events, not key presses; the chord held says which paste. Copy and + // Cut with text selected in a label copy that text ([`leave_to_labels`]), unless another window + // sent them on (a floating document's): those copy the art. The menu bar's Copy and Cut have + // left theirs to the labels already. let mut clip = vec![]; + let forwarded = app.forwarded_frame && ctx.viewport_id() == egui::ViewportId::ROOT; + let to_labels = + |e: egui::Event, id| ctx.input(|i| i.events.contains(&e)) && (left_to_labels(ctx) == Some(id) || (!forwarded && leave_to_labels(ctx, id))); + let (labels_copy, labels_cut) = (to_labels(egui::Event::Copy, "edit.copy"), to_labels(egui::Event::Cut, "edit.cut")); + // The labels' text, or the art copied in the next pass when they copy nothing, replaces what an + // earlier Copy or Cut has not written yet. + if labels_copy || labels_cut { + app.drop_unwritten_formats(); + } ctx.input_mut(|i| { let held = i.modifiers; i.events.retain(|e| { let (id, text) = match e { + egui::Event::Copy if labels_copy => return true, + egui::Event::Cut if labels_cut => return true, egui::Event::Copy => ("edit.copy", None), egui::Event::Cut => ("edit.cut", None), egui::Event::Paste(t) => (paste_command(held), Some(t.clone())), @@ -814,6 +842,7 @@ fn handle_clipboard(app: &mut VectorcraftApp, ctx: &egui::Context, textless_past app.clipboard_in = text.map(|t| Flavour { mime: TEXT, data: t.into_bytes() }); crate::menus::invoke(app, id, json!({})); } + labels_copy || labels_cut } /// Route arrows, digits and letters to a tool while a gesture is active. diff --git a/crates/ui-egui/src/shortcuts/labels.rs b/crates/ui-egui/src/shortcuts/labels.rs new file mode 100644 index 0000000000..949dda133c --- /dev/null +++ b/crates/ui-egui/src/shortcuts/labels.rs @@ -0,0 +1,92 @@ +use egui::text_selection::LabelSelectionState; + +/// The Copy or Cut command left to the labels of viewport `viewport` in this pass. +fn waiting_id(viewport: egui::ViewportId) -> egui::Id { + egui::Id::unique(("copy-waits-for-labels", viewport)) +} + +/// The Copy or Cut command to run in the next pass: the labels it was left to copied nothing. +fn after_labels_id() -> egui::Id { + egui::Id::unique("copy-after-labels") +} + +/// Set when a label in a window of its own (a floating panel's) copied its selected text: the +/// window then sends the Copy or Cut on to the main window ([`crate::panel_docking::forward_keys`]). +fn copied_in_window_id() -> egui::Id { + egui::Id::unique("labels-copied-in-window") +} + +/// Watch the labels' copies from the next pass on ([`leave_to_labels`]). egui runs a plugin only +/// from the pass after the one that adds it. +pub(super) fn watch_label_copies(ctx: &egui::Context) { + if ctx.plugin_opt::().is_none() { + ctx.add_plugin(CopyAfterLabels); + } +} + +/// Copy or Cut `command` (`edit.copy`, `edit.cut`) while text is selected in a label, such as a +/// Document Info value or an Info panel readout: egui copies that text when the Copy or Cut event +/// reaches the labels, so the event is left in the input for them (→ true). When no label of this +/// viewport copies anything in this pass (the selected text is in another window, or its label is +/// not drawn), `command` runs in the next pass ([`take_command_after_labels`]). +pub(crate) fn leave_to_labels(ctx: &egui::Context, command: &'static str) -> bool { + let selected = ctx.with_plugin(|s: &mut LabelSelectionState| s.has_selection()).unwrap_or(false); + if !selected || ctx.plugin_opt::().is_none() { + return false; + } + let id = waiting_id(ctx.viewport_id()); + ctx.data_mut(|d| d.insert_temp(id, command)); + true +} + +/// The Copy or Cut command left to this viewport's labels in this pass, if any. +pub(super) fn left_to_labels(ctx: &egui::Context) -> Option<&'static str> { + let id = waiting_id(ctx.viewport_id()); + ctx.data(|d| d.get_temp::<&'static str>(id)) +} + +/// The Copy or Cut command an earlier pass left to labels that copied nothing. +pub(super) fn take_command_after_labels(ctx: &egui::Context) -> Option<&'static str> { + ctx.data_mut(|d| d.remove_temp::<&'static str>(after_labels_id())) +} + +/// Note that a floating panel's window copied its label text, as [`CopyAfterLabels`] does. +#[cfg(test)] +pub(crate) fn note_copied_in_window(ctx: &egui::Context) { + ctx.data_mut(|d| d.insert_temp(copied_in_window_id(), true)); +} + +/// Did a label in another window copy its selected text since the last call? The Copy or Cut that +/// window sends on is then its own and copies nothing more. +pub(crate) fn take_copied_in_window(ctx: &egui::Context) -> bool { + ctx.data_mut(|d| d.remove_temp::(copied_in_window_id())).unwrap_or(false) +} + +/// Runs after egui's label text selection at the end of each pass, when the labels have copied +/// their selected text. +struct CopyAfterLabels; + +impl egui::Plugin for CopyAfterLabels { + fn debug_name(&self) -> &'static str { + "CopyAfterLabels" + } + + fn on_end_pass(&mut self, ui: &mut egui::Ui) { + let ctx = ui.ctx(); + let copied = ctx.output(|o| o.commands.iter().any(|c| matches!(c, egui::OutputCommand::CopyText(_)))); + if ctx.viewport_id() != egui::ViewportId::ROOT + && copied + && ctx.input(|i| i.events.iter().any(|e| matches!(e, egui::Event::Copy | egui::Event::Cut))) + { + ctx.data_mut(|d| d.insert_temp(copied_in_window_id(), true)); + } + let id = waiting_id(ctx.viewport_id()); + let Some(command) = ctx.data_mut(|d| d.remove_temp::<&'static str>(id)) else { + return; + }; + if !copied { + ctx.data_mut(|d| d.insert_temp(after_labels_id(), command)); + ctx.request_repaint(); + } + } +} diff --git a/crates/ui-egui/src/sysclip.rs b/crates/ui-egui/src/sysclip.rs index 7285d2acf2..2af56093ba 100644 --- a/crates/ui-egui/src/sysclip.rs +++ b/crates/ui-egui/src/sysclip.rs @@ -292,11 +292,17 @@ impl VectorcraftApp { /// dropped, and the system clipboard keeps that copy. pub(crate) fn drop_formats_after_egui_copy(&mut self, ctx: &egui::Context) { let copied = ctx.output(|o| o.commands.iter().any(|c| matches!(c, egui::OutputCommand::CopyText(_) | egui::OutputCommand::CopyImage(_)))); - let publishing = &mut self.clipboard_publishing; if copied { - publishing.unwritten = None; + self.drop_unwritten_formats(); } - if copied && publishing.pending { + } + + /// Drop the formats of an earlier Copy or Cut that are not written yet: something else is + /// being copied (text through egui, the text selected in a label). + pub(crate) fn drop_unwritten_formats(&mut self) { + let publishing = &mut self.clipboard_publishing; + publishing.unwritten = None; + if publishing.pending { publishing.latest.fetch_add(1, Ordering::SeqCst); publishing.pending = false; } diff --git a/crates/ui-egui/src/tests/selectall.rs b/crates/ui-egui/src/tests/selectall.rs index e5efd535cd..39994d3f86 100644 --- a/crates/ui-egui/src/tests/selectall.rs +++ b/crates/ui-egui/src/tests/selectall.rs @@ -1,7 +1,8 @@ //! Select All (Cmd+A) follows the keyboard, from the window and from the system menu bar (macOS): //! a focused text field selects its own text, the Type tool editing selects the text being edited, //! the Artboards panel selects every artboard after a click on one of its rows with the Artboard -//! tool, and only otherwise are all the objects selected. +//! tool, and only otherwise are all the objects selected. The clipboard keys follow it too: Copy +//! and Cut copy the text selected in a field or in a panel's label before the art. use egui::{Event, Key, Modifiers, PointerButton, Pos2, Rect, vec2}; use serde_json::json; @@ -260,6 +261,89 @@ fn the_clipboard_keys_in_a_focused_field_edit_its_text() { } } +/// The rectangle of the text `text` painted last in a frame's output. +fn text_rect(out: &egui::FullOutput, text: &str) -> Rect { + out.shapes + .iter() + .rev() + .find_map(|s| match &s.shape { + egui::Shape::Text(t) if t.galley.text() == text => Some(t.galley.rect.translate(t.pos.to_vec2())), + _ => None, + }) + .unwrap_or_else(|| panic!("no {text:?} painted")) +} + +/// #8432: with a Document Info value's text selected, Copy and Cut (the keys or the menu bar's +/// items) copy that text and leave the art. With nothing selected in the labels, or when the +/// selected label is not drawn, Copy copies the art. +#[test] +fn copy_and_cut_copy_the_text_selected_in_a_panel_label() { + let mut a = App::new(); + a.app.run("window.panel", json!({"panel": "docInfo"})).unwrap(); + crate::panels::set_pstate(&a.ctx, "docinfo-category", "objects".to_string()); + // The two rectangles' outlines: 2 × (20 + 20) + 2 × (37 + 40). + let length = "234 pt"; + let mut out = a.frame(vec![]); + for _ in 0..3 { + out = a.frame(vec![]); + } + let select = |a: &mut App, out: &egui::FullOutput| { + let r = text_rect(out, length); + let (from, to) = (r.left_center() + vec2(1.0, 0.0), r.right_center() - vec2(1.0, 0.0)); + a.frame(vec![Event::PointerMoved(from)]); + a.frame(vec![click(from, true)]); + for k in 1..=4 { + a.frame(vec![Event::PointerMoved(from + (to - from) * k as f32 / 4.0)]); + } + a.frame(vec![click(to, false)]); + a.frame(vec![]) + }; + // Cmd+C or Cmd+X: the chord's frame, then the frame after it, when the keys are let go. + let chord = |a: &mut App, e: Event| { + let out = a.frame(vec![Event::ModifiersChanged(Modifiers::COMMAND), e]); + (out, a.frame(vec![Event::ModifiersChanged(Modifiers::NONE)])) + }; + let out = select(&mut a, &out); + let (copy, after) = chord(&mut a, Event::Copy); + assert_eq!(copied(©), [length], "Copy copies the selected value"); + assert!(copied(&after).is_empty(), "and nothing after it"); + assert!(a.app.session.clipboard.is_empty(), "no art copied"); + let (cut, after) = chord(&mut a, Event::Cut); + assert_eq!(copied(&cut), [length], "Cut copies the selected value"); + assert!(copied(&after).is_empty(), "and nothing after it"); + assert_eq!(a.selected(), 1, "and leaves the art"); + assert!(a.app.session.clipboard.is_empty(), "no art cut"); + // The menu bar's Edit › Copy (macOS). + let menu = a.menu("edit.copy"); + a.frame(vec![]); + assert_eq!(copied(&menu), [length], "the menu's Copy copies the selected value"); + assert!(a.app.session.clipboard.is_empty(), "no art copied"); + // A Copy another window sends on: a floating panel's window whose label copied its text copies + // nothing more. From any other window (a floating document's), the art is copied, not the text + // still selected in this window's panel. + let forward = |a: &mut App| { + crate::panel_docking::forward_keys(&mut a.app, &a.ctx, vec![Event::Copy], Some(Modifiers::COMMAND)); + a.app.forwarded_frame = !a.app.forwarded.is_empty(); + let events = std::iter::once(Event::ModifiersChanged(Modifiers::COMMAND)).chain(std::mem::take(&mut a.app.forwarded)).collect(); + let out = a.frame(events); + a.app.forwarded_frame = false; + (out, a.frame(vec![Event::ModifiersChanged(Modifiers::NONE)])) + }; + crate::shortcuts::note_copied_in_window(&a.ctx); + let (copy, after) = forward(&mut a); + assert!(copied(©).is_empty() && copied(&after).is_empty(), "the window copied its own text"); + assert!(a.app.session.clipboard.is_empty(), "no art copied"); + let (copy, _) = forward(&mut a); + assert!(copied(©).iter().all(|t| t != length), "{length} was copied from the other window"); + assert!(!a.app.session.clipboard.is_empty(), "Copy from the other window copied the art"); + // Tab hides the panels: the selected value is not drawn, so Copy copies the art, a frame later. + select(&mut a, &out); + a.app.ui.dock = false; + let (copy, after) = chord(&mut a, Event::Copy); + assert!(copied(©).is_empty(), "no label copied {length}"); + assert!(copied(&after).iter().any(|t| t.starts_with(" Pos2 { diff --git a/crates/ui-egui/src/tests/sysclip.rs b/crates/ui-egui/src/tests/sysclip.rs index 089fcdafaa..e5e83027d4 100644 --- a/crates/ui-egui/src/tests/sysclip.rs +++ b/crates/ui-egui/src/tests/sysclip.rs @@ -488,6 +488,21 @@ fn text_copied_before_the_formats_are_written_stays_on_the_clipboard() { assert_eq!(t.plain_text(), "Words copied in a field"); } +/// Text a floating panel's label copies after Copy, before the formats are written: its window +/// keeps the Copy, the formats are not written, and the system clipboard keeps the text. +#[test] +fn label_text_copied_before_the_formats_are_written_stays_on_the_clipboard() { + let (mut app, board, hold) = app_with_busy_worker(""); + copy_rect(&mut app); + let ctx = egui::Context::default(); + crate::shortcuts::note_copied_in_window(&ctx); + crate::panel_docking::forward_keys(&mut app, &ctx, vec![egui::Event::Copy], Some(egui::Modifiers::COMMAND)); + assert!(app.forwarded.is_empty(), "the window's Copy stays there"); + drop(hold); + background::wait_all(&mut app); + assert_eq!(board.borrow().writes, 0); +} + /// The Edit menu's Paste into a text field after Copy of type, before the formats are written: it /// waits for them, not for a save started after them, and pastes the type's text, not what /// another app copied before. From c70528d77afa4ff56fa21a78aafa9a941aafe1e9 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 17 Nov 2026 20:02:25 -0800 Subject: [PATCH 2/3] Part of #8432: fix label Copy with floating windows and unwritten formats A floating panel's or document's window notes that one of its labels copied the text a Copy or Cut asked for, so the Copy it sends on to the main window copies no art. The note was also set when a field with the keyboard copied its own text, and it stayed until the next Copy or key a window sent on, which then lost its Copy and the formats of an art Copy still being written. - The note is set only when no widget in that window has the keyboard, and it is cleared when the main window's next pass starts. - Only a Copy or Cut another window sent on skips the main window's labels. Before, any key sent on in the same frame, such as a key let go in a floating panel, made Cmd/Ctrl+C on selected label text copy the art. - The menu bar's Copy and Cut drop the art formats not written yet when they leave the event to the labels, also in a frame in which the main window does not have the keyboard. - A test runs passes of a second viewport and checks that a field's copy is not noted and that the note does not outlast the main window's next pass. Other tests check that Copy with label text selected, by its keys or the menu bar's item, drops the art formats not written yet; that Cut on a chord of the user's own, Cmd/Ctrl+C with other keys sent on, and the menu bar's Copy with a Copy sent on copy the label text; and that the art Copy deferred to the next frame runs even when the window has lost the keyboard by then. - docs/target-app-parity.md lists the behavior and the two menu cases that still copy the art: the in-window Edit menu, whose click clears the label's selection, and the menu bar while the selected text is in a floating panel's own window. --- crates/ui-egui/src/app.rs | 1 + crates/ui-egui/src/app/input.rs | 1 + crates/ui-egui/src/lib.rs | 3 + crates/ui-egui/src/menus/invoke.rs | 3 + crates/ui-egui/src/shortcuts.rs | 2 +- crates/ui-egui/src/shortcuts/labels.rs | 11 +++ crates/ui-egui/src/shortcuts/tests.rs | 34 +++++++ crates/ui-egui/src/tests/selectall.rs | 125 ++++++++++++++++++++----- crates/ui-egui/src/tests/sysclip.rs | 5 + docs/target-app-parity.md | 1 + 10 files changed, 159 insertions(+), 27 deletions(-) diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index e3d2a13d64..4c823d1ffa 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -167,6 +167,7 @@ impl VectorcraftApp { forwarded_modifiers: None, forwarded_modifiers_held: false, forwarded_frame: false, + forwarded_clipboard: false, synthetic_modifiers: false, ime_marked: None, ime_context: Default::default(), diff --git a/crates/ui-egui/src/app/input.rs b/crates/ui-egui/src/app/input.rs index 95f719d08b..5b7310d56a 100644 --- a/crates/ui-egui/src/app/input.rs +++ b/crates/ui-egui/src/app/input.rs @@ -214,6 +214,7 @@ impl VectorcraftApp { raw.events.insert(0, egui::Event::ModifiersChanged(self.host_modifiers)); } self.forwarded_frame = !self.forwarded.is_empty(); + self.forwarded_clipboard = self.forwarded.iter().any(|e| matches!(e, egui::Event::Copy | egui::Event::Cut)); if !self.forwarded.is_empty() { if let Some(m) = self.forwarded_modifiers.take() { raw.events.push(egui::Event::ModifiersChanged(m)); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 0b8ee67183..68fd000e5f 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -304,6 +304,9 @@ pub struct VectorcraftApp { /// This host iteration's input carries forwarded keys: they are the main window's although /// its OS window doesn't have the keyboard (see `shortcuts::handle`). pub(crate) forwarded_frame: bool, + /// The forwarded keys of this host iteration hold a Copy or Cut: it copies the art, not text + /// selected in this window's labels (see `shortcuts::handle_clipboard`). + pub(crate) forwarded_clipboard: bool, /// Synthetic input set the modifiers egui holds (see [`Self::raw_input_hook`]). synthetic_modifiers: bool, /// The marked text the system IME last sent (`None` once it commits or clears). When the Type diff --git a/crates/ui-egui/src/menus/invoke.rs b/crates/ui-egui/src/menus/invoke.rs index ee40c7793e..ede8deaa1e 100644 --- a/crates/ui-egui/src/menus/invoke.rs +++ b/crates/ui-egui/src/menus/invoke.rs @@ -122,6 +122,9 @@ pub fn invoke_from_system_menu(app: &mut VectorcraftApp, ctx: &egui::Context, id if let Some((_, e)) = label_event { ctx.input_mut(|i| i.events.push(e)); } + // The labels' text replaces the formats of an earlier Copy or Cut not written yet, even + // in a frame whose keys are not this window's. + app.drop_unwritten_formats(); } None if id == "select.all" && crate::panels::artboards::selects_all(app, ctx) => crate::panels::artboards::select_all(app), None => invoke(app, id, p), diff --git a/crates/ui-egui/src/shortcuts.rs b/crates/ui-egui/src/shortcuts.rs index 0b93ed3a84..2d815e7e3f 100644 --- a/crates/ui-egui/src/shortcuts.rs +++ b/crates/ui-egui/src/shortcuts.rs @@ -808,7 +808,7 @@ fn handle_clipboard(app: &mut VectorcraftApp, ctx: &egui::Context, textless_past // sent them on (a floating document's): those copy the art. The menu bar's Copy and Cut have // left theirs to the labels already. let mut clip = vec![]; - let forwarded = app.forwarded_frame && ctx.viewport_id() == egui::ViewportId::ROOT; + let forwarded = app.forwarded_clipboard && ctx.viewport_id() == egui::ViewportId::ROOT; let to_labels = |e: egui::Event, id| ctx.input(|i| i.events.contains(&e)) && (left_to_labels(ctx) == Some(id) || (!forwarded && leave_to_labels(ctx, id))); let (labels_copy, labels_cut) = (to_labels(egui::Event::Copy, "edit.copy"), to_labels(egui::Event::Cut, "edit.cut")); diff --git a/crates/ui-egui/src/shortcuts/labels.rs b/crates/ui-egui/src/shortcuts/labels.rs index 949dda133c..1865ca1821 100644 --- a/crates/ui-egui/src/shortcuts/labels.rs +++ b/crates/ui-egui/src/shortcuts/labels.rs @@ -12,6 +12,7 @@ fn after_labels_id() -> egui::Id { /// Set when a label in a window of its own (a floating panel's) copied its selected text: the /// window then sends the Copy or Cut on to the main window ([`crate::panel_docking::forward_keys`]). +/// It lasts until the main window's next pass starts. fn copied_in_window_id() -> egui::Id { egui::Id::unique("labels-copied-in-window") } @@ -71,11 +72,21 @@ impl egui::Plugin for CopyAfterLabels { "CopyAfterLabels" } + /// The windows of their own run their passes inside the main window's pass, and their Copy or + /// Cut is sent on in that pass: a copy noted before it is not theirs. + fn on_begin_pass(&mut self, ui: &mut egui::Ui) { + if ui.ctx().viewport_id() == egui::ViewportId::ROOT { + take_copied_in_window(ui.ctx()); + } + } + fn on_end_pass(&mut self, ui: &mut egui::Ui) { let ctx = ui.ctx(); let copied = ctx.output(|o| o.commands.iter().any(|c| matches!(c, egui::OutputCommand::CopyText(_)))); + // A field with the keyboard copies its own text, and its window sends nothing on. if ctx.viewport_id() != egui::ViewportId::ROOT && copied + && ctx.memory(|m| m.focused().is_none()) && ctx.input(|i| i.events.iter().any(|e| matches!(e, egui::Event::Copy | egui::Event::Cut))) { ctx.data_mut(|d| d.insert_temp(copied_in_window_id(), true)); diff --git a/crates/ui-egui/src/shortcuts/tests.rs b/crates/ui-egui/src/shortcuts/tests.rs index 4fad102f73..87e63c810e 100644 --- a/crates/ui-egui/src/shortcuts/tests.rs +++ b/crates/ui-egui/src/shortcuts/tests.rs @@ -1323,6 +1323,40 @@ fn copy_and_paste_events_use_the_system_clipboard() { assert_eq!(app.session.doc().unwrap().doc.layers[0].children().unwrap().len(), 8); } +/// #8432: a window of its own (a floating panel's) notes that its labels copied the text a Copy +/// asked for, so that the Copy it sends on to the main window copies nothing more. A field with +/// the keyboard copying its own text is not noted, and a note lasts only until the main window's +/// next pass, so it never drops a later Copy from another window. +#[test] +fn a_window_notes_only_its_labels_copies_until_the_next_main_window_pass() { + let ctx = egui::Context::default(); + watch_label_copies(&ctx); + let main_window_pass = || ctx.run_ui(egui::RawInput::default(), |_| {}).drop_without_applying_deltas(); + main_window_pass(); + let window = egui::ViewportId::from_hash_of("floating panel"); + let mut window_input = egui::RawInput { viewport_id: window, events: vec![egui::Event::Copy], ..Default::default() }; + window_input.viewports.insert(window, egui::ViewportInfo { parent: Some(egui::ViewportId::ROOT), ..Default::default() }); + // `field`: the text is copied by a field with the keyboard instead of a label. + let copy_in_window = |field: bool| { + ctx.run_ui(window_input.clone(), |ui| { + if field { + let mut text = String::from("234 pt"); + ui.text_edit_singleline(&mut text).request_focus(); + } + ui.ctx().copy_text("234 pt".into()); + }) + .drop_without_applying_deltas(); + }; + copy_in_window(false); + assert!(take_copied_in_window(&ctx), "the window's label copied the text"); + assert!(!take_copied_in_window(&ctx), "once"); + copy_in_window(false); + main_window_pass(); + assert!(!take_copied_in_window(&ctx), "the main window's next pass forgets the note"); + copy_in_window(true); + assert!(!take_copied_in_window(&ctx), "a field's copy is not noted"); +} + /// While a grid is dragged out, ↑/↓ change its rows and ←/→ its columns. #[test] fn arrows_while_dragging_a_grid_change_its_rows_and_columns() { diff --git a/crates/ui-egui/src/tests/selectall.rs b/crates/ui-egui/src/tests/selectall.rs index 39994d3f86..8b08c79f51 100644 --- a/crates/ui-egui/src/tests/selectall.rs +++ b/crates/ui-egui/src/tests/selectall.rs @@ -15,6 +15,8 @@ struct App { app: VectorcraftApp, ctx: egui::Context, time: f64, + /// The window has the keyboard. + focused: bool, } impl App { @@ -24,7 +26,7 @@ impl App { app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); app.run("shape.rectangle", json!({"x": 200, "y": 150, "width": 20, "height": 20})).unwrap(); app.run("shape.rectangle", json!({"x": 20, "y": 20, "width": 37, "height": 40})).unwrap(); - let mut a = Self { app, ctx: egui::Context::default(), time: 0.0 }; + let mut a = Self { app, ctx: egui::Context::default(), time: 0.0, focused: true }; for _ in 0..3 { a.frame(vec![]); } @@ -38,6 +40,7 @@ impl App { let raw = egui::RawInput { events, time: Some(self.time), + focused: self.focused, screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(1600.0, 850.0))), ..Default::default() }; @@ -273,36 +276,47 @@ fn text_rect(out: &egui::FullOutput, text: &str) -> Rect { .unwrap_or_else(|| panic!("no {text:?} painted")) } +/// Document Info open on its Objects category, after a few frames to lay it out: the last frame. +fn show_document_info(a: &mut App) -> egui::FullOutput { + a.app.run("window.panel", json!({"panel": "docInfo"})).unwrap(); + crate::panels::set_pstate(&a.ctx, "docinfo-category", "objects".to_string()); + let mut out = a.frame(vec![]); + for _ in 0..3 { + out = a.frame(vec![]); + } + out +} + +/// Drag across the label text `text` painted in `out`, selecting it: the frame after the drag. +fn select_label_text(a: &mut App, out: &egui::FullOutput, text: &str) -> egui::FullOutput { + let r = text_rect(out, text); + let (from, to) = (r.left_center() + vec2(1.0, 0.0), r.right_center() - vec2(1.0, 0.0)); + a.frame(vec![Event::PointerMoved(from)]); + a.frame(vec![click(from, true)]); + for k in 1..=4 { + a.frame(vec![Event::PointerMoved(from + (to - from) * k as f32 / 4.0)]); + } + a.frame(vec![click(to, false)]); + a.frame(vec![]) +} + +/// Cmd+C or Cmd+X (`e`): the chord's frame, then the frame after it, when the keys are let go. +fn clipboard_chord(a: &mut App, e: Event) -> (egui::FullOutput, egui::FullOutput) { + let out = a.frame(vec![Event::ModifiersChanged(Modifiers::COMMAND), e]); + (out, a.frame(vec![Event::ModifiersChanged(Modifiers::NONE)])) +} + /// #8432: with a Document Info value's text selected, Copy and Cut (the keys or the menu bar's /// items) copy that text and leave the art. With nothing selected in the labels, or when the /// selected label is not drawn, Copy copies the art. #[test] fn copy_and_cut_copy_the_text_selected_in_a_panel_label() { let mut a = App::new(); - a.app.run("window.panel", json!({"panel": "docInfo"})).unwrap(); - crate::panels::set_pstate(&a.ctx, "docinfo-category", "objects".to_string()); + let out = show_document_info(&mut a); // The two rectangles' outlines: 2 × (20 + 20) + 2 × (37 + 40). let length = "234 pt"; - let mut out = a.frame(vec![]); - for _ in 0..3 { - out = a.frame(vec![]); - } - let select = |a: &mut App, out: &egui::FullOutput| { - let r = text_rect(out, length); - let (from, to) = (r.left_center() + vec2(1.0, 0.0), r.right_center() - vec2(1.0, 0.0)); - a.frame(vec![Event::PointerMoved(from)]); - a.frame(vec![click(from, true)]); - for k in 1..=4 { - a.frame(vec![Event::PointerMoved(from + (to - from) * k as f32 / 4.0)]); - } - a.frame(vec![click(to, false)]); - a.frame(vec![]) - }; - // Cmd+C or Cmd+X: the chord's frame, then the frame after it, when the keys are let go. - let chord = |a: &mut App, e: Event| { - let out = a.frame(vec![Event::ModifiersChanged(Modifiers::COMMAND), e]); - (out, a.frame(vec![Event::ModifiersChanged(Modifiers::NONE)])) - }; + let select = |a: &mut App, out: &egui::FullOutput| select_label_text(a, out, length); + let chord = clipboard_chord; let out = select(&mut a, &out); let (copy, after) = chord(&mut a, Event::Copy); assert_eq!(copied(©), [length], "Copy copies the selected value"); @@ -313,20 +327,45 @@ fn copy_and_cut_copy_the_text_selected_in_a_panel_label() { assert!(copied(&after).is_empty(), "and nothing after it"); assert_eq!(a.selected(), 1, "and leaves the art"); assert!(a.app.session.clipboard.is_empty(), "no art cut"); + // Cut on a chord of the user's own that no other command has, which egui also sends as Cut: the + // keymap does not cut the art after the label. + let overrides = a.app.ui.shortcut_overrides.clone(); + crate::keymap::set_override(&mut a.app.ui.shortcut_overrides, "edit.cut", "Cmd+Alt+Shift+X"); + let cut = a.frame(vec![Event::ModifiersChanged(Modifiers::COMMAND | Modifiers::ALT | Modifiers::SHIFT), Event::Cut]); + a.frame(vec![Event::ModifiersChanged(Modifiers::NONE)]); + assert_eq!(copied(&cut), [length], "Cut on the user's chord copies the selected value"); + assert_eq!(a.selected(), 1, "and leaves the art"); + assert!(a.app.session.clipboard.is_empty(), "no art cut"); + a.app.ui.shortcut_overrides = overrides; // The menu bar's Edit › Copy (macOS). let menu = a.menu("edit.copy"); a.frame(vec![]); assert_eq!(copied(&menu), [length], "the menu's Copy copies the selected value"); assert!(a.app.session.clipboard.is_empty(), "no art copied"); + // The same in a frame that also runs a Copy another window sent on. + (a.app.forwarded_frame, a.app.forwarded_clipboard) = (true, true); + let menu = a.menu("edit.copy"); + (a.app.forwarded_frame, a.app.forwarded_clipboard) = (false, false); + a.frame(vec![]); + assert_eq!(copied(&menu), [length], "the menu's Copy copies the selected value with a Copy sent on"); + assert!(a.app.session.clipboard.is_empty(), "no art copied"); + // Cmd+C in a frame that also runs other keys another window sent on (a key let go there). + a.app.forwarded_frame = true; + let copy = a.frame(vec![Event::ModifiersChanged(Modifiers::COMMAND), Event::Copy]); + a.app.forwarded_frame = false; + a.frame(vec![Event::ModifiersChanged(Modifiers::NONE)]); + assert_eq!(copied(©), [length], "Cmd+C copies the selected value with other keys sent on"); + assert!(a.app.session.clipboard.is_empty(), "no art copied"); // A Copy another window sends on: a floating panel's window whose label copied its text copies // nothing more. From any other window (a floating document's), the art is copied, not the text // still selected in this window's panel. let forward = |a: &mut App| { crate::panel_docking::forward_keys(&mut a.app, &a.ctx, vec![Event::Copy], Some(Modifiers::COMMAND)); a.app.forwarded_frame = !a.app.forwarded.is_empty(); + a.app.forwarded_clipboard = a.app.forwarded_frame; let events = std::iter::once(Event::ModifiersChanged(Modifiers::COMMAND)).chain(std::mem::take(&mut a.app.forwarded)).collect(); let out = a.frame(events); - a.app.forwarded_frame = false; + (a.app.forwarded_frame, a.app.forwarded_clipboard) = (false, false); (out, a.frame(vec![Event::ModifiersChanged(Modifiers::NONE)])) }; crate::shortcuts::note_copied_in_window(&a.ctx); @@ -336,14 +375,48 @@ fn copy_and_cut_copy_the_text_selected_in_a_panel_label() { let (copy, _) = forward(&mut a); assert!(copied(©).iter().all(|t| t != length), "{length} was copied from the other window"); assert!(!a.app.session.clipboard.is_empty(), "Copy from the other window copied the art"); - // Tab hides the panels: the selected value is not drawn, so Copy copies the art, a frame later. + // Tab hides the panels: the selected value is not drawn, so Copy copies the art, a frame later, + // even when the window has lost the keyboard by then. select(&mut a, &out); a.app.ui.dock = false; - let (copy, after) = chord(&mut a, Event::Copy); + let copy = a.frame(vec![Event::ModifiersChanged(Modifiers::COMMAND), Event::Copy]); + a.focused = false; + let after = a.frame(vec![Event::ModifiersChanged(Modifiers::NONE)]); + a.focused = true; assert!(copied(©).is_empty(), "no label copied {length}"); assert!(copied(&after).iter().any(|t| t.starts_with(" Pos2 { diff --git a/crates/ui-egui/src/tests/sysclip.rs b/crates/ui-egui/src/tests/sysclip.rs index e5e83027d4..aac62b4039 100644 --- a/crates/ui-egui/src/tests/sysclip.rs +++ b/crates/ui-egui/src/tests/sysclip.rs @@ -164,6 +164,11 @@ pub(super) fn copy_rect(app: &mut VectorcraftApp) { run(app, "edit.copy", json!({})); } +/// How many times the app wrote the clipboard. +pub(super) fn writes(board: &Rc>) -> usize { + board.borrow().writes +} + pub(super) fn written(board: &Rc>) -> Vec<&'static str> { board.borrow().flavours.iter().map(|f| f.mime).collect() } diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index ef83de54ad..ebc81dcac4 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1158,6 +1158,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur - On Windows and Linux, F2, F3 and F4 run Cut, Copy and Paste alongside Ctrl+X, Ctrl+C and Ctrl+V, and in the web build they do so everywhere except macOS. The Edit menu keeps the Ctrl shortcuts, and Keyboard Shortcuts lists both keys of each row. On macOS only Cmd+X, Cmd+C and Cmd+V are the defaults. F2, F3 and F4 do not run these commands in a focused text field or while the Type tool edits text. A saved shortcut set that assigns one of these keys to another command keeps that assignment. Public reference: [Default keyboard shortcuts](https://helpx.adobe.com/illustrator/using/default-keyboard-shortcuts.html). - Cmd/Ctrl+A follows the keyboard: in a panel, dialog or search field it selects the field's text, while the Type tool edits it selects all of that text (also `select.all` over MCP), and only otherwise every object. On macOS the menu bar's Select All, Cut, Copy and Paste (keys or clicks) act on a focused field too, and elsewhere the in-window menus' do: the field that had the keyboard when the menu opened gets them, with its selection, and the keyboard back (#516). Not yet: Select All across threaded frames (one frame's text is selected). - Cmd/Ctrl+A follows the keyboard: in a panel, dialog or search field it selects the field's text, while the Type tool edits it selects all of that text (also `select.all` over MCP), and only otherwise every object. On macOS the menu bar's Select All, Cut, Copy and Paste (keys or clicks) act on a focused field too, and elsewhere the in-window menus' do: the field that had the keyboard when the menu opened gets them, with its selection, and the keyboard back (#516). In a frame of threaded text, Select All selects the whole story, highlighted in every frame: Copy and Cut take all of it, typing or Delete replaces it as one undo step, and a fill, a size step or a Character or Paragraph panel change applies to every frame's text (VectorCraft's own behavior; `textedit::select_all_in_a_threaded_frame_selects_the_whole_story`, `textedit::typing_over_the_whole_story_replaces_every_frames_text_in_one_step`). + - With text selected in a panel's label, such as a Document Info value, Cmd/Ctrl+C and Cmd/Ctrl+X copy that text and do not copy or cut the art, and so do the menu bar's Copy and Cut on macOS. When the selected label is not drawn (the panels are hidden), the art is copied instead (#8432; VectorCraft's own behavior; `selectall::copy_and_cut_copy_the_text_selected_in_a_panel_label`). Not yet: the in-window Edit menu's Copy and Cut (the click on the menu clears the label's selection first), and the menu bar's Copy and Cut while the selected text is in a floating panel's own window (they copy the art). - With the whole story of threaded text selected by Select All, Remove Line Breaks, Remove Paragraph Breaks and Type › Bullets and Numbering › Convert to Text act on the text of every frame as one undo step, locked and hidden frames included, and the story stays selected after Convert to Text; a selection inside one frame changes only that frame (VectorCraft's own behavior; `textedit::remove_breaks_takes_the_whole_story_that_select_all_selected`, `textedit::convert_to_text_takes_the_whole_story_that_select_all_selected`, `textedit::convert_to_text_of_the_whole_story_converts_locked_and_hidden_frames`, `textedit::remove_breaks_of_the_whole_story_takes_locked_and_hidden_frames`). - When only some frames of a threaded story are selected, Remove Line Breaks and Remove Paragraph Breaks go through that story's frames once for all of them, not once per selected frame; each selected frame still loses its own breaks, in the order selected, as one undo step (`addons::cleanup::text::tests::remove_breaks_goes_through_a_partly_named_story_once`). - With the whole story of threaded text selected by Select All, the Character, Paragraph, OpenType, Tabs and Text Styles panels, the Justification dialog and the font menu change the frames that are in the edited frame's thread at the time of the change, locked and hidden frames included. A frame that has left the thread is not changed, and a frame added to the thread is (VectorCraft's own behavior; `panels::character::tests::story_changes_follow_the_edited_frames_thread_after_the_first_saved_frame_leaves`, `story_changes_leave_out_a_frame_that_left_and_take_in_a_frame_that_joined`, `story_changes_stay_in_the_edited_frame_once_its_thread_is_removed`, `story_changes_take_in_locked_and_hidden_frames`, `panels::tabs::tests::indent_markers_after_select_all_keep_each_story_paragraphs_indents`, `dialogs::justification::tests::justification_dialog_follows_the_story_thread_at_ok`). From 1f92a1c2d7220976ce48d631c0013b3bbda04970 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 17 Nov 2026 21:43:14 -0800 Subject: [PATCH 3/3] Part of #8432: F3 and F2 copy label text, and the menu bar's Cut does too With text selected in a panel's label, Copy and Cut on keys that arrive as key presses (F3 and F2 off macOS, or another key assigned to them) copied or cut the art. They now leave the Copy or Cut to the labels, as Cmd/Ctrl+C and X do. A key another window sent on that runs Copy or Cut still acts on the art. The menu bar's Cut, clicked with no modifier held, also cut the art: its Cut event was turned back into a plain X key press, the shortcut of Toggle Fill/Stroke Focus. The Copy or Cut the menu leaves to the labels is now kept as it is. - Tests: F3 and F2, the menu bar's Cut, and Cut in the unwritten-formats test; the keys another window sends on go through the host's raw input hook; a test drags across a real label in a second viewport and copies it. - Docs: the progress line says Copy and Cut act on the art when the label is not drawn, lists the tests, and names F3 and F2 among the limits in a floating panel's window. --- crates/ui-egui/src/app.rs | 1 + crates/ui-egui/src/app/input.rs | 1 + crates/ui-egui/src/lib.rs | 4 + crates/ui-egui/src/shortcuts.rs | 38 ++++++- crates/ui-egui/src/shortcuts/tests.rs | 42 ++++++++ crates/ui-egui/src/tests/selectall.rs | 146 +++++++++++++++++--------- docs/target-app-parity.md | 2 +- 7 files changed, 179 insertions(+), 55 deletions(-) diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 4c823d1ffa..7e9dd8d823 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -168,6 +168,7 @@ impl VectorcraftApp { forwarded_modifiers_held: false, forwarded_frame: false, forwarded_clipboard: false, + forwarded_presses: vec![], synthetic_modifiers: false, ime_marked: None, ime_context: Default::default(), diff --git a/crates/ui-egui/src/app/input.rs b/crates/ui-egui/src/app/input.rs index 5b7310d56a..1a170a3f11 100644 --- a/crates/ui-egui/src/app/input.rs +++ b/crates/ui-egui/src/app/input.rs @@ -215,6 +215,7 @@ impl VectorcraftApp { } self.forwarded_frame = !self.forwarded.is_empty(); self.forwarded_clipboard = self.forwarded.iter().any(|e| matches!(e, egui::Event::Copy | egui::Event::Cut)); + self.forwarded_presses = self.forwarded.iter().filter(|e| matches!(e, egui::Event::Key { pressed: true, .. })).cloned().collect(); if !self.forwarded.is_empty() { if let Some(m) = self.forwarded_modifiers.take() { raw.events.push(egui::Event::ModifiersChanged(m)); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 68fd000e5f..ed8a53a05d 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -307,6 +307,10 @@ pub struct VectorcraftApp { /// The forwarded keys of this host iteration hold a Copy or Cut: it copies the art, not text /// selected in this window's labels (see `shortcuts::handle_clipboard`). pub(crate) forwarded_clipboard: bool, + /// The key presses among this host iteration's forwarded keys: a Copy or Cut one of them runs + /// (F3 and F2 off macOS) copies the art, not text selected in this window's labels (see + /// `shortcuts::clipboard_key_to_labels`). + pub(crate) forwarded_presses: Vec, /// Synthetic input set the modifiers egui holds (see [`Self::raw_input_hook`]). synthetic_modifiers: bool, /// The marked text the system IME last sent (`None` once it commits or clears). When the Type diff --git a/crates/ui-egui/src/shortcuts.rs b/crates/ui-egui/src/shortcuts.rs index 2d815e7e3f..be0575daa0 100644 --- a/crates/ui-egui/src/shortcuts.rs +++ b/crates/ui-egui/src/shortcuts.rs @@ -343,7 +343,11 @@ pub fn handle(app: &mut VectorcraftApp, ctx: &egui::Context) { crate::ime::controls_without_editor(app, ctx); return; } - ctx.input_mut(unclip_chords); + // The Copy or Cut the menu bar left to the labels came from no key: unclipped, the menu's Cut + // would become a plain X press (Toggle Fill/Stroke Focus). + if left_to_labels(ctx).is_none() { + ctx.input_mut(unclip_chords); + } // Tab is ours when no field has the keyboard (it shows and hides the panels, or goes to the // Type tool): left to egui, it would also move the focus on to a field, and every key after it // would count as typing. @@ -806,7 +810,8 @@ fn handle_clipboard(app: &mut VectorcraftApp, ctx: &egui::Context, textless_past // Clipboard keys arrive as events, not key presses; the chord held says which paste. Copy and // Cut with text selected in a label copy that text ([`leave_to_labels`]), unless another window // sent them on (a floating document's): those copy the art. The menu bar's Copy and Cut have - // left theirs to the labels already. + // left theirs to the labels already and dropped the unwritten formats; dropping them again + // here does nothing more. let mut clip = vec![]; let forwarded = app.forwarded_clipboard && ctx.viewport_id() == egui::ViewportId::ROOT; let to_labels = @@ -845,6 +850,33 @@ fn handle_clipboard(app: &mut VectorcraftApp, ctx: &egui::Context, textless_past labels_copy || labels_cut } +/// Copy or Cut (`id`) on a key that egui does not send as a Copy or Cut event (F3 and F2 off +/// macOS, a key of the user's own): with text selected in a label, the labels copy it, as they do +/// for Cmd/Ctrl+C and X ([`leave_to_labels`]) (→ true). When a key another window sent on in this +/// frame (a floating document's) runs `id`, the art is copied. +fn clipboard_key_to_labels(app: &mut VectorcraftApp, ctx: &egui::Context, id: &str) -> bool { + let (command, event) = match id { + "edit.copy" => ("edit.copy", egui::Event::Copy), + "edit.cut" => ("edit.cut", egui::Event::Cut), + _ => return false, + }; + if ctx.viewport_id() == egui::ViewportId::ROOT && forwarded_key_runs(app, command) || !leave_to_labels(ctx, command) { + return false; + } + ctx.input_mut(|i| i.events.push(event)); + app.drop_unwritten_formats(); + true +} + +/// Does a key press another window sent on in this frame run command `id`? +fn forwarded_key_runs(app: &VectorcraftApp, id: &str) -> bool { + let mirror = crate::shortcut_editor::current(); + let platform = crate::keymap::platform(); + app.forwarded_presses.iter().filter_map(|e| craft_keymap::KeyInput::from_egui(e, platform)).any(|input| { + app.dispatcher.layout().candidates(&input).iter().any(|chord| crate::keymap::lookup(&mirror.keymap, chord).is_some_and(|c| c.id == id)) + }) +} + /// Route arrows, digits and letters to a tool while a gesture is active. fn busy_tool_keys(app: &mut VectorcraftApp, ctx: &egui::Context, view: ViewInfo) { // The arrows while dragging: a polygon's sides, a grid's rows (↑/↓) and columns (←/→)… @@ -1110,6 +1142,8 @@ fn run_hit(app: &mut VectorcraftApp, ctx: &egui::Context, c: &CommandRef) { // With the Artboard tool, Select All's keys after a click on an Artboards panel row select // every artboard instead of the art. crate::panels::artboards::select_all(app); + } else if clipboard_key_to_labels(app, ctx, &c.id) { + // The labels copy their selected text at the end of the pass. } else { match menu_target(app, c) { Some(Ok((id, p))) => crate::menus::invoke(app, &id, p), diff --git a/crates/ui-egui/src/shortcuts/tests.rs b/crates/ui-egui/src/shortcuts/tests.rs index 87e63c810e..4b7db5c26e 100644 --- a/crates/ui-egui/src/shortcuts/tests.rs +++ b/crates/ui-egui/src/shortcuts/tests.rs @@ -1357,6 +1357,48 @@ fn a_window_notes_only_its_labels_copies_until_the_next_main_window_pass() { assert!(!take_copied_in_window(&ctx), "a field's copy is not noted"); } +/// #8432: text selected by a drag across a label in a window of its own, then copied there, is +/// noted as that window's labels' copy. +#[test] +fn a_window_notes_a_copy_of_text_dragged_across_its_label() { + let ctx = egui::Context::default(); + watch_label_copies(&ctx); + ctx.run_ui(egui::RawInput::default(), |_| {}).drop_without_applying_deltas(); + let window = egui::ViewportId::from_hash_of("floating panel"); + let rect = std::cell::Cell::new(egui::Rect::NOTHING); + let time = std::cell::Cell::new(0.0); + // One pass of the window with `events`: the texts it copied. + let pass = |events: Vec| { + time.set(time.get() + 0.1); + let screen = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(400.0, 200.0)); + let mut input = egui::RawInput { viewport_id: window, events, time: Some(time.get()), screen_rect: Some(screen), ..Default::default() }; + input.viewports.insert(window, egui::ViewportInfo { parent: Some(egui::ViewportId::ROOT), ..Default::default() }); + let out = ctx.run_ui(input, |ui| rect.set(ui.label("234 pt").rect)); + let copied: Vec = out + .platform_output + .commands + .iter() + .filter_map(|c| if let egui::OutputCommand::CopyText(t) = c { Some(t.clone()) } else { None }) + .collect(); + out.drop_without_applying_deltas(); + copied + }; + pass(vec![]); + let (from, to) = (rect.get().left_center() + egui::vec2(1.0, 0.0), rect.get().right_center() - egui::vec2(1.0, 0.0)); + let button = + |at, pressed| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE }; + pass(vec![egui::Event::PointerMoved(from)]); + pass(vec![button(from, true)]); + for k in 1..=4 { + pass(vec![egui::Event::PointerMoved(from + (to - from) * k as f32 / 4.0)]); + } + pass(vec![button(to, false)]); + pass(vec![]); + assert!(!take_copied_in_window(&ctx), "nothing copied yet"); + assert_eq!(pass(vec![egui::Event::Copy]), ["234 pt"], "the label copies its selected text"); + assert!(take_copied_in_window(&ctx), "the window's label copy is noted"); +} + /// While a grid is dragged out, ↑/↓ change its rows and ←/→ its columns. #[test] fn arrows_while_dragging_a_grid_change_its_rows_and_columns() { diff --git a/crates/ui-egui/src/tests/selectall.rs b/crates/ui-egui/src/tests/selectall.rs index 8b08c79f51..61bc00a21d 100644 --- a/crates/ui-egui/src/tests/selectall.rs +++ b/crates/ui-egui/src/tests/selectall.rs @@ -37,13 +37,19 @@ impl App { /// host dispatches it before the app's logic. fn frame_with(&mut self, events: Vec, menu: Option<&str>) -> egui::FullOutput { self.time += 0.1; - let raw = egui::RawInput { + let mut raw = egui::RawInput { events, time: Some(self.time), focused: self.focused, screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(1600.0, 850.0))), ..Default::default() }; + // As the hosts do: the keys another window sent on join this frame's input, and the frame + // after them goes back to the keyboard's modifiers. + let app = &self.app; + if !app.forwarded.is_empty() || app.forwarded_frame || app.forwarded_modifiers_held { + self.app.raw_input_hook(&self.ctx, &mut raw); + } let app = &mut self.app; let mut out = self.ctx.run_ui(raw, |ui| { if let Some(id) = menu { @@ -92,6 +98,10 @@ fn press(key: Key, modifiers: Modifiers) -> Event { Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers } } +fn release(key: Key) -> Event { + Event::Key { key, physical_key: None, pressed: false, repeat: false, modifiers: Modifiers::NONE } +} + fn click(at: Pos2, pressed: bool) -> Event { Event::PointerButton { pos: at, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE } } @@ -306,9 +316,9 @@ fn clipboard_chord(a: &mut App, e: Event) -> (egui::FullOutput, egui::FullOutput (out, a.frame(vec![Event::ModifiersChanged(Modifiers::NONE)])) } -/// #8432: with a Document Info value's text selected, Copy and Cut (the keys or the menu bar's -/// items) copy that text and leave the art. With nothing selected in the labels, or when the -/// selected label is not drawn, Copy copies the art. +/// #8432: with a Document Info value's text selected, Copy and Cut (Cmd+C and Cmd+X, F3 and F2, +/// or the menu bar's items) copy that text and leave the art. When the selected label is not +/// drawn, Copy copies the art. #[test] fn copy_and_cut_copy_the_text_selected_in_a_panel_label() { let mut a = App::new(); @@ -336,45 +346,27 @@ fn copy_and_cut_copy_the_text_selected_in_a_panel_label() { assert_eq!(copied(&cut), [length], "Cut on the user's chord copies the selected value"); assert_eq!(a.selected(), 1, "and leaves the art"); assert!(a.app.session.clipboard.is_empty(), "no art cut"); + // Copy and Cut on keys egui sends as key presses (F3 and F2, their keys off macOS). + crate::keymap::set_override(&mut a.app.ui.shortcut_overrides, "edit.copy", "F3"); + crate::keymap::set_override(&mut a.app.ui.shortcut_overrides, "edit.cut", "F2"); + for (key, what) in [(Key::F3, "Copy"), (Key::F2, "Cut")] { + let out = a.frame(vec![press(key, Modifiers::NONE)]); + let after = a.frame(vec![release(key)]); + assert_eq!(copied(&out), [length], "{what} on {key:?} copies the selected value"); + assert!(copied(&after).is_empty(), "and nothing after it"); + assert_eq!(a.selected(), 1, "{what} on {key:?} leaves the art"); + assert!(a.app.session.clipboard.is_empty(), "no art copied or cut"); + } + // The menu bar's Edit › Copy and Edit › Cut (macOS). + for (id, what) in [("edit.copy", "Copy"), ("edit.cut", "Cut")] { + let menu = a.menu(id); + let after = a.frame(vec![]); + assert_eq!(copied(&menu), [length], "the menu's {what} copies the selected value"); + assert!(copied(&after).is_empty(), "and nothing after it"); + assert_eq!(a.selected(), 1, "the menu's {what} leaves the art"); + assert!(a.app.session.clipboard.is_empty(), "no art copied or cut"); + } a.app.ui.shortcut_overrides = overrides; - // The menu bar's Edit › Copy (macOS). - let menu = a.menu("edit.copy"); - a.frame(vec![]); - assert_eq!(copied(&menu), [length], "the menu's Copy copies the selected value"); - assert!(a.app.session.clipboard.is_empty(), "no art copied"); - // The same in a frame that also runs a Copy another window sent on. - (a.app.forwarded_frame, a.app.forwarded_clipboard) = (true, true); - let menu = a.menu("edit.copy"); - (a.app.forwarded_frame, a.app.forwarded_clipboard) = (false, false); - a.frame(vec![]); - assert_eq!(copied(&menu), [length], "the menu's Copy copies the selected value with a Copy sent on"); - assert!(a.app.session.clipboard.is_empty(), "no art copied"); - // Cmd+C in a frame that also runs other keys another window sent on (a key let go there). - a.app.forwarded_frame = true; - let copy = a.frame(vec![Event::ModifiersChanged(Modifiers::COMMAND), Event::Copy]); - a.app.forwarded_frame = false; - a.frame(vec![Event::ModifiersChanged(Modifiers::NONE)]); - assert_eq!(copied(©), [length], "Cmd+C copies the selected value with other keys sent on"); - assert!(a.app.session.clipboard.is_empty(), "no art copied"); - // A Copy another window sends on: a floating panel's window whose label copied its text copies - // nothing more. From any other window (a floating document's), the art is copied, not the text - // still selected in this window's panel. - let forward = |a: &mut App| { - crate::panel_docking::forward_keys(&mut a.app, &a.ctx, vec![Event::Copy], Some(Modifiers::COMMAND)); - a.app.forwarded_frame = !a.app.forwarded.is_empty(); - a.app.forwarded_clipboard = a.app.forwarded_frame; - let events = std::iter::once(Event::ModifiersChanged(Modifiers::COMMAND)).chain(std::mem::take(&mut a.app.forwarded)).collect(); - let out = a.frame(events); - (a.app.forwarded_frame, a.app.forwarded_clipboard) = (false, false); - (out, a.frame(vec![Event::ModifiersChanged(Modifiers::NONE)])) - }; - crate::shortcuts::note_copied_in_window(&a.ctx); - let (copy, after) = forward(&mut a); - assert!(copied(©).is_empty() && copied(&after).is_empty(), "the window copied its own text"); - assert!(a.app.session.clipboard.is_empty(), "no art copied"); - let (copy, _) = forward(&mut a); - assert!(copied(©).iter().all(|t| t != length), "{length} was copied from the other window"); - assert!(!a.app.session.clipboard.is_empty(), "Copy from the other window copied the art"); // Tab hides the panels: the selected value is not drawn, so Copy copies the art, a frame later, // even when the window has lost the keyboard by then. select(&mut a, &out); @@ -387,14 +379,63 @@ fn copy_and_cut_copy_the_text_selected_in_a_panel_label() { assert!(copied(&after).iter().any(|t| t.starts_with(", modifiers: Modifiers| { + crate::panel_docking::forward_keys(&mut a.app, &a.ctx, keys, Some(modifiers)); + (a.frame(vec![]), a.frame(vec![])) + }; + crate::shortcuts::note_copied_in_window(&a.ctx); + let (copy, after) = forward(&mut a, vec![Event::Copy], Modifiers::COMMAND); + assert!(copied(©).is_empty() && copied(&after).is_empty(), "the window copied its own text"); + assert!(a.app.session.clipboard.is_empty(), "no art copied"); + let (copy, _) = forward(&mut a, vec![Event::Copy], Modifiers::COMMAND); + assert!(copied(©).iter().all(|t| t != length), "{length} was copied from the other window"); + assert!(!a.app.session.clipboard.is_empty(), "Copy from the other window copied the art"); + // F3 sent on from another window copies the art too. + let (copy, after) = forward(&mut a, vec![press(Key::F3, Modifiers::NONE), release(Key::F3)], Modifiers::NONE); + assert!(copied(©).iter().chain(&copied(&after)).all(|t| t != length), "{length} was copied by F3 from the other window"); + assert!(copied(©).iter().chain(&copied(&after)).any(|t| t.starts_with("