1use crate::{Affine, Point, Rect, transform::TransformSpace};
2use std::sync::Arc;
3use xrack_runtime::{Node, is_button, is_pane, is_text_input};
4
5#[derive(Clone, Copy, Debug, PartialEq)]
7pub struct RoundedClip {
8 pub bounds: Rect,
9 pub radii: [f32; 4],
10}
11
12impl RoundedClip {
13 pub(crate) fn content_box(node: &Node, bounds: Rect) -> Self {
14 let border = node
15 .length("border-width")
16 .unwrap_or(if node.kind == "ControlFrame" {
17 1.0
18 } else {
19 0.0
20 })
21 .max(0.0);
22 let inner = bounds.inset(border);
23 let radii = corner_radii(node, bounds).map(|radius| (radius - border).max(0.0));
24 Self {
25 bounds: inner,
26 radii: normalize_radii(inner, radii),
27 }
28 }
29
30 pub(crate) fn contains(self, point: Point) -> bool {
31 if !self.bounds.contains(point) {
32 return false;
33 }
34 let [tl, tr, br, bl] = self.radii;
35 let y = point.y - self.bounds.y;
36 let left = inset_over(y, y, self.bounds.height, tl, bl);
37 let right = inset_over(y, y, self.bounds.height, tr, br);
38 point.x >= self.bounds.x + left && point.x <= self.bounds.x + self.bounds.width - right
39 }
40
41 pub(crate) fn clip_rect(self, rect: Rect) -> Rect {
42 let rect = self.bounds.intersect(rect);
43 if rect.width <= 0.0 || rect.height <= 0.0 {
44 return rect;
45 }
46 let [tl, tr, br, bl] = self.radii;
47 let x = rect.x - self.bounds.x;
48 let y = rect.y - self.bounds.y;
49 let left = inset_over(y, y + rect.height, self.bounds.height, tl, bl);
50 let right = inset_over(y, y + rect.height, self.bounds.height, tr, br);
51 let top = inset_over(x, x + rect.width, self.bounds.width, tl, tr);
52 let bottom = inset_over(x, x + rect.width, self.bounds.width, bl, br);
53 rect.intersect(Rect::new(
54 self.bounds.x + left,
55 self.bounds.y + top,
56 self.bounds.width - left - right,
57 self.bounds.height - top - bottom,
58 ))
59 }
60}
61
62fn inset_over(start: f32, end: f32, extent: f32, before: f32, after: f32) -> f32 {
64 let (radius, distance) = if end < before {
65 (before, before - end)
66 } else if start > extent - after {
67 (after, start - (extent - after))
68 } else {
69 return 0.0;
70 };
71 radius - (radius * radius - distance * distance).max(0.0).sqrt()
72}
73
74#[derive(Debug, PartialEq)]
75pub(crate) struct ClipChain {
76 pub(crate) shape: RoundedClip,
77 pub(crate) parent: Option<Arc<Self>>,
78 pub(crate) space: Option<Arc<TransformSpace>>,
79}
80
81impl ClipChain {
82 pub(crate) fn contains(&self, window: Point) -> bool {
83 let inverse = match &self.space {
84 Some(space) => space.inverse,
85 None => Some(Affine::IDENTITY),
86 };
87 inverse.is_some_and(|inverse| self.shape.contains(inverse.map_point(window)))
88 && self
89 .parent
90 .as_deref()
91 .is_none_or(|parent| parent.contains(window))
92 }
93
94 pub(crate) fn clip_rect(&self, rect: Rect, world: Affine, inverse: Affine) -> Rect {
95 let rect = match &self.space {
96 None if world == Affine::IDENTITY => self.shape.clip_rect(rect),
97 Some(space) if space.world == world => self.shape.clip_rect(rect),
98 space => {
99 let (clip_world, clip_inverse) = space
100 .as_ref()
101 .map_or((Affine::IDENTITY, Some(Affine::IDENTITY)), |space| {
102 (space.world, space.inverse)
103 });
104 let Some(clip_inverse) = clip_inverse else {
105 return Rect::default();
106 };
107 let inside = self
108 .shape
109 .clip_rect(world.then(clip_inverse).map_rect(rect));
110 rect.intersect(clip_world.then(inverse).map_rect(inside))
111 }
112 };
113 self.parent
114 .as_deref()
115 .map_or(rect, |parent| parent.clip_rect(rect, world, inverse))
116 }
117}
118
119pub fn corner_radii(node: &Node, bounds: Rect) -> [f32; 4] {
120 let radius = (node.kind == "RoundButton")
121 .then(|| node.length("radius"))
122 .flatten()
123 .or_else(|| node.length("border-radius"))
124 .unwrap_or(if node.kind == "Dialog" {
125 12.0
126 } else if is_pane(node.kind) {
127 8.0
128 } else if node.kind == "RoundButton" {
129 bounds.width.min(bounds.height) * 0.5
130 } else if is_button(node.kind) || is_text_input(node.kind) {
131 7.0
132 } else {
133 0.0
134 });
135 let radii = [
136 "border-top-left-radius",
137 "border-top-right-radius",
138 "border-bottom-right-radius",
139 "border-bottom-left-radius",
140 ]
141 .map(|name| node.length(name).unwrap_or(radius).max(0.0));
142 normalize_radii(bounds, radii)
143}
144
145pub fn normalize_radii(bounds: Rect, radii: [f32; 4]) -> [f32; 4] {
146 let [tl, tr, br, bl] = radii;
147 let scale = [
148 (bounds.width, tl + tr),
149 (bounds.width, bl + br),
150 (bounds.height, tl + bl),
151 (bounds.height, tr + br),
152 ]
153 .into_iter()
154 .filter(|(_, sum)| *sum > 0.0)
155 .fold(1.0_f32, |scale, (side, sum)| scale.min(side / sum));
156 radii.map(|radius| radius * scale)
157}