Skip to main content

Mountain/Binary/Tray/
SwitchTrayIcon.rs

1// =============================================================================
2// Binary / Tray / SwitchTrayIcon
3// =============================================================================
4//
5//! # Switch Tray Icon Command
6//!
7//! Tauri command to dynamically switch the tray icon based on the theme.
8//!
9//! ## RESPONSIBILITIES
10//!
11//! ### Icon Switching
12//! - Switch between light and dark theme tray icons
13//! - Load appropriate icon bytes from embedded resources
14//! - Handle icon loading errors gracefully
15//!
16//! ### Theme Integration
17//! - Respond to theme changes from the frontend
18//! - Provide smooth visual transition when switching themes
19//!
20//! ## ARCHITECTURAL ROLE
21//!
22//! ### Position in Mountain
23//! - Tauri command handler for tray icon switching
24//! - Exposed to frontend for theme integration
25//! - Part of the Binary/Tray subsystem
26//!
27//! ### Dependencies
28//! - Tauri: AppHandle, image loading, and tray API
29//! - log: Error logging
30//!
31//! ### Dependents
32//! - Frontend (Sky): Invokes this command when theme changes
33//!
34//! ## TODO
35//!
36//! ### Immediate Improvements
37//! - Add support for custom icon paths
38//! - Implement icon caching to reduce memory usage
39//!
40//! ### Future Work
41//! - Support for animated tray icons
42//! - Add icon transition effects
43//! - Support for third-party icon themes
44//!
45//! ### Missing Functionality to Probe
46//! - Platform-specific icon format requirements
47//! - Icon size optimization for different DPI settings
48//! - Icon loading performance characteristics
49
50use tauri::{AppHandle, image::Image};
51
52use crate::dev_log;
53
54/// Dynamically switches the tray icon based on the theme (Light/Dark).
55/// Can be invoked from the frontend when the theme changes.
56///
57/// # Parameters
58///
59/// - `App`: Tauri application handle
60/// - `IsDarkMode`: Whether dark mode is active
61///
62/// # Behavior
63///
64/// - Loads the appropriate icon bytes from embedded resources
65/// - Updates the tray icon with the new image
66/// - Logs errors if icon loading or setting fails
67///
68/// # Errors
69///
70/// - Logs warnings/errors but doesn't panic:
71///   - If tray with ID 'tray' not found
72///   - If icon bytes fail to load
73///   - If setting the new icon fails
74#[tauri::command]
75pub fn SwitchTrayIcon(App:AppHandle, IsDarkMode:bool) {
76	dev_log!("window", "[UI] [Tray] Switching icon. IsDarkMode: {}", IsDarkMode);
77
78	const DARK_ICON_BYTES:&[u8] = include_bytes!("../../../icons/32x32.png");
79
80	const LIGHT_ICON_BYTES:&[u8] = include_bytes!("../../../icons/32x32.png");
81
82	let IconBytes = if IsDarkMode { DARK_ICON_BYTES } else { LIGHT_ICON_BYTES };
83
84	if let Some(Tray) = App.tray_by_id("tray") {
85		match Image::from_bytes(IconBytes) {
86			Ok(IconImage) => {
87				if let Err(e) = Tray.set_icon(Some(IconImage)) {
88					dev_log!("window", "error: [UI] [Tray] Failed to set icon: {}", e);
89				}
90			},
91			Err(e) => dev_log!("window", "error: [UI] [Tray] Failed to load icon bytes: {}", e),
92		}
93	} else {
94		dev_log!("window", "warn: [UI] [Tray] Tray with ID 'tray' not found.");
95	}
96}