From 64d29d606ae511f68db9ec9a9dc6cfc1c6d99889 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Thu, 20 Mar 2025 20:07:45 -0700 Subject: [PATCH] refactor(wm): add dep injection to monitor reconiliator This commit adds some dependency injection to the monitor reconciliator module to make it easier to test the behaviour when different kinds of data are returned from win32_display_data. --- Cargo.lock | 2 +- Cargo.toml | 2 +- komorebi/src/border_manager/mod.rs | 8 ++++---- komorebi/src/monitor_reconciliator/mod.rs | 23 ++++++++++++++++------- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8711cb6b..0258de71 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6085,7 +6085,7 @@ dependencies = [ [[package]] name = "win32-display-data" version = "0.1.0" -source = "git+https://github.com/LGUG2Z/win32-display-data?rev=93949750b1f123fb79827ba4d66ffcab68055654#93949750b1f123fb79827ba4d66ffcab68055654" +source = "git+https://github.com/LGUG2Z/win32-display-data?rev=a28c6559a9de2f92c142a714947a9b081776caca#a28c6559a9de2f92c142a714947a9b081776caca" dependencies = [ "itertools 0.14.0", "serde", diff --git a/Cargo.toml b/Cargo.toml index 201bfde5..dd8c083f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,7 +35,7 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] } paste = "1" sysinfo = "0.33" uds_windows = "1" -win32-display-data = { git = "https://github.com/LGUG2Z/win32-display-data", rev = "93949750b1f123fb79827ba4d66ffcab68055654" } +win32-display-data = { git = "https://github.com/LGUG2Z/win32-display-data", rev = "a28c6559a9de2f92c142a714947a9b081776caca" } windows-numerics = { version = "0.2" } windows-implement = { version = "0.60" } windows-interface = { version = "0.59" } diff --git a/komorebi/src/border_manager/mod.rs b/komorebi/src/border_manager/mod.rs index 2270d026..326e3d99 100644 --- a/komorebi/src/border_manager/mod.rs +++ b/komorebi/src/border_manager/mod.rs @@ -501,10 +501,10 @@ pub fn handle_notifications(wm: Arc>) -> color_eyre::Result || focused_window_hwnd != foreground_window { if ws.locked_containers().contains(&idx) { - WindowKind::UnfocusedLocked - } else { - WindowKind::Unfocused - } + WindowKind::UnfocusedLocked + } else { + WindowKind::Unfocused + } } else if c.windows().len() > 1 { WindowKind::Stack } else { diff --git a/komorebi/src/monitor_reconciliator/mod.rs b/komorebi/src/monitor_reconciliator/mod.rs index 13c4e2c7..6294c77f 100644 --- a/komorebi/src/monitor_reconciliator/mod.rs +++ b/komorebi/src/monitor_reconciliator/mod.rs @@ -84,10 +84,12 @@ pub fn insert_in_monitor_cache(serial_or_device_id: &str, monitor: Monitor) { monitor_cache.insert(preferred_id, monitor); } -pub fn attached_display_devices() -> color_eyre::Result> { - let all_displays = win32_display_data::connected_displays_all() - .flatten() - .collect::>(); +pub fn attached_display_devices(display_provider: F) -> color_eyre::Result> +where + F: Fn() -> I + Copy, + I: Iterator>, +{ + let all_displays = display_provider().flatten().collect::>(); let mut serial_id_map = HashMap::new(); @@ -154,7 +156,7 @@ pub fn listen_for_notifications(wm: Arc>) -> color_eyre::Re tracing::info!("created hidden window to listen for monitor-related events"); std::thread::spawn(move || loop { - match handle_notifications(wm.clone()) { + match handle_notifications(wm.clone(), win32_display_data::connected_displays_all) { Ok(()) => { tracing::warn!("restarting finished thread"); } @@ -171,7 +173,14 @@ pub fn listen_for_notifications(wm: Arc>) -> color_eyre::Re Ok(()) } -pub fn handle_notifications(wm: Arc>) -> color_eyre::Result<()> { +pub fn handle_notifications( + wm: Arc>, + display_provider: F, +) -> color_eyre::Result<()> +where + F: Fn() -> I + Copy, + I: Iterator>, +{ tracing::info!("listening"); let receiver = event_rx(); @@ -296,7 +305,7 @@ pub fn handle_notifications(wm: Arc>) -> color_eyre::Result let initial_monitor_count = wm.monitors().len(); // Get the currently attached display devices - let attached_devices = attached_display_devices()?; + let attached_devices = attached_display_devices(display_provider)?; // Make sure that in our state any attached displays have the latest Win32 data for monitor in wm.monitors_mut() {