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 pub Keybinding:String,
14 pub When:Option<String>,
16}
17
18#[derive(Clone)]
20pub struct KeybindingState {
21 Entries:Arc<StandardMutex<Vec<KeybindingEntry>>>,
22}
23
24impl Default for KeybindingState {
25 fn default() -> Self {
26 dev_log!("keybinding", "[KeybindingState] Initializing default keybinding state...");
27 Self { Entries:Arc::new(StandardMutex::new(Vec::new())) }
28 }
29}
30
31impl KeybindingState {
32 pub fn AddKeybinding(&self, CommandId:String, Keybinding:String, When:Option<String>) {
35 if let Ok(mut Guard) = self.Entries.lock() {
36 Guard.retain(|E| E.CommandId != CommandId);
37 Guard.push(KeybindingEntry { CommandId:CommandId.clone(), Keybinding, When });
38 dev_log!("keybinding", "[KeybindingState] Keybinding added for: {}", CommandId);
39 }
40 }
41
42 pub fn RemoveKeybinding(&self, CommandId:&str) {
44 if let Ok(mut Guard) = self.Entries.lock() {
45 Guard.retain(|E| E.CommandId != CommandId);
46 dev_log!("keybinding", "[KeybindingState] Keybinding removed for: {}", CommandId);
47 }
48 }
49
50 pub fn LookupKeybinding(&self, CommandId:&str) -> Option<String> {
52 self.Entries
53 .lock()
54 .ok()
55 .and_then(|Guard| Guard.iter().find(|E| E.CommandId == CommandId).map(|E| E.Keybinding.clone()))
56 }
57
58 pub fn GetAllKeybindings(&self) -> Vec<KeybindingEntry> {
60 self.Entries.lock().ok().map(|Guard| Guard.clone()).unwrap_or_default()
61 }
62}