GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat: sort monitors from left-to-right, top-to-bottom
Lars Bergerbf1210998885
diff --git a/packages/wm/src/common/events/handle_display_settings_changed.rs b/packages/wm/src/common/events/handle_display_settings_changed.rsindex 39568bad..a2670e4a 100644--- a/packages/wm/src/common/events/handle_display_settings_changed.rs+++ b/packages/wm/src/common/events/handle_display_settings_changed.rs@@ -20,8 +20,8 @@ pub fn handle_display_settings_changed() -> anyhow::Result<()> {info!("Display settings changed.");- // TODO: Sort `new_native_monitors` by position.- let native_monitors = Platform::monitors()?;+ let native_monitors = Platform::sorted_monitors()?;+let hardware_ids = native_monitors.iter().filter_map(|m| m.hardware_id().ok())@@ -33,11 +33,10 @@ pub fn handle_display_settings_changed(let mut new_native_monitors = Vec::new();for native_monitor in native_monitors {- // The `monitor_from_native` function uses the monitor's handle to- // find the corresponding monitor. Since the monitor handle can change- // after a display setting change, we also look for a matching device- // path or hardware ID. The hardware ID is not guaranteed to be unique,- // so we match against that last.+ // Get the corresponding `Monitor` instance by either its handle,+ // device path, or hardware ID. Monitor handles and device paths+ // *should* be unique, but can both change over time. The hardware ID+ // is not guaranteed to be unique, so we match against that last.let found_monitor = pending_monitors.iter().find_map(|monitor| {@@ -100,8 +99,15 @@ pub fn handle_display_settings_changed(// Remove any monitors that no longer exist and move their workspaces// to other monitors.+ //+ // Prevent removal of the last monitor (i.e. for when all monitors are+ // disconnected). This will cause the WM's monitors to mismatch the OS+ // monitor state, however, it'll be updated correctly when a new monitor+ // is connected again.for pending_monitor in pending_monitors {- remove_monitor(pending_monitor, state)?;+ if state.monitors().len() != 1 {+ remove_monitor(pending_monitor, state)?;+ }}for window in state.windows() {@@ -112,7 +118,7 @@ pub fn handle_display_settings_changed(// Need to update floating position of moved windows when a monitor is// disconnected or if the primary display is changed. The primary// display dictates the position of 0,0.- let workspace = window.workspace().context("No workspace")?;+ let workspace = window.workspace().context("No workspace.")?;window.set_floating_placement(window.floating_placement()@@ -133,8 +139,9 @@ fn update_monitor(native_monitor: NativeMonitor,state: &mut WmState,) -> anyhow::Result<()> {+ // TODO: Add monitor display trait.info!(- "Monitor: {:?} {} {:?} {:?}",+ "Updating monitor: {:?} {} {:?} {:?}",native_monitor.handle,native_monitor.device_name()?,native_monitor.device_path()?,@@ -155,6 +162,15 @@ fn remove_monitor(monitor: Monitor,state: &mut WmState,) -> anyhow::Result<()> {+ // TODO: Add monitor display trait.+ info!(+ "Removing monitor: {:?} {} {:?} {:?}",+ monitor.native().handle,+ monitor.native().device_name()?,+ monitor.native().device_path()?,+ monitor.native().hardware_id()?+ );+let target_monitor = state.monitors().into_iter()diff --git a/packages/wm/src/common/platform/platform.rs b/packages/wm/src/common/platform/platform.rsindex 7b60b8db..0f10b808 100644--- a/packages/wm/src/common/platform/platform.rs+++ b/packages/wm/src/common/platform/platform.rs@@ -17,6 +17,7 @@ use windows::Win32::UI::{WindowsAndMessaging::{GetDesktopWindow, GetForegroundWindow},};+use crate::common::Rect;use crate::user_config::UserConfig;use super::{@@ -39,8 +40,38 @@ impl Platform {NativeWindow::new(handle)}- pub fn monitors() -> anyhow::Result<Vec<NativeMonitor>> {- native_monitor::available_monitors()+ /// Monitors sorted from left-to-right, top-to-bottom.+ ///+ /// Note that this also ensures that the `NativeMonitor` instances have+ /// valid position values.+ pub fn sorted_monitors() -> anyhow::Result<Vec<NativeMonitor>> {+ let monitors = native_monitor::available_monitors()?;++ // Create a tuple of monitor and its rect.+ let mut monitors_with_rect = monitors+ .into_iter()+ .map(|monitor| {+ let rect = monitor.rect()?.clone();+ anyhow::Ok((monitor, rect))+ })+ .try_collect::<Vec<_>>()?;++ // Sort monitors from left-to-right, top-to-bottom.+ monitors_with_rect.sort_by(|(_, rect_a), (_, rect_b)| {+ if rect_a.x() == rect_b.x() {+ rect_a.y().cmp(&rect_b.y())+ } else {+ rect_a.x().cmp(&rect_b.x())+ }+ });++ // Convert back to a regular vector of monitors.+ Ok(+ monitors_with_rect+ .into_iter()+ .map(|(monitor, _)| monitor)+ .collect(),+ )}pub fn nearest_monitor(window: &NativeWindow) -> NativeMonitor {diff --git a/packages/wm/src/monitors/commands/add_monitor.rs b/packages/wm/src/monitors/commands/add_monitor.rsindex 43d385f5..00d39fe9 100644--- a/packages/wm/src/monitors/commands/add_monitor.rs+++ b/packages/wm/src/monitors/commands/add_monitor.rs@@ -1,4 +1,5 @@use anyhow::Context;+use tracing::info;use crate::{common::platform::NativeMonitor, containers::commands::attach_container,@@ -11,6 +12,15 @@ pub fn add_monitor(state: &mut WmState,config: &UserConfig,) -> anyhow::Result<()> {+ // TODO: Add monitor display trait.+ info!(+ "Adding monitor: {:?} {} {:?} {:?}",+ native_monitor.handle,+ native_monitor.device_name()?,+ native_monitor.device_path()?,+ native_monitor.hardware_id()?+ );+// Create `Monitor` instance. This uses the working area of the monitor// instead of the bounds of the display. The working area excludes// taskbars and other reserved display space.diff --git a/packages/wm/src/wm_state.rs b/packages/wm/src/wm_state.rsindex 7ef6a9b1..042385b4 100644--- a/packages/wm/src/wm_state.rs+++ b/packages/wm/src/wm_state.rs@@ -68,7 +68,7 @@ impl WmState {// Create a monitor, and consequently a workspace, for each detected// native monitor.- for native_monitor in Platform::monitors()? {+ for native_monitor in Platform::sorted_monitors()? {add_monitor(native_monitor, self, config)?;}