use super::{KeyRouting, QUEUE_PER_WIDGET, QUEUE_TOTAL};
use crate::context::{key_events::KeyEvent, Id};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Pushed {
Queued,
Dropped {
first: bool,
},
}
impl KeyRouting {
pub(crate) fn push(&mut self, id: Id, event: KeyEvent) -> Pushed {
let release = event.is_release();
if !release
&& (self.pending >= QUEUE_TOTAL
|| self
.queue
.get(&id)
.is_some_and(|waiting| waiting.len() >= QUEUE_PER_WIDGET))
{
self.dropped += 1;
let first = !self.overflowed;
self.overflowed = true;
return Pushed::Dropped { first };
}
self.queue.entry(id).or_default().push_back(event);
self.pending += 1;
Pushed::Queued
}
pub(crate) fn take(&mut self, id: Id) -> Vec<KeyEvent> {
match self.queue.remove(&id) {
Some(waiting) => {
self.pending -= waiting.len();
waiting.into()
}
None => Vec::new(),
}
}
}