clipped.rs

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