Skip to main content

nsh_core/geometry/
shapes.rs

1//! Shapes generic over their unit, as GPUI's: [`Point`], [`Size`],
2//! [`Bounds`], [`Edges`] and [`Corners`], and the [`Axis`] they are read
3//! [`Along`].
4
5use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Sub, SubAssign};
6
7use serde::{Deserialize, Serialize};
8
9use super::units::{DevicePixels, Half, IsZero, Points, ScaledPixels};
10
11/// A direction in two dimensions (`"vertical"` or `"horizontal"` in JS:
12/// a prop that names one, a split's or a resizable group's).
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, crate::element::PropEnum)]
14pub enum Axis {
15    /// Up and down.
16    Vertical,
17    /// Left and right.
18    Horizontal,
19}
20
21impl Axis {
22    /// The other axis.
23    #[inline]
24    pub fn invert(self) -> Self {
25        match self {
26            Axis::Vertical => Axis::Horizontal,
27            Axis::Horizontal => Axis::Vertical,
28        }
29    }
30}
31
32/// A two-dimensional value read or changed along one [`Axis`].
33pub trait Along {
34    /// The value of one dimension.
35    type Unit;
36
37    /// The value along `axis`.
38    fn along(&self, axis: Axis) -> Self::Unit;
39
40    /// The value with `f` applied to the dimension along `axis`.
41    fn apply_along(&self, axis: Axis, f: impl FnOnce(Self::Unit) -> Self::Unit) -> Self;
42}
43
44/// A point: `x` to the right, `y` down, from the origin of what it is
45/// relative to.
46#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
47#[repr(C)]
48pub struct Point<T> {
49    /// The horizontal coordinate.
50    pub x: T,
51    /// The vertical coordinate.
52    pub y: T,
53}
54
55/// A point at `x`, `y`.
56#[inline]
57pub const fn point<T>(x: T, y: T) -> Point<T> {
58    Point { x, y }
59}
60
61impl<T> Point<T> {
62    /// A point at `x`, `y`.
63    #[inline]
64    pub const fn new(x: T, y: T) -> Self {
65        Self { x, y }
66    }
67}
68
69impl<T: Clone> Point<T> {
70    /// Both coordinates converted by `f`.
71    #[inline]
72    pub fn map<U>(&self, f: impl Fn(T) -> U) -> Point<U> {
73        Point {
74            x: f(self.x.clone()),
75            y: f(self.y.clone()),
76        }
77    }
78}
79
80impl<T: Clone + PartialOrd> Point<T> {
81    /// The larger of each coordinate of both points.
82    pub fn max(&self, other: &Self) -> Self {
83        Point {
84            x: if self.x >= other.x { &self.x } else { &other.x }.clone(),
85            y: if self.y >= other.y { &self.y } else { &other.y }.clone(),
86        }
87    }
88
89    /// The smaller of each coordinate of both points.
90    pub fn min(&self, other: &Self) -> Self {
91        Point {
92            x: if self.x <= other.x { &self.x } else { &other.x }.clone(),
93            y: if self.y <= other.y { &self.y } else { &other.y }.clone(),
94        }
95    }
96
97    /// The point within the box between `min` and `max`.
98    pub fn clamp(&self, min: &Self, max: &Self) -> Self {
99        self.max(min).min(max)
100    }
101}
102
103impl<T: Clone + Sub<Output = T>> Point<T> {
104    /// The point relative to `origin`.
105    #[inline]
106    pub fn relative_to(&self, origin: &Point<T>) -> Point<T> {
107        Point {
108            x: self.x.clone() - origin.x.clone(),
109            y: self.y.clone() - origin.y.clone(),
110        }
111    }
112}
113
114impl<T: Clone> Along for Point<T> {
115    type Unit = T;
116
117    #[inline]
118    fn along(&self, axis: Axis) -> T {
119        match axis {
120            Axis::Horizontal => self.x.clone(),
121            Axis::Vertical => self.y.clone(),
122        }
123    }
124
125    fn apply_along(&self, axis: Axis, f: impl FnOnce(T) -> T) -> Self {
126        match axis {
127            Axis::Horizontal => Point::new(f(self.x.clone()), self.y.clone()),
128            Axis::Vertical => Point::new(self.x.clone(), f(self.y.clone())),
129        }
130    }
131}
132
133impl Point<Points> {
134    /// Scaled to device pixels at `factor` (unrounded).
135    #[inline]
136    pub fn scale(&self, factor: f32) -> Point<ScaledPixels> {
137        Point::new(self.x.scale(factor), self.y.scale(factor))
138    }
139
140    /// The distance from the origin.
141    #[inline]
142    pub fn magnitude(&self) -> f64 {
143        (self.x.0 as f64).hypot(self.y.0 as f64)
144    }
145}
146
147impl<T: Add<Output = T>> Add for Point<T> {
148    type Output = Self;
149    #[inline]
150    fn add(self, rhs: Self) -> Self {
151        Point::new(self.x + rhs.x, self.y + rhs.y)
152    }
153}
154
155impl<T: Sub<Output = T>> Sub for Point<T> {
156    type Output = Self;
157    #[inline]
158    fn sub(self, rhs: Self) -> Self {
159        Point::new(self.x - rhs.x, self.y - rhs.y)
160    }
161}
162
163impl<T: AddAssign> AddAssign for Point<T> {
164    #[inline]
165    fn add_assign(&mut self, rhs: Self) {
166        self.x += rhs.x;
167        self.y += rhs.y;
168    }
169}
170
171impl<T: SubAssign> SubAssign for Point<T> {
172    #[inline]
173    fn sub_assign(&mut self, rhs: Self) {
174        self.x -= rhs.x;
175        self.y -= rhs.y;
176    }
177}
178
179impl<T: Neg<Output = T>> Neg for Point<T> {
180    type Output = Self;
181    #[inline]
182    fn neg(self) -> Self {
183        Point::new(-self.x, -self.y)
184    }
185}
186
187impl<T: Mul<S, Output = T>, S: Clone> Mul<S> for Point<T> {
188    type Output = Self;
189    #[inline]
190    fn mul(self, rhs: S) -> Self {
191        Point::new(self.x * rhs.clone(), self.y * rhs)
192    }
193}
194
195impl<T: MulAssign<S>, S: Clone> MulAssign<S> for Point<T> {
196    #[inline]
197    fn mul_assign(&mut self, rhs: S) {
198        self.x *= rhs.clone();
199        self.y *= rhs;
200    }
201}
202
203impl<T: Div<S, Output = T>, S: Clone> Div<S> for Point<T> {
204    type Output = Self;
205    #[inline]
206    fn div(self, rhs: S) -> Self {
207        Point::new(self.x / rhs.clone(), self.y / rhs)
208    }
209}
210
211impl<T: IsZero> IsZero for Point<T> {
212    fn is_zero(&self) -> bool {
213        self.x.is_zero() && self.y.is_zero()
214    }
215}
216
217impl<T> From<Size<T>> for Point<T> {
218    #[inline]
219    fn from(s: Size<T>) -> Self {
220        Point::new(s.width, s.height)
221    }
222}
223
224/// A width and a height.
225#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
226#[repr(C)]
227pub struct Size<T> {
228    /// The width.
229    pub width: T,
230    /// The height.
231    pub height: T,
232}
233
234/// A size of `width` × `height`.
235#[inline]
236pub const fn size<T>(width: T, height: T) -> Size<T> {
237    Size { width, height }
238}
239
240impl<T> Size<T> {
241    /// A size of `width` × `height`.
242    #[inline]
243    pub const fn new(width: T, height: T) -> Self {
244        Self { width, height }
245    }
246}
247
248impl<T: Clone> Size<T> {
249    /// Both dimensions converted by `f`.
250    #[inline]
251    pub fn map<U>(&self, f: impl Fn(T) -> U) -> Size<U> {
252        Size {
253            width: f(self.width.clone()),
254            height: f(self.height.clone()),
255        }
256    }
257}
258
259impl<T: Clone + PartialOrd> Size<T> {
260    /// The larger of each dimension of both sizes.
261    pub fn max(&self, other: &Self) -> Self {
262        Size {
263            width: if self.width >= other.width {
264                &self.width
265            } else {
266                &other.width
267            }
268            .clone(),
269            height: if self.height >= other.height {
270                &self.height
271            } else {
272                &other.height
273            }
274            .clone(),
275        }
276    }
277
278    /// The smaller of each dimension of both sizes.
279    pub fn min(&self, other: &Self) -> Self {
280        Size {
281            width: if self.width <= other.width {
282                &self.width
283            } else {
284                &other.width
285            }
286            .clone(),
287            height: if self.height <= other.height {
288                &self.height
289            } else {
290                &other.height
291            }
292            .clone(),
293        }
294    }
295}
296
297impl<T: Half> Size<T> {
298    /// The center of a box of this size at the origin.
299    #[inline]
300    pub fn center(&self) -> Point<T> {
301        Point::new(self.width.half(), self.height.half())
302    }
303}
304
305impl<T: Clone> Along for Size<T> {
306    type Unit = T;
307
308    #[inline]
309    fn along(&self, axis: Axis) -> T {
310        match axis {
311            Axis::Horizontal => self.width.clone(),
312            Axis::Vertical => self.height.clone(),
313        }
314    }
315
316    fn apply_along(&self, axis: Axis, f: impl FnOnce(T) -> T) -> Self {
317        match axis {
318            Axis::Horizontal => Size::new(f(self.width.clone()), self.height.clone()),
319            Axis::Vertical => Size::new(self.width.clone(), f(self.height.clone())),
320        }
321    }
322}
323
324impl Size<Points> {
325    /// Scaled to device pixels at `factor` (unrounded).
326    #[inline]
327    pub fn scale(&self, factor: f32) -> Size<ScaledPixels> {
328        Size::new(self.width.scale(factor), self.height.scale(factor))
329    }
330
331    /// In whole device pixels at `scale_factor`.
332    #[inline]
333    pub fn to_device_pixels(self, scale_factor: f32) -> Size<DevicePixels> {
334        Size::new(
335            self.width.to_device_pixels(scale_factor),
336            self.height.to_device_pixels(scale_factor),
337        )
338    }
339}
340
341impl Size<DevicePixels> {
342    /// In points at `scale_factor` device pixels per point.
343    #[inline]
344    pub fn to_points(self, scale_factor: f32) -> Size<Points> {
345        Size::new(
346            self.width.to_points(scale_factor),
347            self.height.to_points(scale_factor),
348        )
349    }
350}
351
352impl<T: Add<Output = T>> Add for Size<T> {
353    type Output = Self;
354    #[inline]
355    fn add(self, rhs: Self) -> Self {
356        Size::new(self.width + rhs.width, self.height + rhs.height)
357    }
358}
359
360impl<T: Sub<Output = T>> Sub for Size<T> {
361    type Output = Self;
362    #[inline]
363    fn sub(self, rhs: Self) -> Self {
364        Size::new(self.width - rhs.width, self.height - rhs.height)
365    }
366}
367
368impl<T: Mul<S, Output = T>, S: Clone> Mul<S> for Size<T> {
369    type Output = Self;
370    #[inline]
371    fn mul(self, rhs: S) -> Self {
372        Size::new(self.width * rhs.clone(), self.height * rhs)
373    }
374}
375
376impl<T: MulAssign<S>, S: Clone> MulAssign<S> for Size<T> {
377    #[inline]
378    fn mul_assign(&mut self, rhs: S) {
379        self.width *= rhs.clone();
380        self.height *= rhs;
381    }
382}
383
384impl<T: Div<S, Output = T>, S: Clone> Div<S> for Size<T> {
385    type Output = Self;
386    #[inline]
387    fn div(self, rhs: S) -> Self {
388        Size::new(self.width / rhs.clone(), self.height / rhs)
389    }
390}
391
392impl<T: IsZero> IsZero for Size<T> {
393    /// Either dimension is zero (the size covers no area), as GPUI's.
394    fn is_zero(&self) -> bool {
395        self.width.is_zero() || self.height.is_zero()
396    }
397}
398
399impl<T> From<Point<T>> for Size<T> {
400    #[inline]
401    fn from(p: Point<T>) -> Self {
402        Size::new(p.x, p.y)
403    }
404}
405
406/// A rectangle: its top left corner (`origin`) and its `size`. A view's
407/// frame is its bounds relative to its parent.
408#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
409#[repr(C)]
410pub struct Bounds<T> {
411    /// The top left corner.
412    pub origin: Point<T>,
413    /// The size.
414    pub size: Size<T>,
415}
416
417/// Bounds at `origin` of `size`.
418#[inline]
419pub const fn bounds<T>(origin: Point<T>, size: Size<T>) -> Bounds<T> {
420    Bounds { origin, size }
421}
422
423impl<T> Bounds<T> {
424    /// Bounds at `origin` of `size`.
425    #[inline]
426    pub const fn new(origin: Point<T>, size: Size<T>) -> Self {
427        Self { origin, size }
428    }
429}
430
431impl<T: Clone> Bounds<T> {
432    /// Every coordinate converted by `f`.
433    #[inline]
434    pub fn map<U>(&self, f: impl Fn(T) -> U) -> Bounds<U> {
435        Bounds {
436            origin: self.origin.map(&f),
437            size: self.size.map(&f),
438        }
439    }
440
441    /// The origin's coordinates converted by `f`, the size kept.
442    #[inline]
443    pub fn map_origin(self, f: impl Fn(T) -> T) -> Bounds<T> {
444        Bounds {
445            origin: self.origin.map(f),
446            size: self.size,
447        }
448    }
449
450    /// The size's dimensions converted by `f`, the origin kept.
451    #[inline]
452    pub fn map_size(self, f: impl Fn(T) -> T) -> Bounds<T> {
453        Bounds {
454            origin: self.origin,
455            size: self.size.map(f),
456        }
457    }
458}
459
460impl<T: Clone + Add<Output = T> + Sub<Output = T>> Bounds<T> {
461    /// The bounds between two corners.
462    #[inline]
463    pub fn from_corners(top_left: Point<T>, bottom_right: Point<T>) -> Self {
464        let size = Size::new(
465            bottom_right.x - top_left.x.clone(),
466            bottom_right.y - top_left.y.clone(),
467        );
468        Bounds {
469            origin: top_left,
470            size,
471        }
472    }
473
474    /// The top edge.
475    #[inline]
476    pub fn top(&self) -> T {
477        self.origin.y.clone()
478    }
479
480    /// The bottom edge.
481    #[inline]
482    pub fn bottom(&self) -> T {
483        self.origin.y.clone() + self.size.height.clone()
484    }
485
486    /// The left edge.
487    #[inline]
488    pub fn left(&self) -> T {
489        self.origin.x.clone()
490    }
491
492    /// The right edge.
493    #[inline]
494    pub fn right(&self) -> T {
495        self.origin.x.clone() + self.size.width.clone()
496    }
497
498    /// The top right corner.
499    #[inline]
500    pub fn top_right(&self) -> Point<T> {
501        Point::new(self.right(), self.top())
502    }
503
504    /// The bottom right corner.
505    #[inline]
506    pub fn bottom_right(&self) -> Point<T> {
507        Point::new(self.right(), self.bottom())
508    }
509
510    /// The bottom left corner.
511    #[inline]
512    pub fn bottom_left(&self) -> Point<T> {
513        Point::new(self.left(), self.bottom())
514    }
515
516    /// The bounds grown by `amount` on every side.
517    #[must_use]
518    pub fn dilate(&self, amount: T) -> Self {
519        let double = amount.clone() + amount.clone();
520        Bounds {
521            origin: self.origin.clone() - point(amount.clone(), amount),
522            size: self.size.clone() + size(double.clone(), double),
523        }
524    }
525
526    /// The bounds grown by each of `amount`'s edges.
527    #[must_use]
528    pub fn extend(&self, amount: Edges<T>) -> Self {
529        Bounds {
530            origin: self.origin.clone() - point(amount.left.clone(), amount.top.clone()),
531            size: self.size.clone() + size(amount.left + amount.right, amount.top + amount.bottom),
532        }
533    }
534
535    /// The space between these bounds and the `outer` bounds around them.
536    pub fn space_within(&self, outer: &Self) -> Edges<T> {
537        Edges {
538            top: self.top() - outer.top(),
539            right: outer.right() - self.right(),
540            bottom: outer.bottom() - self.bottom(),
541            left: self.left() - outer.left(),
542        }
543    }
544}
545
546impl<T: Clone + Add<Output = T> + Sub<Output = T> + Neg<Output = T>> Bounds<T> {
547    /// The bounds shrunk by `amount` on every side (`dilate` of
548    /// `-amount`).
549    #[must_use]
550    pub fn inset(&self, amount: T) -> Self {
551        self.dilate(-amount)
552    }
553}
554
555impl<T: Clone + Add<Output = T> + Sub<Output = T> + Half> Bounds<T> {
556    /// Bounds of `size` centered at `center`.
557    pub fn centered_at(center: Point<T>, size: Size<T>) -> Self {
558        let origin = Point::new(center.x - size.width.half(), center.y - size.height.half());
559        Bounds { origin, size }
560    }
561
562    /// The center.
563    #[inline]
564    pub fn center(&self) -> Point<T> {
565        Point::new(
566            self.origin.x.clone() + self.size.width.half(),
567            self.origin.y.clone() + self.size.height.half(),
568        )
569    }
570
571    /// The width plus the height.
572    #[inline]
573    pub fn half_perimeter(&self) -> T {
574        self.size.width.clone() + self.size.height.clone()
575    }
576}
577
578impl<T: Clone + Add<Output = T> + Sub<Output = T> + PartialOrd> Bounds<T> {
579    /// Whether `point` is inside: on the left or top edge is, on the right
580    /// or bottom one is not.
581    #[inline]
582    pub fn contains(&self, point: &Point<T>) -> bool {
583        point.x >= self.left()
584            && point.x < self.right()
585            && point.y >= self.top()
586            && point.y < self.bottom()
587    }
588
589    /// Whether the bounds lie within `other`.
590    pub fn is_contained_within(&self, other: &Self) -> bool {
591        self.left() >= other.left()
592            && self.right() <= other.right()
593            && self.top() >= other.top()
594            && self.bottom() <= other.bottom()
595    }
596
597    /// Whether the two bounds overlap.
598    #[inline]
599    pub fn intersects(&self, other: &Self) -> bool {
600        let my_lower_right = self.bottom_right();
601        let their_lower_right = other.bottom_right();
602        self.origin.x < their_lower_right.x
603            && my_lower_right.x > other.origin.x
604            && self.origin.y < their_lower_right.y
605            && my_lower_right.y > other.origin.y
606    }
607
608    /// The overlap of two bounds (of no size when they do not overlap).
609    pub fn intersect(&self, other: &Self) -> Self {
610        let upper_left = self.origin.max(&other.origin);
611        let bottom_right = self
612            .bottom_right()
613            .min(&other.bottom_right())
614            .max(&upper_left);
615        Self::from_corners(upper_left, bottom_right)
616    }
617
618    /// The smallest bounds holding both.
619    pub fn union(&self, other: &Self) -> Self {
620        let top_left = self.origin.min(&other.origin);
621        let bottom_right = self.bottom_right().max(&other.bottom_right());
622        Bounds::from_corners(top_left, bottom_right)
623    }
624
625    /// `point` relative to the bounds' origin, if it is inside.
626    pub fn localize(&self, point: &Point<T>) -> Option<Point<T>> {
627        self.contains(point)
628            .then(|| point.relative_to(&self.origin))
629    }
630}
631
632impl<T: PartialOrd + Default> Bounds<T> {
633    /// Whether the bounds cover no area (a dimension zero or less).
634    #[inline]
635    pub fn is_empty(&self) -> bool {
636        !(self.size.width > T::default() && self.size.height > T::default())
637    }
638}
639
640impl Bounds<Points> {
641    /// Bounds that never compare equal to real ones (NaN): a new view's
642    /// first frame is sent whatever it is.
643    pub(crate) const UNSET: Self = Bounds {
644        origin: Point::new(Points(f32::NAN), Points(f32::NAN)),
645        size: Size::new(Points(f32::NAN), Points(f32::NAN)),
646    };
647
648    /// Scaled to device pixels at `factor` (unrounded).
649    #[inline]
650    pub fn scale(&self, factor: f32) -> Bounds<ScaledPixels> {
651        Bounds {
652            origin: self.origin.scale(factor),
653            size: self.size.scale(factor),
654        }
655    }
656
657    /// In whole device pixels at `scale_factor`.
658    #[inline]
659    pub fn to_device_pixels(self, scale_factor: f32) -> Bounds<DevicePixels> {
660        Bounds {
661            origin: self.origin.map(|p| p.to_device_pixels(scale_factor)),
662            size: self.size.to_device_pixels(scale_factor),
663        }
664    }
665}
666
667impl Bounds<DevicePixels> {
668    /// In points at `scale_factor` device pixels per point.
669    #[inline]
670    pub fn to_points(self, scale_factor: f32) -> Bounds<Points> {
671        Bounds {
672            origin: self.origin.map(|p| p.to_points(scale_factor)),
673            size: self.size.to_points(scale_factor),
674        }
675    }
676}
677
678/// The bounds moved by a point.
679impl<T: Add<Output = T>> Add<Point<T>> for Bounds<T> {
680    type Output = Self;
681    #[inline]
682    fn add(self, rhs: Point<T>) -> Self {
683        Bounds {
684            origin: self.origin + rhs,
685            size: self.size,
686        }
687    }
688}
689
690/// The bounds moved back by a point.
691impl<T: Sub<Output = T>> Sub<Point<T>> for Bounds<T> {
692    type Output = Self;
693    #[inline]
694    fn sub(self, rhs: Point<T>) -> Self {
695        Bounds {
696            origin: self.origin - rhs,
697            size: self.size,
698        }
699    }
700}
701
702impl<T: Mul<S, Output = T>, S: Clone> Mul<S> for Bounds<T> {
703    type Output = Self;
704    #[inline]
705    fn mul(self, rhs: S) -> Self {
706        Bounds {
707            origin: self.origin * rhs.clone(),
708            size: self.size * rhs,
709        }
710    }
711}
712
713impl<T: MulAssign<S>, S: Clone> MulAssign<S> for Bounds<T> {
714    #[inline]
715    fn mul_assign(&mut self, rhs: S) {
716        self.origin *= rhs.clone();
717        self.size *= rhs;
718    }
719}
720
721impl<T: Div<S, Output = T>, S: Clone> Div<S> for Bounds<T> {
722    type Output = Self;
723    #[inline]
724    fn div(self, rhs: S) -> Self {
725        Bounds {
726            origin: self.origin / rhs.clone(),
727            size: self.size / rhs,
728        }
729    }
730}
731
732impl<T: IsZero> IsZero for Bounds<T> {
733    fn is_zero(&self) -> bool {
734        self.size.is_zero()
735    }
736}
737
738/// A value for each edge of a box: insets, paddings, borders.
739#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
740#[repr(C)]
741pub struct Edges<T> {
742    /// The top edge.
743    pub top: T,
744    /// The right edge.
745    pub right: T,
746    /// The bottom edge.
747    pub bottom: T,
748    /// The left edge.
749    pub left: T,
750}
751
752impl<T: Clone> Edges<T> {
753    /// `value` on every edge.
754    #[inline]
755    pub fn all(value: T) -> Self {
756        Edges {
757            top: value.clone(),
758            right: value.clone(),
759            bottom: value.clone(),
760            left: value,
761        }
762    }
763
764    /// `[top, right, bottom, left]`, for toolkits that take sides by index.
765    #[inline]
766    pub fn to_array(&self) -> [T; 4] {
767        [
768            self.top.clone(),
769            self.right.clone(),
770            self.bottom.clone(),
771            self.left.clone(),
772        ]
773    }
774}
775
776impl<T> From<[T; 4]> for Edges<T> {
777    /// From `[top, right, bottom, left]`.
778    fn from([top, right, bottom, left]: [T; 4]) -> Self {
779        Edges {
780            top,
781            right,
782            bottom,
783            left,
784        }
785    }
786}
787
788impl<T> Edges<T> {
789    /// Every edge converted by `f`.
790    #[inline]
791    pub fn map<U>(&self, f: impl Fn(&T) -> U) -> Edges<U> {
792        Edges {
793            top: f(&self.top),
794            right: f(&self.right),
795            bottom: f(&self.bottom),
796            left: f(&self.left),
797        }
798    }
799
800    /// Whether `predicate` holds for any edge.
801    #[inline]
802    pub fn any(&self, predicate: impl Fn(&T) -> bool) -> bool {
803        predicate(&self.top)
804            || predicate(&self.right)
805            || predicate(&self.bottom)
806            || predicate(&self.left)
807    }
808}
809
810impl Edges<Points> {
811    /// Zero on every edge.
812    pub fn zero() -> Self {
813        Edges::all(Points::ZERO)
814    }
815
816    /// Scaled to device pixels at `factor` (unrounded).
817    pub fn scale(&self, factor: f32) -> Edges<ScaledPixels> {
818        self.map(|p| p.scale(factor))
819    }
820
821    /// The largest edge.
822    pub fn max(&self) -> Points {
823        self.top.max(self.right).max(self.bottom).max(self.left)
824    }
825}
826
827impl<T: Mul<S, Output = T> + Clone, S: Clone> Mul<S> for Edges<T> {
828    type Output = Self;
829    #[inline]
830    fn mul(self, rhs: S) -> Self {
831        Edges {
832            top: self.top * rhs.clone(),
833            right: self.right * rhs.clone(),
834            bottom: self.bottom * rhs.clone(),
835            left: self.left * rhs,
836        }
837    }
838}
839
840impl<T: IsZero> IsZero for Edges<T> {
841    fn is_zero(&self) -> bool {
842        self.top.is_zero() && self.right.is_zero() && self.bottom.is_zero() && self.left.is_zero()
843    }
844}
845
846impl From<f32> for Edges<Points> {
847    fn from(v: f32) -> Self {
848        Edges::all(Points(v))
849    }
850}
851
852impl From<Points> for Edges<Points> {
853    fn from(p: Points) -> Self {
854        Edges::all(p)
855    }
856}
857
858/// A value for each corner of a box: radii.
859#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
860#[repr(C)]
861pub struct Corners<T> {
862    /// The top left corner.
863    pub top_left: T,
864    /// The top right corner.
865    pub top_right: T,
866    /// The bottom right corner.
867    pub bottom_right: T,
868    /// The bottom left corner.
869    pub bottom_left: T,
870}
871
872impl<T: Clone> Corners<T> {
873    /// `value` at every corner.
874    #[inline]
875    pub fn all(value: T) -> Self {
876        Corners {
877            top_left: value.clone(),
878            top_right: value.clone(),
879            bottom_right: value.clone(),
880            bottom_left: value,
881        }
882    }
883
884    /// `[top_left, top_right, bottom_right, bottom_left]`, clockwise from
885    /// the top left, for toolkits that take corners by index.
886    #[inline]
887    pub fn to_array(&self) -> [T; 4] {
888        [
889            self.top_left.clone(),
890            self.top_right.clone(),
891            self.bottom_right.clone(),
892            self.bottom_left.clone(),
893        ]
894    }
895}
896
897impl<T> From<[T; 4]> for Corners<T> {
898    /// From `[top_left, top_right, bottom_right, bottom_left]`.
899    fn from([top_left, top_right, bottom_right, bottom_left]: [T; 4]) -> Self {
900        Corners {
901            top_left,
902            top_right,
903            bottom_right,
904            bottom_left,
905        }
906    }
907}
908
909impl<T> Corners<T> {
910    /// Every corner converted by `f`.
911    #[inline]
912    pub fn map<U>(&self, f: impl Fn(&T) -> U) -> Corners<U> {
913        Corners {
914            top_left: f(&self.top_left),
915            top_right: f(&self.top_right),
916            bottom_right: f(&self.bottom_right),
917            bottom_left: f(&self.bottom_left),
918        }
919    }
920
921    /// Whether `predicate` holds for any corner.
922    #[inline]
923    pub fn any(&self, predicate: impl Fn(&T) -> bool) -> bool {
924        predicate(&self.top_left)
925            || predicate(&self.top_right)
926            || predicate(&self.bottom_right)
927            || predicate(&self.bottom_left)
928    }
929}
930
931impl Corners<Points> {
932    /// Scaled to device pixels at `factor` (unrounded).
933    pub fn scale(&self, factor: f32) -> Corners<ScaledPixels> {
934        self.map(|p| p.scale(factor))
935    }
936
937    /// The largest radius.
938    pub fn max(&self) -> Points {
939        self.top_left
940            .max(self.top_right)
941            .max(self.bottom_right)
942            .max(self.bottom_left)
943    }
944
945    /// Each radius at most half the shorter side of a box of `size`, as
946    /// GPUI clamps a quad's.
947    pub fn clamp_radii_for_quad_size(self, size: Size<Points>) -> Corners<Points> {
948        let max = size.width.min(size.height) / 2.0;
949        self.map(|r| r.min(max))
950    }
951}
952
953impl<T: Mul<S, Output = T> + Clone, S: Clone> Mul<S> for Corners<T> {
954    type Output = Self;
955    #[inline]
956    fn mul(self, rhs: S) -> Self {
957        Corners {
958            top_left: self.top_left * rhs.clone(),
959            top_right: self.top_right * rhs.clone(),
960            bottom_right: self.bottom_right * rhs.clone(),
961            bottom_left: self.bottom_left * rhs,
962        }
963    }
964}
965
966impl<T: IsZero> IsZero for Corners<T> {
967    fn is_zero(&self) -> bool {
968        self.top_left.is_zero()
969            && self.top_right.is_zero()
970            && self.bottom_right.is_zero()
971            && self.bottom_left.is_zero()
972    }
973}
974
975impl From<f32> for Corners<Points> {
976    fn from(v: f32) -> Self {
977        Corners::all(Points(v))
978    }
979}
980
981impl From<Points> for Corners<Points> {
982    fn from(p: Points) -> Self {
983        Corners::all(p)
984    }
985}