Skip to main content

Mountain/Environment/TreeViewProvider/
UIState.rs

1//! # Tree View UI State Helpers
2//!
3//! Internal helper functions for updating tree view UI properties
4//! (message, title, badge).
5
6use CommonLibrary::Error::CommonError::CommonError;
7use serde_json::json;
8use tauri::Emitter;
9
10use crate::{Environment::Utility, dev_log};
11
12/// Updates the tree view message displayed in the UI.
13pub(super) async fn set_tree_view_message(
14	env:&crate::Environment::MountainEnvironment::MountainEnvironment,
15	view_identifier:String,
16	message:Option<String>,
17) -> Result<(), CommonError> {
18	dev_log!(
19		"extensions",
20		"[TreeViewProvider] Setting message for view '{}': {:?}",
21		view_identifier,
22		message
23	);
24
25	{
26		let mut tree_view_guard = env
27			.ApplicationState
28			.Feature
29			.TreeViews
30			.ActiveTreeViews
31			.lock()
32			.map_err(Utility::MapApplicationStateLockErrorToCommonError)?;
33
34		if let Some(view_state) = tree_view_guard.get_mut(&view_identifier) {
35			view_state.Message = message.clone();
36		}
37	}
38
39	env.ApplicationHandle
40		.emit(
41			"sky://tree-view/set-message",
42			json!({ "ViewIdentifier": view_identifier, "Message": message }),
43		)
44		.map_err(|Error| {
45			CommonError::UserInterfaceInteraction { Reason:format!("Failed to emit tree view message: {}", Error) }
46		})
47}
48
49/// Updates the tree view's title and description.
50pub(super) async fn set_tree_view_title(
51	env:&crate::Environment::MountainEnvironment::MountainEnvironment,
52	view_identifier:String,
53	title:Option<String>,
54	description:Option<String>,
55) -> Result<(), CommonError> {
56	dev_log!(
57		"extensions",
58		"[TreeViewProvider] Setting title/description for view '{}': {:?} {:?}",
59		view_identifier,
60		title,
61		description
62	);
63
64	{
65		let mut tree_view_guard = env
66			.ApplicationState
67			.Feature
68			.TreeViews
69			.ActiveTreeViews
70			.lock()
71			.map_err(Utility::MapApplicationStateLockErrorToCommonError)?;
72
73		if let Some(view_state) = tree_view_guard.get_mut(&view_identifier) {
74			view_state.Title = title.clone();
75			view_state.Description = description.clone();
76		}
77	}
78
79	env.ApplicationHandle
80		.emit(
81			"sky://tree-view/set-title",
82			json!({
83				"ViewIdentifier": view_identifier,
84				"Title": title,
85				"Description": description,
86			}),
87		)
88		.map_err(|Error| {
89			CommonError::UserInterfaceInteraction { Reason:format!("Failed to emit tree view title: {}", Error) }
90		})
91}
92
93/// Sets a badge on the tree view.
94pub(super) async fn set_tree_view_badge(
95	env:&crate::Environment::MountainEnvironment::MountainEnvironment,
96	view_identifier:String,
97	badge:Option<serde_json::Value>,
98) -> Result<(), CommonError> {
99	dev_log!(
100		"extensions",
101		"[TreeViewProvider] Setting badge for view '{}': {:?}",
102		view_identifier,
103		badge
104	);
105
106	// Update state (badge field may need to be added to TreeViewStateDTO)
107	{
108		let mut tree_view_guard = env
109			.ApplicationState
110			.Feature
111			.TreeViews
112			.ActiveTreeViews
113			.lock()
114			.map_err(Utility::MapApplicationStateLockErrorToCommonError)?;
115
116		if let Some(view_state) = tree_view_guard.get_mut(&view_identifier) {
117			// Store badge in ViewState
118			if let Some(badge_value) = &badge {
119				let badge_str = badge_value.to_string();
120				if let Err(e) = view_state.SetBadge(badge_str) {
121					dev_log!("extensions", "warn: Failed to set badge for view '{}': {}", view_identifier, e);
122				}
123			}
124		}
125	}
126
127	// Emit to frontend
128	env.ApplicationHandle
129		.emit(
130			"sky://tree-view/set-badge",
131			json!({ "ViewIdentifier": view_identifier, "Badge": badge }),
132		)
133		.map_err(|Error| {
134			CommonError::UserInterfaceInteraction { Reason:format!("Failed to emit tree view badge: {}", Error) }
135		})
136}