diff --git a/Cargo.lock b/Cargo.lock index 3f4e9ae95..a898a78ed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3226,7 +3226,7 @@ checksum = "92773504d58c093f6de2459af4af33faa518c13451eb8f2b5698ed3d36e7c813" [[package]] name = "ecolor" version = "0.37.0" -source = "git+https://github.com/aiongg/egui.git?rev=42e34e2acf5232928bc5f3fe092733d41b09cfe3#42e34e2acf5232928bc5f3fe092733d41b09cfe3" +source = "git+https://github.com/aiongg/egui.git?rev=9b1f60c60578357c99f51d041f464476d363c338#9b1f60c60578357c99f51d041f464476d363c338" dependencies = [ "bytemuck", "emath", @@ -3235,7 +3235,7 @@ dependencies = [ [[package]] name = "eframe" version = "0.37.0" -source = "git+https://github.com/aiongg/egui.git?rev=42e34e2acf5232928bc5f3fe092733d41b09cfe3#42e34e2acf5232928bc5f3fe092733d41b09cfe3" +source = "git+https://github.com/aiongg/egui.git?rev=9b1f60c60578357c99f51d041f464476d363c338#9b1f60c60578357c99f51d041f464476d363c338" dependencies = [ "ahash", "bytemuck", @@ -3270,7 +3270,7 @@ dependencies = [ [[package]] name = "egui" version = "0.37.0" -source = "git+https://github.com/aiongg/egui.git?rev=42e34e2acf5232928bc5f3fe092733d41b09cfe3#42e34e2acf5232928bc5f3fe092733d41b09cfe3" +source = "git+https://github.com/aiongg/egui.git?rev=9b1f60c60578357c99f51d041f464476d363c338#9b1f60c60578357c99f51d041f464476d363c338" dependencies = [ "accesskit", "ahash", @@ -3289,7 +3289,7 @@ dependencies = [ [[package]] name = "egui-wgpu" version = "0.37.0" -source = "git+https://github.com/aiongg/egui.git?rev=42e34e2acf5232928bc5f3fe092733d41b09cfe3#42e34e2acf5232928bc5f3fe092733d41b09cfe3" +source = "git+https://github.com/aiongg/egui.git?rev=9b1f60c60578357c99f51d041f464476d363c338#9b1f60c60578357c99f51d041f464476d363c338" dependencies = [ "ahash", "bytemuck", @@ -3308,7 +3308,7 @@ dependencies = [ [[package]] name = "egui-winit" version = "0.37.0" -source = "git+https://github.com/aiongg/egui.git?rev=42e34e2acf5232928bc5f3fe092733d41b09cfe3#42e34e2acf5232928bc5f3fe092733d41b09cfe3" +source = "git+https://github.com/aiongg/egui.git?rev=9b1f60c60578357c99f51d041f464476d363c338#9b1f60c60578357c99f51d041f464476d363c338" dependencies = [ "block2 0.6.2", "bytemuck", @@ -3328,7 +3328,7 @@ dependencies = [ [[package]] name = "egui_glow" version = "0.37.0" -source = "git+https://github.com/aiongg/egui.git?rev=42e34e2acf5232928bc5f3fe092733d41b09cfe3#42e34e2acf5232928bc5f3fe092733d41b09cfe3" +source = "git+https://github.com/aiongg/egui.git?rev=9b1f60c60578357c99f51d041f464476d363c338#9b1f60c60578357c99f51d041f464476d363c338" dependencies = [ "bytemuck", "egui", @@ -3341,7 +3341,7 @@ dependencies = [ [[package]] name = "egui_kittest" version = "0.37.0" -source = "git+https://github.com/aiongg/egui.git?rev=42e34e2acf5232928bc5f3fe092733d41b09cfe3#42e34e2acf5232928bc5f3fe092733d41b09cfe3" +source = "git+https://github.com/aiongg/egui.git?rev=9b1f60c60578357c99f51d041f464476d363c338#9b1f60c60578357c99f51d041f464476d363c338" dependencies = [ "eframe", "egui", @@ -3364,7 +3364,7 @@ checksum = "0e9c71c2167ca323c882b99918929403426e2373ea17242ff5653e0d5e1058be" [[package]] name = "emath" version = "0.37.0" -source = "git+https://github.com/aiongg/egui.git?rev=42e34e2acf5232928bc5f3fe092733d41b09cfe3#42e34e2acf5232928bc5f3fe092733d41b09cfe3" +source = "git+https://github.com/aiongg/egui.git?rev=9b1f60c60578357c99f51d041f464476d363c338#9b1f60c60578357c99f51d041f464476d363c338" dependencies = [ "bytemuck", ] @@ -3425,7 +3425,7 @@ dependencies = [ [[package]] name = "epaint" version = "0.37.0" -source = "git+https://github.com/aiongg/egui.git?rev=42e34e2acf5232928bc5f3fe092733d41b09cfe3#42e34e2acf5232928bc5f3fe092733d41b09cfe3" +source = "git+https://github.com/aiongg/egui.git?rev=9b1f60c60578357c99f51d041f464476d363c338#9b1f60c60578357c99f51d041f464476d363c338" dependencies = [ "ahash", "bytemuck", @@ -3451,7 +3451,7 @@ dependencies = [ [[package]] name = "epaint_default_fonts" version = "0.37.0" -source = "git+https://github.com/aiongg/egui.git?rev=42e34e2acf5232928bc5f3fe092733d41b09cfe3#42e34e2acf5232928bc5f3fe092733d41b09cfe3" +source = "git+https://github.com/aiongg/egui.git?rev=9b1f60c60578357c99f51d041f464476d363c338#9b1f60c60578357c99f51d041f464476d363c338" [[package]] name = "equivalent" @@ -10426,19 +10426,19 @@ dependencies = [ [[patch.unused]] name = "egui_demo_lib" version = "0.37.0" -source = "git+https://github.com/aiongg/egui.git?rev=42e34e2acf5232928bc5f3fe092733d41b09cfe3#42e34e2acf5232928bc5f3fe092733d41b09cfe3" +source = "git+https://github.com/aiongg/egui.git?rev=9b1f60c60578357c99f51d041f464476d363c338#9b1f60c60578357c99f51d041f464476d363c338" [[patch.unused]] name = "egui_extras" version = "0.37.0" -source = "git+https://github.com/aiongg/egui.git?rev=42e34e2acf5232928bc5f3fe092733d41b09cfe3#42e34e2acf5232928bc5f3fe092733d41b09cfe3" +source = "git+https://github.com/aiongg/egui.git?rev=9b1f60c60578357c99f51d041f464476d363c338#9b1f60c60578357c99f51d041f464476d363c338" [[patch.unused]] name = "egui_inspection" version = "0.37.0" -source = "git+https://github.com/aiongg/egui.git?rev=42e34e2acf5232928bc5f3fe092733d41b09cfe3#42e34e2acf5232928bc5f3fe092733d41b09cfe3" +source = "git+https://github.com/aiongg/egui.git?rev=9b1f60c60578357c99f51d041f464476d363c338#9b1f60c60578357c99f51d041f464476d363c338" [[patch.unused]] name = "egui_system_font_provider" version = "0.37.0" -source = "git+https://github.com/aiongg/egui.git?rev=42e34e2acf5232928bc5f3fe092733d41b09cfe3#42e34e2acf5232928bc5f3fe092733d41b09cfe3" +source = "git+https://github.com/aiongg/egui.git?rev=9b1f60c60578357c99f51d041f464476d363c338#9b1f60c60578357c99f51d041f464476d363c338" diff --git a/Cargo.toml b/Cargo.toml index 013cacb08..19fcaa42f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -198,20 +198,20 @@ hayro-jbig2 = { path = "vendor/hayro-jbig2" } hayro-cmap = { path = "vendor/hayro-cmap" } hayro-postscript = { path = "vendor/hayro-postscript" } linesweeper = { path = "vendor/linesweeper" } -ecolor = { git = "https://github.com/aiongg/egui.git", rev = "42e34e2acf5232928bc5f3fe092733d41b09cfe3" } -emath = { git = "https://github.com/aiongg/egui.git", rev = "42e34e2acf5232928bc5f3fe092733d41b09cfe3" } -epaint = { git = "https://github.com/aiongg/egui.git", rev = "42e34e2acf5232928bc5f3fe092733d41b09cfe3" } -epaint_default_fonts = { git = "https://github.com/aiongg/egui.git", rev = "42e34e2acf5232928bc5f3fe092733d41b09cfe3" } -egui = { git = "https://github.com/aiongg/egui.git", rev = "42e34e2acf5232928bc5f3fe092733d41b09cfe3" } -egui-winit = { git = "https://github.com/aiongg/egui.git", rev = "42e34e2acf5232928bc5f3fe092733d41b09cfe3" } -egui-wgpu = { git = "https://github.com/aiongg/egui.git", rev = "42e34e2acf5232928bc5f3fe092733d41b09cfe3" } -egui_glow = { git = "https://github.com/aiongg/egui.git", rev = "42e34e2acf5232928bc5f3fe092733d41b09cfe3" } -egui_extras = { git = "https://github.com/aiongg/egui.git", rev = "42e34e2acf5232928bc5f3fe092733d41b09cfe3" } -egui_kittest = { git = "https://github.com/aiongg/egui.git", rev = "42e34e2acf5232928bc5f3fe092733d41b09cfe3" } -egui_system_font_provider = { git = "https://github.com/aiongg/egui.git", rev = "42e34e2acf5232928bc5f3fe092733d41b09cfe3" } -eframe = { git = "https://github.com/aiongg/egui.git", rev = "42e34e2acf5232928bc5f3fe092733d41b09cfe3" } -egui_demo_lib = { git = "https://github.com/aiongg/egui.git", rev = "42e34e2acf5232928bc5f3fe092733d41b09cfe3" } -egui_inspection = { git = "https://github.com/aiongg/egui.git", rev = "42e34e2acf5232928bc5f3fe092733d41b09cfe3" } +ecolor = { git = "https://github.com/aiongg/egui.git", rev = "9b1f60c60578357c99f51d041f464476d363c338" } +emath = { git = "https://github.com/aiongg/egui.git", rev = "9b1f60c60578357c99f51d041f464476d363c338" } +epaint = { git = "https://github.com/aiongg/egui.git", rev = "9b1f60c60578357c99f51d041f464476d363c338" } +epaint_default_fonts = { git = "https://github.com/aiongg/egui.git", rev = "9b1f60c60578357c99f51d041f464476d363c338" } +egui = { git = "https://github.com/aiongg/egui.git", rev = "9b1f60c60578357c99f51d041f464476d363c338" } +egui-winit = { git = "https://github.com/aiongg/egui.git", rev = "9b1f60c60578357c99f51d041f464476d363c338" } +egui-wgpu = { git = "https://github.com/aiongg/egui.git", rev = "9b1f60c60578357c99f51d041f464476d363c338" } +egui_glow = { git = "https://github.com/aiongg/egui.git", rev = "9b1f60c60578357c99f51d041f464476d363c338" } +egui_extras = { git = "https://github.com/aiongg/egui.git", rev = "9b1f60c60578357c99f51d041f464476d363c338" } +egui_kittest = { git = "https://github.com/aiongg/egui.git", rev = "9b1f60c60578357c99f51d041f464476d363c338" } +egui_system_font_provider = { git = "https://github.com/aiongg/egui.git", rev = "9b1f60c60578357c99f51d041f464476d363c338" } +eframe = { git = "https://github.com/aiongg/egui.git", rev = "9b1f60c60578357c99f51d041f464476d363c338" } +egui_demo_lib = { git = "https://github.com/aiongg/egui.git", rev = "9b1f60c60578357c99f51d041f464476d363c338" } +egui_inspection = { git = "https://github.com/aiongg/egui.git", rev = "9b1f60c60578357c99f51d041f464476d363c338" } winit = { git = "https://github.com/storytold/winit.git", rev = "a517b3846ee8457d84ee4975110bfda6797c44dc" } # symphonia's FLAC decoder restores LPC samples as `residual + (prediction as i32)`. In a valid diff --git a/crates/table/src/ui/grid/editor.rs b/crates/table/src/ui/grid/editor.rs index 78c9f635b..0b6c7e675 100644 --- a/crates/table/src/ui/grid/editor.rs +++ b/crates/table/src/ui/grid/editor.rs @@ -67,10 +67,6 @@ impl Draft { self.preedit.is_some() } - pub(super) fn caret(&self) -> usize { - self.caret - } - /// The text as shown: the composition sits at the caret. pub(super) fn shown(&self) -> String { match &self.preedit { @@ -176,12 +172,18 @@ impl Draft { } } - /// Replaces the text from `from` to the end with `reference` (point mode). - pub(super) fn point_at(&mut self, from: usize, reference: &str) { - self.text.truncate(from.min(self.text.len())); + /// Replaces the text from `from` (the caret when `None`) to the end with `reference` (point mode) and returns + /// where the reference starts. A composition in progress ends first, its marked text kept as typed. + pub(super) fn point_at(&mut self, from: Option, reference: &str) -> usize { + if let Some(preedit) = self.preedit.take() { + self.insert(&preedit); + } + let from = from.unwrap_or(self.caret).min(self.text.len()); + self.text.truncate(from); self.text.push_str(reference); self.caret = self.text.len(); self.anchor = None; + from } } @@ -227,13 +229,12 @@ impl EditState { /// Moves the reference cursor to `to` and writes its reference into the draft. `extend` grows the range from the /// cell the cursor started at (Shift+arrow). pub(super) fn point_to(&mut self, to: CellAt, extend: bool) { - let from = self.point.as_ref().map_or(self.draft.caret(), |point| point.from); let anchor = match (&self.point, extend) { (Some(point), true) => point.anchor, _ => to, }; let range = CellRange::cells(anchor, to); - self.draft.point_at(from, &absolute_range_reference(range.clone())); + let from = self.draft.point_at(self.point.as_ref().map(|point| point.from), &absolute_range_reference(range.clone())); self.point = Some(Point { anchor, range, from }); } } diff --git a/crates/text/src/ui/editor.rs b/crates/text/src/ui/editor.rs index b12d0c5c2..d8d7ff145 100644 --- a/crates/text/src/ui/editor.rs +++ b/crates/text/src/ui/editor.rs @@ -477,15 +477,6 @@ impl Component for TextEditor<'_> { edits.reconcile(); let fallback = space.doc_rect(cx.rect); - match press.phase { - PressPhase::Down { start, .. } if start.button == craft_behavior::PointerButton::Primary => { - edits.press(space.doc(start.origin), press.mods.shift, start.clicks, now); - } - PressPhase::DragStart { pos, .. } | PressPhase::Drag { pos, .. } => edits.drag(space.doc(pos)), - PressPhase::Release { .. } | PressPhase::Cancel { .. } => edits.session.dragging = None, - _ => {} - } - let mut ending: Option> = None; for input in inputs { if let Some(range) = ending.take() { @@ -536,6 +527,15 @@ impl Component for TextEditor<'_> { if let Some(range) = ending { edits.drop_marked(range); } + // A press is the pass's last pointer event (H0), so the pass's editor input came before it. + match press.phase { + PressPhase::Down { start, .. } if start.button == craft_behavior::PointerButton::Primary => { + edits.press(space.doc(start.origin), press.mods.shift, start.clicks, now); + } + PressPhase::DragStart { pos, .. } | PressPhase::Drag { pos, .. } => edits.drag(space.doc(pos)), + PressPhase::Release { .. } | PressPhase::Cancel { .. } => edits.session.dragging = None, + _ => {} + } // A primary press elsewhere on the canvas keeps the marked text as typed, as a press on the text does. A // secondary press leaves the session open: the canvas menu it opens acts on the live edit. if canvas_space(&cx.env).is_some_and(|space| space.background().pressed == Some(craft_behavior::PointerButton::Primary)) { diff --git a/crates/text/tests/it/editor.rs b/crates/text/tests/it/editor.rs index 19ffa6eca..b0c21ba80 100644 --- a/crates/text/tests/it/editor.rs +++ b/crates/text/tests/it/editor.rs @@ -90,15 +90,17 @@ struct Session { } fn session(text: &str, quotes: Option) -> Session { - session_on(Host { text: text.to_owned(), ..Host::default() }, quotes) + session_on(Host { text: text.to_owned(), ..Host::default() }, quotes, false) } -fn session_on(host: Host, quotes: Option) -> Session { +/// A session on `host`; with `beside`, a second editor on its own text is shown after the session's. +fn session_on(host: Host, quotes: Option, beside: bool) -> Session { let text = host.text.clone(); let host = Rc::new(RefCell::new(host)); let selection = Rc::new(RefCell::new(TextSelection::caret_at(text.chars().count()))); let commits = Rc::new(Cell::new(0)); let (shown_host, shown_selection, shown_commits) = (Rc::clone(&host), Rc::clone(&selection), Rc::clone(&commits)); + let mut other = (Host { text: "xyz".to_owned(), ..Host::default() }, TextSelection::caret_at(0)); let mut fixture = Fixture::new(move |ui| { let (mut host, mut selection) = (shown_host.borrow_mut(), shown_selection.borrow_mut()); let room = ui.available_rect_before_wrap(); @@ -110,6 +112,10 @@ fn session_on(host: Host, quotes: Option) -> Session { let output = if let Some(style) = quotes { editor.smart_quotes(style) } else { editor }.show_at(ui, bounds); let commits = output.edit.events.iter().filter(|event| matches!(event, craft_behavior::EditEvent::Commit { .. })).count() as u32; shown_commits.set(shown_commits.get() + commits); + if beside { + let (host, selection) = (&mut other.0, &mut other.1); + drop(TextEditor::new("Other", host, Bind::bound(selection)).show_at(ui, bounds.translate(vec2(0.0, 40.0)))); + } }); }); fixture.settle(); @@ -173,10 +179,12 @@ fn preedit(text: &str) -> Event { /// Command+Enter while composing commits nothing. A click in the text that arrives with its release and a late commit /// in one raw frame keeps the marked text as typed and interrupts the composition once: one press per pass holds the /// release and the commit for the next pass, where the commit is stale. Later events of the interrupted composition -/// are dropped until the OS completes the reset; then a new composition starts at the clicked caret. +/// are dropped until the OS completes the reset; then a new composition starts at the clicked caret. A preedit and a +/// press in one raw frame apply in that order: the marked text changes once and the press interrupts. A second editor +/// shown after the composing one leaves the composing editor's report standing. #[trial] fn a_click_during_composition_keeps_the_marked_text_and_interrupts_the_composition_once() { - let mut editor = session("abc", None); + let mut editor = session_on(Host { text: "abc".to_owned(), ..Host::default() }, None, true); editor.focus(); editor.fixture.frame(vec![preedit("kzk")]).settle(); assert_eq!(editor.text(), "abckzk", "the marked text is written at the caret"); @@ -205,6 +213,16 @@ fn a_click_during_composition_keeps_the_marked_text_and_interrupts_the_compositi assert_eq!(editor.text(), "abckzk", "a preedit before the reset completes belongs to the interrupted composition"); editor.fixture.frame(vec![Event::ImeResetCompleted { request_id: reset }, preedit("q")]).settle(); assert_eq!(editor.text(), "aqbckzk", "after the reset a new composition starts at the caret"); + + let at = pos2(6.0, 10.0); + editor.fixture.frame(vec![pointer::moved(at)]); + editor.fixture.frame(vec![preedit("qr"), pointer::press(at), pointer::release(at)]); + let pressed = editor.fixture.ime().cloned().expect("the editor keeps the IME"); + let second = pressed.reset.filter(|_| pressed.interrupt).expect("a press after a preedit in its frame interrupts the composition"); + assert_ne!(second, reset); + editor.fixture.settle(); + assert_eq!(editor.text(), "aqrbckzk", "the preedit before the press changes the marked text once"); + assert_eq!(*editor.selection.borrow(), TextSelection::caret_at(6), "the press placed the caret after the preedit"); assert_eq!(editor.commits.get(), 0, "a press in the text keeps the session open"); } @@ -212,13 +230,13 @@ fn a_click_during_composition_keeps_the_marked_text_and_interrupts_the_compositi /// Alt+Up to move the caret a line up. #[trial] fn formatting_keys_follow_the_flow_and_fall_back_to_caret_moves() { - let mut vertical = session_on(Host { text: "abcdefgh".to_owned(), vertical: true, formats: true, ..Host::default() }, None); + let mut vertical = session_on(Host { text: "abcdefgh".to_owned(), vertical: true, formats: true, ..Host::default() }, None, false); vertical.focus(); vertical.fixture.key(Key::ArrowDown, Modifiers::ALT).settle(); let asked = vertical.host.borrow().styles.last().copied(); assert!(matches!(asked, Some(StyleKey::Kern(by) | StyleKey::Tracking(by)) if by > 0.0), "Alt+Down tracks in vertical text: {asked:?}"); - let mut plain = session_on(Host { text: "abcdefgh".to_owned(), ..Host::default() }, None); + let mut plain = session_on(Host { text: "abcdefgh".to_owned(), ..Host::default() }, None, false); plain.focus(); let before = plain.selection.borrow().caret; assert!(before >= LINE, "the click left the caret past the first line"); diff --git a/crates/ui-contract/src/runtime/egui/fixture.rs b/crates/ui-contract/src/runtime/egui/fixture.rs index cd71b6479..83415d4b6 100644 --- a/crates/ui-contract/src/runtime/egui/fixture.rs +++ b/crates/ui-contract/src/runtime/egui/fixture.rs @@ -137,6 +137,55 @@ fn window_focus_facts(ctx: &Context, events: &[Event], native_pixels_per_point: #[derive(Default)] struct WindowFocus(Option); +/// The fixture's reset bridge, as egui-winit keeps one per window: the reset request it holds for each viewport, and +/// the requests it dropped, which the next frame reports as `Event::ImeResetRetired`. +#[derive(Default)] +struct ImeResets { + pending: egui::ViewportIdMap<(egui::ImeResetId, (egui::Id, u64, egui::IMEPurpose))>, + retired: Vec, +} + +impl ImeResets { + fn retire(&mut self, viewport: egui::ViewportId) { + self.retired.extend(self.pending.remove(&viewport).map(|(id, _)| id)); + } + + /// The frame's input: a completion ends the request, a window focus loss drops it, and the requests dropped since + /// the last frame are reported after the frame's own events. + fn input(&mut self, viewport: egui::ViewportId, events: &mut Vec) { + for event in events.iter() { + match event { + Event::ImeResetCompleted { request_id } if self.pending.get(&viewport).is_some_and(|(id, _)| id == request_id) => { + self.pending.remove(&viewport); + } + Event::WindowFocused(false) => self.retire(viewport), + _ => {} + } + } + events.extend(self.retired.drain(..).map(|request_id| Event::ImeResetRetired { request_id })); + } + + /// The frame's output: egui's dropped requests, a pass with no IME output or another IME context, and a newer + /// request each drop the request held. + fn output(&mut self, viewport: egui::ViewportId, output: &egui::PlatformOutput) { + self.retired.extend(output.retired_ime_resets.iter().copied()); + let Some(ime) = &output.ime else { + self.retire(viewport); + return; + }; + let context = (ime.owner, ime.context_generation, ime.purpose); + if self.pending.get(&viewport).is_some_and(|(_, held)| *held != context) { + self.retire(viewport); + } + if let Some(id) = ime.reset_request + && self.pending.get(&viewport).is_none_or(|(held, _)| *held != id) + { + self.retire(viewport); + self.pending.insert(viewport, (id, context)); + } + } +} + /// Runs one pass of `viewport` at `time` seconds with `events` and `files`, on a window of `native_pixels_per_point` /// (egui's default when `None`). A viewport other than the root is a child window of the root, which stays live. pub(crate) fn run_frame( @@ -145,13 +194,14 @@ pub(crate) fn run_frame( screen: Vec2, native_pixels_per_point: Option, viewport: egui::ViewportId, - events: Vec, + mut events: Vec, files: OsFiles, ui: impl FnMut(&mut Ui), ) -> FrameResult { let screen = Rect::from_min_size(Pos2::ZERO, screen); let hovered_files = files.hovered.iter().map(|(name, mime)| egui::HoveredFile { path: Some(name.into()), mime: mime.clone() }).collect(); let dropped_files = files.dropped.iter().map(|name| Arc::new(DroppedPath(name.into())) as egui::DroppedFileHandle).collect(); + Service::::run(ctx, |resets| resets.input(viewport, &mut events)); let mut viewports = window_focus_facts(ctx, &events, native_pixels_per_point); if viewport != egui::ViewportId::ROOT { viewports @@ -168,6 +218,7 @@ pub(crate) fn run_frame( ..RawInput::default() }; let mut output = drive(|| ctx.run_ui(input, ui)); + Service::::run(ctx, |resets| resets.output(viewport, &output.platform_output)); let mut flat = Vec::new(); for clipped in &output.shapes { let mut shapes = Vec::new(); diff --git a/crates/ui-contract/src/runtime/egui/ime.rs b/crates/ui-contract/src/runtime/egui/ime.rs index 09f540a3f..f20913283 100644 --- a/crates/ui-contract/src/runtime/egui/ime.rs +++ b/crates/ui-contract/src/runtime/egui/ime.rs @@ -12,6 +12,8 @@ pub(crate) enum Raw { Commit(String), /// The OS finished the reset with this request id. ResetCompleted(egui::ImeResetId), + /// The integration dropped the reset request with this id; no completion follows. + ResetRetired(egui::ImeResetId), } /// Removes the IME events from `events` and returns them in order. @@ -32,6 +34,10 @@ pub(crate) fn take_events(events: &mut Vec) -> Vec { taken.push(Raw::ResetCompleted(*request_id)); false } + Event::ImeResetRetired { request_id } => { + taken.push(Raw::ResetRetired(*request_id)); + false + } _ => true, }); taken diff --git a/crates/ui-contract/src/runtime/ime.rs b/crates/ui-contract/src/runtime/ime.rs index de85cf4f5..d1fcfea14 100644 --- a/crates/ui-contract/src/runtime/ime.rs +++ b/crates/ui-contract/src/runtime/ime.rs @@ -3,7 +3,7 @@ //! the one writer of `PlatformOutput.ime`; otherwise both stay with egui and the app's own widgets. use crate::runtime::egui::ime as os; -use crate::runtime::egui::{pass, viewports}; +use crate::runtime::egui::pass; use crate::runtime::frame::{PerViewport, Stored}; use craft_behavior::EditorInput; use egui::{Context, Event, Id, Key, Rect, ViewportId}; @@ -22,8 +22,7 @@ struct Engine { /// A reset was asked for; events of the old composition are dropped until its completion arrives. stale: bool, /// The reset request `stale` waits for, as the pass that published it got it back. `None` before it is published, - /// and once the integration can no longer answer it: a pass with no IME output disallows the IME and an unfocused - /// window drops the pending reset, so the next pass with an owner asks again. + /// and once the integration reports it retired (`Event::ImeResetRetired`): the next pass with an owner asks again. reset: Option, /// Edits for `owner`, in arrival order, until its run of this pass takes them. edits: Vec, @@ -109,6 +108,12 @@ pub(crate) fn begin(ctx: &Context, owner: Option) { engine.reset = None; } } + // The integration dropped the request without completing it; the end hook asks again. + os::Raw::ResetRetired(id) => { + if engine.reset == Some(id) { + engine.reset = None; + } + } _ if engine.stale || engine.owner.is_none() => {} os::Raw::Preedit { text, cursor_chars } => { engine.composing = !text.is_empty(); @@ -151,21 +156,24 @@ pub(crate) fn take(ctx: &Context, owner: Id, read_only: bool) -> Vec (bool, bool) { current(ctx, |engine| engine.editor.filter(|facts| facts.owner == owner).map_or((false, false), |facts| (facts.can_undo, facts.can_redo))) } @@ -192,18 +200,13 @@ pub(crate) fn note_caret(ctx: &Context, owner: Id, field: Rect, caret: Rect, pas /// the output is egui's: an app's own `TextEdit` or IME reader writes it. pub(crate) fn end_pass(ctx: &Context) { let viewport = pass::current_viewport(ctx); - let focused = viewports::current(ctx).focused; let (owner, interrupt, caret, generation) = with(ctx, viewport, |engine| { if !std::mem::take(&mut engine.ran) { engine.edits.clear(); } - if !focused { - engine.reset = None; - } // The fence has no request the integration can answer: the owner's output asks again. if engine.stale && engine.reset.is_none() - && focused && let Some(owner) = engine.owner { engine.interrupt.get_or_insert(owner); @@ -227,10 +230,9 @@ pub(crate) fn end_pass(ctx: &Context) { _ => None, } }; - with(ctx, viewport, |engine| match written { - Some(published) if interrupt.is_some() => engine.reset = published, - Some(_) => {} - // No IME output disallows the IME, and the integration drops the pending reset with it. - None => engine.reset = None, - }); + if let Some(published) = written + && interrupt.is_some() + { + with(ctx, viewport, |engine| engine.reset = published); + } } diff --git a/crates/ui-contract/tests/it/text/editor.rs b/crates/ui-contract/tests/it/text/editor.rs index f36ba5e78..0e94fd751 100644 --- a/crates/ui-contract/tests/it/text/editor.rs +++ b/crates/ui-contract/tests/it/text/editor.rs @@ -17,12 +17,14 @@ const AT: Rect = Rect { min: Pos2::new(10.0, 10.0), max: Pos2::new(210.0, 90.0) const TEXT: &str = "fn main"; const EDITOR: PartKey = PartKey::new("editor"); -/// An editor that keeps no text: it logs the inputs it receives and reports `TEXT` as its one line. +/// An editor that keeps no text: it logs the inputs it receives and, while `report` is set, reports `TEXT` as its one +/// line. #[derive(craft_ui::contract::Component)] struct Probe<'a> { #[common] common: Common, log: &'a RefCell>, + report: bool, } impl Component for Probe<'_> { @@ -49,6 +51,9 @@ impl Component for Probe<'_> { } fn paint(&mut self, _: &mut PaintUi<'_>, cx: &Cx<'_, Self>) { + if !self.report { + return; + } let positions: Vec = (0..TEXT.len()).map(|at| at as f32 * 8.0).collect(); let widths = vec![8.0; TEXT.len()]; let runs = [EditorRun::new(0..TEXT.len(), false, &positions, &widths)]; @@ -108,7 +113,7 @@ impl Rig { } fn show(ui: &mut egui::Ui, log: &RefCell>, id: &Cell, read_only: bool) { - let response = Probe { common: Common::new(), log }.label("Source").read_only(read_only).id_salt("code").show_at(ui, AT); + let response = Probe { common: Common::new(), log, report: true }.label("Source").read_only(read_only).id_salt("code").show_at(ui, AT); id.set(response.id); } @@ -143,8 +148,8 @@ fn preedit(text: &str) -> Event { } /// A reset fences the interrupted composition until its own request completes. Focus leaving an editor while it -/// composes asks for a reset; a pass with no IME output retires the request (the integration drops it with the -/// IME), so focus coming back asks again. The retired request's completion is stale and lets nothing through; the +/// composes asks for a reset; a pass with no IME output drops the request, which the integration reports retired, so +/// focus coming back asks again. The retired request's completion is stale and lets nothing through; the /// new request's completion lets the composition that follows it in. #[trial] fn a_reset_ends_only_with_the_completion_of_its_own_request() { @@ -172,3 +177,49 @@ fn a_reset_ends_only_with_the_completion_of_its_own_request() { "the completion of the latest request ends the fence in its place in the stream: {inputs:?}" ); } + +/// Focus moving from a composing editor to one that reports no caret in that pass interrupts the old editor's +/// composition. The next pass's IME output is the new editor's, a new IME context, so the integration drops the +/// request and reports it retired; the engine then asks again, and the new request's completion gives the new editor +/// its IME. +#[trial] +fn a_reset_the_integration_retires_is_asked_again() { + 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 report = Cell::new(true); + 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: report.get() }.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"); + + report.set(false); + let mut resets = Vec::new(); + for events in [vec![moved(OTHER.center())], vec![press(OTHER.center())], vec![release(OTHER.center())], Vec::new(), Vec::new(), Vec::new()] { + frames.frame(events, &mut ui); + resets.push(frames.ime().and_then(|ime| ime.reset)); + // The new editor reports no caret only in the pass that interrupts. + if resets.last().is_some_and(Option::is_some) { + report.set(true); + } + } + let asked: Vec<_> = resets.iter().skip_while(|reset| reset.is_none()).collect(); + let [Some(interrupted), None, Some(again), ..] = asked.as_slice() else { + panic!( + "focus leaving the composition asks for a reset, the new editor's next output asks nothing, and the retired request is asked again: {resets:?}" + ) + }; + assert_ne!(again, interrupted); + frames.frame(Vec::new(), &mut ui); + second.take(); + + frames.frame(vec![Event::ImeResetCompleted { request_id: *again }, preedit("y")], &mut ui); + let inputs = second.take(); + assert!( + matches!(inputs.as_slice(), [EditorInput::Preedit { text, .. }] if &**text == "y"), + "the new request's completion gives the new editor its IME: {inputs:?}" + ); +}