clipped.rs

 1use std::ops::Range;
 2
 3use pathfinder_geometry::{rect::RectF, vector::Vector2F};
 4use serde_json::json;
 5
 6use crate::{
 7    json, DebugContext, Element, ElementBox, LayoutContext, MeasurementContext, PaintContext,
 8    SizeConstraint,
 9};
10
11pub struct Clipped {
12    child: ElementBox,
13}
14
15impl Clipped {
16    pub fn new(child: ElementBox) -> Self {
17        Self { child }
18    }
19}
20
21impl Element for Clipped {
22    type LayoutState = ();
23    type PaintState = ();
24
25    fn layout(
26        &mut self,
27        constraint: SizeConstraint,
28        cx: &mut LayoutContext,
29    ) -> (Vector2F, Self::LayoutState) {
30        (self.child.layout(constraint, cx), ())
31    }
32
33    fn paint(
34        &mut self,
35        bounds: RectF,
36        visible_bounds: RectF,
37        _: &mut Self::LayoutState,
38        cx: &mut PaintContext,
39    ) -> Self::PaintState {
40        cx.scene.push_layer(Some(bounds));
41        self.child.paint(bounds.origin(), visible_bounds, cx);
42        cx.scene.pop_layer();
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        cx: &MeasurementContext,
53    ) -> Option<RectF> {
54        self.child.rect_for_text_range(range_utf16, cx)
55    }
56
57    fn debug(
58        &self,
59        _: RectF,
60        _: &Self::LayoutState,
61        _: &Self::PaintState,
62        cx: &DebugContext,
63    ) -> json::Value {
64        json!({
65            "type": "Clipped",
66            "child": self.child.debug(cx)
67        })
68    }
69}