Skip to main content

Mountain/IPC/WindServiceHandler/
Theme.rs

1#![allow(non_snake_case)]
2
3//! Theme domain handlers for Wind IPC.
4
5use std::sync::Arc;
6
7use serde_json::{Value, json};
8
9use crate::RunTime::ApplicationRunTime::ApplicationRunTime;
10
11/// Return the active color theme metadata from ConfigurationProvider.
12pub async fn handle_themes_get_active(Runtime:Arc<ApplicationRunTime>) -> Result<Value, String> {
13	use CommonLibrary::Configuration::{
14		ConfigurationProvider::ConfigurationProvider,
15		DTO::ConfigurationOverridesDTO::ConfigurationOverridesDTO,
16	};
17
18	let ThemeId = Runtime
19		.Environment
20		.GetConfigurationValue(Some("workbench.colorTheme".to_string()), ConfigurationOverridesDTO::default())
21		.await
22		.map_err(|Error| format!("themes:getActive failed: {}", Error))?;
23
24	let Id = ThemeId.as_str().unwrap_or("Default Dark Modern").to_string();
25
26	let Kind = if Id.to_lowercase().contains("light") {
27		"light"
28	} else if Id.to_lowercase().contains("high contrast light") {
29		"highContrastLight"
30	} else if Id.to_lowercase().contains("high contrast") {
31		"highContrast"
32	} else {
33		"dark"
34	};
35
36	Ok(json!({ "id": Id, "label": Id, "kind": Kind }))
37}
38
39/// Return installed theme extensions.
40pub async fn handle_themes_list(Runtime:Arc<ApplicationRunTime>) -> Result<Value, String> {
41	let Themes = vec![
42		json!({ "id": "Default Dark Modern", "label": "Default Dark Modern", "kind": "dark" }),
43		json!({ "id": "Default Light Modern", "label": "Default Light Modern", "kind": "light" }),
44		json!({ "id": "Default Dark+", "label": "Default Dark+", "kind": "dark" }),
45		json!({ "id": "Default Light+", "label": "Default Light+", "kind": "light" }),
46		json!({ "id": "High Contrast", "label": "High Contrast", "kind": "highContrast" }),
47		json!({ "id": "High Contrast Light", "label": "High Contrast Light", "kind": "highContrastLight" }),
48	];
49
50	Ok(json!(Themes))
51}
52
53/// Set the active color theme by updating ConfigurationProvider.
54pub async fn handle_themes_set(Runtime:Arc<ApplicationRunTime>, Args:Vec<Value>) -> Result<Value, String> {
55	use CommonLibrary::Configuration::{
56		ConfigurationProvider::ConfigurationProvider,
57		DTO::{ConfigurationOverridesDTO::ConfigurationOverridesDTO, ConfigurationTarget::ConfigurationTarget},
58	};
59	use tauri::Emitter;
60
61	let ThemeId = Args
62		.first()
63		.and_then(|V| V.as_str())
64		.ok_or("themes:set requires themeId as first argument".to_string())?
65		.to_string();
66
67	Runtime
68		.Environment
69		.UpdateConfigurationValue(
70			"workbench.colorTheme".to_string(),
71			json!(ThemeId),
72			ConfigurationTarget::User,
73			ConfigurationOverridesDTO::default(),
74			None,
75		)
76		.await
77		.map_err(|Error| format!("themes:set failed: {}", Error))?;
78
79	let _ = Runtime
80		.Environment
81		.ApplicationHandle
82		.emit("sky://theme/change", json!({ "themeId": ThemeId }));
83
84	Ok(Value::Null)
85}