Skip to content

GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.

feat: check system tray receiver on every message cycle

Lars Berger4024cf3def10
diff --git a/packages/wm/src/common/platform/platform.rs b/packages/wm/src/common/platform/platform.rs
index 58ef91c5..ec0c6863 100644
--- a/packages/wm/src/common/platform/platform.rs
+++ b/packages/wm/src/common/platform/platform.rs
@@ -15,10 +15,11 @@ use windows::{
},
WindowsAndMessaging::{
CreateWindowExW, DispatchMessageW, GetAncestor, GetDesktopWindow,
- GetForegroundWindow, GetMessageW, PostThreadMessageW,
- RegisterClassW, SetCursorPos, TranslateMessage, WindowFromPoint,
- CS_HREDRAW, CS_VREDRAW, CW_USEDEFAULT, GA_ROOT, MSG, SW_NORMAL,
- WM_QUIT, WNDCLASSW, WNDPROC, WS_OVERLAPPEDWINDOW,
+ GetForegroundWindow, GetMessageW, PeekMessageW,
+ PostThreadMessageW, RegisterClassW, SetCursorPos,
+ TranslateMessage, WindowFromPoint, CS_HREDRAW, CS_VREDRAW,
+ CW_USEDEFAULT, GA_ROOT, MSG, PM_REMOVE, SW_NORMAL, WM_QUIT,
+ WNDCLASSW, WNDPROC, WS_OVERLAPPEDWINDOW,
},
},
},
@@ -198,6 +199,18 @@ impl Platform {
}
}
+ /// Runs a single cycle of a message loop on the current thread.
+ pub fn run_message_cycle() {
+ let mut msg = MSG::default();
+
+ if unsafe { PeekMessageW(&mut msg, None, 0, 0, PM_REMOVE) }.as_bool() {
+ unsafe {
+ TranslateMessage(&msg);
+ DispatchMessageW(&msg);
+ }
+ }
+ }
+
/// Gracefully terminates the message loop on the given thread.
pub fn kill_message_loop<T>(
thread: &JoinHandle<T>,
diff --git a/packages/wm/src/sys_tray.rs b/packages/wm/src/sys_tray.rs
index 6846a0c2..52204b70 100644
--- a/packages/wm/src/sys_tray.rs
+++ b/packages/wm/src/sys_tray.rs
@@ -1,14 +1,17 @@
-use std::thread::JoinHandle;
+use std::{thread::JoinHandle, time::Duration};
use tracing::{error, info};
use tray_icon::{
- menu::{AboutMetadata, Menu, MenuEvent, MenuItem, PredefinedMenuItem},
+ menu::{
+ AboutMetadata, CheckMenuItem, Menu, MenuEvent, MenuItem,
+ PredefinedMenuItem,
+ },
Icon, TrayIconBuilder,
};
use crate::common::platform::Platform;
-/// Ordinal to IDI_ICON definition from `resources.rc`.
+/// Ordinal to IDI_ICON definition from `resources.rc` file.
const IDI_ICON: u16 = 0x101;
pub struct SystemTray {
@@ -21,8 +24,11 @@ impl SystemTray {
let tray_menu = Menu::new();
let quit_item = MenuItem::new("Quit", true, None);
+ let animations_item =
+ CheckMenuItem::new("Window animations", true, false, None);
tray_menu.append_items(&[
+ &animations_item,
&PredefinedMenuItem::about(
None,
Some(AboutMetadata {
@@ -35,18 +41,28 @@ impl SystemTray {
&quit_item,
])?;
- let menu_event_rx = MenuEvent::receiver();
-
let icon = Icon::from_resource(IDI_ICON, None)?;
let tray_icon = TrayIconBuilder::new()
.with_menu(Box::new(tray_menu))
- .with_tooltip("test")
+ .with_tooltip(format!("GlazeWM v{}", env!("CARGO_PKG_VERSION")))
.with_icon(icon)
.build()?;
- Platform::run_message_loop();
+ let menu_event_rx = MenuEvent::receiver();
- Ok(())
+ loop {
+ if let Ok(event) = menu_event_rx.try_recv() {
+ if event.id == quit_item.id() {
+ quit_item.set_text("New title");
+ } else if event.id == animations_item.id() {
+ animations_item.set_checked(animations_item.is_checked());
+ }
+ }
+
+ // Add delay of 16ms (60fps) to reduce cpu load.
+ Platform::run_message_cycle();
+ std::thread::sleep(Duration::from_millis(16));
+ }
});
Ok(Self {