mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-07-06 13:05:11 +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> {
|
pub fn remove_window_by_idx(&mut self, idx: usize) -> Option<Window> {
|
||||||
let window = self.windows_mut().remove(idx);
|
let window = self.windows_mut().remove(idx);
|
||||||
|
self.focus_window(idx.saturating_sub(1));
|
||||||
if idx != 0 {
|
|
||||||
self.focus_window(idx - 1);
|
|
||||||
};
|
|
||||||
|
|
||||||
window
|
window
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,7 +115,7 @@ impl Container {
|
|||||||
|
|
||||||
pub fn add_window(&mut self, window: Window) {
|
pub fn add_window(&mut self, window: Window) {
|
||||||
self.windows_mut().push_back(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))]
|
#[tracing::instrument(skip(self))]
|
||||||
|
|||||||
@@ -127,7 +127,7 @@ impl Monitor {
|
|||||||
if idx == 0 {
|
if idx == 0 {
|
||||||
self.workspaces_mut().push_back(Workspace::default());
|
self.workspaces_mut().push_back(Workspace::default());
|
||||||
} else {
|
} else {
|
||||||
self.focus_workspace(idx - 1).ok()?;
|
self.focus_workspace(idx.saturating_sub(1)).ok()?;
|
||||||
};
|
};
|
||||||
|
|
||||||
None
|
None
|
||||||
|
|||||||
@@ -1146,7 +1146,7 @@ impl WindowManager {
|
|||||||
.ok_or_else(|| anyhow!("there is no monitor"))?;
|
.ok_or_else(|| anyhow!("there is no monitor"))?;
|
||||||
|
|
||||||
target_monitor.workspaces_mut().push_back(workspace);
|
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)?;
|
target_monitor.load_focused_workspace(mouse_follows_focus)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1281,10 +1281,9 @@ impl WindowManager {
|
|||||||
let origin_workspace =
|
let origin_workspace =
|
||||||
self.focused_workspace_for_monitor_idx_mut(origin_monitor_idx)?;
|
self.focused_workspace_for_monitor_idx_mut(origin_monitor_idx)?;
|
||||||
|
|
||||||
if origin_workspace.focused_container_idx() != 0 {
|
origin_workspace.focus_container(
|
||||||
origin_workspace
|
origin_workspace.focused_container_idx().saturating_sub(1),
|
||||||
.focus_container(origin_workspace.focused_container_idx() - 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 {
|
while workspace.focused_container_idx() > 0 {
|
||||||
workspace.move_window_to_container(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 {
|
if let Some(hwnd) = focused_hwnd {
|
||||||
@@ -1527,7 +1526,7 @@ impl WindowManager {
|
|||||||
Layout::Default(DefaultLayout::Grid)
|
Layout::Default(DefaultLayout::Grid)
|
||||||
| Layout::Default(DefaultLayout::UltrawideVerticalStack)
|
| Layout::Default(DefaultLayout::UltrawideVerticalStack)
|
||||||
) {
|
) {
|
||||||
new_idx - 1
|
new_idx.saturating_sub(1)
|
||||||
} else {
|
} else {
|
||||||
new_idx
|
new_idx
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -802,7 +802,7 @@ impl Workspace {
|
|||||||
self.resize_dimensions_mut().remove(focused_idx);
|
self.resize_dimensions_mut().remove(focused_idx);
|
||||||
|
|
||||||
if focused_idx < target_container_idx {
|
if focused_idx < target_container_idx {
|
||||||
target_container_idx - 1
|
target_container_idx.saturating_sub(1)
|
||||||
} else {
|
} else {
|
||||||
target_container_idx
|
target_container_idx
|
||||||
}
|
}
|
||||||
@@ -924,8 +924,8 @@ impl Workspace {
|
|||||||
self.containers_mut().remove(focused_idx);
|
self.containers_mut().remove(focused_idx);
|
||||||
self.resize_dimensions_mut().remove(focused_idx);
|
self.resize_dimensions_mut().remove(focused_idx);
|
||||||
|
|
||||||
if focused_idx == self.containers().len() && focused_idx != 0 {
|
if focused_idx == self.containers().len() {
|
||||||
self.focus_container(focused_idx - 1);
|
self.focus_container(focused_idx.saturating_sub(1));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
container.load_focused_window();
|
container.load_focused_window();
|
||||||
@@ -1333,7 +1333,8 @@ impl Workspace {
|
|||||||
.ok_or_else(|| anyhow!("there is no monocle container"))?;
|
.ok_or_else(|| anyhow!("there is no monocle container"))?;
|
||||||
|
|
||||||
let window = *window;
|
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()
|
self.containers_mut()
|
||||||
.resize(restore_idx, Container::default());
|
.resize(restore_idx, Container::default());
|
||||||
}
|
}
|
||||||
@@ -1422,13 +1423,10 @@ impl Workspace {
|
|||||||
|
|
||||||
pub fn focus_previous_container(&mut self) {
|
pub fn focus_previous_container(&mut self) {
|
||||||
let focused_idx = self.focused_container_idx();
|
let focused_idx = self.focused_container_idx();
|
||||||
|
self.focus_container(focused_idx.saturating_sub(1));
|
||||||
if focused_idx != 0 {
|
|
||||||
self.focus_container(focused_idx - 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn focus_last_container(&mut self) {
|
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