clipped.rs

 1use std::ops::Range;
 2
 3use pathfinder_geometry::{rect::RectF, vector::Vector2F};
 4use serde_json::json;
 5
 6use crate::{json, AnyElement, Element, SceneBuilder, SizeConstraint, View, ViewContext};
 7
 8pub struct Clipped<V: View> {
 9    child: AnyElement<V>,
10}
11
12impl<V: View> Clipped<V> {
13    pub fn new(child: AnyElement<V>) -> Self {
14        Self { child }
15    }
16}
17
18impl<V: View> 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        scene: &mut SceneBuilder,
34        bounds: RectF,
35        visible_bounds: RectF,
36        _: &mut Self::LayoutState,
37        view: &mut V,
38        cx: &mut ViewContext<V>,
39    ) -> Self::PaintState {
40        scene.paint_layer(Some(bounds), |scene| {
41            self.child
42                .paint(scene, bounds.origin(), visible_bounds, view, cx)
43        })
44    }
45
46    fn rect_for_text_range(
47        &self,
48        range_utf16: Range<usize>,
49        _: RectF,
50        _: RectF,
51        _: &Self::LayoutState,
52        _: &Self::PaintState,
53        view: &V,
54        cx: &ViewContext<V>,
55    ) -> Option<RectF> {
56        self.child.rect_for_text_range(range_utf16, view, cx)
57    }
58
59    fn debug(
60        &self,
61        _: RectF,
62        _: &Self::LayoutState,
63        _: &Self::PaintState,
64        view: &V,
65        cx: &ViewContext<V>,
66    ) -> json::Value {
67        json!({
68            "type": "Clipped",
69            "child": self.child.debug(view, cx)
70        })
71    }
72}