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	_State:State<'_, Arc<ApplicationState>>,
27	ViewId:String,
28	ElementHandle:Option<String>,
29) -> Result<Value, String> {
30	dev_log!(
31		"commands",
32		"getting TreeView children for '{}', element: {:?}",
33		ViewId,
34		ElementHandle
35	);
36
37	let RunTime = ApplicationHandle.state::<Arc<ApplicationRunTime>>().inner().clone();
38	let Environment:Arc<MountainEnvironment> = RunTime.Environment.clone();
39	let TreeProvider:Arc<dyn CommonTreeViewProvider> = Environment.Require();
40
41	match TreeProvider.GetChildren(ViewId.clone(), ElementHandle).await {
42		Ok(Children) => Ok(json!(Children)),
43		Err(Error) => {
44			let ErrorMessage = format!("Failed to get children for tree view '{}': {}", ViewId, Error);
45			dev_log!("commands", "error: {}", ErrorMessage);
46			Err(ErrorMessage)
47		},
48	}
49}