1use std::sync::Arc;
73
74use CommonLibrary::{
75 Environment::Requires::Requires,
76 TreeView::TreeViewProvider::TreeViewProvider as CommonTreeViewProvider,
77};
78use serde_json::{Value, json};
79use tauri::{AppHandle, Manager, State, Wry, command};
80
81use crate::{
82 ApplicationState::ApplicationState,
83 Environment::MountainEnvironment::MountainEnvironment,
84 RunTime::ApplicationRunTime::ApplicationRunTime,
85};
86
87#[command]
91pub async fn GetTreeViewChildren(
92 ApplicationHandle:AppHandle<Wry>,
93
94 _State:State<'_, Arc<ApplicationState>>,
95
96 ViewId:String,
97
98 ElementHandle:Option<String>,
99) -> Result<Value, String> {
100 log::debug!(
101 "[DispatchLogic] Getting TreeView children for '{}', element: {:?}",
102 ViewId,
103 ElementHandle
104 );
105
106 let RunTime = ApplicationHandle.state::<Arc<ApplicationRunTime>>().inner().clone();
107
108 let Environment:Arc<MountainEnvironment> = RunTime.Environment.clone();
109
110 let TreeProvider:Arc<dyn CommonTreeViewProvider> = Environment.Require();
111
112 match TreeProvider.GetChildren(ViewId.clone(), ElementHandle).await {
113 Ok(Children) => Ok(json!(Children)),
114 Err(Error) => {
115 let ErrorMessage = format!("Failed to get children for tree view '{}': {}", ViewId, Error);
116 log::error!("{}", ErrorMessage);
117 Err(ErrorMessage)
118 },
119 }
120}
121
122#[command]
124pub async fn GetTreeViewItem(
125 ApplicationHandle:AppHandle<Wry>,
126
127 _State:State<'_, Arc<ApplicationState>>,
128
129 ViewId:String,
130
131 ElementHandle:String,
132) -> Result<Value, String> {
133 log::debug!(
134 "[DispatchLogic] Getting TreeView item for '{}', element: {}",
135 ViewId,
136 ElementHandle
137 );
138
139 let RunTime = ApplicationHandle.state::<Arc<ApplicationRunTime>>().inner().clone();
140
141 let Environment:Arc<MountainEnvironment> = RunTime.Environment.clone();
142
143 let TreeProvider:Arc<dyn CommonTreeViewProvider> = Environment.Require();
144
145 match TreeProvider.GetTreeItem(ViewId.clone(), ElementHandle).await {
146 Ok(Item) => Ok(json!(Item)),
147 Err(Error) => {
148 let ErrorMessage = format!("Failed to get tree item for view '{}': {}", ViewId, Error);
149 log::error!("{}", ErrorMessage);
150 Err(ErrorMessage)
151 },
152 }
153}
154
155#[command]
162pub async fn OnTreeViewExpansionChanged(
163 _ApplicationHandle:AppHandle<Wry>,
164
165 _State:State<'_, Arc<ApplicationState>>,
166
167 _ViewId:String,
168
169 _ElementHandle:String,
170
171 _IsExpanded:bool,
172) -> Result<Value, String> {
173 log::warn!("[TreeView Command] OnTreeViewExpansionChanged not implemented");
174
175 Ok(json!({ "success": false, "error": "OnTreeNodeExpanded method not implemented" }))
176}
177
178#[command]
186pub async fn OnTreeViewSelectionChanged(
187 _ApplicationHandle:AppHandle<Wry>,
188
189 _State:State<'_, Arc<ApplicationState>>,
190
191 _ViewId:String,
192
193 _SelectedHandles:Vec<String>,
194) -> Result<Value, String> {
195 log::warn!("[TreeView Command] OnTreeViewSelectionChanged not implemented");
196
197 Ok(json!({ "success": false, "error": "OnTreeSelectionChanged method not implemented" }))
198}
199
200#[command]
202pub async fn RefreshTreeView(
203 ApplicationHandle:AppHandle<Wry>,
204
205 _State:State<'_, Arc<ApplicationState>>,
206
207 ViewId:String,
208
209 ItemsToRefresh:Option<Vec<String>>,
210) -> Result<Value, String> {
211 log::debug!(
212 "[TreeView Command] Refreshing tree view '{}', items: {:?}",
213 ViewId,
214 ItemsToRefresh
215 );
216
217 let RunTime = ApplicationHandle.state::<Arc<ApplicationRunTime>>().inner().clone();
218
219 let Environment:Arc<MountainEnvironment> = RunTime.Environment.clone();
220
221 let RefreshValue:Option<Value> = ItemsToRefresh.and_then(|items| serde_json::to_value(items).ok());
222
223 match Environment.RefreshTreeView(ViewId.clone(), RefreshValue).await {
224 Ok(_) => Ok(json!({ "success": true })),
225 Err(Error) => {
226 let ErrorMessage = format!("Failed to refresh tree view '{}': {}", ViewId, Error);
227 log::error!("{}", ErrorMessage);
228 Err(ErrorMessage)
229 },
230 }
231}
232
233#[command]
235pub async fn RevealTreeViewItem(
236 ApplicationHandle:AppHandle<Wry>,
237
238 _State:State<'_, Arc<ApplicationState>>,
239
240 ViewId:String,
241
242 ItemHandle:String,
243
244 Options:Option<Value>,
245) -> Result<Value, String> {
246 log::debug!("[TreeView Command] Revealing item '{}' in view '{}'", ItemHandle, ViewId);
247
248 let RunTime = ApplicationHandle.state::<Arc<ApplicationRunTime>>().inner().clone();
249
250 let Environment:Arc<MountainEnvironment> = RunTime.Environment.clone();
251
252 let OptionsValue = Options.unwrap_or(json!({}));
253
254 match Environment.RevealTreeItem(ViewId.clone(), ItemHandle, OptionsValue).await {
255 Ok(_) => Ok(json!({ "success": true })),
256 Err(Error) => {
257 let ErrorMessage = format!("Failed to reveal tree item in view '{}': {}", ViewId, Error);
258 log::error!("{}", ErrorMessage);
259 Err(ErrorMessage)
260 },
261 }
262}
263
264#[command]
271pub async fn PersistTreeView(
272 _ApplicationHandle:AppHandle<Wry>,
273
274 _State:State<'_, Arc<ApplicationState>>,
275
276 _ViewId:String,
277) -> Result<Value, String> {
278 log::warn!("[TreeView Command] PersistTreeView not implemented");
279
280 Ok(json!({ "success": false, "error": "PersistTreeViewState method not implemented" }))
281}
282
283#[command]
290pub async fn RestoreTreeView(
291 _ApplicationHandle:AppHandle<Wry>,
292
293 _State:State<'_, Arc<ApplicationState>>,
294
295 _ViewId:String,
296
297 _StateValue:Value,
298) -> Result<Value, String> {
299 log::warn!("[TreeView Command] RestoreTreeView not implemented");
300
301 Ok(json!({ "success": false, "error": "RestoreTreeViewState method not implemented" }))
302}