Skip to content

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

feat: support moving non-tiling windows to workspace

Lars Bergerb23fcf8e11fc
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 5d28d7c0..db006e6a 100644
--- a/packages/wm/src/windows/commands/move_window_to_workspace.rs
+++ b/packages/wm/src/windows/commands/move_window_to_workspace.rs
@@ -1,13 +1,16 @@
use anyhow::Context;
+use tracing::info;
use crate::{
containers::{
- commands::move_container_within_tree, traits::CommonGetters,
+ commands::{move_container_within_tree, set_focused_descendant},
+ traits::{CommonGetters, PositionGetters},
WindowContainer,
},
user_config::UserConfig,
- windows::TilingWindow,
+ windows::{traits::WindowGetters, WindowState},
wm_state::WmState,
+ workspaces::commands::activate_workspace,
};
pub fn move_window_to_workspace(
@@ -16,42 +19,110 @@ pub fn move_window_to_workspace(
state: &mut WmState,
config: &UserConfig,
) -> anyhow::Result<()> {
- match window {
- WindowContainer::TilingWindow(window) => {
- move_tiling_window_to_workspace(
- window,
- workspace_name,
+ 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)
}
- WindowContainer::NonTilingWindow(non_tiling_window) => {
- todo!()
+ };
+
+ if let Some(target_workspace) = target_workspace {
+ if target_workspace.id() == current_workspace.id() {
+ return Ok(());
}
- }
-}
-fn move_tiling_window_to_workspace(
- window_container_to_move: TilingWindow,
- workspace_name: &str,
- state: &mut WmState,
- config: &UserConfig,
-) -> anyhow::Result<()> {
- let workspaces = state.workspaces();
- let workspace = workspaces
- .iter()
- .find(|workspace| workspace.config().name == workspace_name)
- .unwrap();
-
- let target_container =
- workspace.descendant_focus_order().last().unwrap();
-
- move_container_within_tree(
- window_container_to_move.clone().into(),
- target_container,
- workspace.index(),
- state,
- )?;
+ let target_monitor =
+ target_workspace.monitor().context("No monitor.")?;
+
+ // Since target workspace could be on a different monitor, adjustments
+ // might need to be made because of DPI.
+ if current_monitor
+ .has_dpi_difference(&target_monitor.clone().into())?
+ {
+ window.set_has_pending_dpi_adjustment(true);
+ }
+
+ // Update floating placement if the window has to cross monitors.
+ if target_monitor.id() != current_monitor.id() {
+ window.set_floating_placement(
+ window
+ .floating_placement()
+ .translate_to_center(&target_workspace.to_rect()?),
+ );
+ }
+
+ let focus_target = state.focus_target_after_removal(&window);
+
+ // Since the workspace that gets displayed is the last focused child,
+ // focus needs to be reassigned to the displayed workspace.
+ let focus_reset_target = match target_workspace.is_displayed() {
+ true => None,
+ false => target_monitor.descendant_focus_order().next(),
+ };
+
+ let insertion_sibling = target_workspace
+ .descendant_focus_order()
+ .filter_map(|descendant| descendant.as_window_container().ok())
+ .find(|descendant| descendant.state() == WindowState::Tiling);
+
+ // Insert the window into the target workspace.
+ match (window.is_tiling_window(), insertion_sibling.is_some()) {
+ (true, true) => {
+ if let Some(insertion_sibling) = insertion_sibling {
+ move_container_within_tree(
+ window.clone().into(),
+ insertion_sibling.clone().parent().context("No parent.")?,
+ insertion_sibling.index() + 1,
+ state,
+ )?;
+ }
+ }
+ _ => {
+ move_container_within_tree(
+ window.clone().into(),
+ target_workspace.clone().into(),
+ target_workspace.child_count(),
+ state,
+ )?;
+ }
+ }
+
+ if let Some(focus_reset_target) = focus_reset_target {
+ set_focused_descendant(focus_reset_target, None);
+ state.pending_sync.focus_change = true;
+ }
+
+ if let Some(focus_target) = focus_target {
+ set_focused_descendant(focus_target, None);
+ state.pending_sync.focus_change = true;
+ }
+
+ match window {
+ WindowContainer::NonTilingWindow(_) => {
+ state.pending_sync.containers_to_redraw.push(window.into());
+ }
+ WindowContainer::TilingWindow(_) => {
+ state
+ .pending_sync
+ .containers_to_redraw
+ .extend(current_workspace.tiling_children().map(Into::into));
+ }
+ }
+ }
Ok(())
}
diff --git a/packages/wm/src/workspaces/commands/focus_workspace.rs b/packages/wm/src/workspaces/commands/focus_workspace.rs
index 9450647c..7fcd79a1 100644
--- a/packages/wm/src/workspaces/commands/focus_workspace.rs
+++ b/packages/wm/src/workspaces/commands/focus_workspace.rs
@@ -4,6 +4,7 @@ use tracing::info;
use crate::{
common::Direction,
containers::{commands::set_focused_descendant, traits::CommonGetters},
+ monitors::Monitor,
user_config::UserConfig,
wm_state::WmState,
workspaces::{
@@ -33,30 +34,28 @@ pub fn focus_workspace(
) -> anyhow::Result<()> {
let focused_workspace = state
.focused_container()
- .and_then(|c| c.workspace())
+ .and_then(|focused| focused.workspace())
.context("No workspace is currently focused.")?;
- if let Some(workspace_to_focus) =
- target_workspace(target, &focused_workspace, state, config)?
- {
- info!(
- "Focusing workspace: '{}'.",
- workspace_to_focus.config().name
- );
+ let target_workspace =
+ target_workspace(target, &focused_workspace, state, config)?;
+
+ if let Some(target_workspace) = target_workspace {
+ info!("Focusing workspace: '{}'.", target_workspace.config().name);
// Get the currently displayed workspace on the same monitor that the
// workspace to focus is on.
- let displayed_workspace = workspace_to_focus
+ let displayed_workspace = target_workspace
.monitor()
.and_then(|monitor| monitor.displayed_workspace())
.context("No workspace is currently displayed.")?;
// Set focus to whichever window last had focus in workspace. If the
// workspace has no windows, then set focus to the workspace itself.
- let container_to_focus = workspace_to_focus
+ let container_to_focus = target_workspace
.descendant_focus_order()
.next()
- .unwrap_or_else(|| workspace_to_focus.clone().into());
+ .unwrap_or_else(|| target_workspace.clone().into());
set_focused_descendant(container_to_focus, None);
state.pending_sync.focus_change = true;
@@ -65,7 +64,7 @@ pub fn focus_workspace(
state
.pending_sync
.containers_to_redraw
- .extend([displayed_workspace.into(), workspace_to_focus.into()]);
+ .extend([displayed_workspace.into(), target_workspace.into()]);
// Get empty workspace to destroy (if one is found). Cannot destroy
// empty workspaces if they're the only workspace on the monitor.
@@ -97,23 +96,23 @@ fn target_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 => {
- let workspace =
- name_to_workspace(&name, focused_workspace, state, config)?;
-
- Some(workspace)
+ name_to_workspace(&name, &focused_monitor, state, config)?
}
true if config.value.general.toggle_workspace_on_refocus => {
- recent_workspace(focused_workspace, state, config)?
+ recent_workspace(&focused_monitor, state, config)?
}
true => None,
}
}
FocusWorkspaceTarget::Recent => {
- recent_workspace(focused_workspace, state, config)?
+ recent_workspace(&focused_monitor, state, config)?
}
FocusWorkspaceTarget::Next => {
let workspaces = sorted_workspaces(state, config);
@@ -133,9 +132,6 @@ fn target_workspace(
.cloned()
}
FocusWorkspaceTarget::Direction(direction) => {
- let focused_monitor =
- focused_workspace.monitor().context("No focused monitor.")?;
-
let monitor =
state.monitor_in_direction(&focused_monitor, &direction)?;
@@ -149,41 +145,42 @@ fn target_workspace(
/// Retrieves or activates a workspace by its name.
fn name_to_workspace(
workspace_name: &str,
- focused_workspace: &Workspace,
+ target_monitor: &Monitor,
state: &mut WmState,
config: &UserConfig,
-) -> anyhow::Result<Workspace> {
- state
- .workspace_by_name(&workspace_name)
- .map(Ok)
- .unwrap_or_else(|| {
- let focused_monitor =
- focused_workspace.monitor().context("No focused monitor.")?;
-
+) -> anyhow::Result<Option<Workspace>> {
+ match state.workspace_by_name(&workspace_name) {
+ Some(workspace) => Ok(Some(workspace)),
+ None => {
activate_workspace(
Some(&workspace_name),
- &focused_monitor,
+ &target_monitor,
state,
config,
)?;
- state
- .workspace_by_name(&workspace_name)
- .context("Failed to get workspace from name.")
- })
+ Ok(state.workspace_by_name(&workspace_name))
+ }
+ }
}
/// Gets the recent workspace based on `recent_workspace_name` in state.
fn recent_workspace(
- focused_workspace: &Workspace,
+ target_monitor: &Monitor,
state: &mut WmState,
config: &UserConfig,
) -> anyhow::Result<Option<Workspace>> {
- state
- .recent_workspace_name
- .clone()
- .map(|name| name_to_workspace(&name, focused_workspace, state, config))
- .transpose()
+ 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.
diff --git a/resources/sample-config.yaml b/resources/sample-config.yaml
index 32374194..256a09d1 100644
--- a/resources/sample-config.yaml
+++ b/resources/sample-config.yaml
@@ -63,7 +63,7 @@ window_behavior:
fullscreen:
# Maximize the window if possible. If the window doesn't have a
# maximize button, then it'll be fullscreen'ed normally instead.
- maximized: true
+ maximized: false
workspaces:
- name: '1'