GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat: show error dialogs for user-facing errors
Lars Berger9b3d46604c4b
diff --git a/packages/wm/src/common/commands/shell_exec.rs b/packages/wm/src/common/commands/shell_exec.rsindex d7fa81b7..677955ab 100644--- a/packages/wm/src/common/commands/shell_exec.rs+++ b/packages/wm/src/common/commands/shell_exec.rs@@ -1,13 +1,27 @@-use tracing::info;+use tracing::{error, info};use crate::common::platform::Platform;pub fn shell_exec(command: &str) -> anyhow::Result<()> {- let (program, args) = Platform::parse_command(command)?;- info!("Parsed command program: '{}', args: '{}'.", program, args);+ let res =+ Platform::parse_command(command).and_then(|(program, args)| {+ info!("Parsed command program: '{}', args: '{}'.", program, args);- Platform::run_command(&program, &args)?;- info!("Command executed successfully: {}.", command);+ Platform::run_command(&program, &args)+ });++ match res {+ Ok(_) => {+ info!("Command executed successfully: {}.", command);+ }+ Err(err) => {+ let err_message =+ format!("Failed to execute '{}'.\n\nError: {}", command, err);++ error!(err_message);+ Platform::show_error_dialog("Non-fatal error", &err_message);+ }+ }Ok(())}diff --git a/packages/wm/src/common/platform/event_window.rs b/packages/wm/src/common/platform/event_window.rsindex 00afea48..edf66a99 100644--- a/packages/wm/src/common/platform/event_window.rs+++ b/packages/wm/src/common/platform/event_window.rs@@ -159,7 +159,7 @@ impl EventWindow {impl Drop for EventWindow {fn drop(&mut self) {if let Err(err) = self.destroy() {- error!("Failed to gracefully shut down event window: {}", err);+ warn!("Failed to gracefully shut down event window: {}", err);}}}diff --git a/packages/wm/src/common/platform/platform.rs b/packages/wm/src/common/platform/platform.rsindex 2bf6d021..472cd764 100644--- a/packages/wm/src/common/platform/platform.rs+++ b/packages/wm/src/common/platform/platform.rs@@ -19,13 +19,13 @@ use windows::{},WindowsAndMessaging::{CreateWindowExW, DispatchMessageW, GetAncestor, GetDesktopWindow,- GetForegroundWindow, GetMessageW, PeekMessageW,+ GetForegroundWindow, GetMessageW, MessageBoxW, PeekMessageW,PostThreadMessageW, RegisterClassW, SetCursorPos,SystemParametersInfoW, TranslateMessage, WindowFromPoint,ANIMATIONINFO, CS_HREDRAW, CS_VREDRAW, CW_USEDEFAULT, GA_ROOT,- MSG, PM_REMOVE, SPI_GETANIMATION, SPI_SETANIMATION, SW_NORMAL,- SYSTEM_PARAMETERS_INFO_UPDATE_FLAGS, WM_QUIT, WNDCLASSW, WNDPROC,- WS_OVERLAPPEDWINDOW,+ MB_ICONERROR, MB_OK, MSG, PM_REMOVE, SPI_GETANIMATION,+ SPI_SETANIMATION, SW_NORMAL, SYSTEM_PARAMETERS_INFO_UPDATE_FLAGS,+ WM_QUIT, WNDCLASSW, WNDPROC, WS_OVERLAPPEDWINDOW,},},},@@ -431,6 +431,20 @@ impl Platform {unsafe { ShellExecuteExW(&mut exec_info) }?;Ok(())}++ pub fn show_error_dialog(title: &str, message: &str) {+ let title_wide = to_wide(title);+ let message_wide = to_wide(message);++ unsafe {+ MessageBoxW(+ None,+ PCWSTR(message_wide.as_ptr()),+ PCWSTR(title_wide.as_ptr()),+ MB_ICONERROR | MB_OK,+ );+ }+ }}/// Utility function to convert a string to a null-terminated wide string.diff --git a/packages/wm/src/ipc_server.rs b/packages/wm/src/ipc_server.rsindex 3bf53b20..8a6d13c1 100644--- a/packages/wm/src/ipc_server.rs+++ b/packages/wm/src/ipc_server.rs@@ -10,7 +10,7 @@ use tokio::{task,};use tokio_tungstenite::{accept_async, tungstenite::Message};-use tracing::{error, info, warn};+use tracing::{info, warn};use uuid::Uuid;use crate::{@@ -141,7 +141,7 @@ impl IpcServer {if let Err(err) =Self::handle_connection(stream, addr, message_tx).await{- error!("Error handling connection: {}", err);+ warn!("Error handling connection: {}", err);}});}@@ -180,7 +180,7 @@ impl IpcServer {tokio::select! {Some(response) = response_rx.recv() => {if let Err(err) = outgoing.send(response).await {- error!("Error sending response: {}", err);+ warn!("Error sending response: {}", err);break;}}@@ -346,7 +346,7 @@ impl IpcServer {.map(|event_msg| response_tx.send(event_msg));if let Err(err) = res {- error!("Error emitting WM event: {}", err);+ warn!("Error emitting WM event: {}", err);break;}}diff --git a/packages/wm/src/main.rs b/packages/wm/src/main.rsindex fbd6c992..d6560361 100644--- a/packages/wm/src/main.rs+++ b/packages/wm/src/main.rs@@ -9,8 +9,7 @@ use std::{env, path::PathBuf};use anyhow::{Context, Error, Result};use tokio::{process::Command, signal};-use tracing::{debug, error, info, Level};-use tracing_appender::non_blocking::WorkerGuard;+use tracing::{debug, error, info, warn, Level};use tracing_subscriber::{fmt::{self, writer::MakeWriterExt},layer::SubscriberExt,@@ -58,12 +57,11 @@ async fn main() -> Result<()> {} => {let res = start_wm(config_path, verbosity).await;+ // If unable to start the WM, the error is fatal and a message dialog+ // is shown.if let Err(err) = &res {- error!(- error = err.to_string(),- error.backtrace = err.backtrace().to_string(),- is_fatal = true- );+ error!("{:?}", err);+ Platform::show_message_box("Fatal error", &err.to_string());};res@@ -126,13 +124,22 @@ async fn start_wm(wm.process_commands(startup_commands, None, &mut config)?;loop {- tokio::select! {+ let res = tokio::select! {+ Some(_) = tray.exit_rx.recv() => {+ info!("Exiting through system tray.");+ break;+ },+ Some(_) = wm.exit_rx.recv() => {+ info!("Exiting through WM command.");+ break;+ },+ _ = signal::ctrl_c() => {+ info!("Received SIGINT signal.");+ break;+ },Some(event) = event_listener.event_rx.recv() => {debug!("Received platform event: {:?}", event);-- if let Err(err) = wm.process_event(event, &mut config) {- error!("Failed to process event: {:?}", err);- }+ wm.process_event(event, &mut config)},Some((message,@@ -141,15 +148,13 @@ async fn start_wm()) = ipc_server.message_rx.recv() => {info!("Received IPC message: {:?}", message);- if let Err(err) = ipc_server.process_message(+ ipc_server.process_message(message,response_tx,disconnection_tx,&mut wm,&mut config,- ) {- error!("Failed to process IPC message: {:?}", err);- }+ )},Some(wm_event) = wm.event_rx.recv() => {info!("Received WM event: {:?}", wm_event);@@ -167,37 +172,27 @@ async fn start_wm();}- if let Err(err) = ipc_server.process_event(wm_event) {- error!("Failed to emit event over IPC: {:?}", err);- }+ ipc_server.process_event(wm_event)},Some(_) = tray.config_reload_rx.recv() => {- if let Err(err) = wm.process_commands(+ wm.process_commands(vec![InvokeCommand::WmReloadConfig],None,&mut config,- ) {- error!("Failed to reload config: {:?}", err);- }- },- Some(_) = tray.exit_rx.recv() => {- info!("Exiting through system tray.");- break;- },- Some(_) = wm.exit_rx.recv() => {- info!("Exiting through WM command.");- break;- },- _ = signal::ctrl_c() => {- info!("Received SIGINT signal.");- break;+ )+ .and_then(|_| Ok(()))},+ };++ if let Err(err) = res {+ error!("{:?}", err);+ Platform::show_message_box("Non-fatal error", &err.to_string());}}// Broadcast `WmEvent::ApplicationExiting` on shutdown.if let Err(err) = ipc_server.process_event(WmEvent::ApplicationExiting) {- error!(+ warn!("Failed to emit `WmEvent::ApplicationExiting` event over IPC: {:?}",err);diff --git a/packages/wm/src/sys_tray.rs b/packages/wm/src/sys_tray.rsindex 907bcd68..16bc59f2 100644--- a/packages/wm/src/sys_tray.rs+++ b/packages/wm/src/sys_tray.rs@@ -2,7 +2,7 @@ use std::{path::PathBuf, thread::JoinHandle, time::Duration};use anyhow::Context;use tokio::sync::mpsc;-use tracing::{error, info};+use tracing::{info, warn};use tray_icon::{menu::{CheckMenuItem, Menu, MenuEvent, MenuItem, PredefinedMenuItem},Icon, TrayIconBuilder,@@ -120,7 +120,7 @@ impl SystemTray {impl Drop for SystemTray {fn drop(&mut self) {if let Err(err) = self.destroy() {- error!("Failed to gracefully shut down system tray: {}", err);+ warn!("Failed to gracefully shut down system tray: {}", err);}}}