cotis-layout 0.1.0-alpha.1

Flexbox-style layout engine for Cotis
Documentation
//! Element-state query trait implementations on [`CotisLayoutManager`](crate::preamble::CotisLayoutManager).
//!
//! These traits from [`cotis_utils::element_state`] read the layout cache populated after
//! [`CotisLayoutRun::finalize_layouts`](crate::preamble::CotisLayoutRun::finalize_layouts)
//! (or [`LayoutFrameManager::end`](cotis::layout::LayoutFrameManager::end)). At least one
//! frame must complete before queries return data.
//!
//! | Trait | Data source |
//! |-------|-------------|
//! | [`ElementsList`] | Cached element ids and parent links from the last frame |
//! | [`ElementDebugName`](cotis_utils::element_state::ElementDebugName) | `LayoutElement::name` on cached nodes |
//! | [`ElementBoundingBox`] | Final bounds via `LayoutElementInfo::to_bounding_box` |
//! | [`ElementClipOffset`] | Clip scroll offset on the element's config |
//! | [`ElementClipInternalSize`] | Scrollable content extent from direct in-flow children |

use crate::layout_struct::layout_states::{LayoutElement, clip_content_size};
use crate::preamble::CotisLayoutManager;
use cotis::utils::ElementId;
use cotis_utils::element_state::*;
use cotis_utils::math::{BoundingBox, Vector2};

impl ElementsList for CotisLayoutManager {
    fn get_element_ids(&self) -> Vec<ElementId> {
        self.cached_element_ids()
    }

    fn get_parent_id(&self, id: ElementId) -> Option<ElementId> {
        let parent_slot = self.parent_layout_slot_of(id)?;
        Some(self.get_cache_element(parent_slot)?.id)
    }
}

impl ElementDebugName for CotisLayoutManager {
    fn debug_name(&self, id: ElementId) -> Option<&str> {
        let element = self.get_cache_element(id)?;
        Some(&element.name)
    }
}

impl ElementBoundingBox for CotisLayoutManager {
    fn bounding_box(&self, id: ElementId) -> Option<BoundingBox> {
        Some(self.get_cache_element(id)?.info.to_bounding_box())
    }
}

impl ElementClipOffset for CotisLayoutManager {
    fn clip_offset(&self, id: ElementId) -> Option<Vector2> {
        Some(self.get_cache_element(id)?.config.clip.offset)
    }
}

impl ElementClipInternalSize for CotisLayoutManager {
    fn clip_internal_size(&self, id: ElementId) -> Option<Vector2> {
        let parent = self.get_cache_element(id)?;
        let children: Vec<&LayoutElement> = self
            .direct_child_slots(id)
            .iter()
            .filter_map(|&c| self.get_cache_element(c))
            .collect();
        clip_content_size(parent, &children)
    }
}