use crate::{
input::{InputType, Key, KeyMod, Received},
Position,
};
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Input {
pub received: Received,
pub keymod: KeyMod,
pub itype: InputType,
pub cell: Option<Position>,
pub offset: Option<Position>,
}
mod core_impls {
use super::{Input, Position};
use crate::sys::{NcInput, NcReceived};
use core::fmt;
impl fmt::Display for Input {
#[rustfmt::skip]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let cell = if let Some(c) = self.cell { c.to_string() } else { "None".into() };
let offset = if let Some(o) = self.offset { o.to_string() } else { "None".into() };
write!(f,
"{} {} {} {} {}",
self.received, self.keymod, self.itype, cell, offset,
)
}
}
impl fmt::Debug for Input {
#[rustfmt::skip]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let cell = if let Some(c) = self.cell { c.to_string() } else { "None".into() };
let offset = if let Some(o) = self.offset { o.to_string() } else { "None".into() };
write!(f,
"Input {{received:{} mod:{} type:{} cell:{} offset:{} }}",
self.received, self.keymod, self.itype, cell, offset,
)
}
}
impl From<(NcReceived, NcInput)> for Input {
fn from(received_input: (NcReceived, NcInput)) -> Input {
let (received, input) = received_input;
let (mut cell, mut offset) = (None, None);
if let NcReceived::Key(k) = received {
if k.is_mouse() {
if input.y != -1 {
cell = Some(Position::new(input.x, input.y));
}
if input.ypx != -1 {
offset = Some(Position::new(input.xpx, input.ypx));
}
}
};
Input {
received: received.into(),
keymod: input.modifiers.into(),
itype: input.evtype.into(),
cell,
offset,
}
}
}
}
impl Input {
#[inline]
pub const fn received(&self) -> bool {
!matches![self.received, Received::NoInput]
}
#[inline]
pub const fn some_key(&self) -> bool {
matches!(self.received, Received::Key(_))
}
#[inline]
pub fn is_key(&self, key: Key) -> bool {
self.received.is_key(key)
}
#[inline]
pub const fn some_char(&self) -> bool {
matches!(self.received, Received::Char(_))
}
#[inline]
pub const fn is_char(&self, character: char) -> bool {
self.received.is_char(character)
}
#[inline]
pub const fn is_press(&self) -> bool {
self.itype.is_press()
}
#[inline]
pub const fn is_repeat(&self) -> bool {
self.itype.is_repeat()
}
#[inline]
pub const fn is_release(&self) -> bool {
self.itype.is_release()
}
}