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