use std::cell::RefCell;
use crate::event::{Key, Modifiers};
thread_local! {
static QUEUE: RefCell<Vec<(Key, Modifiers)>> = RefCell::new(Vec::new());
}
pub fn push_synthetic_key(key: Key, modifiers: Modifiers) {
QUEUE.with(|q| q.borrow_mut().push((key, modifiers)));
}
pub fn drain_synthetic_keys() -> Vec<(Key, Modifiers)> {
QUEUE.with(|q| q.borrow_mut().drain(..).collect())
}
#[cfg(test)]
pub fn peek_pending_count() -> usize {
QUEUE.with(|q| q.borrow().len())
}
#[cfg(test)]
pub fn clear() {
QUEUE.with(|q| q.borrow_mut().clear());
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn enqueue_and_drain() {
clear();
push_synthetic_key(Key::Char('a'), Modifiers::default());
push_synthetic_key(Key::Backspace, Modifiers::default());
assert_eq!(peek_pending_count(), 2);
let drained = drain_synthetic_keys();
assert_eq!(drained.len(), 2);
assert!(matches!(drained[0].0, Key::Char('a')));
assert!(matches!(drained[1].0, Key::Backspace));
assert_eq!(drain_synthetic_keys().len(), 0);
}
}