GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat: implement exit option on system tray icon
Lars Berger28275aecd6df
diff --git a/packages/wm/src/common/platform/platform.rs b/packages/wm/src/common/platform/platform.rsindex ec0c6863..36678446 100644--- a/packages/wm/src/common/platform/platform.rs+++ b/packages/wm/src/common/platform/platform.rs@@ -200,15 +200,26 @@ impl Platform {}/// Runs a single cycle of a message loop on the current thread.- pub fn run_message_cycle() {+ ///+ /// Is non-blocking and returns immediately if there are no messages.+ pub fn run_message_cycle() -> anyhow::Result<()> {let mut msg = MSG::default();- if unsafe { PeekMessageW(&mut msg, None, 0, 0, PM_REMOVE) }.as_bool() {+ let has_message =+ unsafe { PeekMessageW(&mut msg, None, 0, 0, PM_REMOVE) }.as_bool();++ if has_message {+ if msg.message == WM_QUIT {+ bail!("Received WM_QUIT message.")+ }+unsafe {TranslateMessage(&msg);DispatchMessageW(&msg);}}++ Ok(())}/// Gracefully terminates the message loop on the given thread.diff --git a/packages/wm/src/main.rs b/packages/wm/src/main.rsindex acd41bdb..2490dbad 100644--- a/packages/wm/src/main.rs+++ b/packages/wm/src/main.rs@@ -82,7 +82,7 @@ async fn start_wm(start_watcher_process()?;// Add application icon to system tray.- let _tray = SystemTray::new()?;+ let mut tray = SystemTray::new()?;let mut wm = WindowManager::new(&config)?;@@ -134,6 +134,10 @@ async fn start_wm(error!("Failed to emit event over IPC: {:?}", err);}},+ Some(_) = tray.exit_rx.recv() => {+ info!("Exiting through system tray.");+ break Ok(());+ },_ = signal::ctrl_c() => {info!("Received SIGINT signal.");break Ok(());diff --git a/packages/wm/src/sys_tray.rs b/packages/wm/src/sys_tray.rsindex 52204b70..f3aaac89 100644--- a/packages/wm/src/sys_tray.rs+++ b/packages/wm/src/sys_tray.rs@@ -1,11 +1,9 @@use std::{thread::JoinHandle, time::Duration};+use tokio::sync::mpsc;use tracing::{error, info};use tray_icon::{- menu::{- AboutMetadata, CheckMenuItem, Menu, MenuEvent, MenuItem,- PredefinedMenuItem,- },+ menu::{CheckMenuItem, Menu, MenuEvent, MenuItem, PredefinedMenuItem},Icon, TrayIconBuilder,};@@ -15,34 +13,38 @@ use crate::common::platform::Platform;const IDI_ICON: u16 = 0x101;pub struct SystemTray {+ pub config_reload_rx: mpsc::UnboundedReceiver<()>,+ pub exit_rx: mpsc::UnboundedReceiver<()>,icon_thread: Option<JoinHandle<anyhow::Result<()>>>,}impl SystemTray {pub fn new() -> anyhow::Result<Self> {+ let (exit_tx, exit_rx) = mpsc::unbounded_channel();+ let (config_reload_tx, config_reload_rx) = mpsc::unbounded_channel();+let icon_thread = std::thread::spawn(move || {- let tray_menu = Menu::new();+ let reload_config_item = MenuItem::new("Reload config", true, None);++ let config_dir_item =+ MenuItem::new("Show config folder", true, None);- let quit_item = MenuItem::new("Quit", true, None);let animations_item =CheckMenuItem::new("Window animations", true, false, None);+ let exit_item = MenuItem::new("Exit", true, None);++ let tray_menu = Menu::new();tray_menu.append_items(&[+ &reload_config_item,+ &config_dir_item,&animations_item,- &PredefinedMenuItem::about(- None,- Some(AboutMetadata {- name: Some("test".to_string()),- copyright: Some("Copyright test".to_string()),- ..Default::default()- }),- ),&PredefinedMenuItem::separator(),- &quit_item,+ &exit_item,])?;let icon = Icon::from_resource(IDI_ICON, None)?;- let tray_icon = TrayIconBuilder::new()+ let _tray_icon = TrayIconBuilder::new().with_menu(Box::new(tray_menu)).with_tooltip(format!("GlazeWM v{}", env!("CARGO_PKG_VERSION"))).with_icon(icon)@@ -52,20 +54,33 @@ impl SystemTray {loop {if let Ok(event) = menu_event_rx.try_recv() {- if event.id == quit_item.id() {- quit_item.set_text("New title");+ if event.id == reload_config_item.id() {+ config_reload_tx.send(())?;+ } else if event.id == config_dir_item.id() {+ // TODO: Open config directory.+ todo!()} else if event.id == animations_item.id() {+ // TODO: Enable/disable window animations.animations_item.set_checked(animations_item.is_checked());+ } else if event.id == exit_item.id() {+ exit_tx.send(())?;}}- // Add delay of 16ms (60fps) to reduce cpu load.- Platform::run_message_cycle();+ // Run message loop with a delay of 16ms (60fps).+ if let Err(_) = Platform::run_message_cycle() {+ break;+ }+std::thread::sleep(Duration::from_millis(16));}++ Ok(())});Ok(Self {+ config_reload_rx,+ exit_rx,icon_thread: Some(icon_thread),})}