Replies: 1 comment 1 reply
-
|
Ah, I figured it out, I needed to go via the event proxy: use wry::{
application::{
event::{Event, StartCause, WindowEvent},
event_loop::{ControlFlow, EventLoop},
menu::{MenuBar, MenuItem},
window::WindowBuilder,
},
webview::WebViewBuilder,
};
pub fn window<S: AsRef<str>, T: AsRef<str>>(
url: S,
title: T,
) -> wry::Result<()> {
let event_loop = EventLoop::<String>::with_user_event();
let event_proxy = event_loop.create_proxy();
let mut menu_bar = MenuBar::new();
let mut win_bar = MenuBar::new();
win_bar.add_native_item(MenuItem::About("".to_string()));
win_bar.add_native_item(MenuItem::EnterFullScreen);
win_bar.add_native_item(MenuItem::Quit);
menu_bar.add_submenu("Main", true, win_bar);
let window = WindowBuilder::new()
.with_title(title.as_ref())
.with_menu(menu_bar)
.build(&event_loop)?;
let webview = WebViewBuilder::new(window)?
.with_url(url.as_ref())?
.with_ipc_handler(move |window, message| {
println!("Received message from Javascript {}", message);
event_proxy.send_event(
"window.postMessage('This is a message from Rust')".to_string()).unwrap();
})
.build()?;
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
match event {
Event::NewEvents(StartCause::Init) => {
//println!("Window is ready!")
}
Event::UserEvent(script) => {
webview.evaluate_script(&script);
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => *control_flow = ControlFlow::Exit,
_ => (),
}
});
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I see I can call
set_ipc_handler()in0.13to receive messages from Javascript but I can't see how I can now send messages back to Javascript.I can't call
webview.evaluate_script()because the compiler won't allow that as thewebviewhasn't been created yet in my closure. I was expecting to see something likeWindow.postMessage()and then in Javascript I could listen for themessageevent onwindowto get messages from Rust. Alternatively if I can get a reference to theWebviewfrom theWindowthen i can just callevaluate_script().This is the code I am using:
Awesome work with the new Tao abstraction and I like the look of custom protocols but it's important for me to do bi-directional communication without a custom protocol as a custom protocol creates security hazards for my application.
Thanks for any pointers 🙏
Beta Was this translation helpful? Give feedback.
All reactions