From 611fa3456703e8804ab708831694425894ad79f7 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Mon, 29 Apr 2024 13:52:00 -0700 Subject: [PATCH] fix(stackbar): avoid drops from variable updates This commit ensures that when we call methods to change Container.stackbar we are not unintentionally invoking a Drop which kills a stackbar window that already exists. Checks have been added to make sure that we not change the value of the stackbar variable if it is already an Option::Some. re #746 re #792 --- komorebi/src/container.rs | 33 ++++++++++++++++++++------------- komorebi/src/stackbar.rs | 2 ++ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/komorebi/src/container.rs b/komorebi/src/container.rs index 206136bb..74d780e9 100644 --- a/komorebi/src/container.rs +++ b/komorebi/src/container.rs @@ -159,26 +159,33 @@ impl Container { } pub fn set_stackbar_mode(&mut self, mode: StackbarMode) { - self.stackbar = match mode { - StackbarMode::Always => Stackbar::create().ok(), - StackbarMode::Never => None, + match mode { + StackbarMode::Always => { + if self.stackbar.is_none() { + self.stackbar = Stackbar::create().ok(); + } + } + StackbarMode::Never => { + if self.stackbar.is_some() { + self.stackbar = None + } + } StackbarMode::OnStack => { if self.windows().len() > 1 && self.stackbar().is_none() { - Stackbar::create().ok() - } else { - None + self.stackbar = Stackbar::create().ok(); + } + + if self.windows().len() == 1 && self.stackbar.is_some() { + self.stackbar = None; } } - }; + } } pub fn renew_stackbar(&mut self) { - match &self.stackbar { - None => {} - Some(stackbar) => { - if !WindowsApi::is_window(stackbar.hwnd()) { - self.stackbar = Stackbar::create().ok() - } + if let Some(stackbar) = &self.stackbar { + if !WindowsApi::is_window(stackbar.hwnd()) { + self.stackbar = Stackbar::create().ok() } } } diff --git a/komorebi/src/stackbar.rs b/komorebi/src/stackbar.rs index de7facb7..bca18500 100644 --- a/komorebi/src/stackbar.rs +++ b/komorebi/src/stackbar.rs @@ -74,6 +74,7 @@ pub struct Stackbar { impl Drop for Stackbar { fn drop(&mut self) { if !self.is_cloned { + tracing::debug!("dropping and calling close_window on stackbar"); let _ = WindowsApi::close_window(self.hwnd()); } } @@ -87,6 +88,7 @@ impl Clone for Stackbar { } } } + impl Stackbar { unsafe extern "system" fn window_proc( hwnd: HWND,