mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 04:04:25 +00:00
- note_editor ignores every editor but the IME owner, so a second editor shown after it leaves the owner's composing report standing; editor_history answers non-owners (false, false). - egui repinned to the fork's craft/ime-reset-retired (9b1f60c60): Event::ImeResetRetired reports a reset request egui-winit or PlatformOutput::append dropped without a completion. The engine asks again on the retirement of its own request; the focus check and the no-output retire in end_pass are gone. The test fixture reports retirements where egui-winit does (focus loss, no IME output, another IME context, a newer request, egui's dropped requests). - The sheet grid's point mode ends a composition in the draft, its marked text kept as typed. - TextEditor applies a pass's input before its press (the press is the pass's last pointer event, H0), so a preedit and a press in one raw frame change the marked text once and the press interrupts. Trials: a reset the integration retires is asked again (craft-ui-contract); the click trial gains a second editor shown after the composing one and a preedit-then-press frame (craft-text).
226 lines
9.8 KiB
Rust
226 lines
9.8 KiB
Rust
//! The editor contract: a component with an editor part receives one ordered input stream, a read-only editor
|
|
//! receives only inputs that change no text, and its visible lines become AccessKit run nodes.
|
|
|
|
use craft_behavior::{EditorInput, PartKey, Sense, TextPos};
|
|
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::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::{Frames, trial};
|
|
use craft_ui_test_kit::contract::PlainStyle;
|
|
use egui::{Event, Id, ImeEvent, Key, Modifiers, Pos2, Rect, Response};
|
|
use std::cell::{Cell, RefCell};
|
|
|
|
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, while `report` is set, reports `TEXT` as its one
|
|
/// line.
|
|
#[derive(craft_ui::contract::Component)]
|
|
struct Probe<'a> {
|
|
#[common]
|
|
common: Common,
|
|
log: &'a RefCell<Vec<EditorInput>>,
|
|
report: bool,
|
|
}
|
|
|
|
impl Component for Probe<'_> {
|
|
const ROLE: Role = Role::MULTILINE_TEXT_INPUT;
|
|
const INPUT: craft_ui::contract::Input = craft_ui::contract::Input::Editor;
|
|
const KEYS: KeyUse = KeyUse::CODE;
|
|
type Style = PlainStyle;
|
|
type Output = Response;
|
|
|
|
fn style(_: &StyleCx<'_>, _: ProbeStyleKey) -> PlainStyle {
|
|
PlainStyle::default()
|
|
}
|
|
|
|
fn sense(&self) -> Sense {
|
|
Sense::CLICK | Sense::FOCUS | Sense::TAKE_FOCUS
|
|
}
|
|
|
|
fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) {
|
|
cx.editor(EDITOR, rect, EditorPurpose::Code);
|
|
}
|
|
|
|
fn update(&mut self, cx: &mut Cx<'_, Self>) {
|
|
self.log.borrow_mut().extend(cx.editor_input());
|
|
}
|
|
|
|
fn paint(&mut self, _: &mut PaintUi<'_>, cx: &Cx<'_, Self>) {
|
|
if !self.report {
|
|
return;
|
|
}
|
|
let positions: Vec<f32> = (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)];
|
|
let line = Rect::from_min_size(cx.content_rect.min, egui::vec2(cx.content_rect.width(), 16.0));
|
|
let lines = [EditorLine::new(TEXT, line, line.bottom(), &runs)];
|
|
let end = TextPos::new(0, TEXT.len());
|
|
let caret = Rect::from_min_size(line.min + egui::vec2(TEXT.len() as f32 * 8.0, 0.0), egui::vec2(0.0, 16.0));
|
|
cx.editor_state(EditorState::new(caret, &lines, end));
|
|
}
|
|
|
|
fn finish(self, cx: Cx<'_, Self>) -> Response {
|
|
cx.response.clone()
|
|
}
|
|
}
|
|
|
|
struct Rig {
|
|
frames: Frames,
|
|
log: RefCell<Vec<EditorInput>>,
|
|
id: Cell<Id>,
|
|
read_only: bool,
|
|
}
|
|
|
|
impl Rig {
|
|
fn new() -> Rig {
|
|
let mut rig = Rig { frames: Frames::new(), log: RefCell::new(Vec::new()), id: Cell::new(Id::NULL), read_only: false };
|
|
for _ in 0..3 {
|
|
rig.frame(Vec::new());
|
|
}
|
|
rig
|
|
}
|
|
|
|
fn frame(&mut self, events: Vec<Event>) {
|
|
let (log, id, read_only) = (&self.log, &self.id, self.read_only);
|
|
self.frames.frame(events, |ui| show(ui, log, id, read_only));
|
|
}
|
|
|
|
fn focus(&mut self) {
|
|
let (log, id, read_only) = (&self.log, &self.id, self.read_only);
|
|
click(&mut self.frames, AT.center(), &mut |ui| show(ui, log, id, read_only));
|
|
self.log.borrow_mut().clear();
|
|
}
|
|
|
|
fn take(&self) -> Vec<EditorInput> {
|
|
self.log.take()
|
|
}
|
|
|
|
/// A frame with `events`, and the reset request its IME output carries.
|
|
fn frame_reset(&mut self, events: Vec<Event>) -> Option<egui::ImeResetId> {
|
|
self.frame(events);
|
|
self.frames.ime().and_then(|ime| ime.reset)
|
|
}
|
|
|
|
/// Hover, press, release and an idle frame at `at`, and the reset requests they carry.
|
|
fn click_resets(&mut self, at: Pos2) -> Vec<egui::ImeResetId> {
|
|
[vec![moved(at)], vec![press(at)], vec![release(at)], Vec::new()].into_iter().filter_map(|events| self.frame_reset(events)).collect()
|
|
}
|
|
}
|
|
|
|
fn show(ui: &mut egui::Ui, log: &RefCell<Vec<EditorInput>>, id: &Cell<Id>, read_only: bool) {
|
|
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);
|
|
}
|
|
|
|
/// Text, Backspace, Tab and Enter reach an editor as one input stream in the order they were typed; a read-only
|
|
/// editor still receives moves but none of the inputs that change text.
|
|
#[trial]
|
|
fn editor_input_reaches_an_editor_part_and_read_only_filtering_drops_edits() {
|
|
let mut rig = Rig::new();
|
|
rig.focus();
|
|
rig.frame(vec![Event::Text("x".into())]);
|
|
rig.frame(vec![key_press(Key::Backspace, Modifiers::NONE)]);
|
|
rig.frame(vec![key_press(Key::Tab, Modifiers::NONE)]);
|
|
rig.frame(vec![key_press(Key::Enter, Modifiers::NONE)]);
|
|
let inputs = rig.take();
|
|
assert!(
|
|
matches!(inputs.as_slice(), [EditorInput::Text(text), EditorInput::Delete { forward: false, .. }, EditorInput::Tab, EditorInput::Newline] if &**text == "x"),
|
|
"the inputs arrive in the pass they are typed, in order: {inputs:?}"
|
|
);
|
|
|
|
rig.read_only = true;
|
|
rig.frame(vec![Event::Text("y".into())]);
|
|
rig.frame(vec![key_press(Key::Tab, Modifiers::NONE)]);
|
|
rig.frame(vec![key_press(Key::Backspace, Modifiers::NONE)]);
|
|
assert!(rig.take().is_empty(), "a read-only editor receives no edit");
|
|
rig.frame(vec![key_press(Key::ArrowUp, Modifiers::NONE)]);
|
|
let moves = rig.take();
|
|
assert!(matches!(moves.as_slice(), [EditorInput::Move(..)]), "moves still arrive: {moves:?}");
|
|
}
|
|
|
|
fn preedit(text: &str) -> Event {
|
|
Event::Ime(ImeEvent::Preedit { text: text.to_owned(), active_range_chars: None })
|
|
}
|
|
|
|
/// 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 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() {
|
|
let mut rig = Rig::new();
|
|
rig.focus();
|
|
rig.frame(vec![preedit("k")]);
|
|
assert!(matches!(rig.take().as_slice(), [EditorInput::Preedit { .. }]), "the editor composes");
|
|
|
|
let left = rig.click_resets(Pos2::new(400.0, 300.0));
|
|
let [first] = left.as_slice() else { panic!("focus leaving a composition asks for one reset: {left:?}") };
|
|
for _ in 0..2 {
|
|
assert_eq!(rig.frame_reset(Vec::new()), None, "no editor owns the IME, so no reset is asked for");
|
|
}
|
|
let back = rig.click_resets(AT.center());
|
|
let [second] = back.as_slice() else { panic!("focus coming back while the reset is pending asks again: {back:?}") };
|
|
assert_ne!(first, second);
|
|
rig.take();
|
|
|
|
rig.frame(vec![Event::ImeResetCompleted { request_id: *first }, preedit("x")]);
|
|
assert!(rig.take().is_empty(), "the retired request's completion is stale: the composition stays fenced");
|
|
rig.frame(vec![Event::ImeResetCompleted { request_id: *second }, preedit("y")]);
|
|
let inputs = rig.take();
|
|
assert!(
|
|
matches!(inputs.as_slice(), [EditorInput::Preedit { text, .. }] if &**text == "y"),
|
|
"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:?}"
|
|
);
|
|
}
|