From 67a3c3546f5c192285e0dd3458ce818462ce53e5 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Mon, 10 Jun 2024 14:51:32 -0700 Subject: [PATCH] refactor(wm): use saturating_sub for idx-1 updates This commit ensures that we use the saturating_sub function uniformly when decrementing usize values by 1. --- komorebi/src/container.rs | 8 ++------ komorebi/src/monitor.rs | 2 +- komorebi/src/window_manager.rs | 15 +++++++-------- komorebi/src/workspace.rs | 16 +++++++--------- 4 files changed, 17 insertions(+), 24 deletions(-) diff --git a/komorebi/src/container.rs b/komorebi/src/container.rs index 6bae851e..f73f53a5 100644 --- a/komorebi/src/container.rs +++ b/komorebi/src/container.rs @@ -104,11 +104,7 @@ impl Container { pub fn remove_window_by_idx(&mut self, idx: usize) -> Option { let window = self.windows_mut().remove(idx); - - if idx != 0 { - self.focus_window(idx - 1); - }; - + self.focus_window(idx.saturating_sub(1)); window } @@ -119,7 +115,7 @@ impl Container { pub fn add_window(&mut self, window: Window) { self.windows_mut().push_back(window); - self.focus_window(self.windows().len() - 1); + self.focus_window(self.windows().len().saturating_sub(1)); } #[tracing::instrument(skip(self))] diff --git a/komorebi/src/monitor.rs b/komorebi/src/monitor.rs index ff708ede..7ea7c013 100644 --- a/komorebi/src/monitor.rs +++ b/komorebi/src/monitor.rs @@ -127,7 +127,7 @@ impl Monitor { if idx == 0 { self.workspaces_mut().push_back(Workspace::default()); } else { - self.focus_workspace(idx - 1).ok()?; + self.focus_workspace(idx.saturating_sub(1)).ok()?; }; None diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index 74eaf44e..2ce7a298 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -1146,7 +1146,7 @@ impl WindowManager { .ok_or_else(|| anyhow!("there is no monitor"))?; target_monitor.workspaces_mut().push_back(workspace); - target_monitor.focus_workspace(target_monitor.workspaces().len() - 1)?; + target_monitor.focus_workspace(target_monitor.workspaces().len().saturating_sub(1))?; target_monitor.load_focused_workspace(mouse_follows_focus)?; } @@ -1281,10 +1281,9 @@ impl WindowManager { let origin_workspace = self.focused_workspace_for_monitor_idx_mut(origin_monitor_idx)?; - if origin_workspace.focused_container_idx() != 0 { - origin_workspace - .focus_container(origin_workspace.focused_container_idx() - 1); - } + origin_workspace.focus_container( + origin_workspace.focused_container_idx().saturating_sub(1), + ); } } @@ -1449,10 +1448,10 @@ impl WindowManager { } } - workspace.focus_container(workspace.containers().len() - 1); + workspace.focus_container(workspace.containers().len().saturating_sub(1)); while workspace.focused_container_idx() > 0 { workspace.move_window_to_container(0)?; - workspace.focus_container(workspace.containers().len() - 1); + workspace.focus_container(workspace.containers().len().saturating_sub(1)); } if let Some(hwnd) = focused_hwnd { @@ -1527,7 +1526,7 @@ impl WindowManager { Layout::Default(DefaultLayout::Grid) | Layout::Default(DefaultLayout::UltrawideVerticalStack) ) { - new_idx - 1 + new_idx.saturating_sub(1) } else { new_idx }; diff --git a/komorebi/src/workspace.rs b/komorebi/src/workspace.rs index 5c8debb5..44982143 100644 --- a/komorebi/src/workspace.rs +++ b/komorebi/src/workspace.rs @@ -802,7 +802,7 @@ impl Workspace { self.resize_dimensions_mut().remove(focused_idx); if focused_idx < target_container_idx { - target_container_idx - 1 + target_container_idx.saturating_sub(1) } else { target_container_idx } @@ -924,8 +924,8 @@ impl Workspace { self.containers_mut().remove(focused_idx); self.resize_dimensions_mut().remove(focused_idx); - if focused_idx == self.containers().len() && focused_idx != 0 { - self.focus_container(focused_idx - 1); + if focused_idx == self.containers().len() { + self.focus_container(focused_idx.saturating_sub(1)); } } else { container.load_focused_window(); @@ -1333,7 +1333,8 @@ impl Workspace { .ok_or_else(|| anyhow!("there is no monocle container"))?; let window = *window; - if !self.containers().is_empty() && restore_idx > self.containers().len() - 1 { + if !self.containers().is_empty() && restore_idx > self.containers().len().saturating_sub(1) + { self.containers_mut() .resize(restore_idx, Container::default()); } @@ -1422,13 +1423,10 @@ impl Workspace { pub fn focus_previous_container(&mut self) { let focused_idx = self.focused_container_idx(); - - if focused_idx != 0 { - self.focus_container(focused_idx - 1); - } + self.focus_container(focused_idx.saturating_sub(1)); } fn focus_last_container(&mut self) { - self.focus_container(self.containers().len() - 1); + self.focus_container(self.containers().len().saturating_sub(1)); } }