use std::fmt::Debug;
use inherit_methods_macro::inherit_methods;
use winio_elm::{Component, ComponentSender};
use winio_handle::BorrowedContainer;
use winio_primitive::{Enable, Failable, Layoutable, Point, Size, Visible};
use crate::{
sys,
sys::{Error, Result},
};
#[derive(Debug)]
pub struct WebView {
widget: sys::WebView,
}
impl Failable for WebView {
type Error = Error;
}
#[inherit_methods(from = "self.widget")]
impl WebView {
pub fn source(&self) -> Result<String>;
pub fn set_source(&mut self, s: impl AsRef<str>) -> Result<()>;
pub fn navigate(&mut self, s: impl AsRef<str>) -> Result<()> {
self.set_source(s)
}
pub fn set_html(&mut self, s: impl AsRef<str>) -> Result<()>;
pub fn navigate_to_string(&mut self, s: impl AsRef<str>) -> Result<()> {
self.set_html(s)
}
pub fn can_go_forward(&self) -> Result<bool>;
pub fn go_forward(&mut self) -> Result<()>;
pub fn can_go_back(&self) -> Result<bool>;
pub fn go_back(&mut self) -> Result<()>;
pub fn reload(&mut self) -> Result<()>;
pub fn stop(&mut self) -> Result<()>;
}
#[inherit_methods(from = "self.widget")]
impl Visible for WebView {
fn is_visible(&self) -> Result<bool>;
fn set_visible(&mut self, v: bool) -> Result<()>;
}
#[inherit_methods(from = "self.widget")]
impl Enable for WebView {
fn is_enabled(&self) -> Result<bool>;
fn set_enabled(&mut self, v: bool) -> Result<()>;
}
#[inherit_methods(from = "self.widget")]
impl Layoutable for WebView {
fn loc(&self) -> Result<Point>;
fn set_loc(&mut self, p: Point) -> Result<()>;
fn size(&self) -> Result<Size>;
fn set_size(&mut self, v: Size) -> Result<()>;
}
#[derive(Debug)]
#[non_exhaustive]
pub enum WebViewEvent {
Navigating,
Navigated,
}
#[derive(Debug)]
#[non_exhaustive]
pub enum WebViewMessage {}
impl Component for WebView {
type Error = Error;
type Event = WebViewEvent;
type Init<'a> = BorrowedContainer<'a>;
type Message = WebViewMessage;
async fn init(init: Self::Init<'_>, _sender: &ComponentSender<Self>) -> Result<Self> {
let widget = sys::WebView::new(init).await?;
Ok(Self { widget })
}
async fn start(&mut self, sender: &ComponentSender<Self>) -> ! {
let fut_navigated = async {
loop {
self.widget.wait_navigated().await;
sender.output(WebViewEvent::Navigated);
}
};
let fut_navigating = async {
loop {
self.widget.wait_navigating().await;
sender.output(WebViewEvent::Navigating);
}
};
futures_util::future::join(fut_navigated, fut_navigating)
.await
.0
}
}
winio_handle::impl_as_widget!(WebView, widget);