view_handle.rs

 1use crate::{style::Style, Element, IntoElement, ViewContext};
 2use gpui::{
 3    geometry::{Point, Size},
 4    taffy::style::Overflow,
 5    AnyElement, View, ViewHandle,
 6};
 7
 8impl<ParentView: 'static, ChildView: View> Element<ParentView> for ViewHandle<ChildView> {
 9    type PaintState = AnyElement<ChildView>;
10
11    fn layout(
12        &mut self,
13        _: &mut ParentView,
14        cx: &mut ViewContext<ParentView>,
15    ) -> anyhow::Result<(gpui::LayoutId, Self::PaintState)>
16    where
17        Self: Sized,
18    {
19        let layout_id = cx.add_layout_node(
20            Style {
21                overflow: Point {
22                    x: Overflow::Hidden,
23                    y: Overflow::Hidden,
24                },
25                size: Size::full(),
26                ..Default::default()
27            },
28            None,
29        )?;
30        let element = self.update(cx, |view, cx| view.render(cx));
31        Ok((layout_id, element))
32    }
33
34    fn paint(
35        &mut self,
36        _: &mut ParentView,
37        parent_origin: gpui::geometry::vector::Vector2F,
38        layout: &gpui::Layout,
39        element: &mut AnyElement<ChildView>,
40        cx: &mut ViewContext<ParentView>,
41    ) where
42        Self: Sized,
43    {
44        self.update(cx, |view, cx| {
45            let bounds = layout.bounds + parent_origin;
46            element.layout(gpui::SizeConstraint::strict(bounds.size()), view, cx);
47            cx.paint_layer(Some(layout.bounds), |cx| {
48                element.paint(bounds.origin(), bounds, view, cx);
49            });
50        })
51    }
52}
53
54impl<ParentView: 'static, ChildView: View> IntoElement<ParentView> for ViewHandle<ChildView> {
55    type Element = Self;
56
57    fn into_element(self) -> Self::Element {
58        self
59    }
60}