Skip to content

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

feat: add `wm-toggle-pause` command (#828)

Michael Lohr3429eec55623
diff --git a/packages/wm/src/app_command.rs b/packages/wm/src/app_command.rs
index 3664e167..579e7ebb 100644
--- a/packages/wm/src/app_command.rs
+++ b/packages/wm/src/app_command.rs
@@ -10,7 +10,7 @@ use crate::{
common::{
commands::{
cycle_focus, disable_binding_mode, enable_binding_mode,
- reload_config, shell_exec,
+ reload_config, shell_exec, toggle_pause,
},
Direction, LengthValue, RectDelta, TilingDirection,
},
@@ -156,6 +156,8 @@ pub enum QueryCommand {
Windows,
/// Outputs all active workspaces.
Workspaces,
+ /// Outputs whether the window manager is paused.
+ Paused,
}
#[derive(Clone, Debug, PartialEq, ValueEnum)]
@@ -176,6 +178,7 @@ pub enum SubscribableEvent {
WorkspaceActivated,
WorkspaceDeactivated,
WorkspaceUpdated,
+ PauseChanged,
}
#[derive(Clone, Debug, Parser, PartialEq, Serialize)]
@@ -271,6 +274,7 @@ pub enum InvokeCommand {
WmExit,
WmRedraw,
WmReloadConfig,
+ WmTogglePause,
}
impl InvokeCommand {
@@ -715,6 +719,7 @@ impl InvokeCommand {
Ok(())
}
InvokeCommand::WmReloadConfig => reload_config(state, config),
+ InvokeCommand::WmTogglePause => toggle_pause(state),
}
}
diff --git a/packages/wm/src/common/commands/mod.rs b/packages/wm/src/common/commands/mod.rs
index ccfe9e9f..30cf6292 100644
--- a/packages/wm/src/common/commands/mod.rs
+++ b/packages/wm/src/common/commands/mod.rs
@@ -4,6 +4,7 @@ mod enable_binding_mode;
mod platform_sync;
mod reload_config;
mod shell_exec;
+mod toggle_pause;
pub use cycle_focus::*;
pub use disable_binding_mode::*;
@@ -11,3 +12,4 @@ pub use enable_binding_mode::*;
pub use platform_sync::*;
pub use reload_config::*;
pub use shell_exec::*;
+pub use toggle_pause::*;
diff --git a/packages/wm/src/common/commands/toggle_pause.rs b/packages/wm/src/common/commands/toggle_pause.rs
new file mode 100644
index 00000000..abb32237
--- /dev/null
+++ b/packages/wm/src/common/commands/toggle_pause.rs
@@ -0,0 +1,27 @@
+use crate::{
+ user_config::{UserConfig, WindowRuleEvent},
+ windows::{commands::run_window_rules, traits::WindowGetters},
+ wm_event::WmEvent,
+ wm_state::WmState,
+};
+
+/// Pauses or unpauses the WM.
+pub fn toggle_pause(state: &mut WmState) -> anyhow::Result<()> {
+ let new_paused_state = !state.paused;
+ state.paused = new_paused_state;
+
+ if !new_paused_state {
+ // Redraw full container tree.
+ let root_container = state.root_container.clone();
+ state
+ .pending_sync
+ .containers_to_redraw
+ .push(root_container.into());
+ }
+
+ state.emit_event(WmEvent::PauseChanged {
+ paused: new_paused_state,
+ });
+
+ Ok(())
+}
diff --git a/packages/wm/src/common/platform/event_listener.rs b/packages/wm/src/common/platform/event_listener.rs
index d77e538a..51d5435c 100644
--- a/packages/wm/src/common/platform/event_listener.rs
+++ b/packages/wm/src/common/platform/event_listener.rs
@@ -63,6 +63,7 @@ impl EventListener {
&mut self,
config: &UserConfig,
binding_modes: &[BindingModeConfig],
+ paused: bool,
) {
// Modify keybindings based on active binding modes.
let keybindings = match binding_modes.first() {
@@ -70,8 +71,10 @@ impl EventListener {
None => &config.value.keybindings,
};
- self
- .event_window
- .update(keybindings, config.value.general.focus_follows_cursor);
+ self.event_window.update(
+ keybindings,
+ config.value.general.focus_follows_cursor,
+ paused,
+ );
}
}
diff --git a/packages/wm/src/common/platform/event_window.rs b/packages/wm/src/common/platform/event_window.rs
index 6dbe6c32..3706e006 100644
--- a/packages/wm/src/common/platform/event_window.rs
+++ b/packages/wm/src/common/platform/event_window.rs
@@ -71,6 +71,7 @@ static LAST_MOUSE_EVENT_TIME: AtomicU64 = AtomicU64::new(0);
pub struct EventWindow {
keyboard_hook: Arc<KeyboardHook>,
window_thread: Option<JoinHandle<anyhow::Result<()>>>,
+ paused: bool,
}
impl EventWindow {
@@ -132,6 +133,7 @@ impl EventWindow {
Ok(Self {
keyboard_hook,
window_thread: Some(window_thread),
+ paused: false,
})
}
@@ -139,9 +141,12 @@ impl EventWindow {
&mut self,
keybindings: &Vec<KeybindingConfig>,
enable_mouse_events: bool,
+ paused: bool,
) {
- self.keyboard_hook.update(keybindings);
- ENABLE_MOUSE_EVENTS.store(enable_mouse_events, Ordering::Relaxed);
+ self.keyboard_hook.update(keybindings, paused);
+ ENABLE_MOUSE_EVENTS
+ .store(enable_mouse_events && !paused, Ordering::Relaxed);
+ self.paused = paused;
}
/// Destroys the event window and stops the message loop.
diff --git a/packages/wm/src/common/platform/keyboard_hook.rs b/packages/wm/src/common/platform/keyboard_hook.rs
index 2e457634..a419bf30 100644
--- a/packages/wm/src/common/platform/keyboard_hook.rs
+++ b/packages/wm/src/common/platform/keyboard_hook.rs
@@ -1,6 +1,6 @@
use std::{
collections::HashMap,
- sync::{Arc, Mutex, OnceLock},
+ sync::{atomic::AtomicBool, Arc, Mutex, OnceLock},
};
use tokio::sync::mpsc;
@@ -17,7 +17,10 @@ use windows::Win32::{
};
use super::PlatformEvent;
-use crate::user_config::KeybindingConfig;
+use crate::{
+ app_command::InvokeCommand, common::platform::com,
+ user_config::KeybindingConfig,
+};
/// Global instance of `KeyboardHook`.
///
@@ -52,6 +55,8 @@ pub struct KeyboardHook {
/// final key in a key combination.
keybindings_by_trigger_key:
Arc<Mutex<HashMap<u16, Vec<ActiveKeybinding>>>>,
+
+ paused: AtomicBool,
}
impl KeyboardHook {
@@ -66,6 +71,7 @@ impl KeyboardHook {
keybindings_by_trigger_key: Arc::new(Mutex::new(
Self::keybindings_by_trigger_key(keybindings),
)),
+ paused: AtomicBool::new(false),
});
KEYBOARD_HOOK
@@ -86,9 +92,13 @@ impl KeyboardHook {
Ok(())
}
- pub fn update(&self, keybindings: &Vec<KeybindingConfig>) {
+ pub fn update(&self, keybindings: &Vec<KeybindingConfig>, paused: bool) {
*self.keybindings_by_trigger_key.lock().unwrap() =
Self::keybindings_by_trigger_key(keybindings);
+
+ self
+ .paused
+ .store(paused, std::sync::atomic::Ordering::SeqCst);
}
/// Stops the low-level keyboard hook.
@@ -355,6 +365,21 @@ impl KeyboardHook {
return false;
}
+ // Is the WM paused and the keybinding does not a contain toggle
+ // pause command
+ if self.paused.load(std::sync::atomic::Ordering::SeqCst)
+ && longest_keybinding
+ .config
+ .commands
+ .iter()
+ .find(|command| {
+ matches!(command, InvokeCommand::WmTogglePause)
+ })
+ .is_none()
+ {
+ return false;
+ }
+
// Invoke the callback function for the longest matching
// keybinding.
let _ = self.event_tx.send(PlatformEvent::KeybindingTriggered(
diff --git a/packages/wm/src/ipc_server.rs b/packages/wm/src/ipc_server.rs
index 329488bf..3271621c 100644
--- a/packages/wm/src/ipc_server.rs
+++ b/packages/wm/src/ipc_server.rs
@@ -56,6 +56,7 @@ pub enum ClientResponseData {
TilingDirection(TilingDirectionData),
Windows(WindowsData),
Workspaces(WorkspacesData),
+ Paused(bool),
}
#[derive(Clone, Debug, Deserialize, Serialize)]
@@ -323,6 +324,9 @@ impl IpcServer {
tiling_direction: direction_container.tiling_direction(),
})
}
+ QueryCommand::Paused => {
+ ClientResponseData::Paused(wm.state.paused)
+ }
},
AppCommand::Command {
subject_container_id,
@@ -463,6 +467,7 @@ impl IpcServer {
WmEvent::WorkspaceUpdated { .. } => {
SubscribableEvent::WorkspaceUpdated
}
+ WmEvent::PauseChanged { .. } => SubscribableEvent::PauseChanged,
};
self.event_tx.send((event_type, event))?;
diff --git a/packages/wm/src/main.rs b/packages/wm/src/main.rs
index 904bc7db..c01a4beb 100644
--- a/packages/wm/src/main.rs
+++ b/packages/wm/src/main.rs
@@ -165,10 +165,12 @@ async fn start_wm(
wm_event,
WmEvent::UserConfigChanged { .. }
| WmEvent::BindingModesChanged { .. }
+ | WmEvent::PauseChanged { .. }
) {
event_listener.update(
&config,
&wm.state.binding_modes,
+ wm.state.paused,
);
}
diff --git a/packages/wm/src/wm.rs b/packages/wm/src/wm.rs
index 33150d39..aab5c418 100644
--- a/packages/wm/src/wm.rs
+++ b/packages/wm/src/wm.rs
@@ -95,7 +95,10 @@ impl WindowManager {
}
}?;
- platform_sync(state, config)?;
+ // Skip platform sync when paused.
+ if !state.paused {
+ platform_sync(state, config)?;
+ }
Ok(())
}
diff --git a/packages/wm/src/wm_event.rs b/packages/wm/src/wm_event.rs
index 1ab34434..e9f2e3d7 100644
--- a/packages/wm/src/wm_event.rs
+++ b/packages/wm/src/wm_event.rs
@@ -60,4 +60,7 @@ pub enum WmEvent {
WorkspaceUpdated {
updated_workspace: ContainerDto,
},
+ PauseChanged {
+ paused: bool,
+ },
}
diff --git a/packages/wm/src/wm_state.rs b/packages/wm/src/wm_state.rs
index 424d28a7..98b33768 100644
--- a/packages/wm/src/wm_state.rs
+++ b/packages/wm/src/wm_state.rs
@@ -54,6 +54,9 @@ pub struct WmState {
/// `ignore` command.
pub ignored_windows: Vec<NativeWindow>,
+ /// Whether the WM is paused.
+ pub paused: bool,
+
/// Whether the initial state has been populated.
has_initialized: bool,
@@ -98,6 +101,7 @@ impl WmState {
unmanaged_or_minimized_timestamp: None,
binding_modes: Vec::new(),
ignored_windows: Vec::new(),
+ paused: false,
has_initialized: false,
event_tx,
exit_tx,
diff --git a/resources/assets/sample-config.yaml b/resources/assets/sample-config.yaml
index 7e9ca2a4..a3214197 100644
--- a/resources/assets/sample-config.yaml
+++ b/resources/assets/sample-config.yaml
@@ -149,13 +149,6 @@ binding_modes:
- commands: ['wm-disable-binding-mode --name resize']
bindings: ['escape', 'enter']
- # When enabled, all keybindings are disabled except for alt+shift+p which
- # returns to default keybindings.
- - name: 'pause'
- keybindings:
- - commands: ['wm-disable-binding-mode --name pause']
- bindings: ['alt+shift+p']
-
keybindings:
# Shift focus in a given direction.
- commands: ['focus --direction left']
@@ -193,8 +186,9 @@ keybindings:
- commands: ['wm-enable-binding-mode --name resize']
bindings: ['alt+r']
- # Disables all keybindings until alt+shift+p is pressed again.
- - commands: ['wm-enable-binding-mode --name pause']
+ # Disables window management and all other keybindings until alt+shift+p
+ # is pressed again.
+ - commands: ['wm-toggle-pause']
bindings: ['alt+shift+p']
# Change tiling direction. This determines where new tiling windows will