1use std::any::Any;
4use std::fmt::{Debug, Formatter, Result as FmtResult};
5use std::rc::Rc;
6
7use waft_embedder::{Canvas, Paint, Path, PathBuilder, rrect_radii_elliptical};
8use waft_embedder::{Offset, RRect, Radius, Rect, clamp_double, lerp_double};
9
10use crate::basic_types::TextDirection;
11use crate::borders::{
12 BorderSide, BorderStyle, OutlinedBorder, OutlinedBorderRef, ShapeBorder, ShapeBorderRef,
13 outlined_border_dimensions,
14};
15use crate::draw::draw_oval;
16use crate::edge_insets::EdgeInsetsGeometry;
17
18#[derive(Clone, Copy, PartialEq)]
32pub struct CircleBorder {
33 pub side: BorderSide,
35 pub eccentricity: f64,
41}
42
43impl CircleBorder {
44 pub fn new(side: BorderSide, eccentricity: f64) -> CircleBorder {
46 debug_assert!(
47 eccentricity >= 0.0,
48 "The eccentricity argument {eccentricity} is not greater than or equal to zero."
49 );
50 debug_assert!(
51 eccentricity <= 1.0,
52 "The eccentricity argument {eccentricity} is not less than or equal to one."
53 );
54 CircleBorder { side, eccentricity }
55 }
56
57 pub fn scale(&self, t: f64) -> CircleBorder {
59 CircleBorder::new(self.side.scale(t), self.eccentricity)
60 }
61
62 pub fn copy_with(&self, side: Option<BorderSide>, eccentricity: Option<f64>) -> CircleBorder {
65 CircleBorder::new(
66 side.unwrap_or(self.side),
67 eccentricity.unwrap_or(self.eccentricity),
68 )
69 }
70
71 fn adjust_rect(self, rect: Rect) -> Rect {
72 if self.eccentricity == 0.0 || rect.width() == rect.height() {
73 return Rect::from_circle(rect.center(), rect.shortest_side() / 2.0);
74 }
75 if rect.width() < rect.height() {
76 let delta = (1.0 - self.eccentricity) * (rect.height() - rect.width()) / 2.0;
77 Rect::from_ltrb(rect.left, rect.top + delta, rect.right, rect.bottom - delta)
78 } else {
79 let delta = (1.0 - self.eccentricity) * (rect.width() - rect.height()) / 2.0;
80 Rect::from_ltrb(rect.left + delta, rect.top, rect.right - delta, rect.bottom)
81 }
82 }
83}
84
85impl Default for CircleBorder {
86 fn default() -> CircleBorder {
87 CircleBorder::new(BorderSide::NONE, 0.0)
88 }
89}
90
91impl ShapeBorder for CircleBorder {
92 fn dimensions(&self) -> EdgeInsetsGeometry {
93 outlined_border_dimensions(self.side)
94 }
95
96 fn scale(&self, t: f64) -> ShapeBorderRef {
97 ShapeBorderRef::new(CircleBorder::scale(self, t))
98 }
99
100 fn lerp_from(&self, a: Option<&dyn ShapeBorder>, t: f64) -> Option<ShapeBorderRef> {
101 if let Some(a) = a.and_then(|a| a.as_any().downcast_ref::<CircleBorder>()) {
102 Some(ShapeBorderRef::new(CircleBorder::new(
103 BorderSide::lerp(a.side, self.side, t),
104 clamp_double(
105 lerp_double(Some(a.eccentricity), Some(self.eccentricity), t).unwrap(),
106 0.0,
107 1.0,
108 ),
109 )))
110 } else if a.is_none() {
111 Some(ShapeBorder::scale(self, t))
112 } else {
113 None
114 }
115 }
116
117 fn lerp_to(&self, b: Option<&dyn ShapeBorder>, t: f64) -> Option<ShapeBorderRef> {
118 if let Some(b) = b.and_then(|b| b.as_any().downcast_ref::<CircleBorder>()) {
119 Some(ShapeBorderRef::new(CircleBorder::new(
120 BorderSide::lerp(self.side, b.side, t),
121 clamp_double(
122 lerp_double(Some(self.eccentricity), Some(b.eccentricity), t).unwrap(),
123 0.0,
124 1.0,
125 ),
126 )))
127 } else if b.is_none() {
128 Some(ShapeBorder::scale(self, 1.0 - t))
129 } else {
130 None
131 }
132 }
133
134 fn get_inner_path(&self, rect: Rect, _text_direction: Option<TextDirection>) -> Path {
135 oval_path(self.adjust_rect(rect).deflate(self.side.stroke_inset()))
136 }
137
138 fn get_outer_path(&self, rect: Rect, _text_direction: Option<TextDirection>) -> Path {
139 oval_path(self.adjust_rect(rect))
140 }
141
142 fn hit_test(
143 &self,
144 rect: Rect,
145 position: Offset,
146 _text_direction: Option<TextDirection>,
147 ) -> bool {
148 let adjusted_rect = self.adjust_rect(rect);
149 RRect::from_rect_and_radius(
150 adjusted_rect,
151 Radius::elliptical(adjusted_rect.width() / 2.0, adjusted_rect.height() / 2.0),
152 )
153 .contains(position)
154 }
155
156 fn paint_interior(
157 &self,
158 canvas: &mut Canvas,
159 rect: Rect,
160 paint: &Paint,
161 _text_direction: Option<TextDirection>,
162 ) {
163 if self.eccentricity == 0.0 {
164 canvas.draw_circle(rect.center(), (rect.shortest_side() / 2.0) as f32, paint);
165 } else {
166 draw_oval(canvas, self.adjust_rect(rect), paint);
167 }
168 }
169
170 fn prefer_paint_interior(&self) -> bool {
171 true
172 }
173
174 fn paint(&self, canvas: &mut Canvas, rect: Rect, _text_direction: Option<TextDirection>) {
175 match self.side.style {
176 BorderStyle::None => {}
177 BorderStyle::Solid => {
178 if self.eccentricity == 0.0 {
179 canvas.draw_circle(
180 rect.center(),
181 ((rect.shortest_side() + self.side.stroke_offset()) / 2.0) as f32,
182 &self.side.to_paint(),
183 );
184 } else {
185 let border_rect = self.adjust_rect(rect);
186 draw_oval(
187 canvas,
188 border_rect.inflate(self.side.stroke_offset() / 2.0),
189 &self.side.to_paint(),
190 );
191 }
192 }
193 }
194 }
195
196 fn as_any(&self) -> &dyn Any {
197 self
198 }
199
200 fn as_outlined_border(&self) -> Option<&dyn OutlinedBorder> {
201 Some(self)
202 }
203
204 fn into_outlined_border(self: Rc<Self>) -> Option<Rc<dyn OutlinedBorder>> {
205 Some(self)
206 }
207
208 fn eq_shape(&self, other: &dyn ShapeBorder) -> bool {
209 other
210 .as_any()
211 .downcast_ref::<CircleBorder>()
212 .is_some_and(|other| other == self)
213 }
214}
215
216impl OutlinedBorder for CircleBorder {
217 fn side(&self) -> BorderSide {
218 self.side
219 }
220
221 fn copy_with(&self, side: Option<BorderSide>) -> OutlinedBorderRef {
222 OutlinedBorderRef::new(CircleBorder::copy_with(self, side, None))
223 }
224}
225
226impl Debug for CircleBorder {
227 fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
228 if self.eccentricity != 0.0 {
229 write!(
230 f,
231 "CircleBorder({:?}, eccentricity: {})",
232 self.side, self.eccentricity
233 )
234 } else {
235 write!(f, "CircleBorder({:?})", self.side)
236 }
237 }
238}
239
240pub(crate) fn oval_path(rect: Rect) -> Path {
241 let mut path = PathBuilder::new();
242 let rx = (rect.width() / 2.0) as f32;
243 let ry = (rect.height() / 2.0) as f32;
244 path.rrect_radii_elliptical(rect, [[rx, ry]; 4]);
245 path.build().into()
246}
247
248pub(crate) fn rrect_path(rrect: RRect) -> Path {
249 let mut path = PathBuilder::new();
250 path.rrect_radii_elliptical(rrect.outer_rect(), rrect_radii_elliptical(rrect));
251 path.build().into()
252}
253
254#[cfg(test)]
255mod tests {
256 use super::*;
257 use waft_embedder::Color;
258
259 fn side_width(width: f64) -> BorderSide {
260 BorderSide {
261 width,
262 ..BorderSide::default()
263 }
264 }
265
266 fn contains(path: &Path, x: f64, y: f64) -> bool {
267 path.contains(Offset::new(x, y))
268 }
269
270 #[test]
271 fn defaults() {
272 let border = CircleBorder::default();
273 assert_eq!(border.side, BorderSide::NONE);
274 assert_eq!(border.eccentricity, 0.0);
275 }
276
277 #[test]
278 fn copy_with_eq() {
279 assert_eq!(
280 CircleBorder::default(),
281 CircleBorder::default().copy_with(None, None)
282 );
283 assert_eq!(
284 CircleBorder::new(BorderSide::NONE, 0.5),
285 CircleBorder::default().copy_with(None, Some(0.5))
286 );
287 let side = BorderSide {
288 width: 10.0,
289 color: Color::new(0xFF123456),
290 ..BorderSide::default()
291 };
292 assert_eq!(
293 CircleBorder::default().copy_with(Some(side), None),
294 CircleBorder::new(side, 0.0)
295 );
296 }
297
298 #[test]
299 fn scale_lerp_dimensions() {
300 let c10 = CircleBorder::new(side_width(10.0), 0.0);
301 let c15 = CircleBorder::new(side_width(15.0), 0.0);
302 let c20 = CircleBorder::new(side_width(20.0), 0.0);
303 assert_eq!(c10.dimensions(), EdgeInsetsGeometry::all(10.0));
304 assert_eq!(c10.scale(2.0), c20);
305 assert_eq!(c20.scale(0.5), c10);
306 let as10 = ShapeBorderRef::new(c10);
307 let as20 = ShapeBorderRef::new(c20);
308 assert!(
309 ShapeBorderRef::lerp(Some(&as10), Some(&as20), 0.0)
310 .unwrap()
311 .eq_shape(&c10)
312 );
313 assert!(
314 ShapeBorderRef::lerp(Some(&as10), Some(&as20), 0.5)
315 .unwrap()
316 .eq_shape(&c15)
317 );
318 assert!(
319 ShapeBorderRef::lerp(Some(&as10), Some(&as20), 1.0)
320 .unwrap()
321 .eq_shape(&c20)
322 );
323 }
324
325 #[test]
326 fn paths_for_square_and_eccentric_oval() {
327 let circle_rect = Rect::from_ltwh(50.0, 0.0, 100.0, 100.0);
328 let rect = Rect::from_ltwh(0.0, 0.0, 200.0, 100.0);
329 let bounds = CircleBorder::default()
330 .get_outer_path(rect, None)
331 .as_valo()
332 .bounds();
333 assert!((bounds.x - circle_rect.left as f32).abs() < 0.01);
334 assert!((bounds.y - circle_rect.top as f32).abs() < 0.01);
335 assert!((bounds.width - circle_rect.width() as f32).abs() < 0.01);
336 assert!((bounds.height - circle_rect.height() as f32).abs() < 0.01);
337
338 let oval = CircleBorder::new(BorderSide::NONE, 1.0);
339 let outer = oval.get_outer_path(rect, None).as_valo().bounds();
340 assert!((outer.width - 200.0).abs() < 0.01);
341 assert!((outer.height - 100.0).abs() < 0.01);
342 }
343
344 #[test]
345 fn hit_test_uses_the_adjusted_circle() {
346 let border = CircleBorder::default();
347 let rect = Rect::from_ltwh(0.0, 0.0, 100.0, 20.0);
348 assert!(border.hit_test(rect, Offset::new(50.0, 10.0), None));
349 assert!(!border.hit_test(rect, Offset::new(1.0, 1.0), None));
350 assert!(!contains(&border.get_outer_path(rect, None), 30.0, 10.0));
351 assert!(contains(&border.get_outer_path(rect, None), 50.0, 10.0));
352 }
353}