mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-05-07 11:23:33 +02:00
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.
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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))]
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
};
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user