Skip to main content

Mountain/Environment/StatusBarProvider/
EntryManagement.rs

1//! # StatusBarProvider - Entry Management
2//!
3//! Implementation of status bar entry creation and disposal for
4//! [`MountainEnvironment`]
5
6use CommonLibrary::{
7	Error::CommonError::CommonError,
8	IPC::SkyEvent::SkyEvent,
9	StatusBar::DTO::StatusBarEntryDTO::StatusBarEntryDTO,
10};
11use serde_json::json;
12use tauri::Emitter;
13
14use super::super::{MountainEnvironment::MountainEnvironment, Utility};
15use crate::dev_log;
16
17/// Entry management operations implementation for MountainEnvironment
18pub(super) async fn set_status_bar_entry_impl(
19	env:&MountainEnvironment,
20	entry:StatusBarEntryDTO,
21) -> Result<(), CommonError> {
22	dev_log!("lifecycle", "[StatusBarProvider] Setting entry: {}", entry.EntryIdentifier);
23
24	let mut items_guard = env
25		.ApplicationState
26		.Feature
27		.Markers
28		.ActiveStatusBarItems
29		.lock()
30		.map_err(Utility::ErrorMapping::MapApplicationStateLockErrorToCommonError)?;
31
32	items_guard.insert(entry.EntryIdentifier.clone(), entry.clone());
33
34	drop(items_guard);
35
36	let payload = json!({
37		"id": entry.EntryIdentifier,
38		"itemId": entry.ItemIdentifier,
39		"extensionId": entry.ExtensionIdentifier,
40		"name": entry.Name,
41		"text": entry.Text,
42		"tooltip": entry.Tooltip,
43		"command": entry.Command,
44		"color": entry.Color,
45		"backgroundColor": entry.BackgroundColor,
46		"alignment": if entry.IsAlignedLeft { 0 } else { 1 },
47		"priority": entry.Priority,
48		"accessibilityInformation": entry.AccessibilityInformation,
49	});
50
51	env.ApplicationHandle
52		.emit(SkyEvent::StatusBarSetEntry.AsStr(), payload)
53		.map_err(|error| CommonError::UserInterfaceInteraction { Reason:error.to_string() })
54}
55
56/// Removes a status bar item from the UI.
57pub(super) async fn dispose_status_bar_entry_impl(
58	env:&MountainEnvironment,
59	entry_identifier:String,
60) -> Result<(), CommonError> {
61	dev_log!("lifecycle", "[StatusBarProvider] Disposing entry: {}", entry_identifier);
62
63	env.ApplicationState
64		.Feature
65		.Markers
66		.ActiveStatusBarItems
67		.lock()
68		.map_err(Utility::ErrorMapping::MapApplicationStateLockErrorToCommonError)?
69		.remove(&entry_identifier);
70
71	env.ApplicationHandle
72		.emit(SkyEvent::StatusBarDisposeEntry.AsStr(), json!({ "id": entry_identifier }))
73		.map_err(|error| CommonError::UserInterfaceInteraction { Reason:error.to_string() })
74}