adapter.rs

 1use crate::ViewContext;
 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::ViewContext<V>,
17    ) -> (gpui::geometry::vector::Vector2F, Self::LayoutState) {
18        cx.push_layout_engine(LayoutEngine::new());
19
20        let mut cx = ViewContext::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        bounds: RectF,
40        _visible_bounds: RectF,
41        layout_data: &mut Option<(LayoutEngine, LayoutId)>,
42        view: &mut V,
43        cx: &mut gpui::ViewContext<V>,
44    ) -> Self::PaintState {
45        let (layout_engine, layout_id) = layout_data.take().unwrap();
46        cx.push_layout_engine(layout_engine);
47        self.0
48            .paint(view, bounds.origin(), &mut ViewContext::new(cx));
49        *layout_data = cx.pop_layout_engine().zip(Some(layout_id));
50        debug_assert!(layout_data.is_some());
51    }
52
53    fn rect_for_text_range(
54        &self,
55        _range_utf16: std::ops::Range<usize>,
56        _bounds: RectF,
57        _visible_bounds: RectF,
58        _layout: &Self::LayoutState,
59        _paint: &Self::PaintState,
60        _view: &V,
61        _cx: &gpui::ViewContext<V>,
62    ) -> Option<RectF> {
63        todo!("implement before merging to main")
64    }
65
66    fn debug(
67        &self,
68        _bounds: RectF,
69        _layout: &Self::LayoutState,
70        _paint: &Self::PaintState,
71        _view: &V,
72        _cx: &gpui::ViewContext<V>,
73    ) -> gpui::serde_json::Value {
74        todo!("implement before merging to main")
75    }
76}