devela/ui/event/key/mods.rs
1// devela::ui::event::key::dif
2//
3//! Defines [`KeyMod`], [`KeyMods`].
4//
5// TOC
6// - enum KeyMod
7// - struct KeyMods
8
9#![allow(unused, missing_docs)] // WIP
10
11use crate::{ConstInit, impl_trait, is, set};
12
13/* definitions */
14
15#[doc = crate::_tags!(interaction member)]
16/// Modifier key codes (when pressed by themselves)
17#[doc = crate::_doc_meta!{location("ui/event")}]
18///
19/// These keys modify the behavior of other keys when held down.
20//
21// - https://docs.rs/crossterm/latest/crossterm/event/enum.ModifierKey.html
22#[repr(u8)]
23#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
24#[non_exhaustive]
25#[allow(missing_docs)]
26pub enum KeyMod {
27 /// Left **Shift** key.
28 LeftShift,
29 /// Left **Control** (Control) key.
30 LeftControl,
31 /// Left **Alt** key.
32 LeftAlt,
33 /// Left **Super** key (Windows key on Windows, Command ⌘ on macOS).
34 LeftSuper,
35 /// Right **Shift** key.
36 RightShift,
37 /// Right **Control** key.
38 RightControl,
39 /// Right **Alt** key.
40 RightAlt,
41 /// Right **Super** key (Windows key on Windows, Command ⌘ on macOS).
42 RightSuper,
43 /// Used to access alternative characters on some keyboards.
44 ///
45 /// Also known as *ISO Level 3 Shift*.
46 AltGr,
47 /// **ISO Level 5 Shift** key (used in some advanced keyboard layouts).
48 IsoLevel5Shift,
49}
50impl ConstInit for KeyMod {
51 const INIT: Self = Self::LeftShift;
52}
53#[allow(non_upper_case_globals)]
54impl KeyMod {
55 /// AltGr key.
56 pub const IsoLevel3Shift: KeyMod = KeyMod::AltGr;
57}
58
59set! {
60 #[doc = crate::_tags!(interaction set)]
61 /// A bitfield of key modifiers (Shift, Control…) + extra (repeating, composing).
62 #[doc = crate::_doc_meta!{
63 location("ui/event"),
64 test_size_of(KeyMods = 2|16), // option = 4|32
65 }]
66 #[repr(transparent)]
67 pub struct KeyMods(u16) {
68 /// The *Control* modifier.
69 CONTROL = 0;
70 /// The *Shift* modifier.
71 SHIFT = 1;
72 /// The *Alt* modifier.
73 ALT = 2;
74 /// The *Super* modifier.
75 SUPER = 3;
76 /// The *AltGraph* modifier.
77 ALT_GR = 4;
78 /// The *Caps Lock* modifier.
79 CAPS_LOCK = 5;
80 /// The *Num Lock* modifier.
81 NUM_LOCK = 6;
82 /// The *Scroll Lock* modifier.
83 SCROLL_LOCK = 7;
84 /// The *IsoLevel5Shift* modifier.
85 LEVEL5 = 8;
86 /// Whether a key event is repeating.
87 REPEATING = 9;
88 /// Whether a key event is composing (IME input).
89 COMPOSING = 10;
90 }
91 traits(!Debug);
92}
93impl_trait! { fmt::Debug for KeyMods |self, f| {
94 let c = is![self.has_control(), "C", "-"];
95 let s = is![self.has_shift(), "S", "-"];
96 let a = is![self.has_alt(), "A", "-"];
97 let g = is![self.has_alt_gr(), "G", "-"];
98 let u = is![self.has_super(), "U", "-"];
99 let l = is![self.has_caps_lock(), "L", "-"];
100 let n = is![self.has_num_lock(), "N", "-"];
101 let r = is![self.has_repeating(), "R", "-"];
102 let p = is![self.has_composing(), "P", "-"];
103 write![f, "{c}{s}{a}{g}{u}{l}{n}{r}{p}"]
104} }