Boscop / web-view

Rust bindings for webview, a tiny cross-platform library to render web-based GUIs for desktop applications
MIT License
1.92k stars 175 forks source link

how to create handler? #324

Open ShaoDaTao opened 1 year ago

ShaoDaTao commented 1 year ago

I used web-view,, want to show the small window with notice words in it, and automatically disappear after 2 seconds. so create the handler and use it to exit the window, but how to create the handler?

web_view: 0.7.3

use web_view::*;

fn main() {
    let html = "Hello world!";

    let handle = web_view::Handle::current().unwrap();

    std::thread::spawn(move || {
        std::thread::sleep(std::time::Duration::from_secs(2));
        handle.dispatch(move |webview| {
            webview.terminate();
            Ok(())
        }).unwrap();
    });

    web_view::builder()
        .title("")
        .content(Content::Html(html))
        .size(250, 20)
        .resizable(false)
        .frameless(true)
        .user_data(handle.clone())
        .invoke_handler(|_webview, _arg| Ok(()))
        .run()
        .unwrap();
}

how to create the handler? let handle = web_view::Handle::current().unwrap(); # it's wrong way.

AtomicGamer9523 commented 4 months ago

@ShaoDaTao

Last lines:

let wv = web_view::builder()
    // Builder code
    .build().unwrap() // Don't do .run()
let handle = wv.handle();
// thread spawn code
wv.run()