Mountain/Environment/
KeybindingProvider.rs1use std::{collections::HashMap, sync::Arc};
31
32use CommonLibrary::{
33 Effect::ApplicationRunTime::ApplicationRunTime as _,
34 Error::CommonError::CommonError,
35 FileSystem::ReadFile::ReadFile,
36 Keybinding::KeybindingProvider::KeybindingProvider,
37};
38use async_trait::async_trait;
39use serde_json::{Value, json};
40use tauri::Manager;
41
42use super::{MountainEnvironment::MountainEnvironment, Utility};
43use crate::{RunTime::ApplicationRunTime::ApplicationRunTime, dev_log};
44
45#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
51#[serde(rename_all = "camelCase")]
52struct KeybindingRule {
53 key:String,
54
55 command:String,
56
57 when:Option<String>,
58
59 args:Option<Value>,
60}
61
62#[async_trait]
63impl KeybindingProvider for MountainEnvironment {
64 async fn GetResolvedKeybinding(&self) -> Result<Value, CommonError> {
65 dev_log!("keybinding", "[KeybindingProvider] Resolving all keybindings...");
66
67 let mut ResolvedKeybindings:HashMap<String, KeybindingRule> = HashMap::new();
68
69 let Extensions = self
71 .ApplicationState
72 .Extension
73 .ScannedExtensions
74 .ScannedExtensions
75 .lock()
76 .map_err(Utility::ErrorMapping::MapApplicationStateLockErrorToCommonError)?
77 .clone();
78
79 for Extension in Extensions.values() {
80 if let Some(Contributes) = Extension.Contributes.as_ref().and_then(|c| c.get("keybindings")) {
81 if let Some(KeybindingsArray) = Contributes.as_array() {
82 for KeybindingValue in KeybindingsArray {
83 if let Ok(KeybindingRule) = serde_json::from_value::<KeybindingRule>(KeybindingValue.clone()) {
84 let UniqueKey =
85 format!("{}{}", KeybindingRule.key, KeybindingRule.when.as_deref().unwrap_or(""));
86
87 ResolvedKeybindings.insert(UniqueKey, KeybindingRule);
88 }
89 }
90 }
91 }
92 }
93
94 let UserKeybindingsPath = self
96 .ApplicationHandle
97 .path()
98 .app_config_dir()
99 .map_err(|Error| {
100 CommonError::ConfigurationLoad { Description:format!("Cannot find app config dir: {}", Error) }
101 })?
102 .join("keybindings.json");
103
104 let RunTime = self.ApplicationHandle.state::<Arc<ApplicationRunTime>>().inner().clone();
105
106 if let Ok(Content) = RunTime.Run(ReadFile(UserKeybindingsPath)).await {
107 if let Ok(UserKeybindings) = serde_json::from_slice::<Vec<KeybindingRule>>(&Content) {
108 for UserKeybinding in UserKeybindings {
109 let UniqueKey = format!("{}{}", UserKeybinding.key, UserKeybinding.when.as_deref().unwrap_or(""));
110
111 if UserKeybinding.command.starts_with('-') {
112 ResolvedKeybindings.remove(&UniqueKey);
114 } else {
115 ResolvedKeybindings.insert(UniqueKey, UserKeybinding);
117 }
118 }
119 } else {
120 dev_log!(
121 "keybinding",
122 "warn: [KeybindingProvider] Failed to parse user keybindings.json. It may be malformed."
123 );
124 }
125 }
126
127 let FinalRules:Vec<KeybindingRule> = ResolvedKeybindings.into_values().collect();
128
129 Ok(json!(FinalRules))
130 }
131}