Skip to main content

ai_agent/utils/
modifiers.rs

1// Source: /data/home/swei/claudecode/openclaudecode/src/utils/modifiers.ts
2//! Modifier key utilities.
3
4use serde::{Deserialize, Serialize};
5
6/// Modifier keys
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
8pub enum Modifier {
9    Control,
10    Shift,
11    Alt,
12    Meta,
13    Command,
14    Function,
15}
16
17/// Keyboard shortcut
18#[derive(Debug, Clone, Serialize, Deserialize)]
19pub struct Shortcut {
20    pub key: char,
21    pub modifiers: Vec<Modifier>,
22}
23
24impl Shortcut {
25    pub fn new(key: char) -> Self {
26        Self {
27            key,
28            modifiers: Vec::new(),
29        }
30    }
31
32    pub fn with_modifier(mut self, modifier: Modifier) -> Self {
33        self.modifiers.push(modifier);
34        self
35    }
36
37    pub fn ctrl(key: char) -> Self {
38        Self::new(key).with_modifier(Modifier::Control)
39    }
40
41    pub fn shift(key: char) -> Self {
42        Self::new(key).with_modifier(Modifier::Shift)
43    }
44
45    pub fn alt(key: char) -> Self {
46        Self::new(key).with_modifier(Modifier::Alt)
47    }
48
49    pub fn cmd(key: char) -> Self {
50        Self::new(key).with_modifier(Modifier::Command)
51    }
52
53    pub fn to_string(&self) -> String {
54        let mut parts = Vec::new();
55
56        for m in &self.modifiers {
57            match m {
58                Modifier::Control => parts.push("Ctrl"),
59                Modifier::Shift => parts.push("Shift"),
60                Modifier::Alt => parts.push("Alt"),
61                Modifier::Meta => parts.push("Meta"),
62                Modifier::Command => parts.push("Cmd"),
63                Modifier::Function => parts.push("Fn"),
64            }
65        }
66
67        let key_str = self.key.to_string();
68        parts.push(&key_str);
69        parts.join("+")
70    }
71}