Skip to content

Commit

Permalink
bug: Fix the match & prefix match bug.
Browse files Browse the repository at this point in the history
When the last keychord fails to match, we should check if it itself
matches something.
  • Loading branch information
shanecelis committed Dec 1, 2024
1 parent 7acecd2 commit 9861067
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
12 changes: 9 additions & 3 deletions src/chord.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use bevy::{
input::keyboard::KeyCode,
reflect::{Enum, Reflect},
input::keyboard::KeyCode, prelude::{Deref, DerefMut, Resource}, reflect::{Enum, Reflect}
};

use std::fmt;
use std::{collections::VecDeque, fmt};

use keyseq::Modifiers;

Expand Down Expand Up @@ -56,3 +55,10 @@ impl From<KeyCode> for KeyChord {
pub(crate) fn is_modifier(key: KeyCode) -> bool {
!Modifiers::from(key).is_empty()
}

/// Manually add key chords to be processed as through they were pressed by the
/// user.
///
/// Normally this does not need to be manipulated. It is a kind of escape hatch.
#[derive(Resource, Debug, Deref, DerefMut, Default)]
pub struct KeyChordQueue(pub VecDeque<KeyChord>);
11 changes: 5 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
#![doc = include_str!("../README.md")]
#![forbid(missing_docs)]

pub use chord::KeyChord;
pub use plugin::InputSequencePlugin;
pub use time_limit::TimeLimit;

pub mod action;
pub mod cache;
mod chord;
Expand All @@ -15,6 +11,10 @@ pub mod input_sequence;
mod plugin;
mod time_limit;

pub use chord::{KeyChord, KeyChordQueue};
pub use plugin::{InputSequencePlugin};
pub use time_limit::TimeLimit;

pub use keyseq::{
bevy::{pkey as key, pkeyseq as keyseq},
Modifiers,
Expand All @@ -24,8 +24,7 @@ pub use keyseq::{
pub mod prelude {
pub use super::input_sequence::{ButtonSequence, InputSequence, KeySequence};
pub use super::{action, keyseq, InputSequencePlugin, Modifiers, TimeLimit};

pub use super::chord::KeyChord;
pub use super::{KeyChordQueue, KeyChord};
pub use super::cond_system::IntoCondSystem;
pub use std::time::Duration;
}
28 changes: 21 additions & 7 deletions src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::collections::{HashMap, VecDeque};

use crate::{
cache::InputSequenceCache,
chord::is_modifier,
chord::{KeyChordQueue, is_modifier},
frame_time::FrameTime,
input_sequence::{ButtonSequence, InputSequence, KeySequence},
KeyChord, Modifiers,
Expand Down Expand Up @@ -53,6 +53,7 @@ impl Plugin for InputSequencePlugin {
{
// Add key sequence.
app.init_resource::<InputSequenceCache<KeyChord, ()>>();
app.init_resource::<KeyChordQueue>();

for (schedule, set) in &self.schedules {
if let Some(set) = set {
Expand Down Expand Up @@ -233,21 +234,23 @@ fn key_sequence_matcher(
mut cache: ResMut<InputSequenceCache<KeyChord, ()>>,
frame_count: Res<FrameCount>,
mut commands: Commands,
mut keychord_queue: ResMut<KeyChordQueue>,
) {
let mods = Modifiers::from(&keys);
let now = FrameTime {
frame: frame_count.0,
time: time.elapsed_seconds(),
};
let maybe_start = last_times.front().cloned();
let mut input = keys
let mut input = keychord_queue.drain(..).chain(
keys
.get_just_pressed()
.filter(|k| !is_modifier(**k))
.map(|k| {
let chord = KeyChord(mods, *k);
last_times.push_back(now.clone());
chord
})
}))
.peekable();
if input.peek().is_none() {
return;
Expand Down Expand Up @@ -297,11 +300,22 @@ where
None => {
search.reset();
// This could be the start of a new sequence.
if search.query(&k).is_none() {
// This may not be necessary.
search.reset();
//
// Let's check it.
match search.query(&k) {
Some(Answer::Match) => {
let result = Some(search.value().unwrap());
search.reset();
result
}
Some(Answer::PrefixAndMatch) => Some(search.value().unwrap()),
Some(Answer::Prefix) => None,
None => {
// This may not be necessary.
search.reset();
None
}
}
None
}
}
})
Expand Down

0 comments on commit 9861067

Please sign in to comment.