mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 20:14:31 +01:00
ui-contract: an Alt press an input method types through is typing: the focused text stop that has the IME holds it, so its bound command does not run beside the commit
This commit is contained in:
1 parent
1fc46b5730
commit
508faa1eb7
5 files changed
+53
-4
No files matched your search
@@ -664,6 +664,7 @@ pub(crate) fn input_hook(ctx: &Context, input: &mut RawInput) {
|
||||
// H3: the key stage strips what the runtime owns; egui decides on a repaint from the events left. The events sent
|
||||
// to the focus are counted first, before the IME and key stages take theirs.
|
||||
keys::count_sent(ctx, viewport, &events);
|
||||
keys::note_ime_presses(ctx, viewport, &events);
|
||||
ime::input_hook(ctx, &mut events, viewport);
|
||||
let (stripped, later) = keys::input_hook(ctx, input, &mut events);
|
||||
if !later.is_empty() {
|
||||
|
||||
@@ -33,7 +33,7 @@ pub use input::held;
|
||||
#[doc(hidden)]
|
||||
pub use input::keyed;
|
||||
pub use input::textless_paste;
|
||||
pub(crate) use input::{claims_press, count_sent, input_hook, keys_state, queue_nav, sent, stirred, take_menu_keys, take_nav};
|
||||
pub(crate) use input::{claims_press, count_sent, input_hook, keys_state, note_ime_presses, queue_nav, sent, stirred, take_menu_keys, take_nav};
|
||||
pub use intents::Intents;
|
||||
#[doc(hidden)]
|
||||
pub use intents::intents_of;
|
||||
@@ -89,6 +89,8 @@ struct Press {
|
||||
place: usize,
|
||||
/// The keys down when it went down, itself not among them: what a nudge reads as held with its arrow.
|
||||
held: Vec<Key>,
|
||||
/// An input method committed text right after it: the press typed that text, which a text stop takes.
|
||||
ime: bool,
|
||||
}
|
||||
|
||||
/// An arrow press as a nudge of content: its unit direction, its modifiers and the keys down when it went down.
|
||||
@@ -294,6 +296,8 @@ pub(crate) struct Stage {
|
||||
textless_paste: bool,
|
||||
/// How many inputs of the viewport held an event sent to the focus (`count_sent`).
|
||||
sent: u64,
|
||||
/// The key presses of this input that an input method's commit follows (`note_ime_presses`).
|
||||
ime_presses: Vec<egui::Event>,
|
||||
/// The keys whose press the strip took and whose release it is still to drop.
|
||||
consumed: Vec<Key>,
|
||||
shortcuts: Shortcuts,
|
||||
|
||||
@@ -68,6 +68,8 @@ struct Strip<'a> {
|
||||
struct Arrival {
|
||||
/// The event's arrival time (`Arrived::at`).
|
||||
at: Option<f64>,
|
||||
/// An input method's commit follows the event: a press it follows typed through the input method.
|
||||
ime_next: bool,
|
||||
/// A key press follows the event: a text before it is that press's (the web sends a press's text first).
|
||||
press_next: bool,
|
||||
}
|
||||
@@ -134,7 +136,8 @@ impl Strip<'_> {
|
||||
let pushed = batch.presses.len();
|
||||
let claims = batch.claimed.len();
|
||||
let menu_keys = batch.menu.len();
|
||||
let shortcut_text = self.shortcut_text(&input);
|
||||
// Text an input method commits for the press is typing, whatever the press's chord binds.
|
||||
let shortcut_text = self.shortcut_text(&input) && !arrival.ime_next;
|
||||
let kept = self.press(batch, key, &input, repeat, at);
|
||||
// The canvas's Space keeps its text from every field, as a claim does.
|
||||
let canvas = batch.presses.get(pushed).is_some_and(|press| press.canvas);
|
||||
@@ -147,6 +150,7 @@ impl Strip<'_> {
|
||||
// The release key is kept for withheld presses too: the held-over rule matches repeats by it.
|
||||
if let Some(press) = batch.presses.get_mut(pushed) {
|
||||
press.release = input.physical.or(input.typed);
|
||||
press.ime = arrival.ime_next;
|
||||
press.held = down.iter().copied().filter(|key| Some(*key) != press.release).collect();
|
||||
if kept {
|
||||
press.in_egui = Some(batch.egui_presses);
|
||||
@@ -396,6 +400,7 @@ impl Strip<'_> {
|
||||
canvas: true,
|
||||
place: batch.place,
|
||||
held: Vec::new(),
|
||||
ime: false,
|
||||
});
|
||||
if !repeat {
|
||||
batch.shortcuts.push(Shortcut {
|
||||
@@ -432,6 +437,7 @@ impl Strip<'_> {
|
||||
canvas: false,
|
||||
place: batch.place,
|
||||
held: Vec::new(),
|
||||
ime: false,
|
||||
});
|
||||
false
|
||||
}
|
||||
@@ -462,6 +468,7 @@ impl Strip<'_> {
|
||||
canvas: false,
|
||||
place: batch.place,
|
||||
held: Vec::new(),
|
||||
ime: false,
|
||||
});
|
||||
if !repeat {
|
||||
batch.shortcuts.push(Shortcut {
|
||||
@@ -658,6 +665,7 @@ pub(crate) fn input_hook(ctx: &Context, input: &RawInput, events: &mut Vec<Arriv
|
||||
stage.stirred = false;
|
||||
stage.textless_paste = false;
|
||||
let mut place = 0;
|
||||
let ime_presses = std::mem::take(&mut stage.ime_presses);
|
||||
let press_next: Vec<bool> = (0..events.len())
|
||||
.map(|at| matches!(events.get(at + 1).map(|arrived| &arrived.event), Some(egui::Event::Key { pressed: true, .. })))
|
||||
.collect();
|
||||
@@ -682,7 +690,11 @@ pub(crate) fn input_hook(ctx: &Context, input: &RawInput, events: &mut Vec<Arriv
|
||||
};
|
||||
let down = stage.down.clone();
|
||||
note_down(&mut stage.down, &read);
|
||||
let arrival = Arrival { at: arrived.at, press_next: press_next.get(batch.place).copied().unwrap_or(false) };
|
||||
let arrival = Arrival {
|
||||
at: arrived.at,
|
||||
ime_next: ime_presses.contains(&arrived.event),
|
||||
press_next: press_next.get(batch.place).copied().unwrap_or(false),
|
||||
};
|
||||
strip.event(&mut batch, &mut stage.alt, &mut stage.consumed, &down, read, arrival)
|
||||
});
|
||||
for (physical, mods, text) in std::mem::take(&mut batch.learned) {
|
||||
@@ -727,6 +739,18 @@ pub(crate) fn claims_press(ctx: &Context, input: &RawInput) -> impl Fn(&egui::Ev
|
||||
}
|
||||
}
|
||||
|
||||
/// Notes the key presses of `events` that an input method's commit follows, before the IME stage takes the commits out
|
||||
/// of the input: the key stage reads such a press as typing through the input method (`Press::ime`).
|
||||
pub(crate) fn note_ime_presses(ctx: &Context, viewport: ViewportId, events: &[Arrived]) {
|
||||
let commit = |event: &egui::Event| matches!(event, egui::Event::Ime(egui::ImeEvent::Commit(text)) if !text.is_empty());
|
||||
let presses = events
|
||||
.windows(2)
|
||||
.filter(|pair| matches!(pair[0].event, egui::Event::Key { pressed: true, .. }) && commit(&pair[1].event))
|
||||
.map(|pair| pair[0].event.clone())
|
||||
.collect();
|
||||
with(ctx, viewport, |stage| stage.ime_presses = presses);
|
||||
}
|
||||
|
||||
/// Updates the keys held after `event`: a press adds its key, a release removes it, and the window losing focus
|
||||
/// (which also reads as the right Alt coming up) forgets them all, since their releases go to another window.
|
||||
fn note_down(down: &mut Vec<Key>, event: &KeyEvent) {
|
||||
|
||||
@@ -257,7 +257,11 @@ fn owned_shortcuts(ctx: &Context, target: Option<&StopRec>, batch: &Batch, consu
|
||||
.iter()
|
||||
.zip(consumed)
|
||||
// The canvas's Space is never the focused stop's.
|
||||
.map(|(press, taken)| !press.canvas && holds_press(owner, target, press, *taken == Taken::Yes, press.text.is_some_and(|at| taken_texts[at])))
|
||||
.map(|(press, taken)| {
|
||||
// Text an input method committed for the press goes to the text stop that has the IME.
|
||||
let typed = press.text.is_some_and(|at| taken_texts[at]) || (press.ime && target.is_some_and(|rec| rec.stop.text));
|
||||
!press.canvas && holds_press(owner, target, press, *taken == Taken::Yes, typed)
|
||||
})
|
||||
.collect();
|
||||
// A shortcut without a press of its own (a release, or the clipboard event that stands for ⌘C) follows the owner by
|
||||
// its modifiers, as its press would.
|
||||
@@ -374,6 +378,7 @@ pub fn focus_holds(ctx: &Context, chord: Chord) -> bool {
|
||||
canvas: false,
|
||||
place: 0,
|
||||
held: Vec::new(),
|
||||
ime: false,
|
||||
};
|
||||
let driven = Driven::of(ctx, &rec);
|
||||
let route = press_route(ctx, crate::runtime::windows::os_of(ctx), &rec, &press, driven.or_else(|| list_of_field(ctx, &rec)));
|
||||
|
||||
@@ -100,6 +100,21 @@ fn a_second_press_of_a_held_key_runs_no_shortcut_again() {
|
||||
assert!(ran(&mut frames, &mut show, &field, vec![key_press(Key::S, Modifiers::NONE)]), "a press after the release runs it again");
|
||||
}
|
||||
|
||||
/// In a focused field, a bound Alt chord that an input method types through (its commit follows the press) is typing:
|
||||
/// the field takes the commit and the command does not run. With its key's text the press runs the command. Guards
|
||||
/// against a key stage that ran the command and let the input method's text through as well.
|
||||
#[trial]
|
||||
fn an_alt_chord_an_input_method_types_through_runs_no_command() {
|
||||
let field = RefCell::new(Field::default());
|
||||
let mut frames = frames_for(Chord::new(Mods::ALT, ChordKey::G));
|
||||
let mut show = window(&field);
|
||||
settle(&mut frames, &mut show, true);
|
||||
let alt_g =
|
||||
|then: Event| vec![Event::ModifiersChanged(Modifiers::ALT), key_press(Key::G, Modifiers::ALT), then, key_release(Key::G, Modifiers::ALT)];
|
||||
assert!(ran(&mut frames, &mut show, &field, alt_g(text("g"))), "with its key's text the press runs the command");
|
||||
assert!(!ran(&mut frames, &mut show, &field, alt_g(Event::Ime(ImeEvent::Commit("ğ".into())))), "an input method's commit is typing");
|
||||
}
|
||||
|
||||
/// A Cut with Cmd and Shift held is Cmd+Shift+X, the app's chord, not the focused field's Cut; a plain Cmd+X Cut stays
|
||||
/// the field's. Guards against a key stage that gave every clipboard event to a field that lists the edit, whatever
|
||||
/// the modifiers.
|
||||
|
||||
Reference in new issue
Block a user