From 692cb813ab00f006db903a8e2e734748d3ea114e Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 18 Nov 2026 01:33:08 -0800 Subject: [PATCH] Type: the toggles read typed text, and Cmd+Shift+= picks its command when it runs Superscript, Subscript and Underline apply the typing that Real-time Drawing off leaves unapplied before they read the text, so a pending Backspace counts when they decide whether to turn the format on or off. A Cmd+Shift+= press decides between Superscript and Zoom In when its command runs, after the commands of earlier presses in the same frame, so Cmd+A followed by Cmd+Shift+= applies Superscript to the text Select All selected. --- crates/ui-egui/src/panels/character.rs | 4 +++ crates/ui-egui/src/panels/character/tests.rs | 37 ++++++++++++++++++++ crates/ui-egui/src/shortcuts.rs | 19 +++++----- crates/ui-egui/src/shortcuts/tests.rs | 6 ++++ 4 files changed, 57 insertions(+), 9 deletions(-) diff --git a/crates/ui-egui/src/panels/character.rs b/crates/ui-egui/src/panels/character.rs index 3f72044ff1..1cb759fa69 100644 --- a/crates/ui-egui/src/panels/character.rs +++ b/crates/ui-egui/src/panels/character.rs @@ -540,6 +540,8 @@ pub(crate) fn highlight(app: &mut VectorcraftApp, field: Field) -> Result Result { + // The typing Real-time Drawing off left unapplied is applied before the text is read. + text_editing_kept(app)?; text_style(app).ok_or("select some type first")?; let on = shared(app, |s| s.underline) != Some(true); run_format(app, json!({"underline": on}))?; @@ -550,6 +552,8 @@ pub(crate) fn toggle_underline(app: &mut VectorcraftApp) -> Result Result { + // The typing Real-time Drawing off left unapplied is applied before the text is read. + text_editing_kept(app)?; text_style(app).ok_or("select some type first")?; let change = position_toggle(shared_position(app), position); let to = change["position"].clone(); diff --git a/crates/ui-egui/src/panels/character/tests.rs b/crates/ui-egui/src/panels/character/tests.rs index 7d74e5c022..c6e43871ad 100644 --- a/crates/ui-egui/src/panels/character/tests.rs +++ b/crates/ui-egui/src/panels/character/tests.rs @@ -628,6 +628,43 @@ fn superscript_and_subscript_decide_from_the_characters_they_change() { assert!(scripts(&app, &id).iter().all(|(_, p)| *p == "normal"), "{:?}", scripts(&app, &id)); } +/// With Real-time Drawing off, Superscript, Subscript and Underline decide from the text with the +/// typing applied: after a Backspace not yet applied takes out the one plain character, the rest +/// is all superscript (or underlined), so a press turns it off. +#[test] +fn superscript_and_underline_decide_from_the_text_with_the_typing_applied() { + for (command, set, off) in + [("type.superscript", json!({"position": "superscript"}), json!("normal")), ("type.underline", json!({"underline": true}), json!(false))] + { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.run("file.new", json!({"width": 300, "height": 200})).unwrap(); + app.session.execute("prefs.set", &json!({"key": "realTimeDrawing", "value": false})).unwrap(); + let id = app.run("text.create", json!({"x": 20, "y": 50, "text": "x2", "size": 12})).unwrap()["id"].clone(); + let mut range = set.clone(); + range["id"] = id.clone(); + range["start"] = json!(1); + range["end"] = json!(2); + app.run("text.setRangeStyle", range).unwrap(); + app.run("select.set", json!({"ids": [id.clone()]})).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": 1, "end": 1})); + let ctx = egui::Context::default(); + let backspace = + |pressed| egui::Event::Key { key: egui::Key::Backspace, physical_key: None, pressed, repeat: false, modifiers: Default::default() }; + let raw = egui::RawInput { events: vec![backspace(true), backspace(false)], ..Default::default() }; + ctx.run_ui(raw, |ui| crate::shortcuts::handle(&mut app, ui.ctx())).textures_delta.clear(); + assert_eq!(app.session.tool_options()["typing"], json!(true), "{command}"); + let node = |app: &VectorcraftApp| match &app.session.active().unwrap().doc.node(NodeId(id.as_u64().unwrap())).unwrap().kind { + NodeKind::Text(t) => (t.plain_text(), t.runs.iter().map(|r| (r.style.position.id(), r.style.underline)).collect::>()), + _ => panic!("text"), + }; + assert_eq!(node(&app).0, "x2", "{command}: the Backspace is not applied yet"); + assert_eq!(app.run(command, json!({})).unwrap(), off, "{command}"); + assert_eq!(node(&app), ("2".to_string(), vec![("normal", false)]), "{command}"); + } +} + /// The panel menu's Small Caps, Superscript and Subscript set the selected type's small caps /// and position and show a check mark when on; Superscript and Subscript turn each other off. #[test] diff --git a/crates/ui-egui/src/shortcuts.rs b/crates/ui-egui/src/shortcuts.rs index db4e1bbd1e..e683560b70 100644 --- a/crates/ui-egui/src/shortcuts.rs +++ b/crates/ui-egui/src/shortcuts.rs @@ -374,7 +374,8 @@ pub fn handle(app: &mut VectorcraftApp, ctx: &egui::Context) { !(chord.is_plain() && types_text(chord.key) && !shifted_punctuation || row_key(chord) || gesture_escape(chord)) }); let ran = !hits.is_empty(); - for c in hits { + for hit in hits { + let c = unshifted_when_dimmed(app, hit); run_hit(app, ctx, &c); if dialog_or_palette_open(app) { break; @@ -695,7 +696,8 @@ fn type_tool_input(app: &mut VectorcraftApp, ctx: &egui::Context, composing: boo // cut or paste after a deferred pointer or navigation notice is dropped there). Only chords // with the command key run commands: the rest type or edit. let hits = dispatch(app, ctx, |hit| hit.trigger.chord.mods.cmd); - for c in hits { + for hit in hits { + let c = unshifted_when_dimmed(app, hit); run_hit(app, ctx, &c); if dialog_or_palette_open(app) { break; @@ -990,8 +992,9 @@ fn claimed_tool_keys(app: &mut VectorcraftApp, ctx: &egui::Context, view: ViewIn } /// Runs this frame's key presses against the keymap in effect; `accept` declines a hit, leaving -/// its press (and the text it typed) to what follows. -fn dispatch(app: &mut VectorcraftApp, ctx: &egui::Context, accept: impl FnMut(&Hit) -> bool) -> Vec { +/// its press (and the text it typed) to what follows. Each hit's command is worked out with +/// [`unshifted_when_dimmed`] just before it runs, after the commands of the presses before it. +fn dispatch(app: &mut VectorcraftApp, ctx: &egui::Context, accept: impl FnMut(&Hit) -> bool) -> Vec { let mirror = crate::shortcut_editor::current(); let dispatcher = &mut app.dispatcher; ctx.input_mut(|i| { @@ -1034,22 +1037,20 @@ fn dispatch(app: &mut VectorcraftApp, ctx: &egui::Context, accept: impl FnMut(&H } dispatcher.dispatch_filtered(i, &mirror.keymap, &Active::NONE, accept) }) - .into_iter() - .map(|h| unshifted_when_dimmed(app, &mirror.keymap, h)) - .collect() } /// The command a hit runs. A Shift+`=` chord whose command is dimmed runs what the same chord /// without Shift runs, when that is bound: Cmd+Shift+= (Superscript) zooms in, as Cmd+= does, /// while no type is selected. -fn unshifted_when_dimmed(app: &VectorcraftApp, keymap: &craft_keymap::Keymap, hit: Hit) -> CommandRef { +fn unshifted_when_dimmed(app: &VectorcraftApp, hit: Hit) -> CommandRef { + let mirror = crate::shortcut_editor::current(); let chord = hit.trigger.chord; if chord.key == craft_keymap::Key::Equals && chord.mods.shift && !hit.trigger.double && !crate::menus::enabled(app, &hit.command.id) && let Some(unshifted) = - crate::keymap::lookup(keymap, &craft_keymap::Chord::new(craft_keymap::Mods { shift: false, ..chord.mods }, chord.key)) + crate::keymap::lookup(&mirror.keymap, &craft_keymap::Chord::new(craft_keymap::Mods { shift: false, ..chord.mods }, chord.key)) { return unshifted; } diff --git a/crates/ui-egui/src/shortcuts/tests.rs b/crates/ui-egui/src/shortcuts/tests.rs index 56038d3105..c41bd10c44 100644 --- a/crates/ui-egui/src/shortcuts/tests.rs +++ b/crates/ui-egui/src/shortcuts/tests.rs @@ -1156,6 +1156,12 @@ fn cmd_shift_equals_and_cmd_alt_shift_equals_toggle_superscript_and_subscript() assert!(zoom(&mut app) > before, "{key:?} with Cmd+Shift zooms in"); } assert!(subscript(&mut app), "{:?}", positions(&mut app)); + // Cmd+A and Cmd+Shift+= in one frame: Select All runs first, so Cmd+Shift+= applies + // Superscript to the text it selected instead of zooming in. + let before = zoom(&mut app); + frame(&mut app, [press(Key::A, Modifiers::NONE), press(Key::Equals, shift)].concat()); + assert_eq!(zoom(&mut app), before, "no zoom once Select All selected the type"); + assert!(positions(&mut app).iter().all(|(_, p)| *p == json!("superscript")), "{:?}", positions(&mut app)); } /// Tab shows and hides the panels, and in the Type tool it types a tab, without moving the