use core::cell::Cell;
use std::rc::Rc;
use super::handler::{Handler, erase, unit};
use super::loader::{ErasedLayoutView, LayoutReset, Loader};
use super::view::LayoutView;
use crate::error::Rejection;
pub struct Layout<S> {
pub(crate) loader: Loader<S>,
pub(crate) view: ErasedLayoutView<S>,
pub(crate) reset: Option<LayoutReset>,
pub(crate) transitions: Cell<bool>,
}
impl<S: 'static> Layout<S> {
pub fn new<V, U>(view: V) -> Self
where
V: LayoutView<U, S, ()>,
{
Self {
loader: Loader::new(unit()),
view: Rc::new(move |ui, outlet, cx| view.call(ui, outlet, cx)),
reset: None,
transitions: Cell::new(false),
}
}
pub fn with_load<H, T, D, E, V, U>(load: H, view: V) -> Self
where
H: Handler<T, S, Output = Result<D, E>>,
D: 'static,
E: Into<Rejection>,
V: LayoutView<U, S, D>,
{
Self {
loader: Loader::new(erase(load)),
view: Rc::new(move |ui, outlet, cx| view.call(ui, outlet, cx)),
reset: None,
transitions: Cell::new(false),
}
}
pub fn depends(mut self, key: impl Into<String>) -> Self {
self.loader.keys.push(key.into());
self
}
pub fn reset_layouts(mut self) -> Self {
self.reset = Some(LayoutReset::Root);
self
}
pub fn reset_layouts_to(mut self, name: impl Into<String>) -> Self {
self.reset = Some(LayoutReset::To(name.into()));
self
}
}