Skip to main content

Mountain/Track/Effect/CreateEffectForRequest/
StatusBar.rs

1#![allow(non_snake_case, unused_variables, dead_code, unused_imports)]
2
3use std::{future::Future, pin::Pin, sync::Arc};
4
5use CommonLibrary::{
6	Environment::Requires::Requires,
7	StatusBar::{DTO::StatusBarEntryDTO::StatusBarEntryDTO, StatusBarProvider::StatusBarProvider},
8};
9use serde_json::{Value, json};
10use tauri::Runtime;
11
12use crate::{RunTime::ApplicationRunTime::ApplicationRunTime, Track::Effect::MappedEffectType::MappedEffect};
13
14pub fn CreateEffect<R:Runtime>(MethodName:&str, Parameters:Value) -> Option<Result<MappedEffect, String>> {
15	match MethodName {
16		"$statusBar:set" => {
17			let effect =
18				move |run_time:Arc<ApplicationRunTime>| -> Pin<Box<dyn Future<Output = Result<Value, String>> + Send>> {
19					Box::pin(async move {
20						let provider:Arc<dyn StatusBarProvider> = run_time.Environment.Require();
21						let text = Parameters.get(0).and_then(Value::as_str).unwrap_or("status").to_string();
22						let entry = StatusBarEntryDTO {
23							EntryIdentifier:"id".to_string(),
24							ItemIdentifier:"item".to_string(),
25							ExtensionIdentifier:"ext".to_string(),
26							Name:None,
27							Text:text,
28							Tooltip:None,
29							HasTooltipProvider:false,
30							Command:None,
31							Color:None,
32							BackgroundColor:None,
33							IsAlignedLeft:false,
34							Priority:None,
35							AccessibilityInformation:None,
36						};
37						provider
38							.SetStatusBarEntry(entry)
39							.await
40							.map(|_| json!(null))
41							.map_err(|e| e.to_string())
42					})
43				};
44			Some(Ok(Box::new(effect)))
45		},
46
47		"$statusBar:dispose" => {
48			let effect =
49				move |run_time:Arc<ApplicationRunTime>| -> Pin<Box<dyn Future<Output = Result<Value, String>> + Send>> {
50					Box::pin(async move {
51						let provider:Arc<dyn StatusBarProvider> = run_time.Environment.Require();
52						let id = Parameters.get(0).and_then(Value::as_str).unwrap_or("id").to_string();
53						provider
54							.DisposeStatusBarEntry(id)
55							.await
56							.map(|_| json!(null))
57							.map_err(|e| e.to_string())
58					})
59				};
60			Some(Ok(Box::new(effect)))
61		},
62
63		"$setStatusBarMessage" => {
64			let effect =
65				move |run_time:Arc<ApplicationRunTime>| -> Pin<Box<dyn Future<Output = Result<Value, String>> + Send>> {
66					Box::pin(async move {
67						let provider:Arc<dyn StatusBarProvider> = run_time.Environment.Require();
68						let message_id = Parameters.get(0).and_then(Value::as_str).unwrap_or("msg_id").to_string();
69						let text = Parameters.get(1).and_then(Value::as_str).unwrap_or("message").to_string();
70						provider
71							.SetStatusBarMessage(message_id, text)
72							.await
73							.map(|_| json!(null))
74							.map_err(|e| e.to_string())
75					})
76				};
77			Some(Ok(Box::new(effect)))
78		},
79
80		"$disposeStatusBarMessage" => {
81			let effect =
82				move |run_time:Arc<ApplicationRunTime>| -> Pin<Box<dyn Future<Output = Result<Value, String>> + Send>> {
83					Box::pin(async move {
84						let provider:Arc<dyn StatusBarProvider> = run_time.Environment.Require();
85						let message_id = Parameters.get(0).and_then(Value::as_str).unwrap_or("msg_id").to_string();
86						provider
87							.DisposeStatusBarMessage(message_id)
88							.await
89							.map(|_| json!(null))
90							.map_err(|e| e.to_string())
91					})
92				};
93			Some(Ok(Box::new(effect)))
94		},
95
96		_ => None,
97	}
98}