Skip to main content

glory_ui/each/
mounting.rs

1use super::*;
2
3#[derive(Clone)]
4pub struct UiMount(crate::WeakScope);
5pub struct UiRow<O: Ownership> {
6    mounted: crate::Mounted,
7    _data: O::Ui,
8}
9impl UiMount {
10    pub fn new(scope: &crate::Scope) -> Self {
11        Self(scope.downgrade())
12    }
13}
14impl Mount for UiMount {
15    type Row<O: Ownership> = UiRow<O>;
16    fn mount<O: Ownership, W: Widget>(
17        &self,
18        factory: impl FnOnce(&CoreScope) -> W,
19    ) -> Result<UiRow<O>> {
20        let parent = self.0.upgrade().ok_or(crate::Error::Gone)?;
21        let data = O::ui(&parent)?;
22        let parent = O::ui_parent(&parent, &data);
23        let mounted = parent.mount_scoped(factory)?;
24        Ok(UiRow {
25            mounted,
26            _data: data,
27        })
28    }
29    fn place<O: Ownership>(&self, row: &UiRow<O>, before: Option<&UiRow<O>>) -> Result {
30        row.mounted.place_before(before.map(|row| &row.mounted))
31    }
32    fn has_content<O: Ownership>(&self, row: &UiRow<O>) -> bool {
33        !row.mounted.roots().is_empty()
34    }
35    fn report(&self, error: crate::Error) {
36        if let Some(scope) = self.0.upgrade() {
37            scope.report_error(error);
38        }
39    }
40    fn clear_on_error(&self) -> bool {
41        true
42    }
43}
44
45/// Monomorphized row ownership: keyed/seed rows carry zero data-owner storage.
46#[doc(hidden)]
47pub trait Ownership: 'static {
48    type Ui: 'static;
49    type Sparse: 'static;
50    fn ui(parent: &crate::Scope) -> Result<Self::Ui>;
51    fn ui_parent<'a>(parent: &'a crate::Scope, data: &'a Self::Ui) -> &'a crate::Scope;
52    fn sparse(parent: &CoreScope) -> Self::Sparse;
53    fn sparse_parent<'a>(parent: &'a CoreScope, data: &'a Self::Sparse) -> &'a CoreScope;
54}
55pub struct Direct;
56pub struct Reactive;
57impl Ownership for Direct {
58    type Ui = ();
59    type Sparse = ();
60    fn ui(_: &crate::Scope) -> Result<()> {
61        Ok(())
62    }
63    fn ui_parent<'a>(parent: &'a crate::Scope, _: &'a ()) -> &'a crate::Scope {
64        parent
65    }
66    fn sparse(_: &CoreScope) {}
67    fn sparse_parent<'a>(parent: &'a CoreScope, _: &'a ()) -> &'a CoreScope {
68        parent
69    }
70}
71impl Ownership for Reactive {
72    type Ui = crate::Scope;
73    type Sparse = CoreScope;
74    fn ui(parent: &crate::Scope) -> Result<crate::Scope> {
75        parent.child()
76    }
77    fn ui_parent<'a>(_: &'a crate::Scope, data: &'a crate::Scope) -> &'a crate::Scope {
78        data
79    }
80    fn sparse(parent: &CoreScope) -> CoreScope {
81        parent.child()
82    }
83    fn sparse_parent<'a>(_: &'a CoreScope, data: &'a CoreScope) -> &'a CoreScope {
84        data
85    }
86}