Skip to content

Commit

Permalink
fix(stackbar): avoid drops on notification events
Browse files Browse the repository at this point in the history
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
  • Loading branch information
LGUG2Z committed Apr 30, 2024
1 parent 611fa34 commit 7cab062
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 27 deletions.
19 changes: 14 additions & 5 deletions komorebi/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct Container {
#[getset(get = "pub")]
id: String,
windows: Ring<Window>,
#[serde(skip)]
#[getset(get = "pub", get_mut = "pub")]
stackbar: Option<Stackbar>,
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
}
}
}
Expand Down
23 changes: 1 addition & 22 deletions komorebi/src/stackbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -193,7 +173,6 @@ impl Stackbar {

Ok(Self {
hwnd: hwnd_receiver.recv()?.0,
..Default::default()
})
}

Expand Down

0 comments on commit 7cab062

Please sign in to comment.