-
-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support callback function in eval (#778)
* feat: support callback fn in eval * chore: enable javascriptcore-rs feature v2_28 * chore: add more example to eval * feat(eval): add public fn evaluate_script_with_callback * fix(linux): use ValueExt trait to convert JSValue to JSON * fix(windows): return eval result directly * chore: add changelog * chore(eval): add exception example and doc to the function * Update src/webview/mod.rs Co-authored-by: Ngo Iok Ui (Wu Yu Wei) <[email protected]> * Fix typo --------- Co-authored-by: Ngo Iok Ui (Wu Yu Wei) <[email protected]>
- Loading branch information
Showing
8 changed files
with
215 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"wry": patch | ||
--- | ||
|
||
On Windows, Linux and macOS, add method `evaluate_script_with_callback` to execute javascipt with a callback. | ||
Evaluated result will be serialized into JSON string and pass to the callback. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright 2020-2022 Tauri Programme within The Commons Conservancy | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-License-Identifier: MIT | ||
|
||
fn main() -> wry::Result<()> { | ||
use wry::{ | ||
application::{ | ||
event::{Event, StartCause, WindowEvent}, | ||
event_loop::{ControlFlow, EventLoop}, | ||
window::{Window, WindowBuilder}, | ||
}, | ||
webview::WebViewBuilder, | ||
}; | ||
|
||
enum UserEvents { | ||
ExecEval(), | ||
} | ||
|
||
let event_loop = EventLoop::<UserEvents>::with_user_event(); | ||
let proxy = event_loop.create_proxy(); | ||
|
||
let window = WindowBuilder::new() | ||
.with_title("Hello World") | ||
.build(&event_loop)?; | ||
|
||
let ipc_handler = move |_: &Window, req: String| match req.as_str() { | ||
"exec-eval" => { | ||
let _ = proxy.send_event(UserEvents::ExecEval()); | ||
} | ||
_ => {} | ||
}; | ||
|
||
let _webview = WebViewBuilder::new(window)? | ||
.with_html( | ||
r#" | ||
<button onclick="window.ipc.postMessage('exec-eval')">Exec eval</button> | ||
"#, | ||
)? | ||
.with_ipc_handler(ipc_handler) | ||
.build()?; | ||
|
||
event_loop.run(move |event, _, control_flow| { | ||
*control_flow = ControlFlow::Wait; | ||
|
||
match event { | ||
Event::UserEvent(UserEvents::ExecEval()) => { | ||
// String | ||
_webview | ||
.evaluate_script_with_callback( | ||
"if (!foo) { var foo = 'morbin'; } `${foo} time`", | ||
|result| println!("String: {:?}", result), | ||
) | ||
.unwrap(); | ||
|
||
// Number | ||
_webview | ||
.evaluate_script_with_callback("var num = 9527; num", |result| { | ||
println!("Number: {:?}", result) | ||
}) | ||
.unwrap(); | ||
|
||
// Object | ||
_webview | ||
.evaluate_script_with_callback("var obj = { thank: 'you', '95': 27 }; obj", |result| { | ||
println!("Object: {:?}", result) | ||
}) | ||
.unwrap(); | ||
|
||
// Array | ||
_webview | ||
.evaluate_script_with_callback("var ary = [1,2,3,4,'5']; ary", |result| { | ||
println!("Array: {:?}", result) | ||
}) | ||
.unwrap(); | ||
// Exception thrown | ||
_webview | ||
.evaluate_script_with_callback("throw new Error()", |result| { | ||
println!("Exception Occured: {:?}", result) | ||
}) | ||
.unwrap(); | ||
} | ||
Event::NewEvents(StartCause::Init) => println!("Wry has started!"), | ||
Event::WindowEvent { | ||
event: WindowEvent::CloseRequested, | ||
.. | ||
} => *control_flow = ControlFlow::Exit, | ||
_ => (), | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters