mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 15:44:26 +00:00
Keys: a text field holds the presses whose text it took, in place of the typed-shortcut filter; focus_holds names its letters
This commit is contained in:
@@ -83,8 +83,6 @@ struct Press {
|
||||
pub(crate) struct Shortcut {
|
||||
pub(crate) input: KeyInput,
|
||||
pub(crate) phase: FirePhase,
|
||||
/// The press typed text in this pass.
|
||||
typed: bool,
|
||||
/// The index of the press in `Batch::presses`.
|
||||
press: Option<usize>,
|
||||
}
|
||||
|
||||
@@ -96,14 +96,11 @@ impl Strip<'_> {
|
||||
if key == Some(Key::Enter) {
|
||||
batch.enter_down = Some(false);
|
||||
}
|
||||
batch.shortcuts.push(Shortcut { input, phase: FirePhase::Release, typed: false, press: None });
|
||||
batch.shortcuts.push(Shortcut { input, phase: FirePhase::Release, press: None });
|
||||
true
|
||||
}
|
||||
KeyEvent::Text(text) => {
|
||||
alt.other_key = true;
|
||||
if let Some(press) = batch.shortcuts.last_mut().filter(|shortcut| shortcut.phase == FirePhase::Press) {
|
||||
press.typed = true;
|
||||
}
|
||||
if let Some((input, pressed)) = batch.last_press.take() {
|
||||
if let Some(press) = pressed.and_then(|at| batch.presses.get_mut(at)) {
|
||||
press.text = Some(batch.texts.len());
|
||||
@@ -142,7 +139,7 @@ impl Strip<'_> {
|
||||
// A component that reads every chord gets the event as the chord's press, the way the key made it.
|
||||
self.press(batch, key, &input, false);
|
||||
} else if !sent {
|
||||
batch.shortcuts.push(Shortcut { input, phase: FirePhase::Press, typed: false, press: None });
|
||||
batch.shortcuts.push(Shortcut { input, phase: FirePhase::Press, press: None });
|
||||
}
|
||||
// The event stands for the chord, so the shortcut path is its only reader: egui does not copy or paste
|
||||
// on top of the command.
|
||||
@@ -211,7 +208,7 @@ impl Strip<'_> {
|
||||
// The canvas has Space (`runtime::sampler`): only the shortcut dispatcher reads it, so the dialog's
|
||||
// focused control does not activate.
|
||||
if !repeat {
|
||||
batch.shortcuts.push(Shortcut { input: *input, phase: FirePhase::Press, typed: false, press: None });
|
||||
batch.shortcuts.push(Shortcut { input: *input, phase: FirePhase::Press, press: None });
|
||||
}
|
||||
false
|
||||
} else if (self.foreign && (bare_tab || escape)) || canvas_tab || canvas_edit {
|
||||
@@ -243,7 +240,7 @@ impl Strip<'_> {
|
||||
} else {
|
||||
batch.presses.push(Press { key, mods, repeat, in_egui: None, release: None, text: None });
|
||||
if !repeat {
|
||||
batch.shortcuts.push(Shortcut { input: *input, phase: FirePhase::Press, typed: false, press: Some(batch.presses.len() - 1) });
|
||||
batch.shortcuts.push(Shortcut { input: *input, phase: FirePhase::Press, press: Some(batch.presses.len() - 1) });
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
@@ -183,37 +183,31 @@ fn owned_shortcuts(ctx: &Context, target: Option<&StopRec>, batch: &Batch, consu
|
||||
.zip(consumed)
|
||||
.map(|(press, taken)| holds_press(owner, target, press, *taken == Taken::Yes, press.text.is_some_and(|at| taken_texts[at])))
|
||||
.collect();
|
||||
let keys = if silent {
|
||||
Vec::new()
|
||||
} else {
|
||||
batch
|
||||
.shortcuts
|
||||
.iter()
|
||||
.filter(|shortcut| !(typing && shortcut.typed))
|
||||
.filter(|shortcut| !shortcut.press.is_some_and(|at| held[at]))
|
||||
.copied()
|
||||
.collect()
|
||||
};
|
||||
let keys =
|
||||
if silent { Vec::new() } else { batch.shortcuts.iter().filter(|shortcut| !shortcut.press.is_some_and(|at| held[at])).copied().collect() };
|
||||
(Shortcuts { keys, typing, dialog, layout: Layout::default() }, held)
|
||||
}
|
||||
|
||||
/// Whether the focused stop `target`, whose keys `owner` owns, holds `press`: the press is its own and reaches neither
|
||||
/// egui's input, where the app reads its own keys, nor the app's shortcuts. A widget or popup holds the presses its
|
||||
/// routing `taken`, the keys its filter keeps from the key stage, and the presses whose text it took (`typed`: the
|
||||
/// type-ahead of a list). A menu or a recorder holds every press.
|
||||
/// type-ahead of a list). A text field holds the presses whose text it took, its typing; the egui text widget behind it
|
||||
/// reads the text, not the press. A menu or a recorder holds every press.
|
||||
fn holds_press(owner: KeyOwner, target: Option<&StopRec>, press: &Press, taken: bool, typed: bool) -> bool {
|
||||
let Some(keys) = target.map(|rec| &rec.stop.keys) else { return false };
|
||||
if keeps_every_key(owner) {
|
||||
return true;
|
||||
}
|
||||
let widget = owner != KeyOwner::Blocked && matches!(keys.owner, OwnerClass::Widget | OwnerClass::Popup | OwnerClass::PassivePopup);
|
||||
let open = owner != KeyOwner::Blocked;
|
||||
let widget = open && matches!(keys.owner, OwnerClass::Widget | OwnerClass::Popup | OwnerClass::PassivePopup);
|
||||
let kept = match keys.keep {
|
||||
focus::KeepKeys::Tab => press.key == Key::Tab,
|
||||
focus::KeepKeys::InlineArrows => matches!(press.key, Key::Left | Key::Right),
|
||||
focus::KeepKeys::BlockArrows => matches!(press.key, Key::Left | Key::Right | Key::Up | Key::Down),
|
||||
_ => false,
|
||||
};
|
||||
widget && (taken || typed || kept)
|
||||
let text = open && keys.owner == OwnerClass::Text;
|
||||
(widget && (taken || typed || kept)) || (text && typed)
|
||||
}
|
||||
|
||||
/// `Menu` and `Recorder` keep every key (9.3): no press of theirs reaches egui's input or the app's shortcuts.
|
||||
|
||||
@@ -36,3 +36,37 @@ fn clearing_a_search_field_with_a_draft_leaves_it_empty_after_blur() {
|
||||
fixture.idle(3);
|
||||
assert_eq!(*text.borrow(), "", "leaving the field commits nothing back");
|
||||
}
|
||||
|
||||
/// A letter typed into a text field is the field's own: the field gets the text, the app reads no press of it, and
|
||||
/// `focus_holds` names the letter before it is typed.
|
||||
#[trial]
|
||||
fn a_letter_typed_into_a_field_is_its_own() {
|
||||
let text = Rc::new(RefCell::new(String::new()));
|
||||
let app_read = Rc::new(RefCell::new(Vec::new()));
|
||||
let (shown, read) = (text.clone(), app_read.clone());
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
read.borrow_mut().extend(ui.input(|input| {
|
||||
input
|
||||
.events
|
||||
.iter()
|
||||
.filter_map(|event| match event {
|
||||
egui::Event::Key { key, pressed: true, .. } => Some(*key),
|
||||
_ => None,
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
}));
|
||||
TextField::new("Name", Bind::bound(&mut *shown.borrow_mut())).show(ui);
|
||||
});
|
||||
fixture.settle();
|
||||
fixture.key(Key::Tab, Modifiers::NONE);
|
||||
fixture.settle();
|
||||
assert!(
|
||||
craft_ui::runtime::keys::focus_holds(fixture.ctx(), craft_keymap::Chord::plain(craft_keymap::Key::T)),
|
||||
"the field names a letter as its own"
|
||||
);
|
||||
fixture.frame(vec![craft_ui::testing::input::key_press(Key::T, Modifiers::NONE), craft_ui::testing::input::text("t")]);
|
||||
fixture.frame(vec![craft_ui::testing::focus::key_release(Key::T, Modifiers::NONE)]);
|
||||
fixture.idle(2);
|
||||
assert_eq!(*text.borrow(), "t", "the field gets the text");
|
||||
assert!(!app_read.borrow().contains(&Key::T), "and the app reads no press of it");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user