adapter.rs

 1use crate::{layout_context::LayoutContext, paint_context::PaintContext};
 2use gpui::{geometry::rect::RectF, LayoutEngine, LayoutId};
 3use util::ResultExt;
 4
 5/// Makes a new, playground-style element into a legacy element.
 6pub struct AdapterElement<V>(pub(crate) crate::element::AnyElement<V>);
 7
 8impl<V: 'static> gpui::Element<V> for AdapterElement<V> {
 9    type LayoutState = Option<(LayoutEngine, LayoutId)>;
10    type PaintState = ();
11
12    fn layout(
13        &mut self,
14        constraint: gpui::SizeConstraint,
15        view: &mut V,
16        cx: &mut gpui::LayoutContext<V>,
17    ) -> (gpui::geometry::vector::Vector2F, Self::LayoutState) {
18        cx.push_layout_engine(LayoutEngine::new());
19
20        let size = constraint.max;
21        let mut cx = LayoutContext::new(cx);
22        let layout_id = self.0.layout(view, &mut cx).log_err();
23        if let Some(layout_id) = layout_id {
24            cx.layout_engine()
25                .unwrap()
26                .compute_layout(layout_id, constraint.max)
27                .log_err();
28        }
29
30        let layout_engine = cx.pop_layout_engine();
31        debug_assert!(layout_engine.is_some(),
32            "unexpected layout stack state. is there an unmatched pop_layout_engine in the called code?"
33        );
34
35        (constraint.max, layout_engine.zip(layout_id))
36    }
37
38    fn paint(
39        &mut self,
40        scene: &mut gpui::SceneBuilder,
41        bounds: RectF,
42        visible_bounds: RectF,
43        layout_data: &mut Option<(LayoutEngine, LayoutId)>,
44        view: &mut V,
45        legacy_cx: &mut gpui::PaintContext<V>,
46    ) -> Self::PaintState {
47        let (layout_engine, layout_id) = layout_data.take().unwrap();
48        legacy_cx.push_layout_engine(layout_engine);
49        let mut cx = PaintContext::new(legacy_cx, scene);
50        self.0.paint(view, &mut cx);
51        *layout_data = legacy_cx.pop_layout_engine().zip(Some(layout_id));
52        debug_assert!(layout_data.is_some());
53    }
54
55    fn rect_for_text_range(
56        &self,
57        range_utf16: std::ops::Range<usize>,
58        bounds: RectF,
59        visible_bounds: RectF,
60        layout: &Self::LayoutState,
61        paint: &Self::PaintState,
62        view: &V,
63        cx: &gpui::ViewContext<V>,
64    ) -> Option<RectF> {
65        todo!("implement before merging to main")
66    }
67
68    fn debug(
69        &self,
70        bounds: RectF,
71        layout: &Self::LayoutState,
72        paint: &Self::PaintState,
73        view: &V,
74        cx: &gpui::ViewContext<V>,
75    ) -> gpui::serde_json::Value {
76        todo!("implement before merging to main")
77    }
78}