Skip to content

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

feat: implement `cycle_focus` command

Lars Berger14122163ac79
diff --git a/packages/wm/src/app_command.rs b/packages/wm/src/app_command.rs
index 364c518a..a7ae998f 100644
--- a/packages/wm/src/app_command.rs
+++ b/packages/wm/src/app_command.rs
@@ -9,7 +9,8 @@ use uuid::Uuid;
use crate::{
common::{
commands::{
- disable_binding_mode, enable_binding_mode, reload_config, shell_exec,
+ cycle_focus, disable_binding_mode, enable_binding_mode,
+ reload_config, shell_exec,
},
Direction, LengthValue, RectDelta, ResizeDimension,
},
@@ -222,6 +223,13 @@ pub enum InvokeCommand {
ToggleMinimized,
ToggleTiling,
ToggleTilingDirection,
+ WmCycleFocus {
+ #[clap(long, default_value_t = false)]
+ omit_fullscreen: bool,
+
+ #[clap(long, default_value_t = true)]
+ omit_minimized: bool,
+ },
WmDisableBindingMode {
#[clap(long)]
name: String,
@@ -233,7 +241,6 @@ pub enum InvokeCommand {
WmExit,
WmRedraw,
WmReloadConfig,
- WmToggleFocusState,
}
impl InvokeCommand {
@@ -493,6 +500,10 @@ impl InvokeCommand {
InvokeCommand::ToggleTilingDirection => {
toggle_tiling_direction(subject_container, state, config)
}
+ InvokeCommand::WmCycleFocus {
+ omit_fullscreen,
+ omit_minimized,
+ } => cycle_focus(*omit_fullscreen, *omit_minimized, state, config),
InvokeCommand::WmDisableBindingMode { name } => {
disable_binding_mode(name, state);
Ok(())
@@ -510,7 +521,6 @@ impl InvokeCommand {
Ok(())
}
InvokeCommand::WmReloadConfig => reload_config(state, config),
- InvokeCommand::WmToggleFocusState => todo!(),
}
}
}
diff --git a/packages/wm/src/common/commands/cycle_focus.rs b/packages/wm/src/common/commands/cycle_focus.rs
new file mode 100644
index 00000000..c5994120
--- /dev/null
+++ b/packages/wm/src/common/commands/cycle_focus.rs
@@ -0,0 +1,89 @@
+use anyhow::Context;
+
+use crate::{
+ containers::{commands::set_focused_descendant, traits::CommonGetters},
+ user_config::UserConfig,
+ windows::{traits::WindowGetters, WindowState},
+ wm_state::WmState,
+};
+
+/// Cycles focus through windows of different states. In order, this will
+/// change from tiling -> floating -> fullscreen -> minimized, then back to
+/// tiling.
+///
+/// Does nothing if a workspace is focused.
+pub fn cycle_focus(
+ omit_fullscreen: bool,
+ omit_minimized: bool,
+ state: &mut WmState,
+ config: &UserConfig,
+) -> anyhow::Result<()> {
+ let focused_container =
+ state.focused_container().context("No focused container.")?;
+
+ if let Ok(window) = focused_container.as_window_container() {
+ let workspace = window.workspace().context("No workspace.")?;
+
+ let current = window.state();
+ let mut next = next_state(&current, config);
+
+ loop {
+ // Break if we have cycled back to the current state.
+ if std::mem::discriminant(&current) == std::mem::discriminant(&next)
+ {
+ break;
+ }
+
+ // Skip the next state if it is to be omitted.
+ if (omit_fullscreen && matches!(next, WindowState::Fullscreen(_)))
+ || omit_minimized && matches!(next, WindowState::Minimized)
+ {
+ next = next_state(&next, config);
+ continue;
+ }
+
+ // Get window that matches the next state.
+ let window_of_type = workspace
+ .descendant_focus_order()
+ .filter_map(|descendant| descendant.as_window_container().ok())
+ .find(|descendant| match (descendant.state(), &next) {
+ (WindowState::Floating(_), WindowState::Floating(_)) => true,
+ (WindowState::Fullscreen(_), WindowState::Fullscreen(_)) => true,
+ (WindowState::Minimized, WindowState::Minimized) => true,
+ (WindowState::Tiling, WindowState::Tiling) => true,
+ _ => false,
+ });
+
+ if let Some(window) = window_of_type {
+ set_focused_descendant(window.into(), None);
+ state.pending_sync.focus_change = true;
+ break;
+ }
+
+ next = next_state(&next, config);
+ }
+ }
+
+ Ok(())
+}
+
+fn next_state(
+ current_state: &WindowState,
+ config: &UserConfig,
+) -> WindowState {
+ match current_state {
+ WindowState::Floating(_) => WindowState::Fullscreen(
+ config
+ .value
+ .window_behavior
+ .state_defaults
+ .fullscreen
+ .clone(),
+ ),
+ WindowState::Fullscreen(_) => WindowState::Minimized,
+ WindowState::Minimized => WindowState::Tiling,
+ WindowState::Tiling => WindowState::Floating(
+ config.value.window_behavior.state_defaults.floating.clone(),
+ ),
+ }
+}
diff --git a/packages/wm/src/common/commands/mod.rs b/packages/wm/src/common/commands/mod.rs
index ab26c79c..ccfe9e9f 100644
--- a/packages/wm/src/common/commands/mod.rs
+++ b/packages/wm/src/common/commands/mod.rs
@@ -1,9 +1,11 @@
+mod cycle_focus;
mod disable_binding_mode;
mod enable_binding_mode;
mod platform_sync;
mod reload_config;
mod shell_exec;
+pub use cycle_focus::*;
pub use disable_binding_mode::*;
pub use enable_binding_mode::*;
pub use platform_sync::*;
diff --git a/resources/sample-config.yaml b/resources/sample-config.yaml
index 4e30c7b8..32374194 100644
--- a/resources/sample-config.yaml
+++ b/resources/sample-config.yaml
@@ -168,8 +168,8 @@ keybindings:
- commands: ['toggle-tiling-direction']
bindings: ['alt+v']
- # Change focus between tiling windows and floating/fullscreen windows.
- - commands: ['wm-toggle-focus-state']
+ # Change focus from tiling windows -> floating -> fullscreen.
+ - commands: ['wm-cycle-focus']
bindings: ['alt+space']
# Change the focused window to be floating.