mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-17 08:26:28 +00:00
Merge pull request #8260 from dotCooCoo/feat/type-alt-chords-special-characters
Type: Alt chords type special characters while editing text
This commit is contained in:
@@ -223,6 +223,163 @@ 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<egui::Event> {
|
||||
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())));
|
||||
// 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 },
|
||||
];
|
||||
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}"),
|
||||
(
|
||||
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 }
|
||||
|
||||
@@ -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,104 @@ 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, 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<egui::Event>) {
|
||||
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,
|
||||
};
|
||||
// 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
|
||||
.iter()
|
||||
.enumerate()
|
||||
.take(j)
|
||||
.rev()
|
||||
.find(|(_, e)| !between(e))
|
||||
.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<std::ops::Range<usize>>),
|
||||
@@ -154,6 +252,10 @@ fn type_sequence(ctx: &egui::Context) -> Vec<Seq> {
|
||||
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());
|
||||
|
||||
@@ -1142,6 +1142,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur
|
||||
- While the Type tool edits text, the Character section of the Properties panel shows the font family and style and the Variable Font button of the first selected character (or of the character before the caret), as the Character panel does, not those of the text object's first run. With the Selection tool it shows the first run's, as before (#8189; `panels::properties::tests::character_section_shows_the_type_tool_selections_style`).
|
||||
- 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.
|
||||
|
||||
Reference in New Issue
Block a user