devela/sys/os/browser/web/event/key/
location.rs1use crate::{KeyMod, KeyMods};
11
12#[doc = crate::_tags!(interaction web)]
13#[doc = crate::_doc_meta!{location("sys/os/browser/web")}]
15#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
18pub enum WebKeyLocation {
19 #[default]
21 Standard = 0,
22 Left = 1,
24 Right = 2,
26 NumPad = 3,
28}
29impl WebKeyLocation {
30 pub const fn from_repr(from: u8) -> Self {
32 use WebKeyLocation as L;
33 match from {
34 0 => L::Standard,
35 1 => L::Left,
36 2 => L::Right,
37 3 => L::NumPad,
38 _ => L::Standard,
39 }
40 }
41}
42
43#[rustfmt::skip]
44impl KeyMod {
45 pub const fn from_web_code(code: &str, location: WebKeyLocation) -> Option<Self> {
52 use {KeyMod as K, WebKeyLocation as L};
53 match (code.as_bytes(), location) {
54 (b"ShiftLeft", L::Left) => Some(K::LeftShift),
55 (b"ControlLeft", L::Left) => Some(K::LeftControl),
56 (b"AltLeft", L::Left) => Some(K::LeftAlt),
57 (b"MetaLeft", L::Left) => Some(K::LeftSuper),
58 (b"ShiftRight", L::Right) => Some(K::RightShift),
59 (b"ControlRight", L::Right) => Some(K::RightControl),
60 (b"AltRight", L::Right) => Some(K::RightAlt),
61 (b"MetaRight", L::Right) => Some(K::RightSuper),
62 (b"AltGraph", L::Standard) => Some(K::AltGr),
63 (b"Level5Shift", L::Standard) => Some(K::IsoLevel5Shift),
64 _ => None,
65 }
66 }
67 pub const fn to_web_code(self) -> (&'static str, WebKeyLocation) {
73 use {KeyMod as K, WebKeyLocation as L};
74 match self {
75 K::LeftShift => ("ShiftLeft", L::Left),
76 K::LeftControl => ("ControlLeft", L::Left),
77 K::LeftAlt => ("AltLeft", L::Left),
78 K::LeftSuper => ("MetaLeft", L::Left),
79 K::RightShift => ("ShiftRight", L::Right),
80 K::RightControl => ("ControlRight", L::Right),
81 K::RightAlt => ("AltRight", L::Right),
82 K::RightSuper => ("MetaRight", L::Right),
83 K::AltGr => ("AltGraph", L::Standard),
84 K::IsoLevel5Shift => ("Level5Shift", L::Standard),
85 }
86 }
87 pub const fn from_web_key(key: &str, location: WebKeyLocation) -> Option<Self> {
93 use {KeyMod as K, WebKeyLocation as L};
94 match (key.as_bytes(), location) {
95 (b"Shift", L::Left) => Some(K::LeftShift),
96 (b"Control", L::Left) => Some(K::LeftControl),
97 (b"Alt", L::Left) => Some(K::LeftAlt),
98 (b"Meta", L::Left) => Some(K::LeftSuper),
99 (b"Shift", L::Right) => Some(K::RightShift),
100 (b"Control", L::Right) => Some(K::RightControl),
101 (b"Alt", L::Right) => Some(K::RightAlt),
102 (b"Meta", L::Right) => Some(K::RightSuper),
103 (b"AltGraph", L::Standard) => Some(K::AltGr),
104 (b"Level5Shift", L::Standard) => Some(K::IsoLevel5Shift),
105 _ => None,
106 }
107 }
108 pub const fn to_web_key(self) -> (&'static str, WebKeyLocation) {
113 use {KeyMod as K, WebKeyLocation as L};
114 match self {
115 K::LeftShift => ("Shift", L::Left),
116 K::LeftControl => ("Control", L::Left),
117 K::LeftAlt => ("Alt", L::Left),
118 K::LeftSuper => ("Meta", L::Left),
119 K::RightShift => ("Shift", L::Right),
120 K::RightControl => ("Control", L::Right),
121 K::RightAlt => ("Alt", L::Right),
122 K::RightSuper => ("Meta", L::Right),
123 K::AltGr => ("AltGraph", L::Standard),
124 K::IsoLevel5Shift => ("Level5Shift", L::Standard),
125 }
126 }
127}
128
129impl KeyMods {
130 pub const fn from_web(bits: u8) -> Self {
142 Self::from_bits(bits as u16)
143 }
144
145 pub const fn to_web(self) -> u8 {
149 (self.bits() & 0x00FF) as u8
150 }
151}