diff --git a/src/action.rs b/src/action.rs index d859cd1..8c9758b 100644 --- a/src/action.rs +++ b/src/action.rs @@ -1,7 +1,7 @@ //! The different actions that can be done. use crate::key_code::KeyCode; -use crate::layout::{StackedIter, WaitingAction}; +use crate::layout::{QueuedIter, WaitingAction}; use core::fmt::Debug; /// Behavior configuration of HoldTap. @@ -30,7 +30,7 @@ pub enum HoldTapConfig { /// supplied handler function. /// /// The input to the custom handler will be an iterator that returns - /// [Stacked] [Events](Event). The order of the events matches the order the + /// [Queued] [Events](Event). The order of the events matches the order the /// corresponding key was pressed/released, i.e. the first event is the /// event first received after the HoldTap action key is pressed. /// @@ -49,12 +49,12 @@ pub enum HoldTapConfig { /// ``` /// use keyberon::action::{Action, HoldTapConfig, HoldTapAction}; /// use keyberon::key_code::KeyCode; - /// use keyberon::layout::{StackedIter, WaitingAction, Event}; + /// use keyberon::layout::{QueuedIter, WaitingAction, Event}; /// /// /// Trigger a `Tap` action on the left side of the keyboard if another /// /// key on the left side of the keyboard is pressed. - /// fn left_mod(stacked_iter: StackedIter) -> Option { - /// match stacked_iter.map(|s| s.event()).find(|e| e.is_press()) { + /// fn left_mod(queued_iter: QueuedIter) -> Option { + /// match queued_iter.map(|s| s.event()).find(|e| e.is_press()) { /// Some(Event::Press(_, j)) if j < 6 => Some(WaitingAction::Tap), /// _ => None, /// } @@ -62,8 +62,8 @@ pub enum HoldTapConfig { /// /// /// Trigger a `Tap` action on the right side of the keyboard if another /// /// key on the right side of the keyboard is pressed. - /// fn right_mod(stacked_iter: StackedIter) -> Option { - /// match stacked_iter.map(|s| s.event()).find(|e| e.is_press()) { + /// fn right_mod(queued_iter: QueuedIter) -> Option { + /// match queued_iter.map(|s| s.event()).find(|e| e.is_press()) { /// Some(Event::Press(_, j)) if j > 5 => Some(WaitingAction::Tap), /// _ => None, /// } @@ -89,7 +89,7 @@ pub enum HoldTapConfig { /// tap_hold_interval: 0, /// }); /// ``` - Custom(fn(StackedIter) -> Option), + Custom(fn(QueuedIter) -> Option), } impl Debug for HoldTapConfig { @@ -100,7 +100,7 @@ impl Debug for HoldTapConfig { HoldTapConfig::PermissiveHold => f.write_str("PermissiveHold"), HoldTapConfig::Custom(func) => f .debug_tuple("Custom") - .field(&(*func as fn(StackedIter<'static>) -> Option) as &dyn Debug) + .field(&(*func as fn(QueuedIter<'static>) -> Option) as &dyn Debug) .finish(), } } @@ -113,7 +113,7 @@ impl PartialEq for HoldTapConfig { | (HoldTapConfig::HoldOnOtherKeyPress, HoldTapConfig::HoldOnOtherKeyPress) | (HoldTapConfig::PermissiveHold, HoldTapConfig::PermissiveHold) => true, (HoldTapConfig::Custom(self_func), HoldTapConfig::Custom(other_func)) => { - *self_func as fn(StackedIter<'static>) -> Option == *other_func + *self_func as fn(QueuedIter<'static>) -> Option == *other_func } _ => false, } diff --git a/src/layout.rs b/src/layout.rs index fb1707c..3981e91 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -68,10 +68,10 @@ pub type Layers< K = KeyCode, > = [[[Action; C]; R]; L]; -/// The current event stack. +/// The current event queue. /// -/// Events can be retrieved by iterating over this struct and calling [Stacked::event]. -type Stack = ArrayDeque<[Stacked; 16], arraydeque::behavior::Wrapping>; +/// Events can be retrieved by iterating over this struct and calling [Queued::event]. +type Queue = ArrayDeque<[Queued; 16], arraydeque::behavior::Wrapping>; /// The layout manager. It takes `Event`s and `tick`s as input, and /// generate keyboard reports. @@ -89,7 +89,7 @@ pub struct Layout< default_layer: usize, states: Vec, 64>, waiting: Option>, - stacked: Stack, + queued: Queue, tap_hold_tracker: TapHoldTracker, } @@ -240,33 +240,33 @@ pub enum WaitingAction { } impl WaitingState { - fn tick(&mut self, stacked: &Stack) -> Option { + fn tick(&mut self, queued: &Queue) -> Option { self.timeout = self.timeout.saturating_sub(1); match self.config { HoldTapConfig::Default => (), HoldTapConfig::HoldOnOtherKeyPress => { - if stacked.iter().any(|s| s.event.is_press()) { + if queued.iter().any(|s| s.event.is_press()) { return Some(WaitingAction::Hold); } } HoldTapConfig::PermissiveHold => { - for (x, s) in stacked.iter().enumerate() { + for (x, s) in queued.iter().enumerate() { if s.event.is_press() { let (i, j) = s.event.coord(); let target = Event::Release(i, j); - if stacked.iter().skip(x + 1).any(|s| s.event == target) { + if queued.iter().skip(x + 1).any(|s| s.event == target) { return Some(WaitingAction::Hold); } } } } HoldTapConfig::Custom(func) => { - if let waiting_action @ Some(_) = (func)(StackedIter(stacked.iter())) { + if let waiting_action @ Some(_) = (func)(QueuedIter(queued.iter())) { return waiting_action; } } } - if let Some(&Stacked { since, .. }) = stacked + if let Some(&Queued { since, .. }) = queued .iter() .find(|s| self.is_corresponding_release(&s.event)) { @@ -286,13 +286,13 @@ impl WaitingState { } } -/// An iterator over the currently stacked events. +/// An iterator over the currently queued events. /// -/// Events can be retrieved by iterating over this struct and calling [Stacked::event]. -pub struct StackedIter<'a>(arraydeque::Iter<'a, Stacked>); +/// Events can be retrieved by iterating over this struct and calling [Queued::event]. +pub struct QueuedIter<'a>(arraydeque::Iter<'a, Queued>); -impl<'a> Iterator for StackedIter<'a> { - type Item = &'a Stacked; +impl<'a> Iterator for QueuedIter<'a> { + type Item = &'a Queued; fn next(&mut self) -> Option { self.0.next() } @@ -301,18 +301,18 @@ impl<'a> Iterator for StackedIter<'a> { } } -/// An event, waiting in a stack to be processed. +/// An event, waiting in a queue to be processed. #[derive(Debug)] -pub struct Stacked { +pub struct Queued { event: Event, since: u16, } -impl From for Stacked { +impl From for Queued { fn from(event: Event) -> Self { - Stacked { event, since: 0 } + Queued { event, since: 0 } } } -impl Stacked { +impl Queued { fn tick(&mut self) { self.since = self.since.saturating_add(1); } @@ -345,7 +345,7 @@ impl CustomEvent { self.states = self.states.iter().filter_map(State::tick).collect(); - self.stacked.iter_mut().for_each(Stacked::tick); + self.queued.iter_mut().for_each(Queued::tick); self.tap_hold_tracker.tick(); match &mut self.waiting { - Some(w) => match w.tick(&self.stacked) { + Some(w) => match w.tick(&self.queued) { Some(WaitingAction::Hold) => self.waiting_into_hold(), Some(WaitingAction::Tap) => self.waiting_into_tap(), Some(WaitingAction::NoOp) => self.drop_waiting(), None => CustomEvent::NoEvent, }, - None => match self.stacked.pop_front() { - Some(s) => self.unstack(s), + None => match self.queued.pop_front() { + Some(s) => self.resolve_queued_event(s), None => CustomEvent::NoEvent, }, } } - fn unstack(&mut self, stacked: Stacked) -> CustomEvent { + fn resolve_queued_event(&mut self, queued: Queued) -> CustomEvent { use Event::*; - match stacked.event { + match queued.event { Release(i, j) => { let mut custom = CustomEvent::NoEvent; self.states = self @@ -417,15 +417,15 @@ impl { let action = self.press_as_action((i, j), self.current_layer()); - self.do_action(action, (i, j), stacked.since) + self.do_action(action, (i, j), queued.since) } } } /// Register a key event. pub fn event(&mut self, event: Event) { - if let Some(stacked) = self.stacked.push_back(event.into()) { + if let Some(queued) = self.queued.push_back(event.into()) { self.waiting_into_hold(); - self.unstack(stacked); + self.resolve_queued_event(queued); } } fn press_as_action(&self, coord: (u8, u8), layer: usize) -> &'static Action { @@ -859,16 +859,16 @@ mod test { #[test] fn custom_handler() { - fn always_tap(_: StackedIter) -> Option { + fn always_tap(_: QueuedIter) -> Option { Some(WaitingAction::Tap) } - fn always_hold(_: StackedIter) -> Option { + fn always_hold(_: QueuedIter) -> Option { Some(WaitingAction::Hold) } - fn always_nop(_: StackedIter) -> Option { + fn always_nop(_: QueuedIter) -> Option { Some(WaitingAction::NoOp) } - fn always_none(_: StackedIter) -> Option { + fn always_none(_: QueuedIter) -> Option { None } static LAYERS: Layers<4, 1, 1> = [[[