Skip to content

Commit

Permalink
fix(linux/globalShorcut): extract needed mods from event state, closes
Browse files Browse the repository at this point in the history
…#307, closes #537
  • Loading branch information
amrbashir committed Aug 19, 2022
1 parent 7d2eeee commit ff4f186
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changes/linux-global-shortcut.md
@@ -0,0 +1,5 @@
---
"tao": "patch"
---

On Linux, fix global shortcut are never triggered when a Lock key is ON, eg. NumLock, CapsLock ...etc
11 changes: 6 additions & 5 deletions examples/system_tray.rs
Expand Up @@ -24,7 +24,7 @@ fn main() {
// it to Tao. But on Windows, you will have to account for screen scaling. Here we use 32px,
// since it seems to work well enough in most cases. Be careful about going too high, or
// you'll be bitten by the low-quality downscaling built into the WM.
let path = concat!(env!("CARGO_MANIFEST_DIR"), "/examples/icon.png");
let path = "icon.png";

let main_tray_id = TrayId::new("main-tray");
let second_tray_id = TrayId::new("2nd-tray");
Expand All @@ -38,7 +38,7 @@ fn main() {
.set_icon(icon.clone());
}

let quit = tray_menu.add_item(MenuItemAttributes::new("Quit"));
let mut quit = tray_menu.add_item(MenuItemAttributes::new("Quit"));

#[cfg(target_os = "linux")]
let system_tray = SystemTrayBuilder::new(icon.clone(), Some(tray_menu))
Expand Down Expand Up @@ -73,9 +73,10 @@ fn main() {
} => {
if menu_id == quit.clone().id() {
// drop the system tray before exiting to remove the icon from system tray on Windows
drop(&system_tray);
drop(&second_system_tray);
*control_flow = ControlFlow::Exit;
quit.set_title("Hell");
// drop(&system_tray);
// drop(&second_system_tray);
// *control_flow = ControlFlow::Exit;
} else if menu_id == log.clone().id() {
println!("Log clicked");
}
Expand Down
4 changes: 3 additions & 1 deletion src/platform_impl/linux/global_shortcut.rs
Expand Up @@ -62,7 +62,9 @@ impl ShortcutManager {
(xlib.XNextEvent)(display, &mut event);
if let xlib::KeyRelease = event.get_type() {
let keycode = event.key.keycode;
let modifiers = event.key.state;
// X11 sends masks for Lock keys also and we only care about the 4 below
let modifiers = event.key.state
& (xlib::ControlMask | xlib::ShiftMask |= xlib::Mod4Mask | xlib::Mod1Mask);
if let Some(hotkey_id) = hotkey_map.lock().unwrap().get(&(keycode as i32, modifiers))
{
event_loop_channel
Expand Down
13 changes: 7 additions & 6 deletions src/platform_impl/windows/menu.rs
Expand Up @@ -4,7 +4,7 @@
use std::{collections::HashMap, fmt, ptr, sync::Mutex};

use windows::{
core::{PCWSTR, PWSTR},
core::{PCWSTR, PWSTR, PSTR},
Win32::{
Foundation::{HWND, LPARAM, LRESULT, WPARAM},
UI::{
Expand Down Expand Up @@ -123,14 +123,15 @@ impl MenuItemAttributes {
title.push_str(accelerator.to_string().as_str());
}
unsafe {
let info = MENUITEMINFOW {
cbSize: std::mem::size_of::<MENUITEMINFOW>() as _,
fMask: MIIM_STRING,
dwTypeData: PWSTR(util::encode_wide(title).as_mut_ptr()),
let info = MENUITEMINFOA {
cbSize: std::mem::size_of::<MENUITEMINFOA>() as _,
fMask: MIIM_TYPE,
fType: MFT_STRING,
dwTypeData: PSTR(util::encode_wide(title).as_mut_ptr() as _),
..Default::default()
};

SetMenuItemInfoW(self.1, self.0 as u32, false, &info);
SetMenuItemInfoA(self.1, self.0 as u32, false, &info);
}
}
pub fn set_selected(&mut self, selected: bool) {
Expand Down

0 comments on commit ff4f186

Please sign in to comment.