mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 20:14:31 +01:00
fwstub-1: a bound Alt chord's text is withheld only when a command bound to it is enabled (S-L177; Keymap::holders, commands::runs_now): a disabled one leaves the character typed, as Word's Option+Shift+- em dash; trial
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bf31f87696
commit
6f8bf4265a
5 files changed
+75
-5
No files matched your search
@@ -454,6 +454,19 @@ impl Keymap {
|
||||
self.by_trigger.get(&want)?.iter().find(|&&(i, k)| self.at(i, k).is_some()).map(|&(i, _)| self.catalog[i].command.clone())
|
||||
}
|
||||
|
||||
/// Every command a single press of `chord` is bound to, in any scope, in catalog order, each once: a caller that
|
||||
/// asks whether the press can run anything now reads each one's state.
|
||||
pub fn holders(&self, chord: &Chord) -> Vec<CommandRef> {
|
||||
let want = Trigger { chord: chord.effective(self.platform), double: false };
|
||||
let mut found: Vec<usize> = Vec::new();
|
||||
for &(i, k) in self.by_trigger.get(&want).into_iter().flatten() {
|
||||
if self.at(i, k).is_some() && !found.contains(&i) {
|
||||
found.push(i);
|
||||
}
|
||||
}
|
||||
found.into_iter().map(|i| self.catalog[i].command.clone()).collect()
|
||||
}
|
||||
|
||||
/// Adds a binding to a command.
|
||||
pub fn assign(&mut self, command: &CommandRef, binding: Binding, policy: Policy) -> Result<Assigned, AssignError> {
|
||||
let i = self.find(command).ok_or(AssignError::UnknownCommand)?;
|
||||
|
||||
@@ -693,6 +693,13 @@ pub(crate) fn holder(ctx: &Context, chord: &Chord) -> Option<CommandRef> {
|
||||
with(ctx, |board| board.keymap.as_ref().and_then(|cache| cache.keymap.holder(chord)))
|
||||
}
|
||||
|
||||
/// Whether a command the window's keymap binds `chord` to is enabled, as the last pass resolved it for a surface with
|
||||
/// no component of its own (the state a menu row shows): a press of the chord can run something. Notes no reader.
|
||||
pub(crate) fn runs_now(ctx: &Context, chord: &Chord) -> bool {
|
||||
let holders = with(ctx, |board| board.keymap.as_ref().map(|cache| cache.keymap.holders(chord)).unwrap_or_default());
|
||||
holders.iter().any(|command| resolve_from(ctx, command, None).0.enabled)
|
||||
}
|
||||
|
||||
/// The chords bound to the window's keys, which the input hook takes before any component.
|
||||
pub(crate) fn window_shortcuts(ctx: &Context) -> Arc<[(WindowShortcut, Chord)]> {
|
||||
with(ctx, |board| board.keymap.as_ref().map(|cache| Arc::clone(&cache.window_shortcuts)).unwrap_or_default())
|
||||
|
||||
@@ -26,6 +26,7 @@ pub use board::keymap_builds;
|
||||
#[doc(hidden)]
|
||||
pub use board::note_page_root;
|
||||
pub(crate) use board::publish as publish_for;
|
||||
pub(crate) use board::runs_now;
|
||||
pub(crate) use board::take as take_for_instance;
|
||||
pub(crate) use board::{
|
||||
begin, binds_scoped, edit_chords, end, install, note_focus, platform, register_instance, set_input, take_delivered, take_fired, window_shortcuts,
|
||||
|
||||
@@ -53,15 +53,17 @@ struct Strip<'a> {
|
||||
/// An Alt release in this input ended an Alt+numpad code (`raw_input::take_alt_code_ended`): the code's digits
|
||||
/// never reach this stage, so without this the release would read as a lone Alt.
|
||||
alt_code_ended: bool,
|
||||
/// Whether the window's keymap binds a chord (`commands::holder`), enabled or not.
|
||||
/// Whether the window's keymap binds a chord to a command that is enabled now (`commands::runs_now`).
|
||||
bound: &'a dyn Fn(&Chord) -> bool,
|
||||
}
|
||||
|
||||
impl Strip<'_> {
|
||||
/// Whether the text the platform sends with the press `input` stands for is a shortcut's own and belongs to no
|
||||
/// field: Alt is held and the keymap binds the press, so it fires a command or is refused as one. A platform sends
|
||||
/// its key's text with an Alt shortcut (Alt+Q on Linux). A plain key's text is typing, which a focused field or
|
||||
/// list takes before any shortcut (`route::holds_press`), and Cmd and Ctrl chords type nothing.
|
||||
/// field: Alt is held and the keymap binds the press to a command that is enabled, so it fires (S-L177). A platform
|
||||
/// sends its key's text with an Alt shortcut (Alt+Q on Linux). A bound Alt chord whose command is disabled does
|
||||
/// nothing, so its text is typed, as Word types Option+Shift+-'s em dash where Collapse does not apply. A plain key's
|
||||
/// text is typing, which a focused field or list takes before any shortcut (`route::holds_press`), and Cmd and Ctrl
|
||||
/// chords type nothing.
|
||||
fn shortcut_text(&self, input: &KeyInput) -> bool {
|
||||
input.mods.alt && self.layout.candidates(input).iter().any(|chord| (self.bound)(chord))
|
||||
}
|
||||
@@ -470,7 +472,9 @@ pub(crate) fn input_hook(ctx: &Context, input: &RawInput, events: &mut Vec<Arriv
|
||||
let window_shortcuts = commands::window_shortcuts(ctx);
|
||||
let no_stops = keys.is_none() && !record.foreign && focus::tab_from_nothing(ctx, viewport);
|
||||
let latin = &input.latin_layout_chars;
|
||||
let bound = |chord: &Chord| commands::holder(ctx, chord).is_some();
|
||||
// S-L177: an Alt chord's text is withheld only when the press can run its command; a bound command that is
|
||||
// disabled (Word's Alt+Shift+- Collapse outside Outline) leaves the text typed (Option+Shift+- is an em dash).
|
||||
let bound = |chord: &Chord| commands::runs_now(ctx, chord);
|
||||
let (batch, visible, later) = with(ctx, viewport, |stage| {
|
||||
let claims = stage_claims(&record, stage);
|
||||
let edits = |command| keys.is_some_and(|keys| keys.edits.contains(command));
|
||||
|
||||
@@ -718,3 +718,48 @@ fn the_app_catalog_s_typing_rule_lets_an_alt_chord_fire_while_typing() {
|
||||
fn a_bound_alt_chord_s_text_leaves_the_press_to_its_command() {
|
||||
assert_eq!(alt_while_typing(Key::F7, None), vec![SEARCH.to_owned()], "Alt+F7 fires from the text field");
|
||||
}
|
||||
|
||||
/// The app command on Alt+Shift+- in the disabled-chord trial: a word processor's Collapse, which works in Outline
|
||||
/// view only.
|
||||
const COLLAPSE: &str = "hostKeyTrial.collapse";
|
||||
|
||||
/// What Alt+Shift+- does from a text stop, with the em dash the platform types for it in the same frame, while the
|
||||
/// command bound to it is `enabled`: the commands it fired and the text the stop was given.
|
||||
fn alt_shift_minus(enabled: bool) -> (Vec<String>, String) {
|
||||
let field = named_id("document");
|
||||
let typed = RefCell::new(String::new());
|
||||
let mut show = |ui: &mut Ui| {
|
||||
widget(ui, field);
|
||||
register_stop(ui.ctx(), Stop::new(field, KeyUse::of(Input::Text)));
|
||||
let mut intents = crate::testing::focus::intents_of(ui.ctx(), Some(field), false);
|
||||
while let Some(text) = intents.take_text() {
|
||||
typed.borrow_mut().push_str(&text);
|
||||
}
|
||||
};
|
||||
let Ok(binding) = Binding::parse("Alt+Shift+-") else { return (vec!["the trial's chord does not parse".to_owned()], String::new()) };
|
||||
let Ok(catalog) = AppCatalog::new(vec![CommandDef::new(COLLAPSE, "Collapse", Scope::app()).with_default(binding)], 1) else {
|
||||
return (vec!["the trial's catalog is refused".to_owned()], String::new());
|
||||
};
|
||||
let state = crate::runtime::commands::CommandState::new().enabled(enabled);
|
||||
let mut frames = Frames::new();
|
||||
frames.commands(CommandInput::new().catalog(catalog.typing_rule(TypingRule::PlainKeysWait)).state(COLLAPSE, state));
|
||||
frames.frame(Vec::new(), &mut show);
|
||||
focus_stop(&mut frames, &mut show, field);
|
||||
take_output(&frames.ctx);
|
||||
typed.borrow_mut().clear();
|
||||
let alt_shift = Modifiers { alt: true, shift: true, ..Modifiers::NONE };
|
||||
frames.frame(vec![key_press(Key::Minus, alt_shift), Event::Text("\u{2014}".into())], &mut show);
|
||||
frames.frame(Vec::new(), &mut show);
|
||||
let fired = take_output(&frames.ctx).fired.into_iter().map(|fired| fired.command.id).collect();
|
||||
(fired, typed.take())
|
||||
}
|
||||
|
||||
/// fwstub-1 (4): the text of a bound Alt chord is withheld only when its command runs (S-L177, as production's
|
||||
/// `shortcuts.rs` returned unhandled for a disabled Alt command): with the command disabled the shortcut does nothing
|
||||
/// and the character is typed, as Word types Option+Shift+-'s em dash outside Outline view; enabled, the command runs
|
||||
/// and nothing is typed.
|
||||
#[trial]
|
||||
fn a_disabled_alt_chord_leaves_its_text_typed() {
|
||||
assert_eq!(alt_shift_minus(false), (Vec::new(), "\u{2014}".to_owned()), "disabled: the em dash is typed");
|
||||
assert_eq!(alt_shift_minus(true), (vec![COLLAPSE.to_owned()], String::new()), "enabled: the command runs and types nothing");
|
||||
}
|
||||
Reference in new issue
Block a user