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