list.rs

  1use crate::{
  2    geometry::{rect::RectF, vector::Vector2F},
  3    sum_tree::{self, SumTree},
  4    Element,
  5};
  6use parking_lot::Mutex;
  7use std::sync::Arc;
  8
  9use crate::ElementBox;
 10
 11pub struct List {
 12    state: ListState,
 13}
 14
 15pub struct ListState(Arc<Mutex<StateInner>>);
 16
 17struct StateInner {
 18    elements: Vec<ElementBox>,
 19    heights: SumTree<ElementHeight>,
 20}
 21
 22#[derive(Clone, Debug)]
 23enum ElementHeight {
 24    Pending,
 25    Ready(f32),
 26}
 27
 28#[derive(Clone, Debug, Default)]
 29struct ElementHeightSummary {
 30    pending_count: usize,
 31    height: f32,
 32}
 33
 34impl Element for List {
 35    type LayoutState = ();
 36
 37    type PaintState = ();
 38
 39    fn layout(
 40        &mut self,
 41        constraint: crate::SizeConstraint,
 42        cx: &mut crate::LayoutContext,
 43    ) -> (Vector2F, Self::LayoutState) {
 44        todo!()
 45    }
 46
 47    fn paint(
 48        &mut self,
 49        bounds: RectF,
 50        layout: &mut Self::LayoutState,
 51        cx: &mut crate::PaintContext,
 52    ) -> Self::PaintState {
 53        todo!()
 54    }
 55
 56    fn dispatch_event(
 57        &mut self,
 58        event: &crate::Event,
 59        bounds: RectF,
 60        layout: &mut Self::LayoutState,
 61        paint: &mut Self::PaintState,
 62        cx: &mut crate::EventContext,
 63    ) -> bool {
 64        todo!()
 65    }
 66
 67    fn debug(
 68        &self,
 69        bounds: RectF,
 70        layout: &Self::LayoutState,
 71        paint: &Self::PaintState,
 72        cx: &crate::DebugContext,
 73    ) -> serde_json::Value {
 74        todo!()
 75    }
 76}
 77
 78impl ListState {
 79    pub fn new(elements: Vec<ElementBox>) -> Self {
 80        let mut heights = SumTree::new();
 81        heights.extend(elements.iter().map(|_| ElementHeight::Pending), &());
 82        Self(Arc::new(Mutex::new(StateInner { elements, heights })))
 83    }
 84}
 85
 86impl sum_tree::Item for ElementHeight {
 87    type Summary = ElementHeightSummary;
 88
 89    fn summary(&self) -> Self::Summary {
 90        todo!()
 91    }
 92}
 93
 94impl sum_tree::Summary for ElementHeightSummary {
 95    type Context = ();
 96
 97    fn add_summary(&mut self, summary: &Self, cx: &Self::Context) {
 98        self.pending_count += summary.pending_count;
 99        self.height += summary.height;
100    }
101}