@@ -57,6 +57,9 @@ use wry::{
57
57
} ,
58
58
} ;
59
59
60
+ #[ cfg( target_os = "windows" ) ]
61
+ use wry:: webview:: WebviewExtWindows ;
62
+
60
63
use std:: {
61
64
collections:: HashMap ,
62
65
convert:: TryFrom ,
@@ -281,7 +284,6 @@ impl<'a> From<&WryWindowEvent<'a>> for WindowEventWrapper {
281
284
}
282
285
WryWindowEvent :: CloseRequested => WindowEvent :: CloseRequested ,
283
286
WryWindowEvent :: Destroyed => WindowEvent :: Destroyed ,
284
- WryWindowEvent :: Focused ( focused) => WindowEvent :: Focused ( * focused) ,
285
287
WryWindowEvent :: ScaleFactorChanged {
286
288
scale_factor,
287
289
new_inner_size,
@@ -295,6 +297,15 @@ impl<'a> From<&WryWindowEvent<'a>> for WindowEventWrapper {
295
297
}
296
298
}
297
299
300
+ impl From < & WebviewEvent > for WindowEventWrapper {
301
+ fn from ( event : & WebviewEvent ) -> Self {
302
+ let event = match event {
303
+ WebviewEvent :: Focused ( focused) => WindowEvent :: Focused ( * focused) ,
304
+ } ;
305
+ Self ( Some ( event) )
306
+ }
307
+ }
308
+
298
309
pub struct MonitorHandleWrapper ( MonitorHandle ) ;
299
310
300
311
impl From < MonitorHandleWrapper > for Monitor {
@@ -690,9 +701,17 @@ enum WindowMessage {
690
701
#[ derive( Debug , Clone ) ]
691
702
enum WebviewMessage {
692
703
EvaluateScript ( String ) ,
704
+ #[ allow( dead_code) ]
705
+ WebviewEvent ( WebviewEvent ) ,
693
706
Print ,
694
707
}
695
708
709
+ #[ allow( dead_code) ]
710
+ #[ derive( Debug , Clone ) ]
711
+ enum WebviewEvent {
712
+ Focused ( bool ) ,
713
+ }
714
+
696
715
#[ cfg( feature = "system-tray" ) ]
697
716
#[ derive( Clone ) ]
698
717
pub ( crate ) enum TrayMessage {
@@ -1330,6 +1349,33 @@ impl Runtime for Wry {
1330
1349
pending,
1331
1350
) ?;
1332
1351
1352
+ #[ cfg( target_os = "windows" ) ]
1353
+ {
1354
+ let id = webview. inner . window ( ) . id ( ) ;
1355
+ if let Some ( controller) = webview. inner . controller ( ) {
1356
+ let proxy = self . event_loop . create_proxy ( ) ;
1357
+ controller
1358
+ . add_got_focus ( move |_| {
1359
+ let _ = proxy. send_event ( Message :: Webview (
1360
+ id,
1361
+ WebviewMessage :: WebviewEvent ( WebviewEvent :: Focused ( true ) ) ,
1362
+ ) ) ;
1363
+ Ok ( ( ) )
1364
+ } )
1365
+ . unwrap ( ) ;
1366
+ let proxy = self . event_loop . create_proxy ( ) ;
1367
+ controller
1368
+ . add_lost_focus ( move |_| {
1369
+ let _ = proxy. send_event ( Message :: Webview (
1370
+ id,
1371
+ WebviewMessage :: WebviewEvent ( WebviewEvent :: Focused ( false ) ) ,
1372
+ ) ) ;
1373
+ Ok ( ( ) )
1374
+ } )
1375
+ . unwrap ( ) ;
1376
+ } ;
1377
+ }
1378
+
1333
1379
let dispatcher = WryDispatcher {
1334
1380
window_id : webview. inner . window ( ) . id ( ) ,
1335
1381
context : DispatcherContext {
@@ -1567,6 +1613,12 @@ fn handle_event_loop(
1567
1613
Event :: WindowEvent {
1568
1614
event, window_id, ..
1569
1615
} => {
1616
+ if event == WryWindowEvent :: Focused ( true ) {
1617
+ if let Some ( webview) = webviews. get ( & window_id) {
1618
+ webview. inner . focus ( ) ;
1619
+ } ;
1620
+ }
1621
+
1570
1622
if let Some ( event) = WindowEventWrapper :: from ( & event) . 0 {
1571
1623
for handler in window_event_listeners
1572
1624
. lock ( )
@@ -1768,6 +1820,21 @@ fn handle_event_loop(
1768
1820
WebviewMessage :: Print => {
1769
1821
let _ = webview. inner . print ( ) ;
1770
1822
}
1823
+ WebviewMessage :: WebviewEvent ( event) => {
1824
+ if let Some ( event) = WindowEventWrapper :: from ( & event) . 0 {
1825
+ for handler in window_event_listeners
1826
+ . lock ( )
1827
+ . unwrap ( )
1828
+ . get ( & id)
1829
+ . unwrap ( )
1830
+ . lock ( )
1831
+ . unwrap ( )
1832
+ . values ( )
1833
+ {
1834
+ handler ( & event) ;
1835
+ }
1836
+ }
1837
+ }
1771
1838
}
1772
1839
}
1773
1840
}
0 commit comments