use crate::{KeyMod, KeyMods};
#[doc = crate::_tags!(interaction web)]
#[doc = crate::_doc_meta!{location("sys/os/browser/web")}]
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum WebKeyLocation {
#[default]
Standard = 0,
Left = 1,
Right = 2,
NumPad = 3,
}
impl WebKeyLocation {
pub const fn from_repr(from: u8) -> Self {
use WebKeyLocation as L;
match from {
0 => L::Standard,
1 => L::Left,
2 => L::Right,
3 => L::NumPad,
_ => L::Standard,
}
}
}
#[rustfmt::skip]
impl KeyMod {
pub const fn from_web_code(code: &str, location: WebKeyLocation) -> Option<Self> {
use {KeyMod as K, WebKeyLocation as L};
match (code.as_bytes(), location) {
(b"ShiftLeft", L::Left) => Some(K::LeftShift),
(b"ControlLeft", L::Left) => Some(K::LeftControl),
(b"AltLeft", L::Left) => Some(K::LeftAlt),
(b"MetaLeft", L::Left) => Some(K::LeftSuper),
(b"ShiftRight", L::Right) => Some(K::RightShift),
(b"ControlRight", L::Right) => Some(K::RightControl),
(b"AltRight", L::Right) => Some(K::RightAlt),
(b"MetaRight", L::Right) => Some(K::RightSuper),
(b"AltGraph", L::Standard) => Some(K::AltGr),
(b"Level5Shift", L::Standard) => Some(K::IsoLevel5Shift),
_ => None,
}
}
pub const fn to_web_code(self) -> (&'static str, WebKeyLocation) {
use {KeyMod as K, WebKeyLocation as L};
match self {
K::LeftShift => ("ShiftLeft", L::Left),
K::LeftControl => ("ControlLeft", L::Left),
K::LeftAlt => ("AltLeft", L::Left),
K::LeftSuper => ("MetaLeft", L::Left),
K::RightShift => ("ShiftRight", L::Right),
K::RightControl => ("ControlRight", L::Right),
K::RightAlt => ("AltRight", L::Right),
K::RightSuper => ("MetaRight", L::Right),
K::AltGr => ("AltGraph", L::Standard),
K::IsoLevel5Shift => ("Level5Shift", L::Standard),
}
}
pub const fn from_web_key(key: &str, location: WebKeyLocation) -> Option<Self> {
use {KeyMod as K, WebKeyLocation as L};
match (key.as_bytes(), location) {
(b"Shift", L::Left) => Some(K::LeftShift),
(b"Control", L::Left) => Some(K::LeftControl),
(b"Alt", L::Left) => Some(K::LeftAlt),
(b"Meta", L::Left) => Some(K::LeftSuper),
(b"Shift", L::Right) => Some(K::RightShift),
(b"Control", L::Right) => Some(K::RightControl),
(b"Alt", L::Right) => Some(K::RightAlt),
(b"Meta", L::Right) => Some(K::RightSuper),
(b"AltGraph", L::Standard) => Some(K::AltGr),
(b"Level5Shift", L::Standard) => Some(K::IsoLevel5Shift),
_ => None,
}
}
pub const fn to_web_key(self) -> (&'static str, WebKeyLocation) {
use {KeyMod as K, WebKeyLocation as L};
match self {
K::LeftShift => ("Shift", L::Left),
K::LeftControl => ("Control", L::Left),
K::LeftAlt => ("Alt", L::Left),
K::LeftSuper => ("Meta", L::Left),
K::RightShift => ("Shift", L::Right),
K::RightControl => ("Control", L::Right),
K::RightAlt => ("Alt", L::Right),
K::RightSuper => ("Meta", L::Right),
K::AltGr => ("AltGraph", L::Standard),
K::IsoLevel5Shift => ("Level5Shift", L::Standard),
}
}
}
impl KeyMods {
pub const fn from_web(bits: u8) -> Self {
Self::from_bits(bits as u16)
}
pub const fn to_web(self) -> u8 {
(self.bits() & 0x00FF) as u8
}
}