Skip to content

Instantly share code, notes, and snippets.

@jrop
Created January 5, 2019 04:57
Show Gist options
  • Save jrop/5292e304c68b165e1ac6e35dc9ac36f0 to your computer and use it in GitHub Desktop.
Save jrop/5292e304c68b165e1ac6e35dc9ac36f0 to your computer and use it in GitHub Desktop.
struct WebViewController {
pub web_view_handle: Option<Box<web_view::Handle<()>>>,
}
impl WebViewController {
pub fn new() -> WebViewController {
WebViewController {
web_view_handle: None,
}
}
pub fn web_view<'a>() -> (
web_view::WebView<'a, ()>,
std::sync::Arc<std::sync::Mutex<WebViewController>>,
) {
let controller_mutex_for_closure =
std::sync::Arc::new(std::sync::Mutex::new(WebViewController::new()));
let controller_mutex_for_return = controller_mutex_for_closure.clone();
let wv = web_view::builder()
.title("Minimal webview example")
.content(web_view::Content::Url(
"https://en.m.wikipedia.org/wiki/Main_Page",
))
.size(800, 600)
.resizable(true)
.debug(true)
.user_data(())
.invoke_handler(move |_webview, _arg| {
let ctrl = controller_mutex_for_closure.lock().unwrap();
ctrl.exec_handler(_webview, _arg)
})
.build()
.expect("Could not create WebView");
{
let mut ctrl = controller_mutex_for_return.lock().unwrap();
let handle = wv.handle();
ctrl.web_view_handle = Some(Box::new(handle));
}
(wv, controller_mutex_for_return)
}
fn exec_handler(&self, _webview: &mut web_view::WebView<()>, _arg: &str) -> web_view::WVResult {
Ok(())
}
}
fn main() {
let (wv, _controller_mutex) = WebViewController::web_view();
wv.run().unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment