use std::fmt::Debug;
use crate::{ComponentBuilder, ComponentParts, ComponentSender, Sender};
pub trait Component: Sized + 'static {
type CommandOutput: Debug + Send + 'static;
type Input: Debug + 'static;
type Output: Debug + 'static;
type Init;
type Root: Debug + Clone;
type Widgets: 'static;
#[must_use]
fn builder() -> ComponentBuilder<Self> {
ComponentBuilder::<Self>::default()
}
fn init_root() -> Self::Root;
fn init(
init: Self::Init,
root: Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self>;
#[allow(unused)]
fn update(&mut self, message: Self::Input, sender: ComponentSender<Self>, root: &Self::Root) {}
#[allow(unused)]
fn update_cmd(
&mut self,
message: Self::CommandOutput,
sender: ComponentSender<Self>,
root: &Self::Root,
) {
}
fn update_cmd_with_view(
&mut self,
widgets: &mut Self::Widgets,
message: Self::CommandOutput,
sender: ComponentSender<Self>,
root: &Self::Root,
) {
self.update_cmd(message, sender.clone(), root);
self.update_view(widgets, sender);
}
#[allow(unused)]
fn update_view(&self, widgets: &mut Self::Widgets, sender: ComponentSender<Self>) {}
fn update_with_view(
&mut self,
widgets: &mut Self::Widgets,
message: Self::Input,
sender: ComponentSender<Self>,
root: &Self::Root,
) {
self.update(message, sender.clone(), root);
self.update_view(widgets, sender);
}
#[allow(unused)]
fn shutdown(&mut self, widgets: &mut Self::Widgets, output: Sender<Self::Output>) {}
fn id(&self) -> String {
format!("{:p}", &self)
}
}
pub trait SimpleComponent: Sized + 'static {
type Input: Debug + 'static;
type Output: Debug + 'static;
type Init;
type Root: Debug + Clone;
type Widgets: 'static;
fn init_root() -> Self::Root;
fn init(
init: Self::Init,
root: Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self>;
#[allow(unused)]
fn update(&mut self, message: Self::Input, sender: ComponentSender<Self>) {}
#[allow(unused)]
fn update_cmd(&mut self, input: &Sender<Self::Input>, output: Sender<Self::Output>) {}
#[allow(unused)]
fn update_view(&self, widgets: &mut Self::Widgets, sender: ComponentSender<Self>) {}
#[allow(unused)]
fn shutdown(&mut self, widgets: &mut Self::Widgets, output: Sender<Self::Output>) {}
}
impl<C> Component for C
where
C: SimpleComponent,
{
type Init = C::Init;
type Input = C::Input;
type Output = C::Output;
type Root = C::Root;
type Widgets = C::Widgets;
type CommandOutput = ();
fn init_root() -> Self::Root {
C::init_root()
}
fn init(
init: Self::Init,
root: Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
C::init(init, root, sender)
}
fn update(&mut self, message: Self::Input, sender: ComponentSender<Self>, _root: &Self::Root) {
C::update(self, message, sender);
}
fn update_view(&self, widgets: &mut Self::Widgets, sender: ComponentSender<Self>) {
C::update_view(self, widgets, sender);
}
fn shutdown(&mut self, widgets: &mut Self::Widgets, output: Sender<Self::Output>) {
self.shutdown(widgets, output);
}
}
impl SimpleComponent for () {
type Input = ();
type Output = ();
type Init = ();
type Root = ();
type Widgets = ();
fn init_root() -> Self::Root {}
fn init(
_init: Self::Init,
_root: Self::Root,
_sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
ComponentParts {
model: (),
widgets: (),
}
}
}