identified.rs

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