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