identified.rs

 1use refineable::{Refineable, RefinementCascade};
 2use smallvec::SmallVec;
 3
 4use crate::{
 5    AnyElement, BorrowWindow, Bounds, Element, ElementId, LayoutId, ParentElement, StatefulElement,
 6    Styled, ViewContext,
 7};
 8
 9pub struct Identified<E> {
10    pub(crate) element: E,
11    pub(crate) id: ElementId,
12}
13
14impl<E: Element> Element for Identified<E> {
15    type ViewState = E::ViewState;
16    type ElementState = E::ElementState;
17
18    fn element_id(&self) -> Option<ElementId> {
19        Some(self.id.clone())
20    }
21
22    fn layout(
23        &mut self,
24        state: &mut Self::ViewState,
25        element_state: Option<Self::ElementState>,
26        cx: &mut ViewContext<Self::ViewState>,
27    ) -> (LayoutId, Self::ElementState) {
28        self.element.layout(state, element_state, cx)
29    }
30
31    fn paint(
32        &mut self,
33        bounds: Bounds<crate::Pixels>,
34        state: &mut Self::ViewState,
35        element_state: &mut Self::ElementState,
36        cx: &mut ViewContext<Self::ViewState>,
37    ) {
38        cx.with_element_id(self.id.clone(), |cx| {
39            self.element.paint(bounds, state, element_state, cx)
40        })
41    }
42}
43
44impl<E: Element> StatefulElement for Identified<E> {}
45
46impl<E: Styled> Styled for Identified<E> {
47    type Style = E::Style;
48
49    fn style_cascade(&mut self) -> &mut RefinementCascade<Self::Style> {
50        self.element.style_cascade()
51    }
52    fn declared_style(&mut self) -> &mut <Self::Style as Refineable>::Refinement {
53        self.element.declared_style()
54    }
55}
56
57impl<E: ParentElement> ParentElement for Identified<E> {
58    type State = E::State;
59
60    fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<Self::State>; 2]> {
61        self.element.children_mut()
62    }
63}