Skip to content

Commit 350dc8d

Browse files
committed
refactor(wm): simplify loops on monitor/workspaces
This commit uses the new `known_hwnds` hashmap to simplify looking for windows on all the monitors and all the workspaces. Since now the `known_hwnds` have the monitor/workspace index pair of the window we can simply get that info from the map and immediately access that monitor/workspace or use that index info. This commit already does this on some situations from the `process_event`.
1 parent f7f4104 commit 350dc8d

File tree

1 file changed

+30
-35
lines changed

1 file changed

+30
-35
lines changed

komorebi/src/process_event.rs

+30-35
Original file line numberDiff line numberDiff line change
@@ -305,30 +305,28 @@ impl WindowManager {
305305

306306
let mut needs_reconciliation = false;
307307

308-
for (i, monitors) in self.monitors().iter().enumerate() {
309-
for (j, workspace) in monitors.workspaces().iter().enumerate() {
310-
if workspace.contains_window(window.hwnd) && focused_pair != (i, j) {
311-
// At this point we know we are going to send a notification to the workspace reconciliator
312-
// So we get the topmost window returned by EnumWindows, which is almost always the window
313-
// that has been selected by alt-tab
314-
if let Ok(alt_tab_windows) = WindowsApi::alt_tab_windows() {
315-
if let Some(first) =
316-
alt_tab_windows.iter().find(|w| w.title().is_ok())
317-
{
318-
// If our record of this HWND hasn't been updated in over a minute
319-
let mut instant = ALT_TAB_HWND_INSTANT.lock();
320-
if instant.elapsed().gt(&Duration::from_secs(1)) {
321-
// Update our record with the HWND we just found
322-
ALT_TAB_HWND.store(Some(first.hwnd));
323-
// Update the timestamp of our record
324-
*instant = Instant::now();
325-
}
308+
if let Some((m_idx, w_idx)) = self.known_hwnds.get(&window.hwnd) {
309+
if focused_pair != (*m_idx, *w_idx) {
310+
// At this point we know we are going to send a notification to the workspace reconciliator
311+
// So we get the topmost window returned by EnumWindows, which is almost always the window
312+
// that has been selected by alt-tab
313+
if let Ok(alt_tab_windows) = WindowsApi::alt_tab_windows() {
314+
if let Some(first) =
315+
alt_tab_windows.iter().find(|w| w.title().is_ok())
316+
{
317+
// If our record of this HWND hasn't been updated in over a minute
318+
let mut instant = ALT_TAB_HWND_INSTANT.lock();
319+
if instant.elapsed().gt(&Duration::from_secs(1)) {
320+
// Update our record with the HWND we just found
321+
ALT_TAB_HWND.store(Some(first.hwnd));
322+
// Update the timestamp of our record
323+
*instant = Instant::now();
326324
}
327325
}
328-
329-
workspace_reconciliator::send_notification(i, j);
330-
needs_reconciliation = true;
331326
}
327+
328+
workspace_reconciliator::send_notification(*m_idx, *w_idx);
329+
needs_reconciliation = true;
332330
}
333331
}
334332

@@ -339,11 +337,14 @@ impl WindowManager {
339337
// duplicates across multiple workspaces, as it results in ghost layout tiles.
340338
let mut proceed = true;
341339

342-
for (i, monitor) in self.monitors().iter().enumerate() {
343-
for (j, workspace) in monitor.workspaces().iter().enumerate() {
344-
if workspace.contains_window(window.hwnd)
345-
&& i != self.focused_monitor_idx()
346-
&& j != monitor.focused_workspace_idx()
340+
if let Some((m_idx, w_idx)) = self.known_hwnds.get(&window.hwnd) {
341+
if let Some(focused_workspace_idx) = self
342+
.monitors()
343+
.get(*m_idx)
344+
.map(|m| m.focused_workspace_idx())
345+
{
346+
if *m_idx != self.focused_monitor_idx()
347+
&& *w_idx != focused_workspace_idx
347348
{
348349
tracing::debug!(
349350
"ignoring show event for window already associated with another workspace"
@@ -502,15 +503,9 @@ impl WindowManager {
502503
// This will be true if we have moved to another monitor
503504
let mut moved_across_monitors = false;
504505

505-
for (i, monitors) in self.monitors().iter().enumerate() {
506-
for workspace in monitors.workspaces() {
507-
if workspace.contains_window(window.hwnd) && i != target_monitor_idx {
508-
moved_across_monitors = true;
509-
break;
510-
}
511-
}
512-
if moved_across_monitors {
513-
break;
506+
if let Some((m_idx, _)) = self.known_hwnds.get(&window.hwnd) {
507+
if *m_idx != target_monitor_idx {
508+
moved_across_monitors = true;
514509
}
515510
}
516511

0 commit comments

Comments
 (0)