mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 19:11:26 +00:00
Part of #8432: a Copy or Cut left to the labels keeps its selection
- A Copy or Cut that runs a pass later because no label copied the text acts on the selection it was given. A selection made after it in the same frame (a Select All) is restored afterwards, without the objects a Cut removed and with the pieces it left of a path it split. With another document active by then, it does not run. - The art text that such a Copy or Cut copies at the start of a pass is no longer taken for a label's copy, so a Cut pressed in the next frame is not dropped.
This commit is contained in:
@@ -3,8 +3,9 @@ use super::VectorcraftApp;
|
||||
impl VectorcraftApp {
|
||||
/// Hand a queued text copy to egui. 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.
|
||||
pub(super) fn flush_clipboard_out(&mut self, ctx: &egui::Context) {
|
||||
/// `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`]).
|
||||
pub(crate) fn flush_clipboard_out(&mut self, ctx: &egui::Context) {
|
||||
if let Some(t) = self.clipboard_out.take() {
|
||||
ctx.copy_text(t);
|
||||
}
|
||||
|
||||
@@ -121,7 +121,7 @@ pub fn invoke_from_system_menu(app: &mut VectorcraftApp, ctx: &egui::Context, id
|
||||
};
|
||||
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)) => {
|
||||
None if label_event.as_ref().is_some_and(|(id, _)| crate::shortcuts::leave_to_labels(app, ctx, id)) => {
|
||||
if let Some((_, e)) = label_event {
|
||||
ctx.input_mut(|i| i.events.push(e));
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ pub(crate) use hold_keys::HoldKeys;
|
||||
#[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 labels::{left_to_labels, note_copies_before_labels, run_after_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;
|
||||
@@ -201,11 +201,13 @@ pub fn handle(app: &mut VectorcraftApp, ctx: &egui::Context) {
|
||||
}
|
||||
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.
|
||||
// frame's keys do. The text it copies goes out now, so it is not taken for a label's copy.
|
||||
watch_label_copies(ctx);
|
||||
if let Some(id) = take_command_after_labels(ctx) {
|
||||
crate::menus::invoke(app, id, json!({}));
|
||||
if let Some(c) = take_command_after_labels(ctx) {
|
||||
run_after_labels(app, c);
|
||||
app.flush_clipboard_out(ctx);
|
||||
}
|
||||
note_copies_before_labels(ctx);
|
||||
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;
|
||||
@@ -844,8 +846,9 @@ fn handle_clipboard(app: &mut VectorcraftApp, ctx: &egui::Context, textless_past
|
||||
// here does nothing more.
|
||||
let mut clip = vec![];
|
||||
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 to_labels = |e: egui::Event, id| {
|
||||
ctx.input(|i| i.events.contains(&e)) && (left_to_labels(ctx) == Some(id) || (!forwarded && leave_to_labels(app, 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.
|
||||
@@ -890,7 +893,7 @@ fn clipboard_key_to_labels(app: &mut VectorcraftApp, ctx: &egui::Context, id: &s
|
||||
"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) {
|
||||
if ctx.viewport_id() == egui::ViewportId::ROOT && forwarded_key_runs(app, command) || !leave_to_labels(app, ctx, command) {
|
||||
return false;
|
||||
}
|
||||
ctx.input_mut(|i| i.events.push(event));
|
||||
|
||||
@@ -1,10 +1,39 @@
|
||||
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)]
|
||||
pub(super) 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 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())
|
||||
}
|
||||
|
||||
/// Note the texts copied in this pass so far (the art's, by a Copy or Cut that labels left to this
|
||||
/// pass): a copy at the end of the pass beyond them is the labels'.
|
||||
pub(super) fn note_copies_before_labels(ctx: &egui::Context) {
|
||||
let (id, n) = (copies_before_labels_id(ctx.viewport_id()), copies(ctx));
|
||||
ctx.data_mut(|d| d.insert_temp(id, n));
|
||||
}
|
||||
|
||||
/// 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")
|
||||
@@ -29,26 +58,70 @@ pub(super) fn watch_label_copies(ctx: &egui::Context) {
|
||||
/// 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 {
|
||||
/// not drawn), `command` runs in the next pass ([`take_command_after_labels`]), on the selection of
|
||||
/// `app`'s active document now ([`run_after_labels`]).
|
||||
pub(crate) fn leave_to_labels(app: &VectorcraftApp, 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::<CopyAfterLabels>().is_none() {
|
||||
return false;
|
||||
}
|
||||
let id = waiting_id(ctx.viewport_id());
|
||||
ctx.data_mut(|d| d.insert_temp(id, command));
|
||||
let given = active_selection(app);
|
||||
ctx.data_mut(|d| d.insert_temp(id, LeftToLabels { id: command, given }));
|
||||
true
|
||||
}
|
||||
|
||||
/// 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()))
|
||||
}
|
||||
|
||||
/// 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))
|
||||
ctx.data(|d| d.get_temp::<LeftToLabels>(id)).map(|c| c.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()))
|
||||
pub(super) fn take_command_after_labels(ctx: &egui::Context) -> Option<LeftToLabels> {
|
||||
ctx.data_mut(|d| d.remove_temp::<LeftToLabels>(after_labels_id()))
|
||||
}
|
||||
|
||||
/// 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.
|
||||
pub(super) fn run_after_labels(app: &mut VectorcraftApp, c: LeftToLabels) {
|
||||
let Some((doc, given)) = c.given else {
|
||||
crate::menus::invoke(app, c.id, json!({}));
|
||||
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<NodeId> = 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!({})),
|
||||
}
|
||||
}
|
||||
|
||||
/// Note that a floating panel's window copied its label text, as [`CopyAfterLabels`] does.
|
||||
@@ -82,7 +155,10 @@ impl egui::Plugin for 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(_))));
|
||||
// The art a Copy or Cut copied earlier in this pass is not the labels' copy.
|
||||
let before_id = copies_before_labels_id(ctx.viewport_id());
|
||||
let before = ctx.data_mut(|d| d.remove_temp::<usize>(before_id)).unwrap_or(0);
|
||||
let copied = copies(ctx) > before;
|
||||
// A field with the keyboard copies its own text, and its window sends nothing on.
|
||||
if ctx.viewport_id() != egui::ViewportId::ROOT
|
||||
&& copied
|
||||
@@ -92,7 +168,7 @@ impl egui::Plugin for CopyAfterLabels {
|
||||
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 {
|
||||
let Some(command) = ctx.data_mut(|d| d.remove_temp::<LeftToLabels>(id)) else {
|
||||
return;
|
||||
};
|
||||
if !copied {
|
||||
|
||||
@@ -379,6 +379,84 @@ fn copy_and_cut_copy_the_text_selected_in_a_panel_label() {
|
||||
assert!(copied(&after).iter().any(|t| t.starts_with("<?xml")), "Copy copied the art");
|
||||
}
|
||||
|
||||
/// Drag across a label in a window of its own (a floating panel's), selecting its text there. The
|
||||
/// main window's labels then copy nothing on Copy or Cut, which run on the art a pass later.
|
||||
fn select_label_text_in_another_window(a: &mut App) {
|
||||
let window = egui::ViewportId::from_hash_of("floating panel");
|
||||
let rect = std::cell::Cell::new(Rect::NOTHING);
|
||||
let pass = |a: &mut App, events: Vec<Event>| {
|
||||
a.time += 0.1;
|
||||
let screen = Rect::from_min_size(Pos2::ZERO, vec2(400.0, 200.0));
|
||||
let mut input = egui::RawInput { viewport_id: window, events, time: Some(a.time), screen_rect: Some(screen), ..Default::default() };
|
||||
input.viewports.insert(window, egui::ViewportInfo { parent: Some(egui::ViewportId::ROOT), ..Default::default() });
|
||||
a.ctx.run_ui(input, |ui| rect.set(ui.label("234 pt").rect)).drop_without_applying_deltas();
|
||||
};
|
||||
pass(a, vec![]);
|
||||
let (from, to) = (rect.get().left_center() + vec2(1.0, 0.0), rect.get().right_center() - vec2(1.0, 0.0));
|
||||
pass(a, vec![Event::PointerMoved(from)]);
|
||||
pass(a, vec![click(from, true)]);
|
||||
for k in 1..=4 {
|
||||
pass(a, vec![Event::PointerMoved(from + (to - from) * k as f32 / 4.0)]);
|
||||
}
|
||||
pass(a, vec![click(to, false)]);
|
||||
pass(a, vec![]);
|
||||
}
|
||||
|
||||
/// The objects of the first layer of the active document.
|
||||
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'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();
|
||||
select_label_text_in_another_window(&mut a);
|
||||
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(©).is_empty(), "no label of the main window copied the text");
|
||||
assert!(copied(&cut).iter().any(|t| t.starts_with("<?xml")), "Copy copied the art a frame later");
|
||||
assert_eq!(objects(&a), 1, "Cut cut the selected object a frame later");
|
||||
}
|
||||
|
||||
/// #8432: with text selected in another window's label, a Cut that runs a pass later cuts the
|
||||
/// object selected when it was pressed, not the objects a Select All in the same frame selected.
|
||||
/// The objects Select All selected stay selected after it.
|
||||
#[test]
|
||||
fn a_cut_left_to_the_labels_cuts_the_selection_it_was_given() {
|
||||
let mut a = App::new();
|
||||
select_label_text_in_another_window(&mut a);
|
||||
assert_eq!(a.selected(), 1);
|
||||
a.frame(vec![Event::ModifiersChanged(Modifiers::COMMAND), Event::Cut, press(Key::A, Modifiers::COMMAND)]);
|
||||
a.frame(vec![Event::ModifiersChanged(Modifiers::NONE)]);
|
||||
assert_eq!(objects(&a), 1, "Cut cut only the object selected when it was pressed");
|
||||
assert_eq!(a.selected(), 1, "the other object Select All selected stays selected");
|
||||
}
|
||||
|
||||
/// #8432: as above, with two opposite sides of a rectangle direct-selected: the Cut leaves the other
|
||||
/// two sides as separate paths, which join the objects Select All selected.
|
||||
#[test]
|
||||
fn a_cut_left_to_the_labels_keeps_the_pieces_of_a_split_path_selected() {
|
||||
let mut a = App::new();
|
||||
let rect = a.app.session.active().unwrap().selection.objects[0];
|
||||
a.app
|
||||
.session
|
||||
.select(|_, sel| {
|
||||
sel.set([rect]);
|
||||
sel.anchors.insert(rect, [(0, 0), (0, 1), (0, 2), (0, 3)].into());
|
||||
sel.segments.insert(rect, [(0, 0), (0, 2)].into());
|
||||
})
|
||||
.unwrap();
|
||||
select_label_text_in_another_window(&mut a);
|
||||
a.frame(vec![Event::ModifiersChanged(Modifiers::COMMAND), Event::Cut, press(Key::A, Modifiers::COMMAND)]);
|
||||
a.frame(vec![Event::ModifiersChanged(Modifiers::NONE)]);
|
||||
assert_eq!(objects(&a), 3, "two sides of the rectangle and the other rectangle remain");
|
||||
assert_eq!(a.selected(), 3, "all three are selected after Select All");
|
||||
}
|
||||
|
||||
/// #8432: with a Document Info value's text selected, the keys another window sends on (through
|
||||
/// the host's raw input hook) copy that text only when they are a Copy of the main window's own or
|
||||
/// that window's labels copied it; Copy and F3 from a floating document's window copy the art.
|
||||
|
||||
Reference in New Issue
Block a user