mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-11-27 19:02:47 +01:00
refactor(kde): container operations have the Windows signatures
Container::add_window takes the Window by value, and hide, restore and load_focused_window return nothing (hide and restore take &self), as in komorebi/src/container.rs. The KDE versions returned eyre::Result that was always Ok (their docs said so) and callers wrote ? or unwrap on it; those are gone, as is the eyre import of container.rs. Each operation still does what it did: the same windows are hidden and restored in the same order. No behaviour change. Verified: KDE cargo test -p komorebi --lib --tests (native, FakeBackend): 633 passed, 0 failed. Not run: doc tests, VM suites.
This commit is contained in:
1 parent
0ec3bc8399
commit
ace81232a5
7 files changed
+68
-84
No files matched your search
@@ -14,7 +14,6 @@
|
||||
use crate::lockable_sequence::Lockable;
|
||||
use crate::ring::Ring;
|
||||
use crate::window::Window;
|
||||
use color_eyre::eyre;
|
||||
use nanoid::nanoid;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
@@ -68,9 +67,8 @@ impl Container {
|
||||
|
||||
/// Hides every window of the container but `omit`, or every window when it is `None`
|
||||
///
|
||||
/// It never fails; the result is there so that callers can use `?`.
|
||||
pub fn hide(&mut self, omit: Option<u32>) -> eyre::Result<()> {
|
||||
for window in self.windows_mut().iter_mut().rev() {
|
||||
pub fn hide(&self, omit: Option<u32>) {
|
||||
for window in self.windows().iter().rev() {
|
||||
let mut should_hide = omit.is_none();
|
||||
|
||||
if !should_hide
|
||||
@@ -84,28 +82,20 @@ impl Container {
|
||||
window.hide();
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Restores the focused window of the container, which is the one that is in front when the
|
||||
/// container is a stack
|
||||
///
|
||||
/// It never fails; the result is there so that callers can use `?`.
|
||||
pub fn restore(&mut self) -> eyre::Result<()> {
|
||||
if let Some(window) = self.focused_window_mut() {
|
||||
pub fn restore(&self) {
|
||||
if let Some(window) = self.focused_window() {
|
||||
window.restore();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Hides the unfocused windows of the container and restores the focused one. This function
|
||||
/// is used to make sure we update the window that should be shown on a stack. If the container
|
||||
/// isn't a stack this function won't change anything.
|
||||
///
|
||||
/// It never fails; the result is there so that callers can use `?`.
|
||||
pub fn load_focused_window(&mut self) -> eyre::Result<()> {
|
||||
pub fn load_focused_window(&mut self) {
|
||||
let focused_idx = self.focused_window_idx();
|
||||
|
||||
for (i, window) in self.windows_mut().iter_mut().enumerate() {
|
||||
@@ -115,8 +105,6 @@ impl Container {
|
||||
window.hide();
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The index of the first window of the executable `exe` in this container
|
||||
@@ -165,21 +153,17 @@ impl Container {
|
||||
|
||||
/// Puts a window at the back of the container and gives it the front, hiding every other
|
||||
/// window of the container ([`Window::hide`])
|
||||
///
|
||||
/// It never fails; the result is there so that callers can use `?`.
|
||||
pub fn add_window(&mut self, window: &Window) -> eyre::Result<()> {
|
||||
self.windows_mut().push_back(*window);
|
||||
pub fn add_window(&mut self, window: Window) {
|
||||
self.windows_mut().push_back(window);
|
||||
self.focus_window(self.windows().len().saturating_sub(1));
|
||||
|
||||
let focused_window_idx = self.focused_window_idx();
|
||||
|
||||
for (i, window) in self.windows_mut().iter_mut().enumerate() {
|
||||
for (i, window) in self.windows().iter().enumerate() {
|
||||
if i != focused_window_idx {
|
||||
window.hide();
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
@@ -202,7 +186,7 @@ mod tests {
|
||||
let mut container = Container::default();
|
||||
|
||||
for i in 0..3 {
|
||||
container.add_window(&Window::from(i)).unwrap();
|
||||
container.add_window(Window::from(i));
|
||||
}
|
||||
|
||||
// Should return true for existing windows
|
||||
@@ -219,7 +203,7 @@ mod tests {
|
||||
let mut container = Container::default();
|
||||
|
||||
for i in 0..3 {
|
||||
container.add_window(&Window::from(i)).unwrap();
|
||||
container.add_window(Window::from(i));
|
||||
}
|
||||
|
||||
// Remove window 1
|
||||
@@ -237,7 +221,7 @@ mod tests {
|
||||
let mut container = Container::default();
|
||||
|
||||
for i in 0..3 {
|
||||
container.add_window(&Window::from(i)).unwrap();
|
||||
container.add_window(Window::from(i));
|
||||
}
|
||||
|
||||
// Should be focused on the last created window
|
||||
@@ -257,7 +241,7 @@ mod tests {
|
||||
fn test_add_window() {
|
||||
let mut container = Container::default();
|
||||
|
||||
container.add_window(&Window::from(1)).unwrap();
|
||||
container.add_window(Window::from(1));
|
||||
|
||||
assert_eq!(container.windows().len(), 1);
|
||||
assert_eq!(container.focused_window_idx(), 0);
|
||||
@@ -269,7 +253,7 @@ mod tests {
|
||||
let mut container = Container::default();
|
||||
|
||||
for i in 0..3 {
|
||||
container.add_window(&Window::from(i)).unwrap();
|
||||
container.add_window(Window::from(i));
|
||||
}
|
||||
|
||||
// Should focus on the last created window
|
||||
@@ -293,7 +277,7 @@ mod tests {
|
||||
let mut container = Container::default();
|
||||
|
||||
for i in 0..3 {
|
||||
container.add_window(&Window::from(i)).unwrap();
|
||||
container.add_window(Window::from(i));
|
||||
}
|
||||
|
||||
// Should return the index of the window
|
||||
@@ -325,7 +309,7 @@ mod tests {
|
||||
let mut container = Container::default();
|
||||
for id in ids {
|
||||
fake.open_window(id, "foot", "foot", &format!("window {id}"));
|
||||
container.add_window(&Window::from(id)).unwrap();
|
||||
container.add_window(Window::from(id));
|
||||
}
|
||||
container.focus_window(focus);
|
||||
container
|
||||
@@ -351,7 +335,7 @@ mod tests {
|
||||
with_backend(fake.clone(), || {
|
||||
let mut container = stack_of(&fake, 704..=706, 0);
|
||||
|
||||
container.load_focused_window().unwrap();
|
||||
container.load_focused_window();
|
||||
|
||||
assert!(!fake.is_hidden(704));
|
||||
assert!(fake.is_hidden(705) && fake.is_hidden(706));
|
||||
@@ -364,15 +348,15 @@ mod tests {
|
||||
let fake = Arc::new(FakeBackend::with_one_monitor());
|
||||
|
||||
with_backend(fake.clone(), || {
|
||||
let mut container = stack_of(&fake, 707..=709, 1);
|
||||
let container = stack_of(&fake, 707..=709, 1);
|
||||
|
||||
container.hide(Some(709)).unwrap();
|
||||
container.hide(Some(709));
|
||||
assert!(fake.is_hidden(707) && fake.is_hidden(708) && !fake.is_hidden(709));
|
||||
|
||||
container.hide(None).unwrap();
|
||||
container.hide(None);
|
||||
assert!(fake.is_hidden(709));
|
||||
|
||||
container.restore().unwrap();
|
||||
container.restore();
|
||||
assert!(fake.is_hidden(707) && !fake.is_hidden(708) && fake.is_hidden(709));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -375,7 +375,7 @@ impl Monitor {
|
||||
|
||||
if target_workspace.monocle_container.is_some() {
|
||||
for container in target_workspace.containers_mut() {
|
||||
container.restore()?;
|
||||
container.restore();
|
||||
}
|
||||
|
||||
for window in target_workspace.floating_windows_mut() {
|
||||
|
||||
@@ -440,7 +440,7 @@ impl WindowManager {
|
||||
workspace
|
||||
.focused_container_mut()
|
||||
.ok_or_eyre("there is no focused container")?
|
||||
.add_window(&window)?;
|
||||
.add_window(window);
|
||||
workspace.layer = WorkspaceLayer::Tiling;
|
||||
self.update_focused_workspace(true, false)?;
|
||||
}
|
||||
|
||||
@@ -337,7 +337,7 @@ impl WindowManager {
|
||||
}
|
||||
|
||||
// a monocle, which shows one window of its container, comes as an ordinary container
|
||||
if let Some(mut container) = monocle {
|
||||
if let Some(container) = monocle {
|
||||
let window = container.focused_window().copied();
|
||||
let _ = container.restore();
|
||||
place(
|
||||
@@ -351,7 +351,7 @@ impl WindowManager {
|
||||
if let Some(window) = maximized {
|
||||
let id = window.id;
|
||||
let mut container = crate::container::Container::default();
|
||||
container.add_window(&window)?;
|
||||
container.add_window(window);
|
||||
place(
|
||||
self,
|
||||
Some(window),
|
||||
|
||||
@@ -614,7 +614,7 @@ impl Workspace {
|
||||
|
||||
if let Some(container) = self.containers_mut().get_mut(idx) {
|
||||
for window in windows {
|
||||
container.add_window(&window)?;
|
||||
container.add_window(window);
|
||||
}
|
||||
|
||||
// the window that had focus is the one that is showing
|
||||
@@ -624,7 +624,7 @@ impl Workspace {
|
||||
container.focus_window(at);
|
||||
}
|
||||
|
||||
container.load_focused_window()?;
|
||||
container.load_focused_window();
|
||||
}
|
||||
|
||||
self.focus_container(idx);
|
||||
@@ -655,17 +655,17 @@ impl Workspace {
|
||||
|
||||
// the window that stays was one of the tabs, and may be the one that was out of sight, so
|
||||
// it is put where it is shown as well, or its column is left empty until focus reaches it
|
||||
container.restore()?;
|
||||
container.restore();
|
||||
|
||||
// the strip is about to be told that there are more containers than it has columns for,
|
||||
// and so each of them is a column, until they are put in this one
|
||||
let mut count = 0;
|
||||
for (offset, window) in rest.into_iter().enumerate() {
|
||||
let mut container = Container::default();
|
||||
container.add_window(&window)?;
|
||||
container.add_window(window);
|
||||
|
||||
// it was one of the tabs that were out of sight, and has to be put where it is shown
|
||||
container.restore()?;
|
||||
container.restore();
|
||||
|
||||
self.insert_container_at_idx(idx + 1 + offset, container);
|
||||
count += 1;
|
||||
@@ -887,7 +887,7 @@ mod tests {
|
||||
|
||||
for id in first..first + count {
|
||||
let mut container = Container::default();
|
||||
container.add_window(&Window::from(id)).unwrap();
|
||||
container.add_window(Window::from(id));
|
||||
let idx = workspace.containers().len();
|
||||
workspace.insert_container_at_idx(idx, container);
|
||||
}
|
||||
@@ -910,7 +910,7 @@ mod tests {
|
||||
|
||||
for id in 2..=5 {
|
||||
let mut container = Container::default();
|
||||
container.add_window(&Window::from(id)).unwrap();
|
||||
container.add_window(Window::from(id));
|
||||
let idx = workspace
|
||||
.scrolling_insertion_idx()
|
||||
.unwrap_or_else(|| workspace.containers().len());
|
||||
@@ -1117,7 +1117,7 @@ mod tests {
|
||||
let idx = workspace.focused_container_idx();
|
||||
let container = workspace.containers_mut().get_mut(idx).unwrap();
|
||||
container.focus_window(focused);
|
||||
container.load_focused_window().unwrap();
|
||||
container.load_focused_window();
|
||||
assert!(HIDDEN_WINDOWS.lock().contains(&hidden));
|
||||
|
||||
assert!(workspace.toggle_scrolling_column_stack().unwrap());
|
||||
@@ -1157,7 +1157,7 @@ mod tests {
|
||||
..Workspace::default()
|
||||
};
|
||||
let mut container = Container::default();
|
||||
container.add_window(&Window::from(9101)).unwrap();
|
||||
container.add_window(Window::from(9101));
|
||||
workspace.insert_container_at_idx(0, container);
|
||||
|
||||
workspace.expand_scrolling_column();
|
||||
|
||||
@@ -1323,7 +1323,7 @@ impl WindowManager {
|
||||
}
|
||||
}
|
||||
if changed_focus && let Some(container) = workspace.focused_container_mut() {
|
||||
container.load_focused_window()?;
|
||||
container.load_focused_window();
|
||||
if let Some(window) = container.focused_window() {
|
||||
window.focus(mouse_follows_focus)?;
|
||||
}
|
||||
@@ -1441,7 +1441,7 @@ impl WindowManager {
|
||||
let next_idx = direction.next_idx(current_idx, len);
|
||||
|
||||
container.focus_window(next_idx);
|
||||
container.load_focused_window()?;
|
||||
container.load_focused_window();
|
||||
|
||||
if let Some(window) = container.focused_window() {
|
||||
window.focus(mouse_follows_focus)?;
|
||||
@@ -1634,7 +1634,7 @@ impl WindowManager {
|
||||
workspace.new_monocle_container()?;
|
||||
|
||||
for container in workspace.containers_mut() {
|
||||
container.hide(None)?;
|
||||
container.hide(None);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -1653,7 +1653,7 @@ impl WindowManager {
|
||||
let workspace = self.focused_workspace_mut()?;
|
||||
|
||||
for container in workspace.containers_mut() {
|
||||
container.restore()?;
|
||||
container.restore();
|
||||
}
|
||||
|
||||
workspace.reintegrate_monocle_container()
|
||||
@@ -1681,7 +1681,7 @@ impl WindowManager {
|
||||
.cycle_monocle_container(direction)?;
|
||||
|
||||
for container in self.focused_workspace_mut()?.containers_mut() {
|
||||
container.hide(None)?;
|
||||
container.hide(None);
|
||||
}
|
||||
|
||||
self.update_focused_workspace(true, true)
|
||||
@@ -2515,7 +2515,7 @@ impl WindowManager {
|
||||
|
||||
if target_workspace.monocle_container.is_some() {
|
||||
for container in target_workspace.containers_mut() {
|
||||
container.restore()?;
|
||||
container.restore();
|
||||
}
|
||||
|
||||
for window in target_workspace.floating_windows_mut() {
|
||||
@@ -2845,7 +2845,7 @@ impl WindowManager {
|
||||
container.windows_mut().swap(current_idx, next_idx);
|
||||
|
||||
container.focus_window(next_idx);
|
||||
container.load_focused_window()?;
|
||||
container.load_focused_window();
|
||||
|
||||
if let Some(window) = container.focused_window() {
|
||||
window.focus(mouse_follows_focus)?;
|
||||
@@ -2887,7 +2887,7 @@ impl WindowManager {
|
||||
}
|
||||
|
||||
container.focus_window(idx);
|
||||
container.load_focused_window()?;
|
||||
container.load_focused_window();
|
||||
|
||||
if let Some(window) = container.focused_window() {
|
||||
window.focus(mouse_follows_focus)?;
|
||||
@@ -2934,7 +2934,7 @@ impl WindowManager {
|
||||
&& let Some(w_idx_to_focus) = c.idx_for_window(hwnd)
|
||||
{
|
||||
c.focus_window(w_idx_to_focus);
|
||||
c.load_focused_window()?;
|
||||
c.load_focused_window();
|
||||
}
|
||||
self.update_focused_workspace(self.mouse_follows_focus, true)
|
||||
}
|
||||
@@ -3915,7 +3915,7 @@ impl WindowManager {
|
||||
.ok_or_eyre("there is no workspace for this monitor")?;
|
||||
|
||||
for container in origin_workspace.containers_mut() {
|
||||
container.restore()?;
|
||||
container.restore();
|
||||
}
|
||||
|
||||
origin_workspace.reintegrate_monocle_container()?;
|
||||
|
||||
@@ -545,7 +545,7 @@ impl Workspace {
|
||||
container.focus_window(window_idx);
|
||||
|
||||
if should_load {
|
||||
container.load_focused_window()?;
|
||||
container.load_focused_window();
|
||||
}
|
||||
|
||||
self.focus_container(container_idx);
|
||||
@@ -572,7 +572,7 @@ impl Workspace {
|
||||
};
|
||||
|
||||
let mut container = Container::default();
|
||||
container.add_window(window)?;
|
||||
container.add_window(*window);
|
||||
|
||||
self.insert_container_at_idx(next_idx, container);
|
||||
|
||||
@@ -836,7 +836,7 @@ impl Workspace {
|
||||
}
|
||||
|
||||
for c in self.containers_mut() {
|
||||
c.restore()?;
|
||||
c.restore();
|
||||
}
|
||||
|
||||
return Ok(window);
|
||||
@@ -869,7 +869,7 @@ impl Workspace {
|
||||
self.focus_previous_container();
|
||||
}
|
||||
} else {
|
||||
container.load_focused_window()?;
|
||||
container.load_focused_window();
|
||||
if let Some(window) = container.focused_window() {
|
||||
window.focus(false)?;
|
||||
}
|
||||
@@ -1050,7 +1050,7 @@ impl Workspace {
|
||||
target_container_idx
|
||||
}
|
||||
} else {
|
||||
container.load_focused_window()?;
|
||||
container.load_focused_window();
|
||||
target_container_idx
|
||||
};
|
||||
|
||||
@@ -1059,12 +1059,12 @@ impl Workspace {
|
||||
.get_mut(adjusted_target_container_index)
|
||||
.ok_or_eyre("there is no container")?;
|
||||
|
||||
target_container.add_window(&window)?;
|
||||
target_container.add_window(window);
|
||||
|
||||
self.focus_container(adjusted_target_container_index);
|
||||
self.focused_container_mut()
|
||||
.ok_or_eyre("there is no container")?
|
||||
.load_focused_window()?;
|
||||
.load_focused_window();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1091,13 +1091,13 @@ impl Workspace {
|
||||
if container.windows().is_empty() {
|
||||
self.remove_container_by_idx(focused_container_idx);
|
||||
} else {
|
||||
container.load_focused_window()?;
|
||||
container.load_focused_window();
|
||||
}
|
||||
|
||||
self.new_container_for_window(&window)?;
|
||||
|
||||
let mut container = Container::default();
|
||||
container.add_window(&window)?;
|
||||
container.add_window(window);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1137,7 +1137,7 @@ impl Workspace {
|
||||
}
|
||||
|
||||
for container in self.containers_mut() {
|
||||
container.hide(omit)?;
|
||||
container.hide(omit);
|
||||
}
|
||||
|
||||
if let Some(resize_dimensions) = resize_dimensions {
|
||||
@@ -1149,7 +1149,7 @@ impl Workspace {
|
||||
}
|
||||
|
||||
if let Some(container) = &mut self.monocle_container {
|
||||
container.hide(omit)?;
|
||||
container.hide(omit);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -1172,7 +1172,7 @@ impl Workspace {
|
||||
monitor_wp: &Option<Wallpaper>,
|
||||
) -> eyre::Result<()> {
|
||||
if let Some(container) = &mut self.monocle_container {
|
||||
container.restore()?;
|
||||
container.restore();
|
||||
|
||||
if let Some(window) = container.focused_window() {
|
||||
window.focus(mouse_follows_focus)?;
|
||||
@@ -1193,7 +1193,7 @@ impl Workspace {
|
||||
}
|
||||
|
||||
for container in self.containers_mut() {
|
||||
container.restore()?;
|
||||
container.restore();
|
||||
}
|
||||
|
||||
if let Some(container) = self.focused_container_mut() {
|
||||
@@ -1316,7 +1316,7 @@ impl Workspace {
|
||||
self.monocle_container
|
||||
.as_mut()
|
||||
.ok_or_eyre("there is no monocle container")?
|
||||
.load_focused_window()?;
|
||||
.load_focused_window();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1376,7 +1376,7 @@ impl Workspace {
|
||||
|
||||
self.focused_container_mut()
|
||||
.ok_or_eyre("there is no container")?
|
||||
.load_focused_window()?;
|
||||
.load_focused_window();
|
||||
|
||||
self.monocle_container = None;
|
||||
self.monocle_container_restore_idx = None;
|
||||
@@ -1419,7 +1419,7 @@ impl Workspace {
|
||||
self.monocle_container = None;
|
||||
self.monocle_container_restore_idx = None;
|
||||
} else {
|
||||
monocle_container.load_focused_window()?;
|
||||
monocle_container.load_focused_window();
|
||||
}
|
||||
|
||||
self.maximized_window = Option::from(window);
|
||||
@@ -1446,7 +1446,7 @@ impl Workspace {
|
||||
self.resize_dimensions.remove(focused_idx);
|
||||
}
|
||||
} else {
|
||||
container.load_focused_window()?;
|
||||
container.load_focused_window();
|
||||
}
|
||||
|
||||
self.maximized_window = Option::from(window);
|
||||
@@ -1508,7 +1508,7 @@ impl Workspace {
|
||||
self.monocle_container = None;
|
||||
self.monocle_container_restore_idx = None;
|
||||
} else {
|
||||
monocle_container.load_focused_window()?;
|
||||
monocle_container.load_focused_window();
|
||||
}
|
||||
|
||||
self.maximized_window = Option::from(window);
|
||||
@@ -1542,7 +1542,7 @@ impl Workspace {
|
||||
self.resize_dimensions.remove(container_idx);
|
||||
}
|
||||
} else {
|
||||
container.load_focused_window()?;
|
||||
container.load_focused_window();
|
||||
}
|
||||
|
||||
self.maximized_window = Option::from(window);
|
||||
@@ -1576,7 +1576,7 @@ impl Workspace {
|
||||
}
|
||||
|
||||
let mut container = Container::default();
|
||||
container.add_window(&window)?;
|
||||
container.add_window(window);
|
||||
|
||||
// we shouldn't use insert_container_at_index here because it doesn't make sense for
|
||||
// monocle and maximized toggles which take over the whole screen before being reinserted
|
||||
@@ -1612,7 +1612,7 @@ impl Workspace {
|
||||
self.monocle_container = None;
|
||||
self.monocle_container_restore_idx = None;
|
||||
} else {
|
||||
monocle_container.load_focused_window()?;
|
||||
monocle_container.load_focused_window();
|
||||
}
|
||||
|
||||
window
|
||||
@@ -1634,7 +1634,7 @@ impl Workspace {
|
||||
self.focus_container(focused_idx.saturating_sub(1));
|
||||
}
|
||||
} else {
|
||||
container.load_focused_window()?;
|
||||
container.load_focused_window();
|
||||
}
|
||||
|
||||
window
|
||||
@@ -1658,7 +1658,7 @@ impl Workspace {
|
||||
.ok_or_eyre("there is no floating window")?;
|
||||
|
||||
let mut container = Container::default();
|
||||
container.add_window(&window)?;
|
||||
container.add_window(window);
|
||||
|
||||
self.insert_container_at_idx(focused_idx, container);
|
||||
|
||||
@@ -2529,7 +2529,7 @@ mod tests {
|
||||
// new_container_for_floating_window which usually handles unfloating
|
||||
let window = ws.floating_windows_mut().pop_back().unwrap();
|
||||
let mut container = Container::default();
|
||||
container.add_window(&window).unwrap();
|
||||
container.add_window(window);
|
||||
ws.insert_container_at_idx(ws.focused_container_idx(), container);
|
||||
|
||||
// all indexes should be at their original position
|
||||
@@ -3470,7 +3470,7 @@ mod tests {
|
||||
with_backend(fake.clone(), || {
|
||||
workspace.new_monocle_container().unwrap();
|
||||
for container in workspace.containers_mut() {
|
||||
container.hide(None).unwrap();
|
||||
container.hide(None);
|
||||
}
|
||||
});
|
||||
(fake, workspace)
|
||||
|
||||
Reference in new issue
Block a user