1
+ use std:: rc:: Rc ;
2
+
1
3
use dioxus_core:: ElementId ;
2
4
use dioxus_html:: {
3
5
geometry:: { PixelsRect , PixelsSize , PixelsVector2D } ,
4
6
MountedResult , RenderedElementBacking ,
5
7
} ;
6
8
7
- use crate :: { desktop_context:: DesktopContext , query:: QueryEngine } ;
9
+ use crate :: { desktop_context:: DesktopContext , query:: QueryEngine , WeakDesktopContext } ;
8
10
9
11
#[ derive( Clone ) ]
10
12
/// A mounted element passed to onmounted events
11
13
pub struct DesktopElement {
12
14
id : ElementId ,
13
- webview : DesktopContext ,
15
+ webview : WeakDesktopContext ,
14
16
query : QueryEngine ,
15
17
}
16
18
17
19
impl DesktopElement {
18
20
pub ( crate ) fn new ( id : ElementId , webview : DesktopContext , query : QueryEngine ) -> Self {
21
+ let webview = Rc :: downgrade ( & webview) ;
19
22
Self { id, webview, query }
20
23
}
21
24
}
@@ -28,10 +31,13 @@ macro_rules! scripted_getter {
28
31
Box <dyn futures_util:: Future <Output = dioxus_html:: MountedResult <$output_type>>>,
29
32
> {
30
33
let script = format!( $script, id = self . id. 0 ) ;
31
-
34
+ let webview = self
35
+ . webview
36
+ . upgrade( )
37
+ . expect( "Webview should be alive if the element is being queried" ) ;
32
38
let fut = self
33
39
. query
34
- . new_query:: <Option <$output_type>>( & script, self . webview. clone ( ) )
40
+ . new_query:: <Option <$output_type>>( & script, webview)
35
41
. resolve( ) ;
36
42
Box :: pin( async move {
37
43
match fut. await {
@@ -80,11 +86,11 @@ impl RenderedElementBacking for DesktopElement {
80
86
self . id. 0 ,
81
87
serde_json:: to_string( & behavior) . expect( "Failed to serialize ScrollBehavior" )
82
88
) ;
83
-
84
- let fut = self
85
- . query
86
- . new_query :: < bool > ( & script , self . webview . clone ( ) )
87
- . resolve ( ) ;
89
+ let webview = self
90
+ . webview
91
+ . upgrade ( )
92
+ . expect ( "Webview should be alive if the element is being queried" ) ;
93
+ let fut = self . query . new_query :: < bool > ( & script , webview ) . resolve ( ) ;
88
94
Box :: pin ( async move {
89
95
match fut. await {
90
96
Ok ( true ) => Ok ( ( ) ) ,
@@ -106,11 +112,11 @@ impl RenderedElementBacking for DesktopElement {
106
112
"return window.interpreter.setFocus({}, {});" ,
107
113
self . id. 0 , focus
108
114
) ;
109
-
110
- let fut = self
111
- . query
112
- . new_query :: < bool > ( & script , self . webview . clone ( ) )
113
- . resolve ( ) ;
115
+ let webview = self
116
+ . webview
117
+ . upgrade ( )
118
+ . expect ( "Webview should be alive if the element is being queried" ) ;
119
+ let fut = self . query . new_query :: < bool > ( & script , webview ) . resolve ( ) ;
114
120
115
121
Box :: pin ( async move {
116
122
match fut. await {
0 commit comments