Skip to content

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

feat: add various targets for moving window to workspace

Lars Berger38aab38788c6
diff --git a/packages/wm/src/app_command.rs b/packages/wm/src/app_command.rs
index 3fc674a2..9a697737 100644
--- a/packages/wm/src/app_command.rs
+++ b/packages/wm/src/app_command.rs
@@ -29,7 +29,7 @@ use crate::{
WindowState,
},
wm_state::WmState,
- workspaces::commands::{focus_workspace, FocusWorkspaceTarget},
+ workspaces::{commands::focus_workspace, WorkspaceTarget},
};
#[derive(Clone, Debug, Parser)]
@@ -289,22 +289,22 @@ impl InvokeCommand {
if let Some(name) = &args.workspace {
focus_workspace(
- FocusWorkspaceTarget::Name(name.to_string()),
+ WorkspaceTarget::Name(name.to_string()),
state,
config,
)?;
}
if args.next_workspace {
- focus_workspace(FocusWorkspaceTarget::Next, state, config)?;
+ focus_workspace(WorkspaceTarget::Next, state, config)?;
}
if args.prev_workspace {
- focus_workspace(FocusWorkspaceTarget::Previous, state, config)?;
+ focus_workspace(WorkspaceTarget::Previous, state, config)?;
}
if args.recent_workspace {
- focus_workspace(FocusWorkspaceTarget::Recent, state, config)?;
+ focus_workspace(WorkspaceTarget::Recent, state, config)?;
}
Ok(())
@@ -327,8 +327,40 @@ impl InvokeCommand {
)?;
};
- if let Some(workspace) = &args.workspace {
- move_window_to_workspace(window, workspace, state, config)?;
+ if let Some(name) = &args.workspace {
+ move_window_to_workspace(
+ window.clone(),
+ WorkspaceTarget::Name(name.to_string()),
+ state,
+ config,
+ )?;
+ }
+
+ if args.next_workspace {
+ move_window_to_workspace(
+ window.clone(),
+ WorkspaceTarget::Next,
+ state,
+ config,
+ )?;
+ }
+
+ if args.prev_workspace {
+ move_window_to_workspace(
+ window.clone(),
+ WorkspaceTarget::Previous,
+ state,
+ config,
+ )?;
+ }
+
+ if args.recent_workspace {
+ move_window_to_workspace(
+ window,
+ WorkspaceTarget::Recent,
+ state,
+ config,
+ )?;
}
Ok(())
@@ -592,6 +624,15 @@ pub struct InvokeMoveCommand {
/// Name of workspace to move the window.
#[clap(long)]
workspace: Option<String>,
+
+ #[clap(long)]
+ next_workspace: bool,
+
+ #[clap(long)]
+ prev_workspace: bool,
+
+ #[clap(long)]
+ recent_workspace: bool,
}
#[derive(Args, Clone, Debug, Serialize)]
diff --git a/packages/wm/src/common/events/handle_window_focused.rs b/packages/wm/src/common/events/handle_window_focused.rs
index 8ab81e1f..acf6b0f5 100644
--- a/packages/wm/src/common/events/handle_window_focused.rs
+++ b/packages/wm/src/common/events/handle_window_focused.rs
@@ -8,7 +8,7 @@ use crate::{
windows::traits::WindowGetters,
wm_event::WmEvent,
wm_state::WmState,
- workspaces::commands::{focus_workspace, FocusWorkspaceTarget},
+ workspaces::{commands::focus_workspace, WorkspaceTarget},
};
pub fn handle_window_focused(
@@ -57,7 +57,7 @@ pub fn handle_window_focused(
let workspace = window.workspace().context("No workspace")?;
focus_workspace(
- FocusWorkspaceTarget::Name(workspace.config().name),
+ WorkspaceTarget::Name(workspace.config().name),
state,
config,
)?;
diff --git a/packages/wm/src/windows/commands/move_window_to_workspace.rs b/packages/wm/src/windows/commands/move_window_to_workspace.rs
index 62cff847..13f890a7 100644
--- a/packages/wm/src/windows/commands/move_window_to_workspace.rs
+++ b/packages/wm/src/windows/commands/move_window_to_workspace.rs
@@ -10,41 +10,45 @@ use crate::{
user_config::UserConfig,
windows::{traits::WindowGetters, WindowState},
wm_state::WmState,
- workspaces::commands::activate_workspace,
+ workspaces::{commands::activate_workspace, WorkspaceTarget},
};
pub fn move_window_to_workspace(
window: WindowContainer,
- workspace_name: &str,
+ target: WorkspaceTarget,
state: &mut WmState,
config: &UserConfig,
) -> anyhow::Result<()> {
- info!("Moving window to workspace: '{}'.", workspace_name);
-
let current_workspace = window.workspace().context("No workspace.")?;
let current_monitor =
current_workspace.monitor().context("No monitor.")?;
- // Retrieve or activate a workspace by its name.
- let target_workspace = match state.workspace_by_name(&workspace_name) {
- Some(workspace) => Some(workspace),
- None => {
- activate_workspace(
- Some(&workspace_name),
- &current_monitor,
- state,
- config,
- )?;
-
- state.workspace_by_name(&workspace_name)
- }
- };
+ let (target_workspace_name, target_workspace) =
+ state.workspace_by_target(&current_workspace, target, config)?;
+
+ // Retrieve or activate the target workspace by its name.
+ let target_workspace = match target_workspace {
+ Some(_) => anyhow::Ok(target_workspace),
+ _ => match target_workspace_name {
+ Some(name) => {
+ activate_workspace(Some(&name), &current_monitor, state, config)?;
+
+ Ok(state.workspace_by_name(&name))
+ }
+ _ => Ok(None),
+ },
+ }?;
if let Some(target_workspace) = target_workspace {
if target_workspace.id() == current_workspace.id() {
return Ok(());
}
+ info!(
+ "Moving window to workspace: '{}'.",
+ target_workspace.config().name
+ );
+
let target_monitor =
target_workspace.monitor().context("No monitor.")?;
diff --git a/packages/wm/src/wm_state.rs b/packages/wm/src/wm_state.rs
index b224dd00..41e07c6b 100644
--- a/packages/wm/src/wm_state.rs
+++ b/packages/wm/src/wm_state.rs
@@ -19,7 +19,7 @@ use crate::{
user_config::{BindingModeConfig, UserConfig},
windows::{commands::manage_window, traits::WindowGetters, WindowState},
wm_event::WmEvent,
- workspaces::Workspace,
+ workspaces::{Workspace, WorkspaceTarget},
};
pub struct WmState {
@@ -152,6 +152,20 @@ impl WmState {
.collect()
}
+ /// Gets workspaces sorted by their position in the user config.
+ pub fn sorted_workspaces(&self, config: &UserConfig) -> Vec<Workspace> {
+ let workspace_configs = &config.value.workspaces;
+ let mut workspaces = self.workspaces();
+
+ workspaces.sort_by_key(|workspace| {
+ workspace_configs
+ .iter()
+ .position(|config| config.name == workspace.config().name)
+ });
+
+ workspaces
+ }
+
pub fn windows(&self) -> Vec<WindowContainer> {
self
.root_container
@@ -252,6 +266,90 @@ impl WmState {
.find(|workspace| workspace.config().name == workspace_name)
}
+ /// Gets a workspace and its name by the given target.
+ ///
+ /// Returns a tuple of the workspace name and the `Workspace` instance
+ /// if active.
+ pub fn workspace_by_target(
+ &self,
+ origin_workspace: &Workspace,
+ target: WorkspaceTarget,
+ config: &UserConfig,
+ ) -> anyhow::Result<(Option<String>, Option<Workspace>)> {
+ let (name, workspace) = match target {
+ WorkspaceTarget::Name(name) => {
+ match origin_workspace.config().name == name {
+ false => (Some(name.clone()), self.workspace_by_name(&name)),
+ // Toggle the workspace if it's already focused.
+ true if config.value.general.toggle_workspace_on_refocus => (
+ self.recent_workspace_name.clone(),
+ self
+ .recent_workspace_name
+ .as_ref()
+ .and_then(|name| self.workspace_by_name(name)),
+ ),
+ true => (None, None),
+ }
+ }
+ WorkspaceTarget::Recent => (
+ self.recent_workspace_name.clone(),
+ self
+ .recent_workspace_name
+ .as_ref()
+ .and_then(|name| self.workspace_by_name(&name)),
+ ),
+ WorkspaceTarget::Next => {
+ let workspaces = self.sorted_workspaces(config);
+ let origin_index = workspaces
+ .iter()
+ .position(|workspace| workspace.id() == origin_workspace.id())
+ .context("Failed to get index of given workspace.")?;
+
+ let next_workspace = workspaces
+ .get(origin_index + 1)
+ .or_else(|| workspaces.first());
+
+ (
+ next_workspace.map(|workspace| workspace.config().name),
+ next_workspace.cloned(),
+ )
+ }
+ WorkspaceTarget::Previous => {
+ let workspaces = self.sorted_workspaces(config);
+ let origin_index = workspaces
+ .iter()
+ .position(|workspace| workspace.id() == origin_workspace.id())
+ .context("Failed to get index of given workspace.")?;
+
+ let prev_workspace = workspaces.get(
+ origin_index.checked_sub(1).unwrap_or(workspaces.len() - 1),
+ );
+
+ (
+ prev_workspace.map(|workspace| workspace.config().name),
+ prev_workspace.cloned(),
+ )
+ }
+ WorkspaceTarget::Direction(direction) => {
+ let origin_monitor =
+ origin_workspace.monitor().context("No focused monitor.")?;
+
+ let target_workspace = self
+ .monitor_in_direction(&origin_monitor, &direction)?
+ .and_then(|monitor| monitor.displayed_workspace());
+
+ (
+ target_workspace
+ .as_ref()
+ .map(|workspace| workspace.config().name),
+ target_workspace,
+ )
+ }
+ };
+
+ Ok((name, workspace))
+ }
+
/// Gets windows that should be redrawn.
///
/// When redrawing after a command that changes a window's type (e.g.
diff --git a/packages/wm/src/workspaces/commands/focus_workspace.rs b/packages/wm/src/workspaces/commands/focus_workspace.rs
index 7fcd79a1..bdd43bd6 100644
--- a/packages/wm/src/workspaces/commands/focus_workspace.rs
+++ b/packages/wm/src/workspaces/commands/focus_workspace.rs
@@ -2,25 +2,15 @@ use anyhow::Context;
use tracing::info;
use crate::{
- common::Direction,
containers::{commands::set_focused_descendant, traits::CommonGetters},
- monitors::Monitor,
user_config::UserConfig,
wm_state::WmState,
workspaces::{
commands::{activate_workspace, deactivate_workspace},
- Workspace,
+ WorkspaceTarget,
},
};
-pub enum FocusWorkspaceTarget {
- Name(String),
- Recent,
- Next,
- Previous,
- Direction(Direction),
-}
-
/// Focuses a workspace by a given target. This target can be a workspace
/// name, the most recently focused workspace, the next workspace, the
/// previous workspace, or the workspace in a given direction from the
@@ -28,7 +18,7 @@ pub enum FocusWorkspaceTarget {
///
/// The workspace will be activated if it isn't already active.
pub fn focus_workspace(
- target: FocusWorkspaceTarget,
+ target: WorkspaceTarget,
state: &mut WmState,
config: &UserConfig,
) -> anyhow::Result<()> {
@@ -37,8 +27,24 @@ pub fn focus_workspace(
.and_then(|focused| focused.workspace())
.context("No workspace is currently focused.")?;
- let target_workspace =
- target_workspace(target, &focused_workspace, state, config)?;
+ let (target_workspace_name, target_workspace) =
+ state.workspace_by_target(&focused_workspace, target, config)?;
+
+ // Retrieve or activate the target workspace by its name.
+ let target_workspace = match target_workspace {
+ Some(_) => anyhow::Ok(target_workspace),
+ _ => match target_workspace_name {
+ Some(name) => {
+ let focused_monitor =
+ focused_workspace.monitor().context("No focused monitor.")?;
+
+ activate_workspace(Some(&name), &focused_monitor, state, config)?;
+
+ Ok(state.workspace_by_name(&name))
+ }
+ _ => Ok(None),
+ },
+ }?;
if let Some(target_workspace) = target_workspace {
info!("Focusing workspace: '{}'.", target_workspace.config().name);
@@ -85,128 +91,3 @@ pub fn focus_workspace(
Ok(())
}
-
-/// Gets the workspace to focus based on the given target.
-///
-/// If the target workspace is currently inactive, it gets activated on the
-/// currently focused monitor.
-fn target_workspace(
- target: FocusWorkspaceTarget,
- focused_workspace: &Workspace,
- state: &mut WmState,
- config: &UserConfig,
-) -> anyhow::Result<Option<Workspace>> {
- let focused_monitor =
- focused_workspace.monitor().context("No focused monitor.")?;
-
- let target_workspace = match target {
- FocusWorkspaceTarget::Name(name) => {
- match focused_workspace.config().name == name {
- false => {
- name_to_workspace(&name, &focused_monitor, state, config)?
- }
- true if config.value.general.toggle_workspace_on_refocus => {
- recent_workspace(&focused_monitor, state, config)?
- }
- true => None,
- }
- }
- FocusWorkspaceTarget::Recent => {
- recent_workspace(&focused_monitor, state, config)?
- }
- FocusWorkspaceTarget::Next => {
- let workspaces = sorted_workspaces(state, config);
- let focused_index = workspace_index(&workspaces, focused_workspace)?;
-
- workspaces
- .get(focused_index + 1)
- .or_else(|| workspaces.first())
- .cloned()
- }
- FocusWorkspaceTarget::Previous => {
- let workspaces = sorted_workspaces(state, config);
- let focused_index = workspace_index(&workspaces, focused_workspace)?;
-
- workspaces
- .get(focused_index.checked_sub(1).unwrap_or(workspaces.len() - 1))
- .cloned()
- }
- FocusWorkspaceTarget::Direction(direction) => {
- let monitor =
- state.monitor_in_direction(&focused_monitor, &direction)?;
-
- monitor.and_then(|monitor| monitor.displayed_workspace())
- }
- };
-
- Ok(target_workspace)
-}
-
-/// Retrieves or activates a workspace by its name.
-fn name_to_workspace(
- workspace_name: &str,
- target_monitor: &Monitor,
- state: &mut WmState,
- config: &UserConfig,
-) -> anyhow::Result<Option<Workspace>> {
- match state.workspace_by_name(&workspace_name) {
- Some(workspace) => Ok(Some(workspace)),
- None => {
- activate_workspace(
- Some(&workspace_name),
- &target_monitor,
- state,
- config,
- )?;
-
- Ok(state.workspace_by_name(&workspace_name))
- }
- }
-}
-
-/// Gets the recent workspace based on `recent_workspace_name` in state.
-fn recent_workspace(
- target_monitor: &Monitor,
- state: &mut WmState,
- config: &UserConfig,
-) -> anyhow::Result<Option<Workspace>> {
- if let Some(recent_workspace_name) = state.recent_workspace_name.clone()
- {
- name_to_workspace(
- &recent_workspace_name,
- target_monitor,
- state,
- config,
- )
- } else {
- Ok(None)
- }
-}
-
-// Gets workspaces sorted by their position in the user config.
-fn sorted_workspaces(
- state: &WmState,
- config: &UserConfig,
-) -> Vec<Workspace> {
- let workspace_configs = &config.value.workspaces;
- let mut workspaces = state.workspaces();
-
- workspaces.sort_by_key(|workspace| {
- workspace_configs
- .iter()
- .position(|config| config.name == workspace.config().name)
- });
-
- workspaces
-}
-
-// Gets index of the given workspace within the vector of workspaces.
-fn workspace_index(
- workspaces: &[Workspace],
- workspace: &Workspace,
-) -> anyhow::Result<usize> {
- workspaces
- .iter()
- .position(|w| w.id() == workspace.id())
- .context("Failed to get index of given workspace.")
-}
diff --git a/packages/wm/src/workspaces/mod.rs b/packages/wm/src/workspaces/mod.rs
index 8a95f077..9ddbb32c 100644
--- a/packages/wm/src/workspaces/mod.rs
+++ b/packages/wm/src/workspaces/mod.rs
@@ -1,4 +1,6 @@
pub mod commands;
mod workspace;
+mod workspace_target;
pub use workspace::*;
+pub use workspace_target::*;
diff --git a/packages/wm/src/workspaces/workspace_target.rs b/packages/wm/src/workspaces/workspace_target.rs
new file mode 100644
index 00000000..abda82ca
--- /dev/null
+++ b/packages/wm/src/workspaces/workspace_target.rs
@@ -0,0 +1,9 @@
+use crate::common::Direction;
+
+pub enum WorkspaceTarget {
+ Name(String),
+ Recent,
+ Next,
+ Previous,
+ Direction(Direction),
+}