1
+ use crate :: Webview ;
1
2
use futures:: future:: BoxFuture ;
2
- use webview_official:: WebviewMut ;
3
3
4
4
mod runner;
5
5
6
- type InvokeHandler =
7
- dyn Fn ( WebviewMut , String ) -> BoxFuture < ' static , Result < ( ) , String > > + Send + Sync ;
8
- type Setup = dyn Fn ( WebviewMut , String ) -> BoxFuture < ' static , ( ) > + Send + Sync ;
6
+ type InvokeHandler < W > = dyn Fn ( W , String ) -> BoxFuture < ' static , Result < ( ) , String > > + Send + Sync ;
7
+ type Setup < W > = dyn Fn ( W , String ) -> BoxFuture < ' static , ( ) > + Send + Sync ;
9
8
10
9
/// The application runner.
11
- pub struct App {
10
+ pub struct App < W : Webview > {
12
11
/// The JS message handler.
13
- invoke_handler : Option < Box < InvokeHandler > > ,
12
+ invoke_handler : Option < Box < InvokeHandler < W > > > ,
14
13
/// The setup callback, invoked when the webview is ready.
15
- setup : Option < Box < Setup > > ,
14
+ setup : Option < Box < Setup < W > > > ,
16
15
/// The HTML of the splashscreen to render.
17
16
splashscreen_html : Option < String > ,
18
17
}
19
18
20
- impl App {
19
+ impl < W : Webview + ' static > App < W > {
21
20
/// Runs the app until it finishes.
22
21
pub fn run ( self ) {
23
22
runner:: run ( self ) . expect ( "Failed to build webview" ) ;
@@ -28,7 +27,7 @@ impl App {
28
27
/// The message is considered consumed if the handler exists and returns an Ok Result.
29
28
pub ( crate ) async fn run_invoke_handler (
30
29
& self ,
31
- webview : & mut WebviewMut ,
30
+ webview : & mut W ,
32
31
arg : & str ,
33
32
) -> Result < bool , String > {
34
33
if let Some ( ref invoke_handler) = self . invoke_handler {
@@ -40,7 +39,7 @@ impl App {
40
39
}
41
40
42
41
/// Runs the setup callback if defined.
43
- pub ( crate ) async fn run_setup ( & self , webview : & mut WebviewMut , source : String ) {
42
+ pub ( crate ) async fn run_setup ( & self , webview : & mut W , source : String ) {
44
43
if let Some ( ref setup) = self . setup {
45
44
let fut = setup ( webview. clone ( ) , source) ;
46
45
fut. await ;
@@ -55,16 +54,16 @@ impl App {
55
54
56
55
/// The App builder.
57
56
#[ derive( Default ) ]
58
- pub struct AppBuilder {
57
+ pub struct AppBuilder < W : Webview > {
59
58
/// The JS message handler.
60
- invoke_handler : Option < Box < InvokeHandler > > ,
59
+ invoke_handler : Option < Box < InvokeHandler < W > > > ,
61
60
/// The setup callback, invoked when the webview is ready.
62
- setup : Option < Box < Setup > > ,
61
+ setup : Option < Box < Setup < W > > > ,
63
62
/// The HTML of the splashscreen to render.
64
63
splashscreen_html : Option < String > ,
65
64
}
66
65
67
- impl AppBuilder {
66
+ impl < W : Webview + ' static > AppBuilder < W > {
68
67
/// Creates a new App builder.
69
68
pub fn new ( ) -> Self {
70
69
Self {
@@ -77,7 +76,7 @@ impl AppBuilder {
77
76
/// Defines the JS message handler callback.
78
77
pub fn invoke_handler <
79
78
T : futures:: Future < Output = Result < ( ) , String > > + Send + Sync + ' static ,
80
- F : Fn ( WebviewMut , String ) -> T + Send + Sync + ' static ,
79
+ F : Fn ( W , String ) -> T + Send + Sync + ' static ,
81
80
> (
82
81
mut self ,
83
82
invoke_handler : F ,
@@ -91,7 +90,7 @@ impl AppBuilder {
91
90
/// Defines the setup callback.
92
91
pub fn setup <
93
92
T : futures:: Future < Output = ( ) > + Send + Sync + ' static ,
94
- F : Fn ( WebviewMut , String ) -> T + Send + Sync + ' static ,
93
+ F : Fn ( W , String ) -> T + Send + Sync + ' static ,
95
94
> (
96
95
mut self ,
97
96
setup : F ,
@@ -109,13 +108,16 @@ impl AppBuilder {
109
108
}
110
109
111
110
/// Adds a plugin to the runtime.
112
- pub fn plugin ( self , plugin : impl crate :: plugin:: Plugin + Send + Sync + Sync + ' static ) -> Self {
113
- crate :: async_runtime:: block_on ( crate :: plugin:: register ( plugin) ) ;
111
+ pub fn plugin (
112
+ self ,
113
+ plugin : impl crate :: plugin:: Plugin < W > + Send + Sync + Sync + ' static ,
114
+ ) -> Self {
115
+ crate :: async_runtime:: block_on ( crate :: plugin:: register ( W :: plugin_store ( ) , plugin) ) ;
114
116
self
115
117
}
116
118
117
119
/// Builds the App.
118
- pub fn build ( self ) -> App {
120
+ pub fn build ( self ) -> App < W > {
119
121
App {
120
122
invoke_handler : self . invoke_handler ,
121
123
setup : self . setup ,
0 commit comments