From b33892614b6c21326ec55d9cbd2eb149381ab331 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 16 Nov 2026 14:35:09 -0800 Subject: [PATCH 1/2] Type: Alt chords type special characters while editing text While the Type tool edits text on Windows and Linux, Alt+8, Alt+G, Alt+;, Alt+7, Alt+6, Alt+2, Alt+R, Alt+-, Alt+Shift+-, Alt+[, Alt+Shift+[, Alt+] and Alt+Shift+] type a bullet, copyright sign, ellipsis, pilcrow, section sign, trademark sign, registered sign, en dash, em dash and curly quotes at the caret, or in place of the selected text, as one undo step. Text that the keyboard layout or an input method types for the chord is typed as it came, and AltGr (Ctrl+Alt) keeps its own characters. On macOS, Option types these characters as before. Outside text editing the chords type nothing. --- crates/ui-egui/src/shortcuts/tests.rs | 148 +++++++++++++++++++++++++ crates/ui-egui/src/shortcuts/typing.rs | 97 +++++++++++++++- docs/target-app-parity.md | 1 + 3 files changed, 245 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/shortcuts/tests.rs b/crates/ui-egui/src/shortcuts/tests.rs index a24e598ecc..d0c2ed3e61 100644 --- a/crates/ui-egui/src/shortcuts/tests.rs +++ b/crates/ui-egui/src/shortcuts/tests.rs @@ -223,6 +223,154 @@ fn ctrl_alt_chords_without_typed_text_still_run() { assert_eq!(text_and_layers(&mut app, id), ("Hi".into(), layers + 1), "no layer is added until OK"); } +/// A document with the type object "Hi", edited with the Type tool and the caret after "Hi". +fn editing_hi() -> (VectorcraftApp, u64) { + let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); + app.session.execute("file.new", &json!({"width": 200, "height": 200})).unwrap(); + let id = app.session.execute("text.create", &json!({"x": 20, "y": 50, "text": "Hi"})).unwrap()["id"].as_u64().unwrap(); + app.select_tool("type"); + app.session.apply_actions(vec![vectorcraft_tools::Action::Notify("text.editNew".into())]).unwrap(); + app.session.set_tool_option("select", &json!({"start": 2, "end": 2})); + assert!(app.session.tool_wants_text()); + (app, id) +} + +/// The Alt chords that type a character while text is edited on Windows and Linux: the key, Shift, +/// the text the key types without Alt, and the character. +const ALT_CHARACTER_CHORDS: [(Key, bool, &str, &str); 15] = [ + (Key::Num8, false, "8", "\u{2022}"), + (Key::G, false, "g", "\u{00A9}"), + (Key::Semicolon, false, ";", "\u{2026}"), + (Key::Num7, false, "7", "\u{00B6}"), + (Key::Num6, false, "6", "\u{00A7}"), + (Key::Num2, false, "2", "\u{2122}"), + (Key::R, false, "r", "\u{00AE}"), + (Key::Minus, true, "_", "\u{2014}"), + (Key::Minus, false, "-", "\u{2013}"), + (Key::OpenBracket, false, "[", "\u{201C}"), + (Key::OpenBracket, true, "{", "\u{201D}"), + (Key::OpenCurlyBracket, true, "{", "\u{201D}"), + (Key::CloseBracket, false, "]", "\u{2018}"), + (Key::CloseBracket, true, "}", "\u{2019}"), + (Key::CloseCurlyBracket, true, "}", "\u{2019}"), +]; + +/// An Alt press of `key`, with Shift when `shift`, followed by `typed` when given. +fn alt_press(key: Key, shift: bool, typed: Option<&str>) -> Vec { + let m = if shift { Modifiers::ALT | Modifiers::SHIFT } else { Modifiers::ALT }; + let mut v = press(key, m); + v.extend(typed.map(|t| egui::Event::Text(t.into()))); + v +} + +/// While the Type tool edits text on Windows and Linux, each Alt chord types its character at the +/// caret as one undo step, whether or not the key's own character comes with the press, before it +/// (the web build) or after it, and Undo takes it out. +#[test] +fn alt_chords_type_special_characters_while_editing_text() { + if crate::keymap::platform().is_mac() { + return; + } + let mut wrong = vec![]; + for (key, shift, own, character) in ALT_CHARACTER_CHORDS { + for (typed, text_first) in [(None, false), (Some(own), false), (Some(own.to_uppercase().as_str()), false), (Some(own), true)] { + let (mut app, id) = editing_hi(); + let mut events = alt_press(key, shift, typed); + if text_first { + events.swap(1, 2); + } + frame(&mut app, events); + let case = format!("{key:?} shift={shift} text={typed:?} text first={text_first}"); + let text = text_and_layers(&mut app, id).0; + if text != format!("Hi{character}") { + wrong.push(format!("{case}: text = {text:?}")); + continue; + } + app.session.execute("edit.undo", &json!({})).unwrap(); + let undone = text_and_layers(&mut app, id).0; + if undone != "Hi" { + wrong.push(format!("{case}: after Undo = {undone:?}")); + } + } + } + assert!(wrong.is_empty(), "{wrong:#?}"); +} + +/// An Alt chord replaces the selected text, and a held chord types its character once per press. +#[test] +fn alt_chord_replaces_selected_text_and_repeats() { + if crate::keymap::platform().is_mac() { + return; + } + let (mut app, id) = editing_hi(); + app.session.set_tool_option("select", &json!({"start": 0, "end": 2})); + frame(&mut app, alt_press(Key::G, false, Some("g"))); + assert_eq!(text_and_layers(&mut app, id).0, "\u{00A9}"); + let mut held = alt_press(Key::Num8, false, Some("8")); + held.push(egui::Event::Key { key: Key::Num8, physical_key: Some(Key::Num8), pressed: true, repeat: true, modifiers: Modifiers::ALT }); + held.push(egui::Event::Text("8".into())); + frame(&mut app, held); + assert_eq!(text_and_layers(&mut app, id).0, "\u{00A9}\u{2022}\u{2022}"); +} + +/// Text the keyboard layout or an input method types with Alt (Option on a Mac) is typed once, as +/// it came, and AltGr (Ctrl+Alt) types its own character: no chord character is added. A chord +/// after an input method's text still types its character on Windows and Linux. +#[test] +fn alt_chord_text_from_the_layout_is_typed_once() { + let bullet = if crate::keymap::platform().is_mac() { "" } else { "\u{2022}" }; + let mut ime_commit = alt_press(Key::G, false, None); + ime_commit.push(egui::Event::Ime(egui::ImeEvent::Commit("\u{011F}".into()))); + let mut ime_commit_after_release = alt_press(Key::R, false, None); + ime_commit_after_release.push(egui::Event::ModifiersChanged(Modifiers::NONE)); + ime_commit_after_release.push(egui::Event::Ime(egui::ImeEvent::Commit("\u{0159}".into()))); + let commit_then_chord = vec![ + egui::Event::Ime(egui::ImeEvent::Commit("x".into())), + egui::Event::Key { key: Key::Num8, physical_key: Some(Key::Num8), pressed: true, repeat: false, modifiers: Modifiers::ALT }, + ]; + for (events, expected) in [ + (ime_commit, "Hi\u{011F}"), + (ime_commit_after_release, "Hi\u{0159}"), + (commit_then_chord, format!("Hix{bullet}").as_str()), + (alt_press(Key::G, false, Some("\u{00A9}")), "Hi\u{00A9}"), + (alt_press(Key::G, false, Some("\u{011F}")), "Hi\u{011F}"), + ( + vec![ + egui::Event::ModifiersChanged(Modifiers::ALT), + egui::Event::Text("\u{00A9}".into()), + egui::Event::Key { key: Key::G, physical_key: Some(Key::G), pressed: true, repeat: false, modifiers: Modifiers::ALT }, + ], + "Hi\u{00A9}", + ), + (alt_press(Key::Num8, false, Some("ab")), "Hiab"), + (ctrl_alt_press(Key::Num8, Some("[")), "Hi["), + (ctrl_alt_press(Key::G, Some("g")), "Hig"), + ] { + let (mut app, id) = editing_hi(); + frame(&mut app, events.clone()); + assert_eq!(text_and_layers(&mut app, id).0, expected, "{events:?}"); + } +} + +/// Outside text editing the Alt chords type nothing: with the Selection tool, or the Type tool +/// not editing, the type object keeps its text. +#[test] +fn alt_chords_outside_text_editing_type_nothing() { + for tool in ["selection", "type"] { + let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); + app.session.execute("file.new", &json!({"width": 200, "height": 200})).unwrap(); + let id = app.session.execute("text.create", &json!({"x": 20, "y": 50, "text": "Hi"})).unwrap()["id"].as_u64().unwrap(); + app.select_tool(tool); + app.session.execute("select.set", &json!({"ids": [id]})).unwrap(); + assert!(!app.session.tool_wants_text(), "{tool}"); + for (key, shift, own, _) in ALT_CHARACTER_CHORDS { + frame(&mut app, alt_press(key, shift, Some(own))); + frame(&mut app, alt_press(key, shift, None)); + } + assert_eq!(text_and_layers(&mut app, id).0, "Hi", "{tool}"); + } +} + /// Cmd as egui reports it on this platform: the Command key on macOS, Ctrl elsewhere. fn cmd() -> Modifiers { if crate::keymap::platform().is_mac() { Modifiers::MAC_CMD | Modifiers::COMMAND } else { Modifiers::CTRL | Modifiers::COMMAND } diff --git a/crates/ui-egui/src/shortcuts/typing.rs b/crates/ui-egui/src/shortcuts/typing.rs index 2befac02c3..8471f69c36 100644 --- a/crates/ui-egui/src/shortcuts/typing.rs +++ b/crates/ui-egui/src/shortcuts/typing.rs @@ -1,4 +1,4 @@ -use super::{EngineResult, Key, Mods, SurroundingUnit, ToolKey, UiRequest, VectorcraftApp, ViewInfo, chord_runs_a_command}; +use super::{EngineResult, Key, Mods, SurroundingUnit, ToolKey, UiRequest, VectorcraftApp, ViewInfo, chord_runs_a_command, typing_presses}; use crate::panels::character::TextInput; /// Drop each key press made with Ctrl and Alt held that is followed by the text it typed. Firefox @@ -38,6 +38,97 @@ pub(super) fn is_shortcut_chord(m: egui::Modifiers) -> bool { (m.command || m.ctrl) && !m.alt } +/// The character an Alt chord types while the Type tool edits text on Windows and Linux, with the +/// characters its key types without Alt: Alt+8 •, Alt+G ©, Alt+; …, Alt+7 ¶, Alt+6 §, Alt+2 ™, +/// Alt+R ®, Alt+- en dash, Alt+Shift+- em dash, Alt+[ “, Alt+Shift+[ ”, Alt+] ‘ and Alt+Shift+] ’. +/// A chord with Ctrl or Cmd held, AltGr included, types none of them. +fn alt_chord_character(key: Key, m: egui::Modifiers) -> Option<(char, &'static str)> { + if !m.alt || m.ctrl || m.command || m.mac_cmd { + return None; + } + Some(match (key, m.shift) { + (Key::Num8, false) => ('\u{2022}', "8"), + (Key::G, false) => ('\u{00A9}', "gG"), + (Key::Semicolon, false) => ('\u{2026}', ";"), + (Key::Num7, false) => ('\u{00B6}', "7"), + (Key::Num6, false) => ('\u{00A7}', "6"), + (Key::Num2, false) => ('\u{2122}', "2"), + (Key::R, false) => ('\u{00AE}', "rR"), + (Key::Minus, false) => ('\u{2013}', "-"), + (Key::Minus, true) => ('\u{2014}', "_"), + (Key::OpenBracket, false) => ('\u{201C}', "["), + (Key::OpenBracket | Key::OpenCurlyBracket, true) => ('\u{201D}', "{"), + (Key::CloseBracket, false) => ('\u{2018}', "]"), + (Key::CloseBracket | Key::CloseCurlyBracket, true) => ('\u{2019}', "}"), + _ => return None, + }) +} + +/// Turn each Alt chord of [`alt_chord_character`] into the text of its character. Windows and Linux +/// send the key's own character with the press (Alt+G is a G press and the text "g"); that text is +/// replaced. Any other text that the press typed is what the keyboard layout typed, and it is typed +/// as it came. A press that typed no text gets its character added after it. A text's press is the +/// one [`typing_presses`] pairs it with: the press before it, or in the web build the press after it. +/// An input method's committed or marked text belongs to the last press before it, past modifier +/// changes and key releases; that press is left as it came, since the input method's text is what +/// the key typed. +fn type_alt_chords(events: &mut Vec) { + let chord = |e: &egui::Event| match e { + egui::Event::Key { key, pressed: true, modifiers, .. } => alt_chord_character(*key, *modifiers), + _ => None, + }; + if !events.iter().any(|e| chord(e).is_some()) { + return; + } + let ime_text = |e: &egui::Event| match e { + egui::Event::Ime(egui::ImeEvent::Commit(t) | egui::ImeEvent::Preedit { text: t, .. }) => !t.is_empty(), + _ => false, + }; + // The press an input method's text at `j` belongs to. + let ime_press = |j: usize| { + events + .iter() + .enumerate() + .take(j) + .rev() + .find(|(_, e)| !matches!(e, egui::Event::ModifiersChanged(_) | egui::Event::Key { pressed: false, .. })) + .filter(|(_, e)| matches!(e, egui::Event::Key { pressed: true, .. })) + .map(|(k, _)| k) + }; + let pressed_by = typing_presses(events); + let mut typed = vec![false; events.len()]; + let ime_presses = events.iter().enumerate().filter(|(_, e)| ime_text(e)).map(|(j, _)| ime_press(j)); + for k in pressed_by.iter().copied().chain(ime_presses).flatten() { + if let Some(t) = typed.get_mut(k) { + *t = true; + } + } + let chords: Vec<_> = events.iter().map(chord).collect(); + let mut out = Vec::with_capacity(events.len() + 1); + for (j, e) in std::mem::take(events).into_iter().enumerate() { + match e { + egui::Event::Text(t) => { + let press = pressed_by.get(j).copied().flatten().and_then(|k| chords.get(k).copied().flatten()); + match press { + Some((c, own)) if t.is_empty() || (t.chars().count() == 1 && own.contains(t.as_str())) => { + out.push(egui::Event::Text(c.to_string())); + } + _ => out.push(egui::Event::Text(t)), + } + } + e => { + out.push(e); + if let Some((c, _)) = chords.get(j).copied().flatten() + && !typed.get(j).copied().unwrap_or(false) + { + out.push(egui::Event::Text(c.to_string())); + } + } + } + } + *events = out; +} + enum Seq { Text(String), Preedit(String, Option>), @@ -154,6 +245,10 @@ fn type_sequence(ctx: &egui::Context) -> Vec { let mut edits = vec![]; let zooming = crate::canvas::dragging_zoom(ctx); ctx.input_mut(|i| { + // On a Mac, Option types these characters itself. + if !crate::keymap::platform().is_mac() { + type_alt_chords(&mut i.events); + } // Move the events out instead of cloning their strings: a keyboard hook that reinjects a // batch keeps the batch in `seq` only, not in both the input list and a second copy. let mut kept = Vec::with_capacity(i.events.len()); diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index f506da06e1..75c18d8b03 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1132,6 +1132,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur - With the Selection tool, the Character and Paragraph panels work out the list of selected stories once, not once for each of the 24 fields they show by default, and reuse it until the document, its revision or the selected objects change. The panels show the same values. With 200 threads of 10 frames and 2,000 point type objects selected, one frame of both panels takes 21.7 ms of CPU time in a release build instead of 81.7 ms (`panels::character::tests::kept_selected_stories_follow_the_document_and_the_selection`, `panels_show_the_same_with_the_kept_selected_stories` and `panels_work_out_the_selected_stories_once_per_frame`). - Switching from a Type tool to the Eyedropper keeps a whole story selected by Select All: a pick applies to the text of every frame as one undo step, and switching back restores the story selection (VectorCraft's own behavior; `textedit::switching_to_the_eyedropper_keeps_the_whole_story_selected`). - While the Type tool edits text, a character typed with AltGr is typed and runs no command. Firefox on Windows reports AltGr as Ctrl and Alt held, so in the web build AltGr+L (ł on a Polish layout) does not also open Layer Options for a new layer, and AltGr+Q (@ on a German layout) does not also run Reset Tracking and Kerning. A Ctrl+Alt chord that types no character runs its command. + - While the Type tool edits text on Windows and Linux, Alt chords type special characters at the caret, or in place of the selected text, as one undo step: Alt+8 •, Alt+G ©, Alt+; …, Alt+7 ¶, Alt+6 §, Alt+2 ™, Alt+R ®, Alt+- en dash, Alt+Shift+- em dash, Alt+[ “, Alt+Shift+[ ”, Alt+] ‘ and Alt+Shift+] ’. The Work with type table of [Default keyboard shortcuts](https://web.archive.org/web/20260726060015/https://helpx.adobe.com/illustrator/using/default-keyboard-shortcuts.html) gives these chords for Windows. The chords on Linux, replacing a selection, and typing a character the keyboard layout itself sends with Alt as it came are VectorCraft's own rules. On macOS, Option types these characters as before. Outside text editing the chords type nothing (`shortcuts::tests::alt_chords_type_special_characters_while_editing_text`, `alt_chord_replaces_selected_text_and_repeats`, `alt_chord_text_from_the_layout_is_typed_once`, `alt_chords_outside_text_editing_type_nothing`). - On a layout without Latin letters, a one-key shortcut goes by the position of the key that typed the character, in the web build too, where the character arrives before its key press: on a Russian layout the P key (`з`) chooses the Pen, and two keys pressed in one frame each run their own shortcut. - With the Artboard tool, Cmd/Ctrl+A after a click on a row of the Artboards panel selects every artboard, as the macOS menu bar's Select All does then; the active artboard stays active, and the art's selection does not change. After a press outside the panel's list, Cmd/Ctrl+A selects every object again. It used to select every object after a click in the panel too. Public reference: [Select artboards](https://helpx.adobe.com/illustrator/desktop/create-manage-artboards/add-edit-artboards/select-artboards.html). - Cmd/Ctrl+/ (`appearance.addFill`) and Cmd/Ctrl+Alt+/ (`appearance.addStroke`) add a new fill and a new stroke to the selected objects, as the Appearance panel's Add New Fill and Add New Stroke buttons do, on the keys the reference app's [Default keyboard shortcuts](https://web.archive.org/web/20260726060015/https://helpx.adobe.com/illustrator/using/default-keyboard-shortcuts.html) page lists for adding a new fill and a new stroke. Keyboard Shortcuts lists both under Window › Appearance. From 7d5b4c18cceb2f55b956191448112c130fc020d5 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 16 Nov 2026 16:11:17 -0800 Subject: [PATCH 2/2] Type: an input method's text after an Alt chord is typed once on Windows Windows sends an empty pre-edit before each input method commit, and an input method can delete the text around the caret before it commits. An Alt chord press followed by either of these and then the commit was not paired with the commit, so the chord's character was typed before the committed text. Both events now sit between the press and its text, and the test covers them. --- crates/ui-egui/src/shortcuts/tests.rs | 9 +++++++++ crates/ui-egui/src/shortcuts/typing.rs | 13 ++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/shortcuts/tests.rs b/crates/ui-egui/src/shortcuts/tests.rs index d0c2ed3e61..4fad102f73 100644 --- a/crates/ui-egui/src/shortcuts/tests.rs +++ b/crates/ui-egui/src/shortcuts/tests.rs @@ -324,6 +324,14 @@ fn alt_chord_text_from_the_layout_is_typed_once() { let mut ime_commit_after_release = alt_press(Key::R, false, None); ime_commit_after_release.push(egui::Event::ModifiersChanged(Modifiers::NONE)); ime_commit_after_release.push(egui::Event::Ime(egui::ImeEvent::Commit("\u{0159}".into()))); + // Windows sends an empty pre-edit before each commit, and an input method can delete the text + // around the caret between the press and its text. + let mut ime_commit_after_empty_preedit = alt_press(Key::G, false, None); + ime_commit_after_empty_preedit.extend([ + egui::Event::Ime(egui::ImeEvent::DeleteSurrounding { before_chars: 0, after_chars: 0 }), + egui::Event::Ime(egui::ImeEvent::Preedit { text: String::new(), active_range_chars: None }), + egui::Event::Ime(egui::ImeEvent::Commit("\u{011F}".into())), + ]); let commit_then_chord = vec![ egui::Event::Ime(egui::ImeEvent::Commit("x".into())), egui::Event::Key { key: Key::Num8, physical_key: Some(Key::Num8), pressed: true, repeat: false, modifiers: Modifiers::ALT }, @@ -331,6 +339,7 @@ fn alt_chord_text_from_the_layout_is_typed_once() { for (events, expected) in [ (ime_commit, "Hi\u{011F}"), (ime_commit_after_release, "Hi\u{0159}"), + (ime_commit_after_empty_preedit, "Hi\u{011F}"), (commit_then_chord, format!("Hix{bullet}").as_str()), (alt_press(Key::G, false, Some("\u{00A9}")), "Hi\u{00A9}"), (alt_press(Key::G, false, Some("\u{011F}")), "Hi\u{011F}"), diff --git a/crates/ui-egui/src/shortcuts/typing.rs b/crates/ui-egui/src/shortcuts/typing.rs index 8471f69c36..94a5817d9e 100644 --- a/crates/ui-egui/src/shortcuts/typing.rs +++ b/crates/ui-egui/src/shortcuts/typing.rs @@ -70,8 +70,7 @@ fn alt_chord_character(key: Key, m: egui::Modifiers) -> Option<(char, &'static s /// as it came. A press that typed no text gets its character added after it. A text's press is the /// one [`typing_presses`] pairs it with: the press before it, or in the web build the press after it. /// An input method's committed or marked text belongs to the last press before it, past modifier -/// changes and key releases; that press is left as it came, since the input method's text is what -/// the key typed. +/// changes, key releases, empty pre-edits and deletions around the caret; that press is left as it came, since the input method's text is what the key typed. fn type_alt_chords(events: &mut Vec) { let chord = |e: &egui::Event| match e { egui::Event::Key { key, pressed: true, modifiers, .. } => alt_chord_character(*key, *modifiers), @@ -84,6 +83,14 @@ fn type_alt_chords(events: &mut Vec) { egui::Event::Ime(egui::ImeEvent::Commit(t) | egui::ImeEvent::Preedit { text: t, .. }) => !t.is_empty(), _ => false, }; + // Events between a press and the input method's text it typed. Windows sends an empty pre-edit + // before each commit, and an input method can delete the text around the caret first. + let between = |e: &egui::Event| match e { + egui::Event::ModifiersChanged(_) | egui::Event::Key { pressed: false, .. } => true, + egui::Event::Ime(egui::ImeEvent::Preedit { text, .. }) => text.is_empty(), + egui::Event::Ime(egui::ImeEvent::DeleteSurrounding { .. } | egui::ImeEvent::DeleteSurroundingBytes { .. }) => true, + _ => false, + }; // The press an input method's text at `j` belongs to. let ime_press = |j: usize| { events @@ -91,7 +98,7 @@ fn type_alt_chords(events: &mut Vec) { .enumerate() .take(j) .rev() - .find(|(_, e)| !matches!(e, egui::Event::ModifiersChanged(_) | egui::Event::Key { pressed: false, .. })) + .find(|(_, e)| !between(e)) .filter(|(_, e)| matches!(e, egui::Event::Key { pressed: true, .. })) .map(|(k, _)| k) };