From 96332b2578f77916496e809858014aab61444ba2 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Sat, 31 Oct 2026 19:13:35 -0700 Subject: [PATCH] refactor(windows): PlatformMonitorId next to PlatformWindowId; monitor_idx_from_current_pos has KDE's text komorebi-core gets pub type PlatformMonitorId

=

::Id, the monitor counterpart of PlatformWindowId (blocker 5 of core-model.md section 18). The Windows daemon aliases it (window::PlatformMonitorId, the shared MonitorId) and gets window::monitor_id_at_cursor() like KDE's, so monitor_idx_from_current_pos reads: ask the monitor id at the cursor, compare monitor.id == monitor_id, and only then the Windows-only stale-handle fallback (WindowsApi::monitor(monitor_id.to_hmonitor())). Same calls as before (cursor_pos, monitor_from_point), in the same order. Not a behaviour change. kfde: the alias is generic over MonitorPlatform; KDE and macOS can alias it the same way. Verified: tools/fast-check native (core tests), bare Windows clippy -D warnings, KDE check + lib tests, macOS fake-CC type-check PASS; e2e: commands (monitor-from-cursor commands), hotplug scenarios if any. --- crates/komorebi-core/src/monitor.rs | 3 +++ komorebi/src/window.rs | 11 +++++++++++ komorebi/src/window_manager.rs | 7 ++++--- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/crates/komorebi-core/src/monitor.rs b/crates/komorebi-core/src/monitor.rs index 27644982..9e279de7 100644 --- a/crates/komorebi-core/src/monitor.rs +++ b/crates/komorebi-core/src/monitor.rs @@ -23,6 +23,9 @@ use serde::de::DeserializeOwned; use std::collections::VecDeque; use std::fmt::Debug; +/// What names a monitor of platform `P`, as [`PlatformWindowId`] does for a window +pub type PlatformMonitorId

=

::Id; + /// What the shared [`Monitor`] needs of a platform, besides what its workspaces need. pub trait MonitorPlatform: WorkspacePlatform { /// What names a monitor: `isize` on Windows (a monitor handle), `u32` on KDE. diff --git a/komorebi/src/window.rs b/komorebi/src/window.rs index 1d825d0b..278f908f 100644 --- a/komorebi/src/window.rs +++ b/komorebi/src/window.rs @@ -74,6 +74,17 @@ pub fn foreground_window_id() -> Option { WindowsApi::foreground_window().ok() } +/// What names a monitor: the type the shared monitor and window manager code compares ids as +pub type PlatformMonitorId = komorebi_core::PlatformMonitorId; + +/// The monitor the pointer is on, if the system says (the nearest one when it is on none) +pub fn monitor_id_at_cursor() -> Option { + let point = WindowsApi::cursor_pos().ok()?; + Some(PlatformMonitorId::from_hmonitor( + WindowsApi::monitor_from_point(point), + )) +} + /// Where the pointer is, if the system says pub fn cursor_position() -> Option<(i32, i32)> { WindowsApi::cursor_pos() diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index 2abcb4f4..e3d66a57 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -74,6 +74,7 @@ use crate::static_config::StaticConfig; use crate::static_config::StaticConfigExt; use crate::transparency_manager; use crate::window::Window; +use crate::window::monitor_id_at_cursor; use crate::window_manager_event::WindowManagerEvent; use crate::windows_api::WindowsApi; use crate::winevent_listener; @@ -3786,17 +3787,17 @@ impl WindowManager { } pub fn monitor_idx_from_current_pos(&mut self) -> Option { - let hmonitor = WindowsApi::monitor_from_point(WindowsApi::cursor_pos().ok()?); + let monitor_id = monitor_id_at_cursor()?; for (i, monitor) in self.monitors().iter().enumerate() { - if monitor.id.to_hmonitor() == hmonitor { + if monitor.id == monitor_id { return Option::from(i); } } // our hmonitor might be stale, so if we didn't return above, try querying via the latest // info taken from win32_display_data and update our hmonitor while we're at it - if let Ok(latest) = WindowsApi::monitor(hmonitor) { + if let Ok(latest) = WindowsApi::monitor(monitor_id.to_hmonitor()) { for (i, monitor) in self.monitors_mut().iter_mut().enumerate() { if monitor.extra.device_id == latest.extra.device_id { monitor.id = latest.id;