clipped.rs

 1use std::ops::Range;
 2
 3use pathfinder_geometry::{rect::RectF, vector::Vector2F};
 4use serde_json::json;
 5
 6use crate::{json, AnyElement, Element, SizeConstraint, ViewContext};
 7
 8pub struct Clipped<V> {
 9    child: AnyElement<V>,
10}
11
12impl<V> Clipped<V> {
13    pub fn new(child: AnyElement<V>) -> Self {
14        Self { child }
15    }
16}
17
18impl<V: 'static> Element<V> for Clipped<V> {
19    type LayoutState = ();
20    type PaintState = ();
21
22    fn layout(
23        &mut self,
24        constraint: SizeConstraint,
25        view: &mut V,
26        cx: &mut ViewContext<V>,
27    ) -> (Vector2F, Self::LayoutState) {
28        (self.child.layout(constraint, view, cx), ())
29    }
30
31    fn paint(
32        &mut self,
33        bounds: RectF,
34        visible_bounds: RectF,
35        _: &mut Self::LayoutState,
36        view: &mut V,
37        cx: &mut ViewContext<V>,
38    ) -> Self::PaintState {
39        cx.scene().push_layer(Some(bounds));
40        let state = self.child.paint(bounds.origin(), visible_bounds, view, cx);
41        cx.scene().pop_layer();
42        state
43    }
44
45    fn rect_for_text_range(
46        &self,
47        range_utf16: Range<usize>,
48        _: RectF,
49        _: RectF,
50        _: &Self::LayoutState,
51        _: &Self::PaintState,
52        view: &V,
53        cx: &ViewContext<V>,
54    ) -> Option<RectF> {
55        self.child.rect_for_text_range(range_utf16, view, cx)
56    }
57
58    fn debug(
59        &self,
60        _: RectF,
61        _: &Self::LayoutState,
62        _: &Self::PaintState,
63        view: &V,
64        cx: &ViewContext<V>,
65    ) -> json::Value {
66        json!({
67            "type": "Clipped",
68            "child": self.child.debug(view, cx)
69        })
70    }
71}