From df07409a2f8f63cacbdee4f21f5a9c4634c0aed1 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Fri, 20 Aug 2021 12:01:11 -0700 Subject: [PATCH] refactor(workspace): extract fns for container focusing --- komorebi/src/workspace.rs | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/komorebi/src/workspace.rs b/komorebi/src/workspace.rs index a399ee3f..d2e16c1c 100644 --- a/komorebi/src/workspace.rs +++ b/komorebi/src/workspace.rs @@ -303,7 +303,7 @@ impl Workspace { pub fn add_container(&mut self, container: Container) { self.containers_mut().push_back(container); - self.focus_container(self.containers().len() - 1); + self.focus_last_container(); } fn remove_container_by_idx(&mut self, idx: usize) -> Option { @@ -358,9 +358,7 @@ impl Workspace { } } - if container_idx != 0 { - self.focus_container(container_idx - 1); - } + self.focus_previous_container(); Ok(()) } @@ -368,10 +366,7 @@ impl Workspace { pub fn remove_focused_container(&mut self) -> Option { let focused_idx = self.focused_container_idx(); let container = self.remove_container_by_idx(focused_idx); - - if focused_idx != 0 { - self.focus_container(focused_idx - 1); - } + self.focus_previous_container(); container } @@ -560,10 +555,7 @@ impl Workspace { self.set_monocle_container(Option::from(container)); self.set_monocle_container_restore_idx(Option::from(focused_idx)); - - if focused_idx != 0 { - self.focus_container(focused_idx - 1); - } + self.focus_previous_container(); self.monocle_container_mut() .as_mut() @@ -626,9 +618,7 @@ impl Workspace { window.maximize(); } - if focused_idx != 0 { - self.focus_container(focused_idx - 1); - } + self.focus_previous_container(); Ok(()) } @@ -716,4 +706,16 @@ impl Workspace { vec } + + fn focus_previous_container(&mut self) { + let focused_idx = self.focused_container_idx(); + + if focused_idx != 0 { + self.focus_container(focused_idx - 1); + } + } + + fn focus_last_container(&mut self) { + self.focus_container(self.containers().len() - 1); + } }