From 67c3c579a9b2f28e276e5430fef0f37712bb7618 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Sat, 21 Nov 2026 21:27:18 +0700 Subject: [PATCH] type-w1: net change of 1 commits - type-w1: written from its sheet --- crates/ui-contract/src/runtime/edit.rs | 5 +- crates/ui-contract/src/runtime/ime.rs | 12 ++-- crates/ui-contract/tests/it/text/editor.rs | 66 ++++++++++++++++++++++ 3 files changed, 75 insertions(+), 8 deletions(-) diff --git a/crates/ui-contract/src/runtime/edit.rs b/crates/ui-contract/src/runtime/edit.rs index 371ca8d6e..d1ac47df4 100644 --- a/crates/ui-contract/src/runtime/edit.rs +++ b/crates/ui-contract/src/runtime/edit.rs @@ -126,13 +126,12 @@ pub struct PreviewWatch { impl PreviewWatch { /// Arms the watch for the component `owner`. pub fn arm(&mut self, ctx: &Context, owner: Id) { - let _ = (ctx, owner); - todo!("ty.X2") + self.armed = Some((ctx.clone(), owner)); } /// Disarms the watch: its drop notes nothing. pub fn disarm(&mut self) { - todo!("ty.X2") + self.armed = None; } } diff --git a/crates/ui-contract/src/runtime/ime.rs b/crates/ui-contract/src/runtime/ime.rs index ea9bfe0cb..a3f0543b0 100644 --- a/crates/ui-contract/src/runtime/ime.rs +++ b/crates/ui-contract/src/runtime/ime.rs @@ -34,6 +34,8 @@ struct Engine { preedit: String, caret: Option<(Id, Rect, Rect, bool)>, interrupt: Option, + /// An interrupt asked the platform to discard its marked text; `take_discard` reads it once. + discard: bool, /// What the editor that owns the IME reported at its last pass. editor: Option, } @@ -56,6 +58,7 @@ impl Engine { self.preedit.clear(); self.stale = true; self.interrupt = Some(owner); + self.discard = true; } } @@ -85,11 +88,10 @@ pub(crate) fn input_hook(ctx: &Context, events: &mut Vec, viewport: Vie }); } -/// Whether the engine's last `interrupt` asks the platform to discard the marked text: once for each interrupt. -#[allow(dead_code)] // P3(type-w1): ty.X5 -pub(crate) fn take_discard(ctx: &Context) -> bool { - let _ = ctx; - todo!("ty.X5") +/// Whether an `interrupt` of the engine of `viewport` asks the platform to discard the marked text: once for each +/// interrupt. The engine is per viewport and the caller runs before any pass, so it names the viewport. +pub(crate) fn take_discard(ctx: &Context, viewport: ViewportId) -> bool { + with(ctx, viewport, |engine| std::mem::take(&mut engine.discard)) } /// The begin hook, after pending focus moves are applied: resolves the owner and turns the queue into edits. diff --git a/crates/ui-contract/tests/it/text/editor.rs b/crates/ui-contract/tests/it/text/editor.rs index 0e94fd751..c2e5bf445 100644 --- a/crates/ui-contract/tests/it/text/editor.rs +++ b/crates/ui-contract/tests/it/text/editor.rs @@ -2,16 +2,20 @@ //! receives only inputs that change no text, and its visible lines become AccessKit run nodes. use craft_behavior::{EditorInput, PartKey, Sense, TextPos}; +use craft_host::target::ShellInfo; use craft_ui::contract::{Common, Component, Cx, PaintUi, PartsCx, Props as _, Role, Show as _}; use craft_ui::primitives::{EditorLine, EditorPurpose, EditorRun, EditorState}; +use craft_ui::runtime::InstallSpec; use craft_ui::runtime::focus::KeyUse; use craft_ui::style::StyleCx; use craft_ui::testing::input::key_press; use craft_ui::testing::pointer::{click, moved, press, release}; +use craft_ui::testing::windows::FakeSystem; use craft_ui::testing::{Frames, trial}; use craft_ui_test_kit::contract::PlainStyle; use egui::{Event, Id, ImeEvent, Key, Modifiers, Pos2, Rect, Response}; use std::cell::{Cell, RefCell}; +use std::sync::Arc; const AT: Rect = Rect { min: Pos2::new(10.0, 10.0), max: Pos2::new(210.0, 90.0) }; const TEXT: &str = "fn main"; @@ -223,3 +227,65 @@ fn a_reset_the_integration_retires_is_asked_again() { "the new request's completion gives the new editor its IME: {inputs:?}" ); } + +/// Focus moving from a composing editor to another ends the old one's input: it receives nothing after the move, and +/// the new editor receives nothing of the old composition's late preedit until the reset's own completion arrives. +#[trial] +fn an_owner_change_mid_composition_interrupts_it_and_drops_its_late_input() { + const OTHER: Rect = Rect { min: Pos2::new(10.0, 110.0), max: Pos2::new(210.0, 190.0) }; + let (first, second) = (RefCell::new(Vec::new()), RefCell::new(Vec::new())); + let mut ui = |ui: &mut egui::Ui| { + Probe { common: Common::new(), log: &first, report: true }.label("First").id_salt("first").show_at(ui, AT); + Probe { common: Common::new(), log: &second, report: true }.label("Second").id_salt("second").show_at(ui, OTHER); + }; + let mut frames = Frames::new(); + click(&mut frames, AT.center(), &mut ui); + frames.frame(vec![preedit("k")], &mut ui); + assert!(matches!(first.take().as_slice(), [EditorInput::Preedit { .. }]), "the first editor composes"); + + let mut request = None; + for events in [vec![moved(OTHER.center())], vec![press(OTHER.center())], vec![release(OTHER.center())]] { + frames.frame(events, &mut ui); + request = request.or(frames.ime().and_then(|ime| ime.reset)); + } + let Some(request) = request else { panic!("the owner change asks for a reset") }; + first.take(); + second.take(); + + frames.frame(vec![preedit("late")], &mut ui); + assert!(first.take().is_empty(), "the old editor gets no input after the owner change"); + assert!(second.take().is_empty(), "the old composition's late preedit reaches no editor before the reset completes"); + + frames.frame(vec![Event::ImeResetCompleted { request_id: request }, preedit("y")], &mut ui); + assert!(first.take().is_empty(), "the old editor still gets nothing"); + let inputs = second.take(); + assert!( + matches!(inputs.as_slice(), [EditorInput::Preedit { text, .. }] if &**text == "y"), + "after the completion the new editor receives its own preedit: {inputs:?}" + ); +} + +/// An editor losing focus while it composes asks the system once to discard the marked text: later frames and a retired +/// request asked again add no discard. +#[trial] +fn an_interrupted_composition_asks_the_system_to_discard_its_marked_text_once() { + let system = Arc::new(FakeSystem::new()); + let mut frames = Frames::uninstalled(); + let installed = craft_ui::install(&frames.ctx, InstallSpec::new(ShellInfo::default(), system.clone())); + assert!(installed.is_ok(), "the context takes the runtime once"); + let (log, id) = (RefCell::new(Vec::new()), Cell::new(Id::NULL)); + let mut ui = |ui: &mut egui::Ui| show(ui, &log, &id, false); + frames.idle(3, &mut ui); + click(&mut frames, AT.center(), &mut ui); + frames.frame(vec![preedit("k")], &mut ui); + assert_eq!(system.discards(), 0, "a composition under way is not discarded"); + + let outside = Pos2::new(400.0, 300.0); + for events in [vec![moved(outside)], vec![press(outside)], vec![release(outside)]] { + frames.frame(events, &mut ui); + } + frames.idle(3, &mut ui); + assert_eq!(system.discards(), 1, "focus leaving the composition asks for one discard"); + frames.idle(6, &mut ui); + assert_eq!(system.discards(), 1, "idle frames and the end hook's re-ask add none"); +}