@@ -181,6 +181,13 @@ macro_rules! window_getter {
181181 } } ;
182182}
183183
184+ macro_rules! event_loop_window_getter {
185+ ( $self: ident, $message: expr) => { {
186+ let ( tx, rx) = channel( ) ;
187+ getter!( $self, rx, Message :: EventLoopWindowTarget ( $message( tx) ) )
188+ } } ;
189+ }
190+
184191macro_rules! webview_getter {
185192 ( $self: ident, $message: expr) => { {
186193 let ( tx, rx) = channel( ) ;
@@ -1268,6 +1275,10 @@ pub enum WebviewMessage {
12681275 IsDevToolsOpen ( Sender < bool > ) ,
12691276}
12701277
1278+ pub enum EventLoopWindowTargetMessage {
1279+ CursorPosition ( Sender < Result < PhysicalPosition < f64 > > > ) ,
1280+ }
1281+
12711282pub type CreateWindowClosure < T > =
12721283 Box < dyn FnOnce ( & EventLoopWindowTarget < Message < T > > ) -> Result < WindowWrapper > + Send > ;
12731284
@@ -1282,6 +1293,7 @@ pub enum Message<T: 'static> {
12821293 Application ( ApplicationMessage ) ,
12831294 Window ( WindowId , WindowMessage ) ,
12841295 Webview ( WindowId , WebviewId , WebviewMessage ) ,
1296+ EventLoopWindowTarget ( EventLoopWindowTargetMessage ) ,
12851297 CreateWebview ( WindowId , CreateWebviewClosure ) ,
12861298 CreateWindow ( WindowId , CreateWindowClosure < T > ) ,
12871299 CreateRawWindow (
@@ -2325,11 +2337,7 @@ impl<T: UserEvent> RuntimeHandle<T> for WryHandle<T> {
23252337 }
23262338
23272339 fn cursor_position ( & self ) -> Result < PhysicalPosition < f64 > > {
2328- self
2329- . context
2330- . main_thread
2331- . window_target
2332- . cursor_position ( )
2340+ event_loop_window_getter ! ( self , EventLoopWindowTargetMessage :: CursorPosition ) ?
23332341 . map ( PhysicalPositionWrapper )
23342342 . map ( Into :: into)
23352343 . map_err ( |_| Error :: FailedToGetCursorPosition )
@@ -2616,11 +2624,7 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
26162624 }
26172625
26182626 fn cursor_position ( & self ) -> Result < PhysicalPosition < f64 > > {
2619- self
2620- . context
2621- . main_thread
2622- . window_target
2623- . cursor_position ( )
2627+ event_loop_window_getter ! ( self , EventLoopWindowTargetMessage :: CursorPosition ) ?
26242628 . map ( PhysicalPositionWrapper )
26252629 . map ( Into :: into)
26262630 . map_err ( |_| Error :: FailedToGetCursorPosition )
@@ -3452,6 +3456,14 @@ fn handle_user_message<T: UserEvent>(
34523456 }
34533457
34543458 Message :: UserEvent ( _) => ( ) ,
3459+ Message :: EventLoopWindowTarget ( message) => match message {
3460+ EventLoopWindowTargetMessage :: CursorPosition ( sender) => {
3461+ let pos = event_loop
3462+ . cursor_position ( )
3463+ . map_err ( |_| Error :: FailedToSendMessage ) ;
3464+ sender. send ( pos) . unwrap ( ) ;
3465+ }
3466+ } ,
34553467 }
34563468}
34573469
0 commit comments