1use core::cell::Cell;
4use std::rc::Rc;
5
6use super::handler::{Handler, erase, unit};
7use super::loader::{ErasedLayoutView, LayoutReset, Loader};
8use super::view::LayoutView;
9use crate::error::Rejection;
10
11pub struct Layout<S> {
17 pub(crate) loader: Loader<S>,
18 pub(crate) view: ErasedLayoutView<S>,
19 pub(crate) reset: Option<LayoutReset>,
20 pub(crate) transitions: Cell<bool>,
22}
23
24impl<S: 'static> Layout<S> {
25 pub fn new<V, U>(view: V) -> Self
27 where
28 V: LayoutView<U, S, ()>,
29 {
30 Self {
31 loader: Loader::new(unit()),
32 view: Rc::new(move |ui, outlet, cx| view.call(ui, outlet, cx)),
33 reset: None,
34 transitions: Cell::new(false),
35 }
36 }
37
38 pub fn with_load<H, T, D, E, V, U>(load: H, view: V) -> Self
41 where
42 H: Handler<T, S, Output = Result<D, E>>,
43 D: 'static,
44 E: Into<Rejection>,
45 V: LayoutView<U, S, D>,
46 {
47 Self {
48 loader: Loader::new(erase(load)),
49 view: Rc::new(move |ui, outlet, cx| view.call(ui, outlet, cx)),
50 reset: None,
51 transitions: Cell::new(false),
52 }
53 }
54
55 pub fn depends(mut self, key: impl Into<String>) -> Self {
57 self.loader.keys.push(key.into());
58 self
59 }
60
61 pub fn reset_layouts(mut self) -> Self {
63 self.reset = Some(LayoutReset::Root);
64 self
65 }
66
67 pub fn reset_layouts_to(mut self, name: impl Into<String>) -> Self {
69 self.reset = Some(LayoutReset::To(name.into()));
70 self
71 }
72}