GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
fix: error with bound `keep_alive` workspaces; move existing workspaces to bound monitor (#1102)
Lars Berger6242f8a0c575
diff --git a/packages/wm/src/commands/monitor/add_monitor.rs b/packages/wm/src/commands/monitor/add_monitor.rsindex 11cdbe95..f1a24bad 100644--- a/packages/wm/src/commands/monitor/add_monitor.rs+++ b/packages/wm/src/commands/monitor/add_monitor.rs@@ -1,12 +1,15 @@use anyhow::Context;-use tracing::{info, warn};+use tracing::info;use wm_common::WmEvent;use wm_platform::NativeMonitor;use crate::{- commands::{container::attach_container, workspace::activate_workspace},- models::Monitor,- traits::CommonGetters,+ commands::{+ container::{attach_container, move_container_within_tree},+ workspace::{activate_workspace, sort_workspaces},+ },+ models::{Monitor, Workspace},+ traits::{CommonGetters, PositionGetters, WindowGetters},user_config::UserConfig,wm_state::WmState,};@@ -33,25 +36,111 @@ pub fn add_monitor(added_monitor: monitor.to_dto()?,});- // Active all keep_alive workspaces for this monitor- config.keep_alive_workspace_configs().iter().for_each(- |workspace_config| {- #[allow(clippy::cast_possible_truncation)]- if workspace_config.bind_to_monitor == Some(monitor.index() as u32) {- activate_workspace(- Some(&workspace_config.name),- Some(monitor.clone()),- state,- config,- )- .unwrap_or_else(|err| warn!("{err}"));- }- },- );-- // Activate a workspace on the newly added monitor.- activate_workspace(None, Some(monitor), state, config)- .context("At least 1 workspace is required per monitor.")?;+ let bound_workspace_configs = config+ .value+ .workspaces+ .iter()+ .filter(|config| {+ config.bind_to_monitor.is_some_and(|monitor_index| {+ monitor.index() == monitor_index as usize+ })+ })+ .collect::<Vec<_>>();++ for workspace_config in bound_workspace_configs {+ let existing_workspace =+ state.workspace_by_name(&workspace_config.name);++ if let Some(existing_workspace) = existing_workspace {+ // Move workspaces that should be bound to the newly added monitor.+ move_workspace_to_monitor(+ &existing_workspace,+ &monitor,+ state,+ config,+ )?;+ } else if workspace_config.keep_alive {+ // Activate all `keep_alive` workspaces for this monitor.+ activate_workspace(+ Some(&workspace_config.name),+ Some(monitor.clone()),+ state,+ config,+ )?;+ }+ }++ // Make sure the monitor has at least one workspace. This will+ // automatically prioritize bound workspace configs and fall back to the+ // first available one if needed.+ if monitor.child_count() == 0 {+ activate_workspace(None, Some(monitor), state, config)?;+ }++ Ok(())+}++// TODO: Move to its own file once `swap-workspace` PR is merged.+// Ref: https://github.com/glzr-io/glazewm/pull/980.+pub fn move_workspace_to_monitor(+ workspace: &Workspace,+ target_monitor: &Monitor,+ state: &mut WmState,+ config: &UserConfig,+) -> anyhow::Result<()> {+ let origin_monitor = workspace.monitor().context("No monitor.")?;++ // Get currently displayed workspace on the target monitor.+ let displayed_workspace = target_monitor+ .displayed_workspace()+ .context("No displayed workspace.")?;++ move_container_within_tree(+ &workspace.clone().into(),+ &target_monitor.clone().into(),+ target_monitor.child_count(),+ state,+ )?;++ let windows = workspace+ .descendants()+ .filter_map(|descendant| descendant.as_window_container().ok());++ for window in windows {+ window.set_has_pending_dpi_adjustment(true);++ window.set_floating_placement(+ window+ .floating_placement()+ .translate_to_center(&workspace.to_rect()?),+ );+ }++ state+ .pending_sync+ .queue_container_to_redraw(workspace.clone())+ .queue_container_to_redraw(displayed_workspace);++ match origin_monitor.child_count() {+ 0 => {+ // Prevent origin monitor from having no workspaces.+ activate_workspace(None, Some(origin_monitor), state, config)?;+ }+ _ => {+ // Redraw the workspace on the origin monitor.+ state.pending_sync.queue_container_to_redraw(+ origin_monitor+ .displayed_workspace()+ .context("No displayed workspace.")?,+ );+ }+ }++ sort_workspaces(target_monitor, config)?;++ state.emit_event(WmEvent::WorkspaceUpdated {+ updated_workspace: workspace.to_dto()?,+ });Ok(())}diff --git a/packages/wm/src/user_config.rs b/packages/wm/src/user_config.rsindex b5c70c68..50ea81c1 100644--- a/packages/wm/src/user_config.rs+++ b/packages/wm/src/user_config.rs@@ -281,15 +281,6 @@ impl UserConfig {.collect()}- pub fn keep_alive_workspace_configs(&self) -> Vec<&WorkspaceConfig> {- self- .value- .workspaces- .iter()- .filter(|config| config.keep_alive)- .collect()- }-pub fn workspace_config_for_monitor(&self,monitor: &Monitor,