text: the text editor takes its inputs and commands in arrival order with editor_steps, so typing after a commit's chord is not the session's

This commit is contained in:
bflatastic
2026-11-23 10:11:24 +07:00
parent f6fd9623b0
commit fb939c5c7f
4 changed files with 60 additions and 31 deletions
+24 -29
View File
@@ -34,8 +34,8 @@ use craft_behavior::{
};
use craft_ui::canvas::{canvas_space, context_click};
use craft_ui::contract::{
Animate as _, Bind, BindMut, CommandSet as _, Commands as _, Common, Component, Cx, Edit as _, EditResponse, Focus as _, MeasureCx, Outcome,
PaintUi, PartsCx, Pointer as _, Props as _, Remember as _, Role,
Animate as _, Bind, BindMut, CommandSet as _, Commands as _, Common, Component, Cx, Edit as _, EditResponse, EditorStep, Focus as _, MeasureCx,
Outcome, PaintUi, PartsCx, Pointer as _, Props as _, Remember as _, Role,
};
use craft_ui::primitives::{EditorLine, EditorPurpose, EditorRun, EditorState, PaintShape, StrokeWidth};
use craft_ui::runtime::commands::ClipItem;
@@ -454,8 +454,8 @@ fn show_i_beam(cx: &mut Cx<'_, TextEditor<'_>>, phase: &PressPhase, vertical: bo
}
/// The clipboard's text when the pass's input asks to paste and the clipboard has it ready.
fn pasted_text(cx: &mut Cx<'_, TextEditor<'_>>, inputs: &[EditorInput]) -> Option<std::sync::Arc<str>> {
if !inputs.iter().any(|input| matches!(input, EditorInput::Edit(EditCommand::Paste))) {
fn pasted_text(cx: &mut Cx<'_, TextEditor<'_>>, steps: &[EditorStep<TextEditorCommand>]) -> Option<std::sync::Arc<str>> {
if !steps.iter().any(|step| matches!(step, EditorStep::Input(EditorInput::Edit(EditCommand::Paste)))) {
return None;
}
match cx.clipboard().paste() {
@@ -504,14 +504,12 @@ impl Component for TextEditor<'_> {
fn update(&mut self, cx: &mut Cx<'_, Self>) {
let space = Space::of(&cx.env);
let before = cx.value().copied().unwrap_or_default();
let inputs: Vec<EditorInput> = cx.editor_input().collect();
let mut fired = Vec::new();
while let Some((command, _)) = cx.take_command::<TextEditorCommand>() {
fired.push(command);
}
// The inputs and the commands keys fired, in the order their events came: typing after a commit's chord is
// not the session's.
let steps = cx.editor_steps::<TextEditorCommand>();
let press = *cx.press();
let now = cx.now();
let pasted = pasted_text(cx, &inputs);
let pasted = pasted_text(cx, &steps);
let mut copied: Option<String> = None;
let mut session = std::mem::take(cx.memory::<Session>());
let mut edits = Edits {
@@ -529,7 +527,20 @@ impl Component for TextEditor<'_> {
let fallback = space.doc_rect(cx.rect);
let mut ending: Option<Range<usize>> = None;
for input in inputs {
let mut character_panel = false;
for step in steps {
// A commit or cancel ends the session; an input method's committed text still lands.
if (edits.done.commit || edits.done.cancel) && ending.is_none() {
break;
}
let input = match step {
EditorStep::Input(input) => input,
EditorStep::Command(command, _) => {
edits.command(command, &mut character_panel);
continue;
}
_ => continue,
};
if let Some(range) = ending.take() {
if let EditorInput::Text(text) = &input {
edits.commit_marked(range, text);
@@ -589,8 +600,8 @@ impl Component for TextEditor<'_> {
}
// Commands and the claimed Escape arrive with the pass's own input: a second pass of the frame takes none, so
// a commit, a cancel or the panel toggle is reported by one pass only. Commit and Cancel wait while a
// composition is open: the key belongs to the input method then.
// a commit, a cancel or the panel toggle is reported by one pass only. Escape waits while a composition is
// open: the key belongs to the input method then.
if cx.take_claimed(ESCAPE) && edits.session.preedit.is_none() {
if edits.esc_commits {
edits.done.commit = true;
@@ -598,22 +609,6 @@ impl Component for TextEditor<'_> {
edits.done.cancel = true;
}
}
let mut character_panel = false;
for command in fired {
match command.along_flow(edits.host.vertical()) {
TextEditorCommand::Commit => edits.done.commit |= edits.session.preedit.is_none(),
TextEditorCommand::Cancel => edits.done.cancel |= edits.session.preedit.is_none(),
TextEditorCommand::CharacterPanel => character_panel = true,
command => {
if let Some(key) = command.style_key(edits.selection.is_empty()) {
let handled = edits.host.style_key(key, edits.selection);
if !handled && let Some((step, mods)) = command.fallback() {
edits.move_along(step, mods);
}
}
}
}
}
let selection = edits.selection;
let done = edits.done;
+2 -1
View File
@@ -2,7 +2,8 @@
//! toggle. They sit in the `textEditor` scope, a typing scope: while the editor holds the keys they reach it before an
//! app's shortcut on the same chord (Cmd+T is Free Transform elsewhere), and the keymap's typing rule, which keeps Alt
//! chords with a text field's text, lets them through (Alt+arrows kern and step leading, baseline and tracking). The
//! editor reads them with `take_command`, so the keymap rebinds them like any command; the editor reads no raw keys.
//! editor reads them in order with its inputs (`editor_steps`), so the keymap rebinds them like any command; the editor
//! reads no raw keys.
use craft_behavior::{Granularity, MoveMods, NavMove};
+19 -1
View File
@@ -7,7 +7,7 @@ use std::ops::Range;
use craft_behavior::kurbo::Point;
use craft_behavior::{Granularity, MoveMods, NavMove, SurroundingUnit};
use super::{EditKind, TextEditorHost, TextSelection};
use super::{EditKind, TextEditorCommand, TextEditorHost, TextSelection};
use crate::quotes::{QuoteStyle, smart_quotes};
/// Most clicks in a run that select more: 2 a word, 3 a line, 4 a paragraph, 5 all text.
@@ -300,6 +300,24 @@ impl Edits<'_> {
}
}
/// Runs one of the session's commands. Commit and Cancel wait while a composition is open: the key belongs to the
/// input method then. The Character panel toggle sets `character_panel`.
pub fn command(&mut self, command: TextEditorCommand, character_panel: &mut bool) {
match command.along_flow(self.host.vertical()) {
TextEditorCommand::Commit => self.done.commit |= self.session.preedit.is_none(),
TextEditorCommand::Cancel => self.done.cancel |= self.session.preedit.is_none(),
TextEditorCommand::CharacterPanel => *character_panel = true,
command => {
if let Some(key) = command.style_key(self.selection.is_empty()) {
let handled = self.host.style_key(key, self.selection);
if !handled && let Some((step, mods)) = command.fallback() {
self.move_along(step, mods);
}
}
}
}
}
/// The caret move an arrow key means, in the flow of the text: in vertical text Up is the previous char.
pub fn motion(&mut self, step: NavMove, mods: MoveMods) {
let step = flow_move(step, self.host.vertical());
+15
View File
@@ -167,6 +167,21 @@ fn command_enter_commits_one_session_and_a_session_without_edits_commits_nothing
assert_eq!(editor.text(), "abcd");
}
/// Typing that comes after Cmd+Enter in the same frame is not the committed session's.
#[trial]
fn command_enter_commits_the_typing_before_it_and_none_after_it_in_one_frame() {
use craft_ui::testing::focus::key_release;
use craft_ui::testing::input::key_press;
let mut editor = session("abc", None);
editor.focus();
let cmd_enter =
[Event::ModifiersChanged(Modifiers::COMMAND), key_press(Key::Enter, Modifiers::COMMAND), key_release(Key::Enter, Modifiers::COMMAND)];
let events = [vec![Event::Text("d".into())], cmd_enter.to_vec(), vec![Event::ModifiersChanged(Modifiers::NONE), Event::Text("e".into())]];
editor.fixture.frame(events.concat()).settle();
assert_eq!(*editor.ends.borrow(), [EditorEnd::Commit]);
assert_eq!(editor.text(), "abcd");
}
/// A primary press on the canvas background commits and keeps the marked text as typed; a press on the text and a
/// secondary press on the background (the canvas menu acts on the live edit) leave the session open.
#[trial]