Skip to main content

egui/data/input/
modifier_names.rs

1use super::Modifiers;
2
3/// Names of different modifier keys.
4///
5/// Used to name modifiers.
6#[derive(Clone, Copy, Debug, Eq, PartialEq)]
7pub struct ModifierNames<'a> {
8    pub is_short: bool,
9
10    pub alt: &'a str,
11    pub ctrl: &'a str,
12    pub shift: &'a str,
13    pub mac_cmd: &'a str,
14    pub mac_alt: &'a str,
15
16    /// What goes between the names
17    pub concat: &'a str,
18}
19
20impl ModifierNames<'static> {
21    /// ⌥ ⌃ ⇧ ⌘ - NOTE: not supported by the default egui font.
22    pub const SYMBOLS: Self = Self {
23        is_short: true,
24        alt: "⌥",
25        ctrl: "⌃",
26        shift: "⇧",
27        mac_cmd: "⌘",
28        mac_alt: "⌥",
29        concat: "",
30    };
31
32    /// Alt, Ctrl, Shift, Cmd
33    pub const NAMES: Self = Self {
34        is_short: false,
35        alt: "Alt",
36        ctrl: "Ctrl",
37        shift: "Shift",
38        mac_cmd: "Cmd",
39        mac_alt: "Option",
40        concat: "+",
41    };
42}
43
44impl ModifierNames<'_> {
45    pub fn format(&self, modifiers: &Modifiers, is_mac: bool) -> String {
46        let mut s = String::new();
47
48        let mut append_if = |modifier_is_active, modifier_name| {
49            if modifier_is_active {
50                if !s.is_empty() {
51                    s += self.concat;
52                }
53                s += modifier_name;
54            }
55        };
56
57        if is_mac {
58            append_if(modifiers.ctrl, self.ctrl);
59            append_if(modifiers.shift, self.shift);
60            append_if(modifiers.alt, self.mac_alt);
61            append_if(modifiers.mac_cmd || modifiers.command, self.mac_cmd);
62        } else {
63            append_if(modifiers.ctrl || modifiers.command, self.ctrl);
64            append_if(modifiers.alt, self.alt);
65            append_if(modifiers.shift, self.shift);
66        }
67
68        s
69    }
70}