Skip to main content

Mountain/Command/TreeView/
GetTreeViewItem.rs

1
2//! Tauri command - fetch a single tree item's metadata (label, icon,
3//! description, command, contextValue) by its element handle.
4
5use std::sync::Arc;
6
7use CommonLibrary::{
8	Environment::Requires::Requires,
9	TreeView::TreeViewProvider::TreeViewProvider as CommonTreeViewProvider,
10};
11use serde_json::{Value, json};
12use tauri::{AppHandle, Manager, State, Wry, command};
13
14use crate::{
15	ApplicationState::State::ApplicationState::ApplicationState,
16	Environment::MountainEnvironment::MountainEnvironment,
17	RunTime::ApplicationRunTime::ApplicationRunTime,
18	dev_log,
19};
20
21#[command]
22pub async fn GetTreeViewItem(
23	ApplicationHandle:AppHandle<Wry>,
24
25	_State:State<'_, Arc<ApplicationState>>,
26
27	ViewId:String,
28
29	ElementHandle:String,
30) -> Result<Value, String> {
31	dev_log!("commands", "getting TreeView item for '{}', element: {}", ViewId, ElementHandle);
32
33	let RunTime = ApplicationHandle.state::<Arc<ApplicationRunTime>>().inner().clone();
34
35	let Environment:Arc<MountainEnvironment> = RunTime.Environment.clone();
36
37	let TreeProvider:Arc<dyn CommonTreeViewProvider> = Environment.Require();
38
39	match TreeProvider.GetTreeItem(ViewId.clone(), ElementHandle).await {
40		Ok(Item) => Ok(json!(Item)),
41
42		Err(Error) => {
43			let ErrorMessage = format!("Failed to get tree item for view '{}': {}", ViewId, Error);
44
45			dev_log!("commands", "error: {}", ErrorMessage);
46
47			Err(ErrorMessage)
48		},
49	}
50}