Skip to content

Commit 64d29d6

Browse files
committed
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.
1 parent 072a62c commit 64d29d6

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] }
3535
paste = "1"
3636
sysinfo = "0.33"
3737
uds_windows = "1"
38-
win32-display-data = { git = "https://github.com/LGUG2Z/win32-display-data", rev = "93949750b1f123fb79827ba4d66ffcab68055654" }
38+
win32-display-data = { git = "https://github.com/LGUG2Z/win32-display-data", rev = "a28c6559a9de2f92c142a714947a9b081776caca" }
3939
windows-numerics = { version = "0.2" }
4040
windows-implement = { version = "0.60" }
4141
windows-interface = { version = "0.59" }

komorebi/src/border_manager/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -501,10 +501,10 @@ pub fn handle_notifications(wm: Arc<Mutex<WindowManager>>) -> color_eyre::Result
501501
|| focused_window_hwnd != foreground_window
502502
{
503503
if ws.locked_containers().contains(&idx) {
504-
WindowKind::UnfocusedLocked
505-
} else {
506-
WindowKind::Unfocused
507-
}
504+
WindowKind::UnfocusedLocked
505+
} else {
506+
WindowKind::Unfocused
507+
}
508508
} else if c.windows().len() > 1 {
509509
WindowKind::Stack
510510
} else {

komorebi/src/monitor_reconciliator/mod.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,12 @@ pub fn insert_in_monitor_cache(serial_or_device_id: &str, monitor: Monitor) {
8484
monitor_cache.insert(preferred_id, monitor);
8585
}
8686

87-
pub fn attached_display_devices() -> color_eyre::Result<Vec<Monitor>> {
88-
let all_displays = win32_display_data::connected_displays_all()
89-
.flatten()
90-
.collect::<Vec<_>>();
87+
pub fn attached_display_devices<F, I>(display_provider: F) -> color_eyre::Result<Vec<Monitor>>
88+
where
89+
F: Fn() -> I + Copy,
90+
I: Iterator<Item = Result<win32_display_data::Device, win32_display_data::Error>>,
91+
{
92+
let all_displays = display_provider().flatten().collect::<Vec<_>>();
9193

9294
let mut serial_id_map = HashMap::new();
9395

@@ -154,7 +156,7 @@ pub fn listen_for_notifications(wm: Arc<Mutex<WindowManager>>) -> color_eyre::Re
154156
tracing::info!("created hidden window to listen for monitor-related events");
155157

156158
std::thread::spawn(move || loop {
157-
match handle_notifications(wm.clone()) {
159+
match handle_notifications(wm.clone(), win32_display_data::connected_displays_all) {
158160
Ok(()) => {
159161
tracing::warn!("restarting finished thread");
160162
}
@@ -171,7 +173,14 @@ pub fn listen_for_notifications(wm: Arc<Mutex<WindowManager>>) -> color_eyre::Re
171173
Ok(())
172174
}
173175

174-
pub fn handle_notifications(wm: Arc<Mutex<WindowManager>>) -> color_eyre::Result<()> {
176+
pub fn handle_notifications<F, I>(
177+
wm: Arc<Mutex<WindowManager>>,
178+
display_provider: F,
179+
) -> color_eyre::Result<()>
180+
where
181+
F: Fn() -> I + Copy,
182+
I: Iterator<Item = Result<win32_display_data::Device, win32_display_data::Error>>,
183+
{
175184
tracing::info!("listening");
176185

177186
let receiver = event_rx();
@@ -296,7 +305,7 @@ pub fn handle_notifications(wm: Arc<Mutex<WindowManager>>) -> color_eyre::Result
296305
let initial_monitor_count = wm.monitors().len();
297306

298307
// Get the currently attached display devices
299-
let attached_devices = attached_display_devices()?;
308+
let attached_devices = attached_display_devices(display_provider)?;
300309

301310
// Make sure that in our state any attached displays have the latest Win32 data
302311
for monitor in wm.monitors_mut() {

0 commit comments

Comments
 (0)