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::State::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!(
108 "commands",
109 "getting TreeView children for '{}', element: {:?}",
110 ViewId,
111 ElementHandle
112 );
113
114 let RunTime = ApplicationHandle.state::<Arc<ApplicationRunTime>>().inner().clone();
115
116 let Environment:Arc<MountainEnvironment> = RunTime.Environment.clone();
117
118 let TreeProvider:Arc<dyn CommonTreeViewProvider> = Environment.Require();
119
120 match TreeProvider.GetChildren(ViewId.clone(), ElementHandle).await {
121 Ok(Children) => Ok(json!(Children)),
122 Err(Error) => {
123 let ErrorMessage = format!("Failed to get children for tree view '{}': {}", ViewId, Error);
124 dev_log!("commands", "error: {}", ErrorMessage);
125 Err(ErrorMessage)
126 },
127 }
128}
129
130#[command]
132pub async fn GetTreeViewItem(
133 ApplicationHandle:AppHandle<Wry>,
134
135 _State:State<'_, Arc<ApplicationState>>,
136
137 ViewId:String,
138
139 ElementHandle:String,
140) -> Result<Value, String> {
141 dev_log!("commands", "getting TreeView item for '{}', element: {}", ViewId, ElementHandle);
142
143 let RunTime = ApplicationHandle.state::<Arc<ApplicationRunTime>>().inner().clone();
144
145 let Environment:Arc<MountainEnvironment> = RunTime.Environment.clone();
146
147 let TreeProvider:Arc<dyn CommonTreeViewProvider> = Environment.Require();
148
149 match TreeProvider.GetTreeItem(ViewId.clone(), ElementHandle).await {
150 Ok(Item) => Ok(json!(Item)),
151 Err(Error) => {
152 let ErrorMessage = format!("Failed to get tree item for view '{}': {}", ViewId, Error);
153 dev_log!("commands", "error: {}", ErrorMessage);
154 Err(ErrorMessage)
155 },
156 }
157}
158
159#[command]
166pub async fn OnTreeViewExpansionChanged(
167 _ApplicationHandle:AppHandle<Wry>,
168
169 _State:State<'_, Arc<ApplicationState>>,
170
171 _ViewId:String,
172
173 _ElementHandle:String,
174
175 _IsExpanded:bool,
176) -> Result<Value, String> {
177 dev_log!("commands", "warn: OnTreeViewExpansionChanged not implemented");
178
179 Ok(json!({ "success": false, "error": "OnTreeNodeExpanded method not implemented" }))
180}
181
182#[command]
190pub async fn OnTreeViewSelectionChanged(
191 _ApplicationHandle:AppHandle<Wry>,
192
193 _State:State<'_, Arc<ApplicationState>>,
194
195 _ViewId:String,
196
197 _SelectedHandles:Vec<String>,
198) -> Result<Value, String> {
199 dev_log!("commands", "warn: OnTreeViewSelectionChanged not implemented");
200
201 Ok(json!({ "success": false, "error": "OnTreeSelectionChanged method not implemented" }))
202}
203
204#[command]
206pub async fn RefreshTreeView(
207 ApplicationHandle:AppHandle<Wry>,
208
209 _State:State<'_, Arc<ApplicationState>>,
210
211 ViewId:String,
212
213 ItemsToRefresh:Option<Vec<String>>,
214) -> Result<Value, String> {
215 dev_log!("commands", "refreshing tree view '{}', items: {:?}", ViewId, ItemsToRefresh);
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 dev_log!("commands", "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 dev_log!("commands", "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 dev_log!("commands", "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 dev_log!("commands", "warn: 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 dev_log!("commands", "warn: RestoreTreeView not implemented");
300
301 Ok(json!({ "success": false, "error": "RestoreTreeViewState method not implemented" }))
302}