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 pub(crate) data_mut: bool,
24}
25
26impl<S: 'static> Layout<S> {
27 pub fn new<V, U>(view: V) -> Self
29 where
30 V: LayoutView<U, S, ()>,
31 {
32 Self {
33 loader: Loader::new(unit()),
34 data_mut: view.borrows_data_mut(),
35 view: Rc::new(move |ui, children, cx| view.call(ui, children, cx)),
36 reset: None,
37 transitions: Cell::new(false),
38 }
39 }
40
41 pub fn with_load<H, T, D, E, V, U>(load: H, view: V) -> Self
45 where
46 H: Handler<T, S, Output = Result<D, E>>,
47 D: 'static,
48 E: Into<Rejection>,
49 V: LayoutView<U, S, D>,
50 {
51 Self {
52 loader: Loader::new(erase(load)),
53 data_mut: view.borrows_data_mut(),
54 view: Rc::new(move |ui, children, cx| view.call(ui, children, cx)),
55 reset: None,
56 transitions: Cell::new(false),
57 }
58 }
59
60 pub fn depends(mut self, key: impl Into<String>) -> Self {
62 self.loader.keys.push(key.into());
63 self
64 }
65
66 pub fn reset_layouts(mut self) -> Self {
68 self.reset = Some(LayoutReset::Root);
69 self
70 }
71
72 pub fn reset_layouts_to(mut self, name: impl Into<String>) -> Self {
74 self.reset = Some(LayoutReset::To(name.into()));
75 self
76 }
77}