Skip to content

EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust

Merge origin/main into fix/issue-2226-duplicate-during-text-edit

Christopher Linnettd1a505fdf5bf
diff --cc crates/ui-egui/src/panels/viewer_text.rs
index bb66c3a31,ce387f6ca..d4a5c8ccf
--- a/crates/ui-egui/src/panels/viewer_text.rs
+++ b/crates/ui-egui/src/panels/viewer_text.rs
@@@ -1349,6 -1263,121 +1263,125 @@@ fn keyboard(app: &mut EffectcraftApp, u
}
}
+ /// An application key press in the edited text: caret moves, deletes, insertions, formatting and
+ /// the editor's own chords.
+ fn key_event(
+ app: &mut EffectcraftApp,
+ ui: &egui::Ui,
+ ctx: &egui::Context,
+ sel: &effectcraft_engine::commands::text_edit::TextEdit,
+ key: egui::Key,
+ m: egui::Modifiers,
+ ) {
+ use egui::Key;
+ let cmd = m.command;
+ let mv = |app: &mut EffectcraftApp, to: &str| {
+ exec(app, "text.moveCaret", json!({"to": to, "extend": m.shift}));
+ };
+ match key {
+ Key::ArrowLeft | Key::ArrowRight if m.alt => {
+ // Kerning at the caret, tracking over a selection.
+ let step = if cmd { 100.0 } else { 20.0 } * if key == Key::ArrowLeft { -1.0 } else { 1.0 };
+ let doc = layer_doc(&app.session, sel.layer).unwrap_or_default();
+ if sel.is_empty() {
+ if sel.caret < doc.char_len() {
+ let cur = match doc.style_at(sel.caret).kerning {
+ Kerning::Manual(v) => v,
+ _ => 0.0,
+ };
+ exec(
+ app,
+ "layer.setText",
+ json!({"layer": sel.layer.0, "range": [sel.caret, sel.caret + 1], "kerning": cur + step, "merge": format!("text-kern-{}", sel.caret)}),
+ );
+ }
+ } else {
+ let r = sel.range();
+ let cur = doc.style_at(r.start).tracking;
+ exec(app, "layer.setText", json!({"layer": sel.layer.0, "range": [r.start, r.end], "tracking": cur + step, "merge": "text-track"}));
+ }
+ }
+ Key::ArrowLeft => mv(app, if cmd { "wordLeft" } else { "left" }),
+ Key::ArrowRight => mv(app, if cmd { "wordRight" } else { "right" }),
+ Key::ArrowUp => mv(app, if cmd { "paraUp" } else { "up" }),
+ Key::ArrowDown => mv(app, if cmd { "paraDown" } else { "down" }),
+ Key::Home => mv(app, if cmd { "start" } else { "lineStart" }),
+ Key::End => mv(app, if cmd { "end" } else { "lineEnd" }),
+ Key::Backspace => {
+ let k = typing_key(ui, false);
+ let before = ime_stamp(app, ctx);
+ if exec(app, "text.delete", json!({"word": cmd || m.alt, "merge": k})).is_some() {
+ note_ime_application_edit(app, ctx, before);
+ }
+ }
+ Key::Delete => {
+ let k = typing_key(ui, false);
+ let before = ime_stamp(app, ctx);
+ if exec(app, "text.delete", json!({"direction": "forward", "word": cmd || m.alt, "merge": k})).is_some() {
+ note_ime_application_edit(app, ctx, before);
+ }
+ }
+ Key::Enter if cmd => {
+ exec(app, "text.endEdit", json!({}));
+ }
+ Key::Enter => {
+ let k = typing_key(ui, false);
+ let before = ime_stamp(app, ctx);
+ if exec(app, "text.insert", json!({"text": "\n", "merge": k})).is_some() {
+ note_ime_application_edit(app, ctx, before);
+ }
+ }
+ Key::Tab => {
+ let k = typing_key(ui, false);
+ let before = ime_stamp(app, ctx);
+ if exec(app, "text.insert", json!({"text": "\t", "merge": k})).is_some() {
+ note_ime_application_edit(app, ctx, before);
+ }
+ }
+ Key::Escape => {
+ exec(app, "text.endEdit", json!({}));
+ }
+ // The editor owns exactly Cmd/Ctrl+A (Select All) and Cmd/Ctrl+Z with or
+ // without Shift (Undo, Redo). Other modifiers on those keys aren't editing
+ // chords, so they fall through to the binding match below: Shift+A is
+ // Deselect All's default, and Alt variants are free for a rebound one.
+ Key::A if cmd && !m.shift && !m.alt => {
+ exec(app, "text.setSelection", json!({"select": "all"}));
+ }
+ Key::Z if cmd && !m.alt => {
+ let before = ime_stamp(app, ctx);
+ if crate::menus::invoke(app, ctx, if m.shift { "edit.redo" } else { "edit.undo" }, json!({})).is_ok() {
+ note_ime_application_edit(app, ctx, before);
+ }
+ }
+ Key::Comma | Key::Period if cmd && m.shift => {
+ let doc = layer_doc(&app.session, sel.layer).unwrap_or_default();
+ let r = sel.range();
+ let size = sel.pending.as_ref().map_or_else(|| doc.style_at(r.start).size, |p| p.size);
+ let ns = (size + if key == Key::Period { 2.0 } else { -2.0 }).max(1.0);
+ exec(app, "layer.setText", json!({"layer": sel.layer.0, "range": [r.start, r.end], "size": ns}));
+ }
+ // The app-wide shortcut dispatcher is skipped while this editor holds the
- // keyboard, so the Deselect All chord (Cmd/Ctrl+Shift+A or F2 by default)
- // must be matched here and run through the command dispatcher like undo
- // above. Matching the command's current binding, not a fixed key, keeps
- // rebound shortcuts working and gives every route one final state; the
- // forwarding test also refuses chords a focused editor must never surrender
- // (see `forwardable_during_text_edit`).
- _ if crate::menus::chord_matches(&app.session, "edit.deselectAll", key, m) && crate::menus::forwardable_during_text_edit(m, key) => {
- let _ = crate::menus::invoke(app, ctx, "edit.deselectAll", json!({}));
++ // keyboard, so commands that end the edit by moving the selection — Deselect
++ // All (Cmd/Ctrl+Shift+A or F2 by default) and Duplicate (Cmd/Ctrl+D) — must be
++ // matched here and run through the command dispatcher like undo above.
++ // Matching each command's current binding, not a fixed key, keeps rebound
++ // shortcuts working and gives every route one final state; a chord bound to
++ // both commands resolves in list order. The forwarding test also refuses
++ // chords a focused editor must never surrender (see
++ // `forwardable_during_text_edit`).
++ _ if crate::menus::forwardable_during_text_edit(m, key)
++ && let Some(cmd_id) = ["edit.deselectAll", "edit.duplicate"].into_iter().find(|id| crate::menus::chord_matches(&app.session, id, key, m)) =>
++ {
++ let _ = crate::menus::invoke(app, ctx, cmd_id, json!({}));
+ }
+ _ => {}
+ }
+ if !matches!(key, Key::Backspace | Key::Delete | Key::Enter | Key::Tab) {
+ typing_key(ui, true);
+ }
+ }
+
/// Delete the text an IME marks for replacement around the editor's selection.
///
/// `before`/`after` count UTF-8 bytes (`DeleteSurroundingBytes`, what winit reports) or