Skip to main content

Mountain/IPC/WindServiceHandlers/UI/
Theme.rs

1#![allow(non_snake_case, unused_variables)]
2//! Theme IPC handlers. `themes:getActive` / `themes:list` / `themes:set`
3//! drive the workbench's colour-theme picker and the RunTime theme swap.
4//!
5//! Source of truth: `workbench.colorTheme` inside `ConfigurationProvider`.
6//! A `themes:set` writes the key and emits `SkyEvent::ThemeChange` so
7//! Monaco and the Sky shell re-tint in-place without a window reload.
8
9use std::sync::Arc;
10
11use CommonLibrary::IPC::SkyEvent::SkyEvent;
12use serde_json::{Value, json};
13
14use crate::RunTime::ApplicationRunTime::ApplicationRunTime;
15
16pub async fn ThemesGetActive(RunTime:Arc<ApplicationRunTime>) -> Result<Value, String> {
17	use CommonLibrary::Configuration::{
18		ConfigurationProvider::ConfigurationProvider,
19		DTO::ConfigurationOverridesDTO::ConfigurationOverridesDTO,
20	};
21
22	let ThemeId = RunTime
23		.Environment
24		.GetConfigurationValue(Some("workbench.colorTheme".to_string()), ConfigurationOverridesDTO::default())
25		.await
26		.map_err(|Error| format!("themes:getActive failed: {}", Error))?;
27
28	let Id = ThemeId.as_str().unwrap_or("Default Dark Modern").to_string();
29
30	// Infer kind from id string.
31	let Kind = if Id.to_lowercase().contains("light") {
32		"light"
33	} else if Id.to_lowercase().contains("high contrast light") {
34		"highContrastLight"
35	} else if Id.to_lowercase().contains("high contrast") {
36		"highContrast"
37	} else {
38		"dark"
39	};
40
41	Ok(json!({ "id": Id, "label": Id, "kind": Kind }))
42}
43
44pub async fn ThemesList(_runtime:Arc<ApplicationRunTime>) -> Result<Value, String> {
45	let Themes = vec![
46		json!({ "id": "Default Dark Modern", "label": "Default Dark Modern", "kind": "dark" }),
47		json!({ "id": "Default Light Modern", "label": "Default Light Modern", "kind": "light" }),
48		json!({ "id": "Default Dark+", "label": "Default Dark+", "kind": "dark" }),
49		json!({ "id": "Default Light+", "label": "Default Light+", "kind": "light" }),
50		json!({ "id": "High Contrast", "label": "High Contrast", "kind": "highContrast" }),
51		json!({ "id": "High Contrast Light", "label": "High Contrast Light", "kind": "highContrastLight" }),
52	];
53	Ok(json!(Themes))
54}
55
56pub async fn ThemesSet(RunTime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
57	use CommonLibrary::Configuration::{
58		ConfigurationProvider::ConfigurationProvider,
59		DTO::{ConfigurationOverridesDTO::ConfigurationOverridesDTO, ConfigurationTarget::ConfigurationTarget},
60	};
61	use tauri::Emitter;
62
63	let ThemeId = Arguments
64		.first()
65		.and_then(|V| V.as_str())
66		.ok_or("themes:set requires themeId as first argument".to_string())?
67		.to_string();
68
69	RunTime
70		.Environment
71		.UpdateConfigurationValue(
72			"workbench.colorTheme".to_string(),
73			json!(ThemeId),
74			ConfigurationTarget::User,
75			ConfigurationOverridesDTO::default(),
76			None,
77		)
78		.await
79		.map_err(|Error| format!("themes:set failed: {}", Error))?;
80
81	let _ = RunTime
82		.Environment
83		.ApplicationHandle
84		.emit(SkyEvent::ThemeChange.AsStr(), json!({ "themeId": ThemeId }));
85
86	Ok(Value::Null)
87}