mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 11:38:08 +00:00
IME: an interrupt of a framework editor publishes the app's own reset request of that pass, and the request counts as the app's only when the published output carries it
This commit is contained in:
@@ -78,6 +78,9 @@ pub(crate) struct Output {
|
||||
pub caret: Rect,
|
||||
/// Interrupt the OS composition and ask for a reset.
|
||||
pub interrupt: bool,
|
||||
/// The reset request the app's own editor asked for this pass (`app_reset`), which an interrupt publishes instead
|
||||
/// of asking a new one: egui keeps one unpublished request per viewport, so a new one would replace the app's.
|
||||
pub app_reset: Option<egui::ImeResetId>,
|
||||
/// The context generation the tracker gave the owner: it changes when the owner, its epoch, its focus or its
|
||||
/// purpose changes, and stays for ordinary edits.
|
||||
pub generation: u64,
|
||||
@@ -89,9 +92,10 @@ pub(crate) struct Output {
|
||||
}
|
||||
|
||||
/// Writes the pass's IME output; egui reads it only when it is `Some` at the end of the pass. Returns the reset
|
||||
/// request an interrupt published: its completion is the one that matches (`None` when the ids ran out).
|
||||
/// request an interrupt published, the app's own when it asked for one: its completion is the one that matches
|
||||
/// (`None` when the ids ran out).
|
||||
pub(crate) fn write(ctx: &Context, viewport: ViewportId, out: Output) -> Option<egui::ImeResetId> {
|
||||
let reset = out.interrupt.then(|| ctx.request_ime_reset(viewport)).flatten();
|
||||
let reset = out.interrupt.then(|| out.app_reset.or_else(|| ctx.request_ime_reset(viewport))).flatten();
|
||||
ctx.output_mut(|output| {
|
||||
let mut ime = egui::output::IMEOutput::new(
|
||||
out.owner,
|
||||
@@ -117,10 +121,10 @@ pub(crate) fn app_reset(ctx: &Context, viewport: ViewportId) -> Option<egui::Ime
|
||||
}
|
||||
|
||||
/// Interrupts the composition of the IME output the app's own editor wrote this pass, in its context, and asks for a
|
||||
/// reset. An editor that asked for its own reset this pass (`app_reset`) keeps its request, so the platform answers the
|
||||
/// app's id and the app gets its completion. Returns the reset request the output carries, as `write` does.
|
||||
pub(crate) fn interrupt_written(ctx: &Context, viewport: ViewportId) -> Option<egui::ImeResetId> {
|
||||
let reset = app_reset(ctx, viewport).or_else(|| ctx.request_ime_reset(viewport));
|
||||
/// reset. An editor that asked for its own reset this pass (`app`, from `app_reset`) keeps its request, so the platform
|
||||
/// answers the app's id and the app gets its completion. Returns the reset request the output carries, as `write` does.
|
||||
pub(crate) fn interrupt_written(ctx: &Context, viewport: ViewportId, app: Option<egui::ImeResetId>) -> Option<egui::ImeResetId> {
|
||||
let reset = app.or_else(|| ctx.request_ime_reset(viewport));
|
||||
ctx.output_mut(|output| {
|
||||
if let Some(ime) = &mut output.ime {
|
||||
ime.should_interrupt_composition = true;
|
||||
|
||||
@@ -497,7 +497,7 @@ pub(crate) fn end_pass(ctx: &Context) {
|
||||
let app_wrote = ctx.output(|output| output.ime.as_ref().map(|ime| (ime.owner, ime.context_generation)));
|
||||
// egui publishes the app's request only in a pass that has an IME output.
|
||||
let app_reset = app_wrote.and(os::app_reset(ctx, viewport));
|
||||
let (owner, interrupt, caret, update, published, host) = with(ctx, viewport, |engine| {
|
||||
let (owner, interrupt, caret, update, published, host, app_request) = with(ctx, viewport, |engine| {
|
||||
let ran = std::mem::take(&mut engine.ran);
|
||||
if !ran {
|
||||
engine.edits.clear();
|
||||
@@ -515,20 +515,15 @@ pub(crate) fn end_pass(ctx: &Context) {
|
||||
let last_host = engine.host;
|
||||
let kept = engine.host.filter(|host| app_wrote.is_some_and(|(owner, _)| owner == host.owner)).is_some_and(|host| host.composing);
|
||||
engine.host = app_wrote.filter(|_| engine.owner.is_none()).map(|(owner, generation)| HostIme { owner, generation, composing: kept });
|
||||
if let Some(id) = app_reset.filter(|_| engine.owner.is_none())
|
||||
&& !engine.host_resets.contains(&id)
|
||||
{
|
||||
engine.host_resets.push(id);
|
||||
// Completions arrive in request order: the latest request's completion ends the interrupted composition,
|
||||
// whichever request the fence waited for.
|
||||
if engine.stale {
|
||||
engine.reset = Some(id);
|
||||
}
|
||||
}
|
||||
// The app's request while no framework editor owns the IME: every output this pass publishes carries it, the
|
||||
// app's own or the engine's interrupt, and a new one becomes the fence's request below.
|
||||
let app_request = app_reset.filter(|_| engine.owner.is_none());
|
||||
let fresh = app_request.filter(|id| !engine.host_resets.contains(id));
|
||||
// The fence has no request the integration can answer: the owner's output asks again, a framework editor's
|
||||
// or the app's own.
|
||||
if engine.stale
|
||||
&& engine.reset.is_none()
|
||||
&& fresh.is_none()
|
||||
&& let Some(owner) = engine.owner.or(engine.host.map(|host| host.owner))
|
||||
{
|
||||
engine.interrupt.get_or_insert(owner);
|
||||
@@ -536,13 +531,13 @@ pub(crate) fn end_pass(ctx: &Context) {
|
||||
let caret = engine.caret.take();
|
||||
let password = caret.filter(|(id, ..)| Some(*id) == engine.owner).map(|(.., password)| password);
|
||||
let update = engine.observe(viewport, frame_nr, pass_nr, password);
|
||||
(engine.owner, engine.interrupt.take(), caret, update, engine.published, last_host)
|
||||
(engine.owner, engine.interrupt.take(), caret, update, engine.published, last_host, app_request)
|
||||
});
|
||||
let host_interrupt = interrupt.filter(|old| owner.is_none() && app_wrote.is_some_and(|(id, _)| id == *old));
|
||||
let written = if owner.is_none() && interrupt.is_none() {
|
||||
None
|
||||
} else if host_interrupt.is_some() {
|
||||
Some((os::interrupt_written(ctx, viewport), None))
|
||||
Some((os::interrupt_written(ctx, viewport, app_request), None))
|
||||
} else {
|
||||
os::clear(ctx);
|
||||
match (caret, interrupt, update) {
|
||||
@@ -553,6 +548,7 @@ pub(crate) fn end_pass(ctx: &Context) {
|
||||
field,
|
||||
caret,
|
||||
interrupt: interrupt.is_some(),
|
||||
app_reset: app_request,
|
||||
generation,
|
||||
password,
|
||||
surrounding: update.surrounding_text,
|
||||
@@ -569,6 +565,7 @@ pub(crate) fn end_pass(ctx: &Context) {
|
||||
field: Rect::NOTHING,
|
||||
caret: Rect::NOTHING,
|
||||
interrupt: true,
|
||||
app_reset: app_request,
|
||||
generation,
|
||||
password: false,
|
||||
surrounding: IMESurroundingText::Unavailable,
|
||||
@@ -579,14 +576,27 @@ pub(crate) fn end_pass(ctx: &Context) {
|
||||
_ => None,
|
||||
}
|
||||
};
|
||||
if let Some((reset, generation)) = written {
|
||||
with(ctx, viewport, |engine| {
|
||||
// The app's request counts as its own only when the published output carries it: egui's own output when the
|
||||
// engine wrote none, else the engine's.
|
||||
let carried = app_request.filter(|id| written.as_ref().is_none_or(|(reset, _)| *reset == Some(*id)));
|
||||
with(ctx, viewport, |engine| {
|
||||
if let Some((reset, generation)) = written {
|
||||
if generation.is_some() {
|
||||
engine.published = generation;
|
||||
}
|
||||
if interrupt.is_some() {
|
||||
engine.reset = reset;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
if let Some(id) = carried
|
||||
&& !engine.host_resets.contains(&id)
|
||||
{
|
||||
engine.host_resets.push(id);
|
||||
// Completions arrive in request order: the latest request's completion ends the interrupted composition,
|
||||
// whichever request the fence waited for.
|
||||
if engine.stale {
|
||||
engine.reset = Some(id);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ use crate::runtime::egui::{areas, fixture};
|
||||
use crate::runtime::env::{Env, RootEnv};
|
||||
use crate::runtime::focus::{EditSet, FocusPolicy, FocusTarget, KeepKeys, KeyUse, host_canvas, request};
|
||||
use crate::runtime::frame::{ChromeToggle, take_output};
|
||||
use crate::runtime::ime;
|
||||
use crate::runtime::keys::with_window_intents;
|
||||
use crate::style::{ComponentStyle, StyleCx};
|
||||
use crate::testing::focus::{Stop, egui_focused, key_release, named_id, pop_scope, push_scope, register_stop, widget};
|
||||
@@ -297,6 +298,59 @@ fn an_app_editor_s_later_reset_ends_the_fence() {
|
||||
assert!(editor.borrow().composing, "the editor composes again");
|
||||
}
|
||||
|
||||
/// A framework text field of `id`, as a text component's part draws it: a text stop that takes the IME edits meant for
|
||||
/// it and notes its caret.
|
||||
fn framework_field(ui: &mut Ui, id: Id) {
|
||||
let rect = widget(ui, id);
|
||||
register_stop(ui.ctx(), Stop::new(id, KeyUse::of(Input::Text)).text());
|
||||
let _ = ime::take(ui.ctx(), id, false);
|
||||
ime::note_caret(ui.ctx(), id, rect, rect, false);
|
||||
}
|
||||
|
||||
/// A press on the app's editor takes the focus from a composing framework field, and the editor asks for its own reset
|
||||
/// in the pass the field loses the IME, as EffectCraft's editor does when its caret moves after the press. The interrupt
|
||||
/// of the field's composition publishes the editor's request, so the platform's answer reaches the editor, the field's
|
||||
/// late commit stays fenced until then, and the commit after it reaches the editor.
|
||||
#[trial]
|
||||
fn an_app_editor_s_reset_asked_as_it_takes_the_focus_from_a_composing_framework_field_completes() {
|
||||
let editor = Rc::new(RefCell::new(fixture::ResettingEditor::default()));
|
||||
let held = Rc::clone(&editor);
|
||||
let rect = Rect::from_min_size(Pos2::new(100.0, 100.0), egui::vec2(200.0, 30.0));
|
||||
let framework = named_id("framework field");
|
||||
let mut show = move |ui: &mut Ui| {
|
||||
framework_field(ui, framework);
|
||||
fixture::host_resetting_ime_editor(ui, host_editor_id(), rect, &mut held.borrow_mut());
|
||||
};
|
||||
let mut frames = Frames::new();
|
||||
frames.idle(2, &mut show);
|
||||
request(&frames.ctx, FocusTarget::Id(framework));
|
||||
frames.frame(Vec::new(), &mut show);
|
||||
frames.frame(vec![Event::Ime(ImeEvent::Preedit { text: "k".to_owned(), active_range_chars: None })], &mut show);
|
||||
assert_eq!(frames.ime().map(|ime| ime.owner), Some(framework), "the framework field composes");
|
||||
let at = rect.center();
|
||||
frames.frame(vec![moved(at), press(at)], &mut show);
|
||||
assert!(!frames.ime().is_some_and(|ime| ime.interrupt), "the field keeps the IME through the press pass");
|
||||
editor.borrow_mut().ask = true;
|
||||
frames.frame(vec![release(at)], &mut show);
|
||||
let own = editor.borrow().reset;
|
||||
assert!(own.is_some(), "the editor asked for its own reset");
|
||||
let ime = frames.ime().cloned();
|
||||
assert!(ime.as_ref().is_some_and(|ime| ime.owner == framework && ime.interrupt), "the pass interrupts the field's composition");
|
||||
assert_eq!(ime.and_then(|ime| ime.reset), own, "the interrupt publishes the editor's request, not a new one");
|
||||
let Some(own) = own else { return };
|
||||
frames.frame(Vec::new(), &mut show);
|
||||
frames.frame(
|
||||
vec![
|
||||
Event::Ime(ImeEvent::Commit("Q".to_owned())),
|
||||
Event::ImeResetCompleted { request_id: own },
|
||||
Event::Ime(ImeEvent::Commit("X".to_owned())),
|
||||
],
|
||||
&mut show,
|
||||
);
|
||||
assert_eq!(editor.borrow().reset, None, "the completion of its request reaches the editor");
|
||||
assert_eq!(editor.borrow().text, "X", "the field's late commit is fenced and the commit after the completion reaches the editor");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_focused_egui_field_keeps_escape_and_the_clipboard_and_tabs_on_through_the_one_order() {
|
||||
let (host, mut show) = host_window();
|
||||
|
||||
Reference in New Issue
Block a user