forked from tauri-apps/wry
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tauri-apps#474): implement callback for evaluating javascript in…
… webview2
- Loading branch information
Showing
4 changed files
with
78 additions
and
9 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,66 @@ | ||
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-License-Identifier: MIT | ||
|
||
use tao::event::{DeviceEvent, ElementState}; | ||
|
||
fn main() -> wry::Result<()> { | ||
use wry::{ | ||
application::{ | ||
event::{Event, StartCause, WindowEvent}, | ||
event_loop::{ControlFlow, EventLoop}, | ||
window::WindowBuilder, | ||
}, | ||
webview::WebViewBuilder, | ||
}; | ||
|
||
let event_loop = EventLoop::new(); | ||
let window = WindowBuilder::new() | ||
.with_title("Hello World") | ||
.build(&event_loop)?; | ||
let webview = WebViewBuilder::new(window)? | ||
.with_url("https://html5test.com")? | ||
.build()?; | ||
|
||
event_loop.run(move |event, _, control_flow| { | ||
*control_flow = ControlFlow::Wait; | ||
|
||
match event { | ||
Event::NewEvents(StartCause::Init) => println!("Wry has started!"), | ||
Event::WindowEvent { | ||
event: WindowEvent::CloseRequested, | ||
.. | ||
} => *control_flow = ControlFlow::Exit, | ||
Event::DeviceEvent { | ||
event: DeviceEvent::Key(raw), | ||
.. | ||
} => { | ||
if raw.state == ElementState::Released { | ||
let _ = webview.evaluate_script( | ||
format!( | ||
" | ||
window.__WRY_HEADS__ = Math.random() < 0.5; | ||
window.alert(window.__WRY_HEADS__ ? 'Heads!' : 'Tails!'); | ||
window.__WRY_HEADS__; | ||
" | ||
) | ||
.as_str(), | ||
|result| { | ||
let heads: bool = serde_json::from_str(result.as_str()).unwrap(); | ||
println!( | ||
"{}", | ||
match heads { | ||
true => "Heads!", | ||
false => "Tails!", | ||
} | ||
) | ||
}, | ||
); | ||
} | ||
} | ||
_ => { | ||
let _ = webview.resize(); | ||
} | ||
} | ||
}); | ||
} |
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