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("