nandi/jolt-nativepublic Fork 0
109c7e403bee6fa2ec5a768c39d45341fc10cd6e
Commits
Clone
git clone https://git.rickub.com/nandi/jolt-native.git
git clone ssh://git@rickub.com/nandi/jolt-native.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Bring vidya in cfd3e36 · on 109c7e403bee6fa2ec5a768c39d45341fc10cd6e · nandi · 19d ago
hotkeys.rs · 113 lines · 3.1 KBRust Blame HistoryRaw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//! Keyboard shortcut helpers for egui apps.
//!
//! Thin wrappers around egui’s input API so call sites stay readable and
//! platform labels stay consistent. Uses [`egui::Modifiers::COMMAND`] (Cmd on
//! macOS, Ctrl elsewhere) — no global registry or remapping.

use egui::{Key, Modifiers, Ui};

/// True when running on macOS (Cmd-based shortcuts / ⌘ labels).
#[inline]
pub fn is_macos() -> bool {
    cfg!(target_os = "macos")
}

/// Platform command modifier glyph for labels: `"⌘"` on macOS, `"Ctrl"` elsewhere.
#[inline]
pub fn command_glyph() -> &'static str {
    if is_macos() {
        ""
    } else {
        "Ctrl"
    }
}

/// Human-readable label for a command+key chord (`"⌘F"` / `"Ctrl+F"`).
pub fn command_shortcut_label(key: Key) -> String {
    let name = key.name();
    if is_macos() {
        format!("{}{}", command_glyph(), name)
    } else {
        format!("{}+{}", command_glyph(), name)
    }
}

/// Human-readable label for command+shift+key (`"⇧⌘F"` / `"Ctrl+Shift+F"`).
pub fn command_shift_shortcut_label(key: Key) -> String {
    let name = key.name();
    if is_macos() {
        format!("{}{}", command_glyph(), name)
    } else {
        format!("{}+Shift+{}", command_glyph(), name)
    }
}

/// Label for the Escape key.
#[inline]
pub fn escape_label() -> &'static str {
    "Esc"
}

/// Consume a command+key chord this frame (Cmd/Ctrl+key).
///
/// Returns `true` if the chord was pressed and consumed so it won’t also
/// type into a focused text field.
pub fn consume_command(ui: &Ui, key: Key) -> bool {
    ui.input_mut(|i| i.consume_key(Modifiers::COMMAND, key))
}

/// Consume a command+shift+key chord this frame.
pub fn consume_command_shift(ui: &Ui, key: Key) -> bool {
    ui.input_mut(|i| i.consume_key(Modifiers::COMMAND | Modifiers::SHIFT, key))
}

/// Consume bare Escape this frame.
pub fn consume_escape(ui: &Ui) -> bool {
    ui.input_mut(|i| i.consume_key(Modifiers::NONE, Key::Escape))
}

/// Non-consuming peek: true if command+key is pressed this frame.
pub fn command_pressed(ui: &Ui, key: Key) -> bool {
    ui.input(|i| i.key_pressed(key) && i.modifiers.matches_exact(Modifiers::COMMAND))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn command_glyph_matches_target() {
        if cfg!(target_os = "macos") {
            assert_eq!(command_glyph(), "");
            assert!(is_macos());
        } else {
            assert_eq!(command_glyph(), "Ctrl");
            assert!(!is_macos());
        }
    }

    #[test]
    fn command_shortcut_label_formats_f() {
        let label = command_shortcut_label(Key::F);
        if cfg!(target_os = "macos") {
            assert_eq!(label, "⌘F");
        } else {
            assert_eq!(label, "Ctrl+F");
        }
    }

    #[test]
    fn command_shift_shortcut_label_formats_f() {
        let label = command_shift_shortcut_label(Key::F);
        if cfg!(target_os = "macos") {
            assert_eq!(label, "⇧⌘F");
        } else {
            assert_eq!(label, "Ctrl+Shift+F");
        }
    }

    #[test]
    fn escape_label_is_esc() {
        assert_eq!(escape_label(), "Esc");
    }
}