Mountain/ApplicationState/State/FeatureState/Keybindings/
KeybindingState.rs1use std::sync::{Arc, Mutex as StandardMutex};
2
3use serde::{Deserialize, Serialize};
4
5use crate::dev_log;
6
7#[derive(Clone, Debug, Serialize, Deserialize)]
9pub struct KeybindingEntry {
10 pub CommandId:String,
12
13 pub Keybinding:String,
15
16 pub When:Option<String>,
18}
19
20#[derive(Clone)]
22pub struct KeybindingState {
23 Entries:Arc<StandardMutex<Vec<KeybindingEntry>>>,
24}
25
26impl Default for KeybindingState {
27 fn default() -> Self {
28 dev_log!("keybinding", "[KeybindingState] Initializing default keybinding state...");
29
30 Self { Entries:Arc::new(StandardMutex::new(Vec::new())) }
31 }
32}
33
34impl KeybindingState {
35 pub fn AddKeybinding(&self, CommandId:String, Keybinding:String, When:Option<String>) {
38 if let Ok(mut Guard) = self.Entries.lock() {
39 Guard.retain(|E| E.CommandId != CommandId);
40
41 Guard.push(KeybindingEntry { CommandId:CommandId.clone(), Keybinding, When });
42
43 dev_log!("keybinding", "[KeybindingState] Keybinding added for: {}", CommandId);
44 }
45 }
46
47 pub fn RemoveKeybinding(&self, CommandId:&str) {
49 if let Ok(mut Guard) = self.Entries.lock() {
50 Guard.retain(|E| E.CommandId != CommandId);
51
52 dev_log!("keybinding", "[KeybindingState] Keybinding removed for: {}", CommandId);
53 }
54 }
55
56 pub fn LookupKeybinding(&self, CommandId:&str) -> Option<String> {
58 self.Entries
59 .lock()
60 .ok()
61 .and_then(|Guard| Guard.iter().find(|E| E.CommandId == CommandId).map(|E| E.Keybinding.clone()))
62 }
63
64 pub fn GetAllKeybindings(&self) -> Vec<KeybindingEntry> {
66 self.Entries.lock().ok().map(|Guard| Guard.clone()).unwrap_or_default()
67 }
68}