use crate::{ConstInit, Key, KeyMods, KeyState};
#[doc = crate::_tags!(event interaction)]
#[doc = crate::_doc_meta!{
location("ui/event"),
test_size_of(EventKey = 20|160; niche Option),
}]
#[doc = "See also [`EventKeyFfi`]."]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct EventKey {
pub semantic: Key,
pub physical: Key,
pub mods: KeyMods,
pub state: KeyState,
}
impl ConstInit for EventKey {
const INIT: Self = Self {
semantic: Key::INIT,
physical: Key::INIT,
mods: KeyMods::INIT,
state: KeyState::INIT,
};
}
impl EventKey {
pub const fn new(semantic: Key, physical: Key, mods: KeyMods, state: KeyState) -> Self {
Self { semantic, physical, mods, state }
}
pub const fn press(key: Key) -> Self {
Self::new(key, key, KeyMods::new(), KeyState::Press)
}
pub const fn modified_press(key: Key, mods: KeyMods) -> Self {
Self::new(key, key, mods, KeyState::Press)
}
pub const fn text(c: char) -> Self {
Self::new(Key::Char(c), Key::Unknown, KeyMods::new(), KeyState::Press)
}
pub const fn modified_text(c: char, mods: KeyMods) -> Self {
Self::new(Key::Char(c), Key::Unknown, mods, KeyState::Press)
}
pub const fn with_state(mut self, state: KeyState) -> Self {
self.state = state;
self
}
pub const fn with_mods(mut self, mods: KeyMods) -> Self {
self.mods = mods;
self
}
}
#[cfg(ffi··)]
pub use ffi::*;
#[cfg(ffi··)]
#[rustfmt::skip]
#[cfg_attr(nightly_doc, doc(cfg(ffi··)))]
mod ffi {
use super::*;
use crate::KeyFfi;
#[doc = crate::_tags!(event interaction ffi)]
#[doc = crate::_doc_meta!{location("ui/event")}]
#[repr(C)]
#[allow(missing_docs)]
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct EventKeyFfi {
#[doc = crate::_tags!(ffi)]
pub semantic: KeyFfi,
#[doc = crate::_tags!(ffi)]
pub physical: KeyFfi,
pub state: KeyState,
pub mods: KeyMods,
}
impl EventKey {
pub const fn to_ffi(&self) -> EventKeyFfi {
EventKeyFfi {
semantic: self.semantic.to_ffi(),
physical: self.physical.to_ffi(),
state: self.state,
mods: self.mods,
}
}
pub const fn from_ffi(from: &EventKeyFfi) -> EventKey {
EventKey {
semantic: Key::from_ffi(from.semantic),
physical: Key::from_ffi(from.physical),
state: from.state,
mods: from.mods,
}
}
}
impl From<&EventKey> for EventKeyFfi { fn from(e: &EventKey) -> Self { EventKey::to_ffi(e) } }
impl From<&EventKeyFfi> for EventKey { fn from(e: &EventKeyFfi) -> Self { Self::from_ffi(e) } }
impl From<EventKey> for EventKeyFfi { fn from(e: EventKey) -> Self { EventKey::to_ffi(&e) } }
impl From<EventKeyFfi> for EventKey { fn from(e: EventKeyFfi) -> Self { Self::from_ffi(&e) } }
}