Merge pull request #69 from cjpais/tray-icon-theme-windows

Respect OS Theme with Tray Icon
This commit is contained in:
CJ Pais 2025-08-02 12:09:59 -07:00 committed by GitHub
commit f8afa2b6a2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 46 additions and 6 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

View file

@ -78,9 +78,25 @@ pub fn run() {
))
.manage(Mutex::new(ShortcutToggleStates::default()))
.setup(move |app| {
// Get the current theme to set the appropriate initial icon
let initial_theme = if let Some(main_window) = app.get_webview_window("main") {
main_window.theme().unwrap_or(tauri::Theme::Dark)
} else {
tauri::Theme::Dark
};
println!("Initial system theme: {:?}", initial_theme);
// Choose the appropriate initial icon based on theme
let initial_icon_path = match initial_theme {
tauri::Theme::Dark => "resources/tray_idle.png",
tauri::Theme::Light => "resources/tray_idle_dark.png",
_ => "resources/tray_idle.png", // Default fallback
};
let tray = TrayIconBuilder::new()
.icon(Image::from_path(app.path().resolve(
"resources/tray_idle.png",
initial_icon_path,
tauri::path::BaseDirectory::Resource,
)?)?)
.show_menu_on_left_click(true)
@ -151,6 +167,11 @@ pub fn run() {
}
}
}
tauri::WindowEvent::ThemeChanged(theme) => {
println!("Theme changed to: {:?}", theme);
// Update tray icon to match new theme, maintaining idle state
utils::change_tray_icon(&window.app_handle(), utils::TrayIconState::Idle);
}
_ => {}
})
.invoke_handler(tauri::generate_handler![

View file

@ -14,7 +14,7 @@ use std::thread;
use tauri::image::Image;
use tauri::menu::{Menu, MenuItem, PredefinedMenuItem};
use tauri::tray::TrayIcon;
use tauri::{AppHandle, Emitter, Manager, WebviewWindowBuilder};
use tauri::{AppHandle, Emitter, Manager, Theme, WebviewWindowBuilder};
use tauri_plugin_clipboard_manager::ClipboardExt;
/// Sends a paste command (Cmd+V or Ctrl+V) using platform-specific virtual key codes.
@ -82,13 +82,32 @@ pub enum TrayIconState {
Transcribing,
}
/// Gets the current system theme, defaulting to Dark if unavailable
fn get_current_theme(app: &AppHandle) -> Theme {
if let Some(main_window) = app.get_webview_window("main") {
main_window.theme().unwrap_or(Theme::Dark)
} else {
Theme::Dark
}
}
pub fn change_tray_icon(app: &AppHandle, icon: TrayIconState) {
let tray = app.state::<TrayIcon>();
let theme = get_current_theme(app);
let icon_path = match icon {
TrayIconState::Idle => "resources/tray_idle.png",
TrayIconState::Recording => "resources/tray_recording.png",
TrayIconState::Transcribing => "resources/tray_transcribing.png",
let icon_path = match (theme, &icon) {
// Dark theme uses regular icons (lighter colored for visibility)
(Theme::Dark, TrayIconState::Idle) => "resources/tray_idle.png",
(Theme::Dark, TrayIconState::Recording) => "resources/tray_recording.png",
(Theme::Dark, TrayIconState::Transcribing) => "resources/tray_transcribing.png",
// Light theme uses dark icons (darker colored for visibility)
(Theme::Light, TrayIconState::Idle) => "resources/tray_idle_dark.png",
(Theme::Light, TrayIconState::Recording) => "resources/tray_recording_dark.png",
(Theme::Light, TrayIconState::Transcribing) => "resources/tray_transcribing_dark.png",
// Fallback for any other theme variants
(_, TrayIconState::Idle) => "resources/tray_idle.png",
(_, TrayIconState::Recording) => "resources/tray_recording.png",
(_, TrayIconState::Transcribing) => "resources/tray_transcribing.png",
};
let _ = tray.set_icon(Some(