This commit is contained in:
bflatastic committed 2026-11-18 11:22:10 +07:00
commit 7a707ff65a
7 files changed
+255 -13

No files matched your search

+12 -1
View File
@@ -110,10 +110,21 @@ pub(super) fn field_event(app: &mut VectorcraftApp, id: &str) -> Option<egui::Ev
/// key presses, see [`crate::native_menu`]). While a text field has the keyboard, Select All, Cut,
/// Copy and Paste act on the field's text, as their keys do. While its keys select every artboard
/// ([`crate::panels::artboards::selects_all`]), Select All does too. Everything else (and those
/// commands with no field focused) goes to [`invoke`].
/// commands with no field focused) goes to [`invoke`]. With text selected in a label (a Document
/// Info value), Copy and Cut copy that text, as their keys do ([`crate::shortcuts::leave_to_labels`]).
pub fn invoke_from_system_menu(app: &mut VectorcraftApp, ctx: &egui::Context, id: &str, p: Value) {
let label_event = match id {
"edit.copy" => 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),
}
+7 -2
View File
@@ -1344,8 +1344,13 @@ pub(crate) fn close_overlays_for_modal(app: &mut VectorcraftApp, ctx: &egui::Con
/// 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<egui::Event>, modifiers: Option<egui::Modifiers>) {
/// 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<egui::Event>, modifiers: Option<egui::Modifiers>) {
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;
}
+35 -6
View File
@@ -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<Modifiers>) {
/// 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<Modifiers>) -> 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.
+92
View File
@@ -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::<CopyAfterLabels>().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::<CopyAfterLabels>().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::<bool>(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();
}
}
}
+9 -3
View File
@@ -343,11 +343,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;
}
+85 -1
View File
@@ -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(&copy), [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(&copy).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(&copy).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(&copy).is_empty(), "no label copied {length}");
assert!(copied(&after).iter().any(|t| t.starts_with("<?xml")), "Copy copied the art");
}
/// The centre of the text `label` painted last (in the topmost layer) in a frame's output; with
/// `title`, the first one painted (the menu bar's).
fn text_at(out: &egui::FullOutput, label: &str, title: bool) -> Pos2 {
+15
View File
@@ -579,6 +579,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.