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
54	Ok(json!(Themes))
55}
56
57pub async fn ThemesSet(RunTime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
58	use CommonLibrary::Configuration::{
59		ConfigurationProvider::ConfigurationProvider,
60		DTO::{ConfigurationOverridesDTO::ConfigurationOverridesDTO, ConfigurationTarget::ConfigurationTarget},
61	};
62	use tauri::Emitter;
63
64	let ThemeId = Arguments
65		.first()
66		.and_then(|V| V.as_str())
67		.ok_or("themes:set requires themeId as first argument".to_string())?
68		.to_string();
69
70	RunTime
71		.Environment
72		.UpdateConfigurationValue(
73			"workbench.colorTheme".to_string(),
74			json!(ThemeId),
75			ConfigurationTarget::User,
76			ConfigurationOverridesDTO::default(),
77			None,
78		)
79		.await
80		.map_err(|Error| format!("themes:set failed: {}", Error))?;
81
82	let _ = RunTime
83		.Environment
84		.ApplicationHandle
85		.emit(SkyEvent::ThemeChange.AsStr(), json!({ "themeId": ThemeId }));
86
87	Ok(Value::Null)
88}