@@ -198,8 +198,6 @@ impl TreeChangeHandler for AdapterChangeHandler<'_> {
198198 }
199199}
200200
201- static NEXT_ADAPTER_ID : AtomicUsize = AtomicUsize :: new ( 0 ) ;
202-
203201pub ( crate ) struct AdapterImpl {
204202 id : usize ,
205203 messages : Sender < Message > ,
@@ -208,13 +206,13 @@ pub(crate) struct AdapterImpl {
208206
209207impl AdapterImpl {
210208 fn new (
209+ id : usize ,
211210 initial_state : TreeUpdate ,
212211 is_window_focused : bool ,
213212 root_window_bounds : WindowBounds ,
214213 action_handler : Box < dyn ActionHandler + Send > ,
215214 ) -> Self {
216215 let tree = Tree :: new ( initial_state, is_window_focused) ;
217- let id = NEXT_ADAPTER_ID . fetch_add ( 1 , Ordering :: SeqCst ) ;
218216 let ( messages, context) = {
219217 let mut app_context = AppContext :: write ( ) ;
220218 let messages = app_context. messages . clone ( ) . unwrap ( ) ;
@@ -430,7 +428,10 @@ impl Drop for AdapterImpl {
430428
431429pub ( crate ) type LazyAdapter = Pin < Arc < Lazy < AdapterImpl , Boxed < AdapterImpl > > > > ;
432430
431+ static NEXT_ADAPTER_ID : AtomicUsize = AtomicUsize :: new ( 0 ) ;
432+
433433pub struct Adapter {
434+ id : usize ,
434435 r#impl : LazyAdapter ,
435436 is_window_focused : Arc < AtomicBool > ,
436437 root_window_bounds : Arc < Mutex < WindowBounds > > ,
@@ -442,6 +443,7 @@ impl Adapter {
442443 source : impl ' static + FnOnce ( ) -> TreeUpdate + Send ,
443444 action_handler : Box < dyn ActionHandler + Send > ,
444445 ) -> Self {
446+ let id = NEXT_ADAPTER_ID . fetch_add ( 1 , Ordering :: SeqCst ) ;
445447 let is_window_focused = Arc :: new ( AtomicBool :: new ( false ) ) ;
446448 let is_window_focused_copy = is_window_focused. clone ( ) ;
447449 let root_window_bounds = Arc :: new ( Mutex :: new ( Default :: default ( ) ) ) ;
@@ -451,6 +453,7 @@ impl Adapter {
451453 let is_window_focused = is_window_focused_copy. load ( Ordering :: Relaxed ) ;
452454 let root_window_bounds = * root_window_bounds_copy. lock ( ) . unwrap ( ) ;
453455 AdapterImpl :: new (
456+ id,
454457 source ( ) ,
455458 is_window_focused,
456459 root_window_bounds,
@@ -460,11 +463,12 @@ impl Adapter {
460463 . boxed ( ) ,
461464 ) ) ;
462465 let adapter = Self {
466+ id,
463467 r#impl : r#impl. clone ( ) ,
464468 is_window_focused,
465469 root_window_bounds,
466470 } ;
467- block_on ( async move { ActivationContext :: activate_eventually ( r#impl) . await } ) ;
471+ block_on ( async move { ActivationContext :: activate_eventually ( id , r#impl) . await } ) ;
468472 adapter
469473 }
470474
@@ -496,6 +500,14 @@ impl Adapter {
496500 }
497501}
498502
503+ impl Drop for Adapter {
504+ fn drop ( & mut self ) {
505+ block_on ( async {
506+ ActivationContext :: remove_adapter ( self . id ) . await ;
507+ } )
508+ }
509+ }
510+
499511pub ( crate ) enum Message {
500512 RegisterInterfaces {
501513 adapter_id : usize ,
0 commit comments