-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
70 lines (60 loc) · 1.83 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use std::{io::stdin, thread};
use wry::{
application::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop, EventLoopProxy},
window::{Window, WindowBuilder},
},
webview::WebViewBuilder,
};
const JS_SCRIPT: &str = r#"
window.ipc.postMessage("Hi");
"#;
fn main() -> wry::Result<()> {
let event_loop = EventLoop::<String>::with_user_event();
let window = WindowBuilder::new()
.with_visible(false)
.build(&event_loop)?;
let proxy = event_loop.create_proxy();
thread::spawn(move || {
logic(proxy);
});
let handler = move |_window: &Window, msg: String| {
println!("Handler received `{}`", &msg);
};
let webview = WebViewBuilder::new(window)?
.with_html("")?
.with_ipc_handler(handler)
.build()?;
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
match event {
Event::UserEvent(script) => {
if let Err(err) = webview.evaluate_script(&script) {
eprintln!("{err} executing script `{script}`.\n");
}
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => *control_flow = ControlFlow::Exit,
_ => (),
}
});
}
fn logic(proxy: EventLoopProxy<String>) {
eprintln!("Example: sending {JS_SCRIPT}.");
if let Err(err) = proxy.send_event(JS_SCRIPT.into()) {
eprintln!("{err} sending script.\n");
}
let stdin = stdin();
loop {
let mut buf = String::new();
if let Err(err) = stdin.read_line(&mut buf) {
eprintln!("{err} reading stdin.\n")
};
if let Err(err) = proxy.send_event(buf) {
eprintln!("{err} sending script.\n");
}
}
}