Skip to main content

Mountain/ApplicationState/State/FeatureState/Keybindings/
KeybindingState.rs

1use std::sync::{Arc, Mutex as StandardMutex};
2
3use serde::{Deserialize, Serialize};
4
5use crate::dev_log;
6
7/// A single registered dynamic keybinding entry.
8#[derive(Clone, Debug, Serialize, Deserialize)]
9pub struct KeybindingEntry {
10	/// Command identifier (e.g. "workbench.action.files.save").
11	pub CommandId:String,
12	/// Key expression (e.g. "ctrl+s", "cmd+shift+p").
13	pub Keybinding:String,
14	/// Optional when-clause (e.g. "editorFocus && !editorReadonly").
15	pub When:Option<String>,
16}
17
18/// Stores dynamically registered keyboard shortcuts.
19#[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	/// Register a dynamic keybinding (replaces any existing entry for the same
33	/// command).
34	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	/// Remove all dynamic keybindings for a command.
43	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	/// Return the resolved keybinding string for a command, or `None`.
51	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	/// Return all registered dynamic keybinding entries.
59	pub fn GetAllKeybindings(&self) -> Vec<KeybindingEntry> {
60		self.Entries.lock().ok().map(|Guard| Guard.clone()).unwrap_or_default()
61	}
62}