Skip to main content

Mountain/Command/TreeView/
GetTreeViewChildren.rs

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