Skip to main content

Mountain/IPC/WindServiceHandlers/UI/
Keybinding.rs

1#![allow(non_snake_case, unused_variables)]
2//! Dynamic keybinding handlers. Extensions' `package.json > contributes.
3//! keybindings` is a declarative registry; this surface is for the
4//! imperative `keybindings:add/remove/lookup/getAll` IPC path Wind uses
5//! for RunTime registrations (e.g. palette-installed commands).
6
7use std::sync::Arc;
8
9use serde_json::{Value, json};
10
11use crate::RunTime::ApplicationRunTime::ApplicationRunTime;
12
13pub async fn KeybindingAdd(RunTime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
14	let CommandId = Arguments
15		.first()
16		.and_then(|V| V.as_str())
17		.ok_or("keybinding:add requires commandId".to_string())?
18		.to_owned();
19
20	let KeyExpression = Arguments
21		.get(1)
22		.and_then(|V| V.as_str())
23		.ok_or("keybinding:add requires keybinding".to_string())?
24		.to_owned();
25
26	let When = Arguments.get(2).and_then(|V| V.as_str()).map(str::to_owned);
27
28	RunTime
29		.Environment
30		.ApplicationState
31		.Feature
32		.Keybindings
33		.AddKeybinding(CommandId, KeyExpression, When);
34
35	Ok(Value::Null)
36}
37
38pub async fn KeybindingRemove(RunTime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
39	let CommandId = Arguments
40		.first()
41		.and_then(|V| V.as_str())
42		.ok_or("keybinding:remove requires commandId".to_string())?;
43
44	RunTime
45		.Environment
46		.ApplicationState
47		.Feature
48		.Keybindings
49		.RemoveKeybinding(CommandId);
50
51	Ok(Value::Null)
52}
53
54pub async fn KeybindingLookup(RunTime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
55	let CommandId = Arguments
56		.first()
57		.and_then(|V| V.as_str())
58		.ok_or("keybinding:lookup requires commandId".to_string())?;
59
60	let Binding = RunTime
61		.Environment
62		.ApplicationState
63		.Feature
64		.Keybindings
65		.LookupKeybinding(CommandId);
66
67	Ok(Binding.map(|B| json!(B)).unwrap_or(Value::Null))
68}
69
70pub async fn KeybindingGetAll(RunTime:Arc<ApplicationRunTime>) -> Result<Value, String> {
71	let All = RunTime.Environment.ApplicationState.Feature.Keybindings.GetAllKeybindings();
72
73	Ok(json!(All))
74}