From 9a084a53aa37fda46c062fb9baef118f647a1606 Mon Sep 17 00:00:00 2001 From: Julian Orth Date: Tue, 8 Oct 2024 22:28:44 +0200 Subject: [PATCH] foreign-toplevel: store weak references --- src/ifs/ext_foreign_toplevel_handle_v1.rs | 11 +++++----- src/ifs/ext_foreign_toplevel_list_v1.rs | 9 +++----- src/tree/toplevel.rs | 25 +++++++++++++++++++++-- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/ifs/ext_foreign_toplevel_handle_v1.rs b/src/ifs/ext_foreign_toplevel_handle_v1.rs index 4ce7fc10..750507d6 100644 --- a/src/ifs/ext_foreign_toplevel_handle_v1.rs +++ b/src/ifs/ext_foreign_toplevel_handle_v1.rs @@ -3,7 +3,7 @@ use { client::{Client, ClientError}, leaks::Tracker, object::{Object, Version}, - tree::ToplevelNode, + tree::ToplevelOpt, wire::{ext_foreign_toplevel_handle_v1::*, ExtForeignToplevelHandleV1Id}, }, std::rc::Rc, @@ -14,16 +14,15 @@ pub struct ExtForeignToplevelHandleV1 { pub id: ExtForeignToplevelHandleV1Id, pub client: Rc, pub tracker: Tracker, - pub toplevel: Rc, + pub toplevel: ToplevelOpt, pub version: Version, } impl ExtForeignToplevelHandleV1 { fn detach(&self) { - self.toplevel - .tl_data() - .handles - .remove(&(self.client.id, self.id)); + if let Some(tl) = self.toplevel.get() { + tl.tl_data().handles.remove(&(self.client.id, self.id)); + } } } diff --git a/src/ifs/ext_foreign_toplevel_list_v1.rs b/src/ifs/ext_foreign_toplevel_list_v1.rs index 352ebb9d..cc427eba 100644 --- a/src/ifs/ext_foreign_toplevel_list_v1.rs +++ b/src/ifs/ext_foreign_toplevel_list_v1.rs @@ -8,7 +8,7 @@ use { }, leaks::Tracker, object::{Object, Version}, - tree::{NodeVisitorBase, ToplevelNode}, + tree::{NodeVisitorBase, ToplevelOpt}, wire::{ ext_foreign_toplevel_list_v1::*, ExtForeignToplevelHandleV1Id, ExtForeignToplevelListV1Id, @@ -105,10 +105,7 @@ impl ExtForeignToplevelListV1 { }); } - pub fn publish_toplevel( - &self, - tl: &Rc, - ) -> Option> { + pub fn publish_toplevel(&self, tl: ToplevelOpt) -> Option> { let id: ExtForeignToplevelHandleV1Id = match self.client.new_id() { Ok(i) => i, Err(e) => { @@ -120,7 +117,7 @@ impl ExtForeignToplevelListV1 { id, client: self.client.clone(), tracker: Default::default(), - toplevel: tl.clone(), + toplevel: tl, version: self.version, }); track!(self.client, handle); diff --git a/src/tree/toplevel.rs b/src/tree/toplevel.rs index d5566023..d42280d3 100644 --- a/src/tree/toplevel.rs +++ b/src/tree/toplevel.rs @@ -29,7 +29,7 @@ use { std::{ cell::{Cell, RefCell}, ops::Deref, - rc::Rc, + rc::{Rc, Weak}, }, }; @@ -226,6 +226,23 @@ pub struct FullscreenedData { pub workspace: Rc, } +#[derive(Clone)] +pub struct ToplevelOpt { + toplevel: Weak, + identifier: ToplevelIdentifier, +} + +impl ToplevelOpt { + pub fn get(&self) -> Option> { + let tl = self.toplevel.upgrade()?; + if tl.tl_data().identifier.get() == self.identifier { + Some(tl) + } else { + None + } + } +} + pub struct ToplevelData { pub self_active: Cell, pub client: Option>, @@ -372,7 +389,11 @@ impl ToplevelData { title: &str, app_id: &str, ) { - let handle = match list.publish_toplevel(toplevel) { + let opt = ToplevelOpt { + toplevel: Rc::downgrade(toplevel), + identifier: self.identifier.get(), + }; + let handle = match list.publish_toplevel(opt) { None => return, Some(handle) => handle, };