Skip to content

Commit

Permalink
refactor(wm): use saturating_sub for idx-1 updates
Browse files Browse the repository at this point in the history
This commit ensures that we use the saturating_sub function uniformly
when decrementing usize values by 1.
  • Loading branch information
LGUG2Z committed Jun 10, 2024
1 parent 888b674 commit 67a3c35
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 24 deletions.
8 changes: 2 additions & 6 deletions komorebi/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,7 @@ impl Container {

pub fn remove_window_by_idx(&mut self, idx: usize) -> Option<Window> {
let window = self.windows_mut().remove(idx);

if idx != 0 {
self.focus_window(idx - 1);
};

self.focus_window(idx.saturating_sub(1));
window
}

Expand All @@ -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))]
Expand Down
2 changes: 1 addition & 1 deletion komorebi/src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 7 additions & 8 deletions komorebi/src/window_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
}

Expand Down Expand Up @@ -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),
);
}
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
};
Expand Down
16 changes: 7 additions & 9 deletions komorebi/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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));
}
}

0 comments on commit 67a3c35

Please sign in to comment.