Better error handling. Removed duplicate code

This commit is contained in:
Vladimir Borisov 2021-07-14 18:30:19 +03:00
parent 90463729da
commit e6ebcc46f5

View file

@ -1,53 +1,69 @@
// mod stremio_wevbiew {
use native_windows_gui::{self as nwg, PartialUi};
use once_cell::unsync::OnceCell;
use std::mem;
use std::rc::Rc;
use webview2::Controller;
use winapi::shared::windef::HWND__;
use winapi::um::winuser::*;
#[derive(Default)]
pub struct WebView {
controller: Rc<OnceCell<Controller>>,
}
impl WebView {
fn resize_to_window_bounds(controller: Option<&Controller>, hwnd: Option<*mut HWND__>) {
if let (Some(controller), Some(hwnd)) = (controller, hwnd) {
unsafe {
let mut rect = mem::zeroed();
GetClientRect(hwnd, &mut rect);
controller.put_bounds(rect).ok();
controller.put_is_visible(true).ok();
}
}
}
}
impl PartialUi for WebView {
fn build_partial<W: Into<nwg::ControlHandle>>(
data: &mut Self,
parent: Option<W>,
) -> Result<(), nwg::NwgError> {
let parent = parent.unwrap().into();
let hwnd = parent.hwnd().unwrap();
let parent = parent.expect("No parent window").into();
let hwnd = parent.hwnd().expect("Cannot obtain window handle");
let controller_clone = data.controller.clone();
let result = webview2::EnvironmentBuilder::new()
.with_additional_browser_arguments("--disable-gpu")
.build(move |env| {
env.unwrap().create_controller(hwnd, move |c| {
let c = c.unwrap();
if let Ok(c2) = c.get_controller2() {
c2.put_default_background_color(webview2_sys::Color {
r: 255,
g: 255,
b: 255,
a: 0,
})
.unwrap();
} else {
eprintln!("failed to get interface to controller2");
}
unsafe {
let mut rect = mem::zeroed();
GetClientRect(hwnd, &mut rect);
c.put_bounds(rect).unwrap();
}
let webview = c.get_webview().unwrap();
webview.navigate("edge://gpu").unwrap();
webview
.navigate("https://www.boyanpetrov.rip/stremio/index.html")
.unwrap();
// c.put_is_visible(true).expect("Cannot show the WebView");
controller_clone.set(c).unwrap();
Ok(())
})
env.expect("Cannot obtain webview environment")
.create_controller(hwnd, move |controller| {
let controller = controller.expect("Cannot obtain webview controller");
WebView::resize_to_window_bounds(Some(&controller), parent.hwnd());
if let Ok(controller2) = controller.get_controller2() {
controller2
.put_default_background_color(webview2_sys::Color {
r: 255,
g: 255,
b: 255,
a: 0,
})
.ok();
} else {
eprintln!("failed to get interface to controller2");
}
let webview = controller
.get_webview()
.expect("Cannot obtain webview from controller");
// webview.navigate("edge://gpu").expect("Cannot load the webUI");
webview
.navigate("https://www.boyanpetrov.rip/stremio/index.html")
.expect("Cannot load the webUI");
// controller.put_is_visible(true).expect("Cannot show the WebView");
controller_clone
.set(controller)
.expect("Cannot update the controller");
Ok(())
})
});
if let Err(e) = result {
nwg::modal_fatal_message(
@ -67,22 +83,14 @@ impl PartialUi for WebView {
use nwg::Event as E;
match evt {
E::OnResize | E::OnWindowMaximize => {
if let Some(controller) = self.controller.get() {
unsafe {
let mut rect = mem::zeroed();
GetClientRect(handle.hwnd().unwrap(), &mut rect);
controller.put_bounds(rect).unwrap();
controller.put_is_visible(true).unwrap();
}
}
WebView::resize_to_window_bounds(self.controller.get(), handle.hwnd());
}
E::OnWindowMinimize => {
if let Some(controller) = self.controller.get() {
controller.put_is_visible(false).unwrap();
controller.put_is_visible(false).ok();
}
}
_ => {}
}
}
}
// }