diff --git a/agent/src/tray/mod.rs b/agent/src/tray/mod.rs index 6285bf2..593d6ea 100644 --- a/agent/src/tray/mod.rs +++ b/agent/src/tray/mod.rs @@ -12,6 +12,11 @@ use std::sync::Arc; use tray_icon::{Icon, TrayIcon, TrayIconBuilder, TrayIconEvent}; use tracing::{info, warn}; +#[cfg(windows)] +use windows::Win32::UI::WindowsAndMessaging::{ + PeekMessageW, TranslateMessage, DispatchMessageW, MSG, PM_REMOVE, +}; + /// Events that can be triggered from the tray menu #[derive(Debug, Clone)] pub enum TrayAction { @@ -83,6 +88,10 @@ impl TrayController { /// Process pending menu events (call this from the main loop) pub fn process_events(&self) -> Option { + // Pump Windows message queue to process tray icon events + #[cfg(windows)] + pump_windows_messages(); + // Check for menu events if let Ok(event) = MenuEvent::receiver().try_recv() { if event.id == self.end_session_item.id() { @@ -107,6 +116,19 @@ impl TrayController { } } +/// Pump the Windows message queue to process tray icon events +#[cfg(windows)] +fn pump_windows_messages() { + unsafe { + let mut msg = MSG::default(); + // Process all pending messages + while PeekMessageW(&mut msg, None, 0, 0, PM_REMOVE).as_bool() { + let _ = TranslateMessage(&msg); + DispatchMessageW(&msg); + } + } +} + /// Create a simple default icon (green circle for connected) fn create_default_icon() -> Result { // Create a simple 32x32 green icon