use inherit_methods_macro::inherit_methods;
use winio_elm::{Component, ComponentSender};
use winio_handle::BorrowedContainer;
use winio_primitive::{
Enable, Failable, HAlign, Layoutable, Point, Size, TextWidget, ToolTip, Visible,
};
use crate::{
sys,
sys::{Error, Result},
};
#[derive(Debug)]
pub struct Edit {
widget: sys::Edit,
}
impl Failable for Edit {
type Error = Error;
}
#[inherit_methods(from = "self.widget")]
impl ToolTip for Edit {
fn tooltip(&self) -> Result<String>;
fn set_tooltip(&mut self, s: impl AsRef<str>) -> Result<()>;
}
#[inherit_methods(from = "self.widget")]
impl TextWidget for Edit {
fn text(&self) -> Result<String>;
fn set_text(&mut self, s: impl AsRef<str>) -> Result<()>;
}
#[inherit_methods(from = "self.widget")]
impl Edit {
pub fn is_password(&self) -> Result<bool>;
pub fn set_password(&mut self, v: bool) -> Result<()>;
pub fn halign(&self) -> Result<HAlign>;
pub fn set_halign(&mut self, align: HAlign) -> Result<()>;
pub fn is_readonly(&self) -> Result<bool>;
pub fn set_readonly(&mut self, v: bool) -> Result<()>;
}
#[inherit_methods(from = "self.widget")]
impl Visible for Edit {
fn is_visible(&self) -> Result<bool>;
fn set_visible(&mut self, v: bool) -> Result<()>;
}
#[inherit_methods(from = "self.widget")]
impl Enable for Edit {
fn is_enabled(&self) -> Result<bool>;
fn set_enabled(&mut self, v: bool) -> Result<()>;
}
#[inherit_methods(from = "self.widget")]
impl Layoutable for Edit {
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<()>;
fn preferred_size(&self) -> Result<Size>;
}
#[derive(Debug)]
#[non_exhaustive]
pub enum EditEvent {
Change,
}
#[derive(Debug)]
#[non_exhaustive]
pub enum EditMessage {}
impl Component for Edit {
type Error = Error;
type Event = EditEvent;
type Init<'a> = BorrowedContainer<'a>;
type Message = EditMessage;
async fn init(init: Self::Init<'_>, _sender: &ComponentSender<Self>) -> Result<Self> {
let widget = sys::Edit::new(init)?;
Ok(Self { widget })
}
async fn start(&mut self, sender: &ComponentSender<Self>) -> ! {
loop {
self.widget.wait_change().await;
sender.output(EditEvent::Change);
}
}
}
winio_handle::impl_as_widget!(Edit, widget);