Skip to content

Commit

Permalink
feat: implement TrayIcon::set_show_menu_on_left_click on windows (#199
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Klemen2 authored Oct 14, 2024
1 parent daa3f7f commit 19e67de
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changes/with_menu_on_left_click_windows.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tray-icon": "minor"
---

Implemented `TrayIcon::set_show_menu_on_left_click` on windows
10 changes: 7 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
16 changes: 15 additions & 1 deletion src/platform_impl/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32> =
Expand Down Expand Up @@ -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<S: AsRef<str>>(&mut self, _title: Option<S>) {}

pub fn set_visible(&mut self, visible: bool) -> crate::Result<()> {
Expand Down Expand Up @@ -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!(
Expand Down

0 comments on commit 19e67de

Please sign in to comment.