| Bring vidya in cfd3e36 nandi 19d ago | 1 | //! Keyboard shortcut helpers for egui apps. |
| 2 | //! |
| 3 | //! Thin wrappers around egui’s input API so call sites stay readable and |
| 4 | //! platform labels stay consistent. Uses [`egui::Modifiers::COMMAND`] (Cmd on |
| 5 | //! macOS, Ctrl elsewhere) — no global registry or remapping. |
| 6 | |
| 7 | use egui::{Key, Modifiers, Ui}; |
| 8 | |
| 9 | /// True when running on macOS (Cmd-based shortcuts / ⌘ labels). |
| 10 | #[inline] |
| 11 | pub fn is_macos() -> bool { |
| 12 | cfg!(target_os = "macos") |
| 13 | } |
| 14 | |
| 15 | /// Platform command modifier glyph for labels: `"⌘"` on macOS, `"Ctrl"` elsewhere. |
| 16 | #[inline] |
| 17 | pub fn command_glyph() -> &'static str { |
| 18 | if is_macos() { |
| 19 | "⌘" |
| 20 | } else { |
| 21 | "Ctrl" |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | /// Human-readable label for a command+key chord (`"⌘F"` / `"Ctrl+F"`). |
| 26 | pub fn command_shortcut_label(key: Key) -> String { |
| 27 | let name = key.name(); |
| 28 | if is_macos() { |
| 29 | format!("{}{}", command_glyph(), name) |
| 30 | } else { |
| 31 | format!("{}+{}", command_glyph(), name) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | /// Human-readable label for command+shift+key (`"⇧⌘F"` / `"Ctrl+Shift+F"`). |
| 36 | pub fn command_shift_shortcut_label(key: Key) -> String { |
| 37 | let name = key.name(); |
| 38 | if is_macos() { |
| 39 | format!("⇧{}{}", command_glyph(), name) |
| 40 | } else { |
| 41 | format!("{}+Shift+{}", command_glyph(), name) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /// Label for the Escape key. |
| 46 | #[inline] |
| 47 | pub fn escape_label() -> &'static str { |
| 48 | "Esc" |
| 49 | } |
| 50 | |
| 51 | /// Consume a command+key chord this frame (Cmd/Ctrl+key). |
| 52 | /// |
| 53 | /// Returns `true` if the chord was pressed and consumed so it won’t also |
| 54 | /// type into a focused text field. |
| 55 | pub fn consume_command(ui: &Ui, key: Key) -> bool { |
| 56 | ui.input_mut(|i| i.consume_key(Modifiers::COMMAND, key)) |
| 57 | } |
| 58 | |
| 59 | /// Consume a command+shift+key chord this frame. |
| 60 | pub fn consume_command_shift(ui: &Ui, key: Key) -> bool { |
| 61 | ui.input_mut(|i| i.consume_key(Modifiers::COMMAND | Modifiers::SHIFT, key)) |
| 62 | } |
| 63 | |
| 64 | /// Consume bare Escape this frame. |
| 65 | pub fn consume_escape(ui: &Ui) -> bool { |
| 66 | ui.input_mut(|i| i.consume_key(Modifiers::NONE, Key::Escape)) |
| 67 | } |
| 68 | |
| 69 | /// Non-consuming peek: true if command+key is pressed this frame. |
| 70 | pub fn command_pressed(ui: &Ui, key: Key) -> bool { |
| 71 | ui.input(|i| i.key_pressed(key) && i.modifiers.matches_exact(Modifiers::COMMAND)) |
| 72 | } |
| 73 | |
| 74 | #[cfg(test)] |
| 75 | mod tests { |
| 76 | use super::*; |
| 77 | |
| 78 | #[test] |
| 79 | fn command_glyph_matches_target() { |
| 80 | if cfg!(target_os = "macos") { |
| 81 | assert_eq!(command_glyph(), "⌘"); |
| 82 | assert!(is_macos()); |
| 83 | } else { |
| 84 | assert_eq!(command_glyph(), "Ctrl"); |
| 85 | assert!(!is_macos()); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | #[test] |
| 90 | fn command_shortcut_label_formats_f() { |
| 91 | let label = command_shortcut_label(Key::F); |
| 92 | if cfg!(target_os = "macos") { |
| 93 | assert_eq!(label, "⌘F"); |
| 94 | } else { |
| 95 | assert_eq!(label, "Ctrl+F"); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | #[test] |
| 100 | fn command_shift_shortcut_label_formats_f() { |
| 101 | let label = command_shift_shortcut_label(Key::F); |
| 102 | if cfg!(target_os = "macos") { |
| 103 | assert_eq!(label, "⇧⌘F"); |
| 104 | } else { |
| 105 | assert_eq!(label, "Ctrl+Shift+F"); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | #[test] |
| 110 | fn escape_label_is_esc() { |
| 111 | assert_eq!(escape_label(), "Esc"); |
| 112 | } |
| 113 | } |