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 log::{debug, error, warn};
51use tauri::{AppHandle, image::Image};
52
53/// Dynamically switches the tray icon based on the theme (Light/Dark).
54/// Can be invoked from the frontend when the theme changes.
55///
56/// # Parameters
57///
58/// - `App`: Tauri application handle
59/// - `IsDarkMode`: Whether dark mode is active
60///
61/// # Behavior
62///
63/// - Loads the appropriate icon bytes from embedded resources
64/// - Updates the tray icon with the new image
65/// - Logs errors if icon loading or setting fails
66///
67/// # Errors
68///
69/// - Logs warnings/errors but doesn't panic:
70/// - If tray with ID 'tray' not found
71/// - If icon bytes fail to load
72/// - If setting the new icon fails
73#[tauri::command]
74pub fn SwitchTrayIcon(App:AppHandle, IsDarkMode:bool) {
75 debug!("[UI] [Tray] Switching icon. IsDarkMode: {}", IsDarkMode);
76
77 const DARK_ICON_BYTES:&[u8] = include_bytes!("../../../icons/32x32.png");
78
79 const LIGHT_ICON_BYTES:&[u8] = include_bytes!("../../../icons/32x32.png");
80
81 let IconBytes = if IsDarkMode { DARK_ICON_BYTES } else { LIGHT_ICON_BYTES };
82
83 if let Some(Tray) = App.tray_by_id("tray") {
84 match Image::from_bytes(IconBytes) {
85 Ok(IconImage) => {
86 if let Err(e) = Tray.set_icon(Some(IconImage)) {
87 error!("[UI] [Tray] Failed to set icon: {}", e);
88 }
89 },
90 Err(e) => error!("[UI] [Tray] Failed to load icon bytes: {}", e),
91 }
92 } else {
93 warn!("[UI] [Tray] Tray with ID 'tray' not found.");
94 }
95}