identified.rs

 1use crate::{BorrowWindow, Bounds, Element, ElementId, LayoutId, ViewContext};
 2use anyhow::Result;
 3
 4pub trait Identified {
 5    fn id(&self) -> ElementId;
 6}
 7
 8pub struct ElementWithId<E> {
 9    pub(crate) element: E,
10    pub(crate) id: ElementId,
11}
12
13impl<E: Element> Element for ElementWithId<E> {
14    type State = E::State;
15    type FrameState = E::FrameState;
16
17    fn layout(
18        &mut self,
19        state: &mut Self::State,
20        cx: &mut ViewContext<Self::State>,
21    ) -> Result<(LayoutId, Self::FrameState)> {
22        self.element.layout(state, cx)
23    }
24
25    fn paint(
26        &mut self,
27        bounds: Bounds<crate::Pixels>,
28        state: &mut Self::State,
29        frame_state: &mut Self::FrameState,
30        cx: &mut ViewContext<Self::State>,
31    ) -> Result<()> {
32        cx.with_element_id(self.id.clone(), |cx| {
33            self.element.paint(bounds, state, frame_state, cx)
34        })
35    }
36}
37
38impl<E> Identified for ElementWithId<E> {
39    fn id(&self) -> ElementId {
40        self.id.clone()
41    }
42}