From 7cab0621243c8bbaee83fe200c0724d5b77dcff5 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Mon, 29 Apr 2024 17:31:23 -0700 Subject: [PATCH] fix(stackbar): avoid drops on notification events This commit completely removes the custom Clone and Drop trait implementation for Stackbar, and moves the handling to be explicit within container.rs, which helps us to avoid unintentional drops when the Stackbar struct is cloned for Notification events sent to subscribers such as Zebar. re #746 --- komorebi/src/container.rs | 19 ++++++++++++++----- komorebi/src/stackbar.rs | 23 +---------------------- 2 files changed, 15 insertions(+), 27 deletions(-) diff --git a/komorebi/src/container.rs b/komorebi/src/container.rs index 74d780e9..6dbd2c16 100644 --- a/komorebi/src/container.rs +++ b/komorebi/src/container.rs @@ -18,6 +18,7 @@ pub struct Container { #[getset(get = "pub")] id: String, windows: Ring, + #[serde(skip)] #[getset(get = "pub", get_mut = "pub")] stackbar: Option, } @@ -124,7 +125,10 @@ impl Container { let window = self.windows_mut().remove(idx); if matches!(*STACKBAR_MODE.lock(), StackbarMode::OnStack) && self.windows().len() <= 1 { - self.stackbar = None; + if let Some(stackbar) = &self.stackbar { + let _ = WindowsApi::close_window(stackbar.hwnd()); + self.stackbar = None; + } } if idx != 0 { @@ -166,17 +170,22 @@ impl Container { } } StackbarMode::Never => { - if self.stackbar.is_some() { - self.stackbar = None + if let Some(stackbar) = &self.stackbar { + let _ = WindowsApi::close_window(stackbar.hwnd()); } + + self.stackbar = None } StackbarMode::OnStack => { if self.windows().len() > 1 && self.stackbar().is_none() { self.stackbar = Stackbar::create().ok(); } - if self.windows().len() == 1 && self.stackbar.is_some() { - self.stackbar = None; + if let Some(stackbar) = &self.stackbar { + if self.windows().len() == 1 { + let _ = WindowsApi::close_window(stackbar.hwnd()); + self.stackbar = None; + } } } } diff --git a/komorebi/src/stackbar.rs b/komorebi/src/stackbar.rs index bca18500..c60f3265 100644 --- a/komorebi/src/stackbar.rs +++ b/komorebi/src/stackbar.rs @@ -64,29 +64,9 @@ use crate::STACKBAR_UNFOCUSED_TEXT_COLOUR; use crate::TRANSPARENCY_COLOUR; use crate::WINDOWS_BY_BAR_HWNDS; -#[derive(Debug, Default, Serialize, Deserialize, JsonSchema)] +#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)] pub struct Stackbar { pub(crate) hwnd: isize, - #[serde(skip)] - pub is_cloned: bool, -} - -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()); - } - } -} - -impl Clone for Stackbar { - fn clone(&self) -> Self { - Self { - hwnd: self.hwnd, - is_cloned: true, - } - } } impl Stackbar { @@ -193,7 +173,6 @@ impl Stackbar { Ok(Self { hwnd: hwnd_receiver.recv()?.0, - ..Default::default() }) }