Skip to main content

Mountain/Command/TreeView/
GetTreeViewChildren.rs

1
2//! Tauri command - fetch children for a tree node. `ElementHandle =
3//! None` returns the root level. Dispatches through
4//! `MountainEnvironment::Require<dyn TreeViewProvider>`.
5
6use std::sync::Arc;
7
8use CommonLibrary::{
9	Environment::Requires::Requires,
10	TreeView::TreeViewProvider::TreeViewProvider as CommonTreeViewProvider,
11};
12use serde_json::{Value, json};
13use tauri::{AppHandle, Manager, State, Wry, command};
14
15use crate::{
16	ApplicationState::State::ApplicationState::ApplicationState,
17	Environment::MountainEnvironment::MountainEnvironment,
18	RunTime::ApplicationRunTime::ApplicationRunTime,
19	dev_log,
20};
21
22#[command]
23pub async fn GetTreeViewChildren(
24	ApplicationHandle:AppHandle<Wry>,
25
26	_State:State<'_, Arc<ApplicationState>>,
27
28	ViewId:String,
29
30	ElementHandle:Option<String>,
31) -> Result<Value, String> {
32	dev_log!(
33		"commands",
34		"getting TreeView children for '{}', element: {:?}",
35		ViewId,
36		ElementHandle
37	);
38
39	let RunTime = ApplicationHandle.state::<Arc<ApplicationRunTime>>().inner().clone();
40
41	let Environment:Arc<MountainEnvironment> = RunTime.Environment.clone();
42
43	let TreeProvider:Arc<dyn CommonTreeViewProvider> = Environment.Require();
44
45	match TreeProvider.GetChildren(ViewId.clone(), ElementHandle).await {
46		Ok(Children) => Ok(json!(Children)),
47
48		Err(Error) => {
49			let ErrorMessage = format!("Failed to get children for tree view '{}': {}", ViewId, Error);
50
51			dev_log!("commands", "error: {}", ErrorMessage);
52
53			Err(ErrorMessage)
54		},
55	}
56}