use std::cell::RefCell;
use std::fmt;
use std::ops::{Deref, DerefMut};
use std::rc::Rc;
use iced_core::{Element, Settings, Theme, window};
use iced_runtime::Task;
use crate::config::HarnessConfig;
use crate::harness::Harness;
type View<Props, M> = dyn for<'a> Fn(&'a Props) -> Element<'a, M, Theme, iced_renderer::Renderer>;
type OnMessage<Props, M> = dyn Fn(&mut Props, &M);
struct Parts<Props, M> {
make: Box<dyn Fn() -> Props>,
view: Box<View<Props, M>>,
on_message: RefCell<Option<Box<OnMessage<Props, M>>>>,
}
pub struct MountProgram<Props, M> {
parts: Rc<Parts<Props, M>>,
}
impl<Props, M> fmt::Debug for MountProgram<Props, M> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("MountProgram").finish_non_exhaustive()
}
}
impl<Props: 'static, M: Clone + fmt::Debug + Send + 'static> iced_program::Program
for MountProgram<Props, M>
{
type State = Props;
type Message = M;
type Theme = Theme;
type Renderer = iced_renderer::Renderer;
type Executor = iced_futures::backend::null::Executor;
fn name() -> &'static str {
"icedwright_mount"
}
fn settings(&self) -> Settings {
Settings::default()
}
fn window(&self) -> Option<window::Settings> {
Some(window::Settings::default())
}
fn boot(&self) -> (Props, Task<M>) {
((self.parts.make)(), Task::none())
}
fn update(&self, state: &mut Props, message: M) -> Task<M> {
if let Some(apply) = self.parts.on_message.borrow().as_ref() {
apply(state, &message);
}
Task::none()
}
fn view<'a>(
&self,
state: &'a Props,
_: window::Id,
) -> Element<'a, M, Theme, iced_renderer::Renderer> {
(self.parts.view)(state)
}
fn title(&self, _: &Props, _: window::Id) -> String {
"Mount".to_owned()
}
}
#[must_use]
pub struct Mount<Props: 'static, M: Clone + fmt::Debug + Send + 'static> {
harness: Harness<MountProgram<Props, M>>,
parts: Rc<Parts<Props, M>>,
}
impl<Props: 'static, M: Clone + fmt::Debug + Send + 'static> fmt::Debug for Mount<Props, M> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Mount")
.field("harness", &self.harness)
.finish_non_exhaustive()
}
}
impl<Props: 'static, M: Clone + fmt::Debug + Send + 'static> Mount<Props, M> {
pub fn new(
props: Props,
view: impl Fn(&Props) -> Element<'_, M, Theme, iced_renderer::Renderer> + 'static,
) -> crate::Result<Self>
where
Props: Clone,
{
Self::from_fn(move || props.clone(), view)
}
pub fn from_fn(
make: impl Fn() -> Props + 'static,
view: impl Fn(&Props) -> Element<'_, M, Theme, iced_renderer::Renderer> + 'static,
) -> crate::Result<Self> {
let parts = Rc::new(Parts {
make: Box::new(make),
view: Box::new(view),
on_message: RefCell::new(None),
});
Self::boot(parts, HarnessConfig::default())
}
fn boot(
parts: Rc<Parts<Props, M>>,
config: HarnessConfig<MountProgram<Props, M>>,
) -> crate::Result<Self> {
let program = MountProgram {
parts: parts.clone(),
};
let harness = Harness::with_config(program, config)?;
Ok(Self { harness, parts })
}
pub fn with_config(self, config: HarnessConfig<MountProgram<Props, M>>) -> crate::Result<Self> {
let Self { harness, parts } = self;
drop(harness);
Self::boot(parts, config)
}
pub fn on_message(self, apply: impl Fn(&mut Props, &M) + 'static) -> Self {
self.parts.on_message.replace(Some(Box::new(apply)));
self
}
pub fn set_props(&mut self, change: impl FnOnce(&mut Props)) -> crate::Result {
change(&mut self.harness.state);
self.harness.dirty = true;
self.harness.settle()
}
pub fn into_harness(self) -> Harness<MountProgram<Props, M>> {
self.harness
}
}
impl<Props: 'static, M: Clone + fmt::Debug + Send + 'static> Deref for Mount<Props, M> {
type Target = Harness<MountProgram<Props, M>>;
fn deref(&self) -> &Self::Target {
&self.harness
}
}
impl<Props: 'static, M: Clone + fmt::Debug + Send + 'static> DerefMut for Mount<Props, M> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.harness
}
}