From 19e67de6ff0b66241fddde507eb82e96781b6c36 Mon Sep 17 00:00:00 2001 From: Klemen Date: Mon, 14 Oct 2024 13:07:15 +0200 Subject: [PATCH] feat: implement `TrayIcon::set_show_menu_on_left_click` on windows (#199) --- .changes/with_menu_on_left_click_windows.md | 5 +++++ src/lib.rs | 10 +++++++--- src/platform_impl/windows/mod.rs | 16 +++++++++++++++- 3 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 .changes/with_menu_on_left_click_windows.md diff --git a/.changes/with_menu_on_left_click_windows.md b/.changes/with_menu_on_left_click_windows.md new file mode 100644 index 0000000..bd6e4d9 --- /dev/null +++ b/.changes/with_menu_on_left_click_windows.md @@ -0,0 +1,5 @@ +--- +"tray-icon": "minor" +--- + +Implemented `TrayIcon::set_show_menu_on_left_click` on windows diff --git a/src/lib.rs b/src/lib.rs index 9c18227..f5a3f33 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -387,11 +387,15 @@ impl TrayIcon { let _ = is_template; } - /// Disable or enable showing the tray menu on left click. **macOS only**. + /// Disable or enable showing the tray menu on left click. + /// + /// ## Platform-specific: + /// + /// - **Linux:** Unsupported. pub fn set_show_menu_on_left_click(&self, enable: bool) { - #[cfg(target_os = "macos")] + #[cfg(any(target_os = "macos", target_os = "windows"))] self.tray.borrow_mut().set_show_menu_on_left_click(enable); - #[cfg(not(target_os = "macos"))] + #[cfg(not(any(target_os = "macos", target_os = "windows")))] let _ = enable; } diff --git a/src/platform_impl/windows/mod.rs b/src/platform_impl/windows/mod.rs index fed9a95..513f18c 100644 --- a/src/platform_impl/windows/mod.rs +++ b/src/platform_impl/windows/mod.rs @@ -44,7 +44,7 @@ const WM_USER_SHOW_TRAYICON: u32 = 6005; const WM_USER_HIDE_TRAYICON: u32 = 6006; const WM_USER_UPDATE_TRAYTOOLTIP: u32 = 6007; const WM_USER_LEAVE_TIMER_ID: u32 = 6008; - +const WM_USER_SHOW_MENU_ON_LEFT_CLICK: u32 = 6009; /// When the taskbar is created, it registers a message with the "TaskbarCreated" string and then broadcasts this message to all top-level windows /// When the application receives this message, it should assume that any taskbar icons it added have been removed and add them again. static S_U_TASKBAR_RESTART: Lazy = @@ -224,6 +224,17 @@ impl TrayIcon { Ok(()) } + pub fn set_show_menu_on_left_click(&mut self, enable: bool) { + unsafe { + SendMessageW( + self.hwnd, + WM_USER_SHOW_MENU_ON_LEFT_CLICK, + enable as usize, + 0, + ); + } + } + pub fn set_title>(&mut self, _title: Option) {} pub fn set_visible(&mut self, visible: bool) -> crate::Result<()> { @@ -325,6 +336,9 @@ unsafe extern "system" fn tray_proc( &userdata.tooltip, ); } + WM_USER_SHOW_MENU_ON_LEFT_CLICK => { + userdata.menu_on_left_click = wparam != 0; + } WM_USER_TRAYICON if matches!(