@@ -198,8 +198,6 @@ impl TreeChangeHandler for AdapterChangeHandler<'_> {
198
198
}
199
199
}
200
200
201
- static NEXT_ADAPTER_ID : AtomicUsize = AtomicUsize :: new ( 0 ) ;
202
-
203
201
pub ( crate ) struct AdapterImpl {
204
202
id : usize ,
205
203
messages : Sender < Message > ,
@@ -208,13 +206,13 @@ pub(crate) struct AdapterImpl {
208
206
209
207
impl AdapterImpl {
210
208
fn new (
209
+ id : usize ,
211
210
initial_state : TreeUpdate ,
212
211
is_window_focused : bool ,
213
212
root_window_bounds : WindowBounds ,
214
213
action_handler : Box < dyn ActionHandler + Send > ,
215
214
) -> Self {
216
215
let tree = Tree :: new ( initial_state, is_window_focused) ;
217
- let id = NEXT_ADAPTER_ID . fetch_add ( 1 , Ordering :: SeqCst ) ;
218
216
let ( messages, context) = {
219
217
let mut app_context = AppContext :: write ( ) ;
220
218
let messages = app_context. messages . clone ( ) . unwrap ( ) ;
@@ -430,7 +428,10 @@ impl Drop for AdapterImpl {
430
428
431
429
pub ( crate ) type LazyAdapter = Pin < Arc < Lazy < AdapterImpl , Boxed < AdapterImpl > > > > ;
432
430
431
+ static NEXT_ADAPTER_ID : AtomicUsize = AtomicUsize :: new ( 0 ) ;
432
+
433
433
pub struct Adapter {
434
+ id : usize ,
434
435
r#impl : LazyAdapter ,
435
436
is_window_focused : Arc < AtomicBool > ,
436
437
root_window_bounds : Arc < Mutex < WindowBounds > > ,
@@ -442,6 +443,7 @@ impl Adapter {
442
443
source : impl ' static + FnOnce ( ) -> TreeUpdate + Send ,
443
444
action_handler : Box < dyn ActionHandler + Send > ,
444
445
) -> Self {
446
+ let id = NEXT_ADAPTER_ID . fetch_add ( 1 , Ordering :: SeqCst ) ;
445
447
let is_window_focused = Arc :: new ( AtomicBool :: new ( false ) ) ;
446
448
let is_window_focused_copy = is_window_focused. clone ( ) ;
447
449
let root_window_bounds = Arc :: new ( Mutex :: new ( Default :: default ( ) ) ) ;
@@ -451,6 +453,7 @@ impl Adapter {
451
453
let is_window_focused = is_window_focused_copy. load ( Ordering :: Relaxed ) ;
452
454
let root_window_bounds = * root_window_bounds_copy. lock ( ) . unwrap ( ) ;
453
455
AdapterImpl :: new (
456
+ id,
454
457
source ( ) ,
455
458
is_window_focused,
456
459
root_window_bounds,
@@ -460,11 +463,12 @@ impl Adapter {
460
463
. boxed ( ) ,
461
464
) ) ;
462
465
let adapter = Self {
466
+ id,
463
467
r#impl : r#impl. clone ( ) ,
464
468
is_window_focused,
465
469
root_window_bounds,
466
470
} ;
467
- block_on ( async move { ActivationContext :: activate_eventually ( r#impl) . await } ) ;
471
+ block_on ( async move { ActivationContext :: activate_eventually ( id , r#impl) . await } ) ;
468
472
adapter
469
473
}
470
474
@@ -496,6 +500,14 @@ impl Adapter {
496
500
}
497
501
}
498
502
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
+
499
511
pub ( crate ) enum Message {
500
512
RegisterInterfaces {
501
513
adapter_id : usize ,
0 commit comments