Skip to main content

cotis_layout/cotis_traits/
secondary_traits.rs

1//! Element-state query trait implementations on [`CotisLayoutManager`](crate::preamble::CotisLayoutManager).
2//!
3//! These traits from [`cotis_utils::element_state`] read the layout cache populated after
4//! [`CotisLayoutRun::finalize_layouts`](crate::preamble::CotisLayoutRun::finalize_layouts)
5//! (or [`LayoutFrameManager::end`](cotis::layout::LayoutFrameManager::end)). At least one
6//! frame must complete before queries return data.
7//!
8//! | Trait | Data source |
9//! |-------|-------------|
10//! | [`ElementsList`] | Cached element ids and parent links from the last frame |
11//! | [`ElementDebugName`](cotis_utils::element_state::ElementDebugName) | `LayoutElement::name` on cached nodes |
12//! | [`ElementBoundingBox`] | Final bounds via `LayoutElementInfo::to_bounding_box` |
13//! | [`ElementClipOffset`] | Clip scroll offset on the element's config |
14//! | [`ElementClipInternalSize`] | Scrollable content extent from direct in-flow children |
15
16use crate::layout_struct::layout_states::{LayoutElement, clip_content_size};
17use crate::preamble::CotisLayoutManager;
18use cotis::utils::ElementId;
19use cotis_utils::element_state::*;
20use cotis_utils::math::{BoundingBox, Vector2};
21
22impl ElementsList for CotisLayoutManager {
23    fn get_element_ids(&self) -> Vec<ElementId> {
24        self.cached_element_ids()
25    }
26
27    fn get_parent_id(&self, id: ElementId) -> Option<ElementId> {
28        let parent_slot = self.parent_layout_slot_of(id)?;
29        Some(self.get_cache_element(parent_slot)?.id.clone())
30    }
31}
32
33impl ElementDebugName for CotisLayoutManager {
34    fn debug_name(&self, id: ElementId) -> Option<&str> {
35        let element = self.get_cache_element(id)?;
36        Some(&element.name)
37    }
38}
39
40impl ElementBoundingBox for CotisLayoutManager {
41    fn bounding_box(&self, id: ElementId) -> Option<BoundingBox> {
42        Some(self.get_cache_element(id)?.info.to_bounding_box())
43    }
44}
45
46impl ElementClipOffset for CotisLayoutManager {
47    fn clip_offset(&self, id: ElementId) -> Option<Vector2> {
48        Some(self.get_cache_element(id)?.config.clip.offset)
49    }
50}
51
52impl ElementClipInternalSize for CotisLayoutManager {
53    fn clip_internal_size(&self, id: ElementId) -> Option<Vector2> {
54        let parent = self.get_cache_element(id)?;
55        let children: Vec<&LayoutElement> = self
56            .direct_child_slots(id)
57            .iter()
58            .filter_map(|&c| self.get_cache_element(c))
59            .collect();
60        clip_content_size(parent, &children)
61    }
62}