@@ -13,6 +13,7 @@ use ndk::{
13
13
native_window:: NativeWindow ,
14
14
} ;
15
15
use ndk_glue:: { Event , Rect } ;
16
+ use raw_window_handle:: RawWindowHandle ;
16
17
use std:: {
17
18
collections:: VecDeque ,
18
19
sync:: { Arc , Mutex , RwLock , RwLockReadGuard } ,
@@ -72,6 +73,7 @@ impl<T: 'static> EventLoop<T> {
72
73
Self {
73
74
window_target : event_loop:: EventLoopWindowTarget {
74
75
p : EventLoopWindowTarget {
76
+ raw_window_handle : Default :: default ( ) ,
75
77
_marker : std:: marker:: PhantomData ,
76
78
} ,
77
79
_marker : std:: marker:: PhantomData ,
@@ -117,6 +119,9 @@ impl<T: 'static> EventLoop<T> {
117
119
let native_window_lock = ndk_glue:: native_window ( ) ;
118
120
// The window could have gone away before we got the message
119
121
if native_window_lock. is_some ( ) {
122
+ self . window_target
123
+ . p
124
+ . update_native_window ( native_window_lock. as_ref ( ) ) ;
120
125
self . native_window_lock = Some ( native_window_lock) ;
121
126
call_event_handler ! (
122
127
event_handler,
@@ -140,6 +145,7 @@ impl<T: 'static> EventLoop<T> {
140
145
event:: Event :: Suspended
141
146
) ;
142
147
}
148
+ self . window_target . p . update_native_window ( None ) ;
143
149
}
144
150
Event :: Pause => self . running = false ,
145
151
Event :: Resume => self . running = true ,
@@ -418,10 +424,25 @@ impl<T> Clone for EventLoopProxy<T> {
418
424
}
419
425
420
426
pub struct EventLoopWindowTarget < T : ' static > {
427
+ raw_window_handle : Arc < Mutex < Option < RawWindowHandle > > > ,
421
428
_marker : std:: marker:: PhantomData < T > ,
422
429
}
423
430
424
431
impl < T : ' static > EventLoopWindowTarget < T > {
432
+ fn update_native_window ( & self , native_window : Option < & NativeWindow > ) {
433
+ match native_window {
434
+ Some ( native_window) => {
435
+ let mut handle = raw_window_handle:: android:: AndroidHandle :: empty ( ) ;
436
+ handle. a_native_window =
437
+ unsafe { native_window. ptr ( ) . as_mut ( ) as * mut _ as * mut _ } ;
438
+ * self . raw_window_handle . lock ( ) . unwrap ( ) = Some ( RawWindowHandle :: Android ( handle) ) ;
439
+ }
440
+ None => {
441
+ * self . raw_window_handle . lock ( ) . unwrap ( ) = None ;
442
+ }
443
+ }
444
+ }
445
+
425
446
pub fn primary_monitor ( & self ) -> Option < monitor:: MonitorHandle > {
426
447
Some ( monitor:: MonitorHandle {
427
448
inner : MonitorHandle ,
@@ -456,16 +477,20 @@ impl DeviceId {
456
477
#[ derive( Clone , Copy , Debug , Default , Eq , PartialEq ) ]
457
478
pub struct PlatformSpecificWindowBuilderAttributes ;
458
479
459
- pub struct Window ;
480
+ pub struct Window {
481
+ raw_window_handle : Arc < Mutex < Option < RawWindowHandle > > > ,
482
+ }
460
483
461
484
impl Window {
462
485
pub fn new < T : ' static > (
463
- _el : & EventLoopWindowTarget < T > ,
486
+ el : & EventLoopWindowTarget < T > ,
464
487
_window_attrs : window:: WindowAttributes ,
465
488
_: PlatformSpecificWindowBuilderAttributes ,
466
489
) -> Result < Self , error:: OsError > {
467
490
// FIXME this ignores requested window attributes
468
- Ok ( Self )
491
+ Ok ( Self {
492
+ raw_window_handle : Arc :: clone ( & el. raw_window_handle ) ,
493
+ } )
469
494
}
470
495
471
496
pub fn id ( & self ) -> WindowId {
@@ -581,14 +606,14 @@ impl Window {
581
606
}
582
607
583
608
pub fn raw_window_handle ( & self ) -> raw_window_handle:: RawWindowHandle {
584
- let a_native_window = if let Some ( native_window ) = ndk_glue :: native_window ( ) . as_ref ( ) {
585
- unsafe { native_window . ptr ( ) . as_mut ( ) as * mut _ as * mut _ }
586
- } else {
587
- panic ! ( "Cannot get the native window, it's null and will always be null before Event::Resumed and after Event::Suspended. Make sure you only call this function between those events." ) ;
588
- } ;
589
- let mut handle = raw_window_handle :: android :: AndroidHandle :: empty ( ) ;
590
- handle . a_native_window = a_native_window ;
591
- raw_window_handle :: RawWindowHandle :: Android ( handle )
609
+ self . raw_window_handle
610
+ . lock ( )
611
+ . unwrap ( )
612
+ . as_ref ( )
613
+ . expect (
614
+ "The window can be obtained only between `Event::Resumed` and `Event::Suspended`!" ,
615
+ )
616
+ . clone ( )
592
617
}
593
618
594
619
pub fn config ( & self ) -> Configuration {
0 commit comments