diff --git a/crates/ui-egui/src/app/input.rs b/crates/ui-egui/src/app/input.rs index 36f773d85b..cf01577c99 100644 --- a/crates/ui-egui/src/app/input.rs +++ b/crates/ui-egui/src/app/input.rs @@ -1,15 +1,19 @@ use super::{VectorcraftApp, menus, shortcuts}; impl VectorcraftApp { - /// Each pass, before the UI: the rich clipboard formats a paste into a text field must find - /// written, and the clicks of the native menu bar. egui gives a frame's events to its first - /// pass only, so a second pass of the frame finds no new paste or click and repeats nothing. + /// Each pass, before the UI: the Copy or Cut the last pass left to labels that copied nothing, + /// the rich clipboard formats a paste into a text field must find written, the clicks of the + /// native menu bar, and a Copy or Cut this pass leaves to the labels. egui gives a frame's + /// events to its first pass only, so a second pass of the frame finds no new paste or click + /// and repeats nothing. pub(super) fn handle_input(&mut self, ctx: &egui::Context) { + shortcuts::run_left_by_labels(self, ctx); self.write_formats_before_text_input(ctx); menus::native::run(self, ctx); // A Copy taken by the native menu is published in this same pass: an idle web host may // have no further frame to run it in. self.flush_clipboard_out(); + shortcuts::leave_to_labels(self, ctx); } /// Each pass, last in the UI: the window's keys that no component took. Escape ends one thing diff --git a/crates/ui-egui/src/shortcuts.rs b/crates/ui-egui/src/shortcuts.rs index e1d1f160de..127ac430e8 100644 --- a/crates/ui-egui/src/shortcuts.rs +++ b/crates/ui-egui/src/shortcuts.rs @@ -11,9 +11,11 @@ use vectorcraft_tools::ToolKey; use crate::VectorcraftApp; mod held_tool; +mod labels; mod presentation; pub(crate) use held_tool::{HeldTool, note_tool_use}; +pub(crate) use labels::{leave_to_labels, run_left_by_labels}; use presentation::escape_resets_rotation; pub(crate) use presentation::presentation_step; diff --git a/crates/ui-egui/src/shortcuts/labels.rs b/crates/ui-egui/src/shortcuts/labels.rs new file mode 100644 index 0000000000..dd24086d04 --- /dev/null +++ b/crates/ui-egui/src/shortcuts/labels.rs @@ -0,0 +1,144 @@ +//! Copy and Cut while text is selected in a label, such as a Document Info value or an Info panel readout. The key +//! stage leaves the Copy or Cut event in egui's input then, for the labels to copy their text. When no label of the +//! window copies anything in that pass (the selected text is in another window, or its label is not drawn), the +//! command runs in the next pass, on the selection it was given. + +use craft::ui::runtime::windows::WindowHost; +use egui::text_selection::LabelSelectionState; +use serde_json::json; +use vectorcraft_doc::{NodeId, Selection}; + +use crate::VectorcraftApp; + +/// A Copy or Cut command left to the labels, with the active document (its `uid`) and the +/// selection it was given then. +#[derive(Clone, Default)] +struct LeftToLabels { + id: &'static str, + given: Option<(u64, Selection)>, +} + +/// 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)) +} + +/// How many texts viewport `viewport` had copied in this pass before its labels could copy theirs. +fn copies_before_labels_id(viewport: egui::ViewportId) -> egui::Id { + egui::Id::unique(("copies-before-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") +} + +/// The texts copied in this pass of the current viewport so far. +fn copies(ctx: &egui::Context) -> usize { + ctx.output(|o| o.commands.iter().filter(|c| matches!(c, egui::OutputCommand::CopyText(_))).count()) +} + +/// First in a pass: the Copy or Cut an earlier pass left to labels that copied nothing runs now, on the selection it +/// was given ([`run_after_labels`]). Watches the labels' copies from the next pass on: egui runs a plugin only from +/// the pass after the one that adds it. +pub(crate) fn run_left_by_labels(app: &mut VectorcraftApp, ctx: &egui::Context) { + if ctx.plugin_opt::().is_none() { + ctx.add_plugin(CopyAfterLabels); + } + if let Some(c) = ctx.data_mut(|d| d.remove_temp::(after_labels_id())) { + run_after_labels(app, c); + } +} + +/// After the pass's input, once the text it copied has gone out: a Copy or Cut event still in the input while text +/// is selected in a label, and no text field has the keys, is the labels'. The texts copied so far are noted, so a +/// copy at the end of the pass beyond them is the labels' ([`CopyAfterLabels`]). 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. +pub(crate) fn leave_to_labels(app: &mut VectorcraftApp, ctx: &egui::Context) { + let viewport = ctx.viewport_id(); + let n = copies(ctx); + ctx.data_mut(|d| d.insert_temp(copies_before_labels_id(viewport), n)); + let command = ctx.input(|i| { + i.events.iter().find_map(|e| match e { + egui::Event::Copy => Some("edit.copy"), + egui::Event::Cut => Some("edit.cut"), + _ => None, + }) + }); + let selected = ctx.with_plugin(|s: &mut LabelSelectionState| s.has_selection()).unwrap_or(false); + let Some(command) = command.filter(|_| selected && !WindowHost::of(ctx).typing() && ctx.plugin_opt::().is_some()) else { + return; + }; + let given = active_selection(app); + ctx.data_mut(|d| d.insert_temp(waiting_id(viewport), LeftToLabels { id: command, given })); + app.drop_unwritten_formats(); +} + +/// The `uid` of `app`'s active document and its selection. +fn active_selection(app: &VectorcraftApp) -> Option<(u64, Selection)> { + app.session.active().map(|st| (st.uid, st.selection.clone())) +} + +/// Run Copy or Cut `c`, which labels left to this pass, on the selection it was given. A selection +/// made in the same frame after it (a Select All) comes back after it, without the objects a Cut +/// removed and with the pieces it left of a path it split. With another document active by now, +/// or none, it does not run. The text it copies goes out now, so it is not taken for a label's copy. +fn run_after_labels(app: &mut VectorcraftApp, c: LeftToLabels) { + let Some((doc, given)) = c.given else { + crate::menus::invoke(app, c.id, json!({})); + app.flush_clipboard_out(); + return; + }; + match active_selection(app) { + None => {} + Some((active, _)) if active != doc => {} + Some((_, now)) if now != given => { + if let Err(e) = app.session.select(|_, sel| *sel = given.clone()) { + app.status(e.to_string()); + return; + } + let before = app.session.active().map(|st| st.doc.clone()); + crate::menus::invoke(app, c.id, json!({})); + // The pieces a Cut left of a path it split are new objects: they join the later + // selection when it held an object the Cut acted on. + let acted_on = given.objects.iter().any(|id| now.contains(*id)); + let pieces: Vec = match (before, app.session.active()) { + (Some(before), Some(st)) if acted_on => st.selection.objects.iter().copied().filter(|id| before.node(*id).is_none()).collect(), + _ => vec![], + }; + if let Err(e) = app.session.select(|_, sel| { + *sel = now; + sel.add_all(pieces); + }) { + app.status(e.to_string()); + } + } + _ => crate::menus::invoke(app, c.id, json!({})), + } + app.flush_clipboard_out(); +} + +/// 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 viewport = ctx.viewport_id(); + // The art a Copy or Cut copied earlier in this pass is not the labels' copy. + let before = ctx.data_mut(|d| d.remove_temp::(copies_before_labels_id(viewport))).unwrap_or(0); + let Some(command) = ctx.data_mut(|d| d.remove_temp::(waiting_id(viewport))) else { + return; + }; + // In a pass egui discards too: its rerun gets no input, so nothing leaves the command to the labels again. + if copies(ctx) == before { + 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 bbc433777c..cc214b08dd 100644 --- a/crates/ui-egui/src/sysclip.rs +++ b/crates/ui-egui/src/sysclip.rs @@ -332,7 +332,7 @@ impl VectorcraftApp { /// Hand a queued text copy to the system clipboard. Called after `tick`, after the UI phase's /// input and at the end of the pass; the later calls are what an input- or menu-driven fallback /// Copy needs, and `take` keeps it single-shot. A Copy or Cut that labels left to the next pass - /// hands its text over as soon as it runs ([`crate::shortcuts::handle`]). + /// hands its text over as soon as it runs ([`crate::shortcuts::run_left_by_labels`]). pub(crate) fn flush_clipboard_out(&mut self) { if let Some(text) = self.clipboard_out.take() { // Something else is being copied: the formats of an earlier Copy not written yet would replace it. diff --git a/crates/ui-egui/src/tests/selectall.rs b/crates/ui-egui/src/tests/selectall.rs index 350eaae4b0..8810452d08 100644 --- a/crates/ui-egui/src/tests/selectall.rs +++ b/crates/ui-egui/src/tests/selectall.rs @@ -409,8 +409,9 @@ fn objects(a: &App) -> usize { a.app.session.active().unwrap().doc.layers[0].children().unwrap().len() } -/// #8432: with text selected in another window's label, Copy and then Cut in the main window copy -/// the art and then cut it: that window's text is not the main window's label selection. +/// #8432: with text selected in another window's label, Copy and then Cut in the main window's next +/// frame copy the art and then cut it: the art Copy runs in the Cut's frame, and its text is not +/// taken for a label's copy of the Cut. #[test] fn copy_then_cut_with_text_selected_in_another_window_copy_and_cut_the_art() { let mut a = App::new(); @@ -418,9 +419,44 @@ fn copy_then_cut_with_text_selected_in_another_window_copy_and_cut_the_art() { let copy = a.frame(vec![Event::ModifiersChanged(Modifiers::COMMAND), Event::Copy]); let cut = a.frame(vec![Event::ModifiersChanged(Modifiers::COMMAND), Event::Cut]); a.frame(vec![Event::ModifiersChanged(Modifiers::NONE)]); - assert!(copied(©).iter().chain(&copied(&cut)).all(|t| t != "234 pt"), "the other window's text was not copied"); - assert!(copied(©).iter().chain(&copied(&cut)).any(|t| t.starts_with("