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