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, gpui2-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 mut cx = LayoutContext::new(cx);
21        let layout_id = self.0.layout(view, &mut cx).log_err();
22        if let Some(layout_id) = layout_id {
23            cx.layout_engine()
24                .unwrap()
25                .compute_layout(layout_id, constraint.max)
26                .log_err();
27        }
28
29        let layout_engine = cx.pop_layout_engine();
30        debug_assert!(layout_engine.is_some(),
31            "unexpected layout stack state. is there an unmatched pop_layout_engine in the called code?"
32        );
33
34        (constraint.max, layout_engine.zip(layout_id))
35    }
36
37    fn paint(
38        &mut self,
39        scene: &mut gpui::SceneBuilder,
40        bounds: RectF,
41        _visible_bounds: RectF,
42        layout_data: &mut Option<(LayoutEngine, LayoutId)>,
43        view: &mut V,
44        legacy_cx: &mut gpui::PaintContext<V>,
45    ) -> Self::PaintState {
46        let (layout_engine, layout_id) = layout_data.take().unwrap();
47        legacy_cx.push_layout_engine(layout_engine);
48        let mut cx = PaintContext::new(legacy_cx, scene);
49        self.0.paint(view, bounds.origin(), &mut cx);
50        *layout_data = legacy_cx.pop_layout_engine().zip(Some(layout_id));
51        debug_assert!(layout_data.is_some());
52    }
53
54    fn rect_for_text_range(
55        &self,
56        _range_utf16: std::ops::Range<usize>,
57        _bounds: RectF,
58        _visible_bounds: RectF,
59        _layout: &Self::LayoutState,
60        _paint: &Self::PaintState,
61        _view: &V,
62        _cx: &gpui::ViewContext<V>,
63    ) -> Option<RectF> {
64        todo!("implement before merging to main")
65    }
66
67    fn debug(
68        &self,
69        _bounds: RectF,
70        _layout: &Self::LayoutState,
71        _paint: &Self::PaintState,
72        _view: &V,
73        _cx: &gpui::ViewContext<V>,
74    ) -> gpui::serde_json::Value {
75        todo!("implement before merging to main")
76    }
77}