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