@@ -533,6 +533,19 @@ impl TryFrom<Icon> for WryIcon {
533
533
534
534
struct WindowEventWrapper ( Option < WindowEvent > ) ;
535
535
536
+ impl WindowEventWrapper {
537
+ fn parse ( webview : & WindowHandle , event : & WryWindowEvent ) -> Self {
538
+ match event {
539
+ // resized event from tao doesn't include a reliable size on macOS
540
+ // because wry replaces the NSView
541
+ WryWindowEvent :: Resized ( _) => Self ( Some ( WindowEvent :: Resized (
542
+ PhysicalSizeWrapper ( webview. inner_size ( ) ) . into ( ) ,
543
+ ) ) ) ,
544
+ e => e. into ( ) ,
545
+ }
546
+ }
547
+ }
548
+
536
549
impl < ' a > From < & WryWindowEvent < ' a > > for WindowEventWrapper {
537
550
fn from ( event : & WryWindowEvent < ' a > ) -> Self {
538
551
let event = match event {
@@ -1424,6 +1437,13 @@ impl WindowHandle {
1424
1437
Self :: Window ( w) => w,
1425
1438
}
1426
1439
}
1440
+
1441
+ fn inner_size ( & self ) -> WryPhysicalSize < u32 > {
1442
+ match self {
1443
+ WindowHandle :: Window ( w) => w. inner_size ( ) ,
1444
+ WindowHandle :: Webview ( w) => w. inner_size ( ) ,
1445
+ }
1446
+ }
1427
1447
}
1428
1448
1429
1449
#[ derive( Debug ) ]
@@ -1899,7 +1919,7 @@ fn handle_user_message(
1899
1919
)
1900
1920
. unwrap ( ) ,
1901
1921
WindowMessage :: InnerSize ( tx) => tx
1902
- . send ( PhysicalSizeWrapper ( window . inner_size ( ) ) . into ( ) )
1922
+ . send ( PhysicalSizeWrapper ( webview . inner . inner_size ( ) ) . into ( ) )
1903
1923
. unwrap ( ) ,
1904
1924
WindowMessage :: OuterSize ( tx) => tx
1905
1925
. send ( PhysicalSizeWrapper ( window. outer_size ( ) ) . into ( ) )
@@ -1929,7 +1949,8 @@ fn handle_user_message(
1929
1949
WindowMessage :: GtkWindow ( tx) => tx. send ( GtkWindow ( window. gtk_window ( ) . clone ( ) ) ) . unwrap ( ) ,
1930
1950
// Setters
1931
1951
WindowMessage :: Center ( tx) => {
1932
- tx. send ( center_window ( window) ) . unwrap ( ) ;
1952
+ tx. send ( center_window ( window, webview. inner . inner_size ( ) ) )
1953
+ . unwrap ( ) ;
1933
1954
}
1934
1955
WindowMessage :: RequestUserAttention ( request_type) => {
1935
1956
window. request_user_attention ( request_type. map ( |r| r. 0 ) ) ;
@@ -2272,19 +2293,26 @@ fn handle_event_loop(
2272
2293
}
2273
2294
}
2274
2295
2275
- if let Some ( event) = WindowEventWrapper :: from ( & event) . 0 {
2276
- for handler in window_event_listeners
2277
- . lock ( )
2278
- . unwrap ( )
2279
- . get ( & window_id)
2280
- . unwrap ( )
2281
- . lock ( )
2282
- . unwrap ( )
2283
- . values ( )
2284
- {
2285
- handler ( & event) ;
2296
+ {
2297
+ let windows_lock = windows. lock ( ) . expect ( "poisoned webview collection" ) ;
2298
+ if let Some ( window_handle) = windows_lock. get ( & window_id) . map ( |w| & w. inner ) {
2299
+ if let Some ( event) = WindowEventWrapper :: parse ( window_handle, & event) . 0 {
2300
+ drop ( windows_lock) ;
2301
+ for handler in window_event_listeners
2302
+ . lock ( )
2303
+ . unwrap ( )
2304
+ . get ( & window_id)
2305
+ . unwrap ( )
2306
+ . lock ( )
2307
+ . unwrap ( )
2308
+ . values ( )
2309
+ {
2310
+ handler ( & event) ;
2311
+ }
2312
+ }
2286
2313
}
2287
2314
}
2315
+
2288
2316
match event {
2289
2317
WryWindowEvent :: CloseRequested => {
2290
2318
let ( tx, rx) = channel ( ) ;
@@ -2409,10 +2437,9 @@ fn on_window_close<'a>(
2409
2437
}
2410
2438
}
2411
2439
2412
- fn center_window ( window : & Window ) -> Result < ( ) > {
2440
+ fn center_window ( window : & Window , window_size : WryPhysicalSize < u32 > ) -> Result < ( ) > {
2413
2441
if let Some ( monitor) = window. current_monitor ( ) {
2414
2442
let screen_size = monitor. size ( ) ;
2415
- let window_size = window. inner_size ( ) ;
2416
2443
let x = ( screen_size. width - window_size. width ) / 2 ;
2417
2444
let y = ( screen_size. height - window_size. height ) / 2 ;
2418
2445
window. set_outer_position ( WryPhysicalPosition :: new ( x, y) ) ;
@@ -2527,7 +2554,7 @@ fn create_webview(
2527
2554
. insert ( window. id ( ) , WindowMenuEventListeners :: default ( ) ) ;
2528
2555
2529
2556
if window_builder. center {
2530
- let _ = center_window ( & window) ;
2557
+ let _ = center_window ( & window, window . inner_size ( ) ) ;
2531
2558
}
2532
2559
let mut webview_builder = WebViewBuilder :: new ( window)
2533
2560
. map_err ( |e| Error :: CreateWebview ( Box :: new ( e) ) ) ?
0 commit comments