zed/crates/gpui3/src/geometry.rs

1078 lines
26 KiB
Rust
Raw Normal View History

2023-09-19 19:19:22 +00:00
use core::fmt::Debug;
use derive_more::{Add, AddAssign, Div, Mul, Sub, SubAssign};
use refineable::Refineable;
2023-10-04 15:59:29 +00:00
use std::{
2023-10-05 20:42:11 +00:00
cmp, fmt,
2023-10-04 15:59:29 +00:00
ops::{Add, AddAssign, Div, Mul, MulAssign, Sub, SubAssign},
};
2023-09-19 19:19:22 +00:00
2023-09-28 03:14:09 +00:00
#[derive(Refineable, Default, Add, AddAssign, Sub, SubAssign, Copy, Debug, PartialEq, Eq, Hash)]
2023-09-19 19:19:22 +00:00
#[refineable(debug)]
#[repr(C)]
pub struct Point<T: Clone + Debug> {
pub x: T,
pub y: T,
}
pub fn point<T: Clone + Debug>(x: T, y: T) -> Point<T> {
Point { x, y }
}
impl<T: Clone + Debug> Point<T> {
pub fn new(x: T, y: T) -> Self {
Self { x, y }
}
2023-09-20 02:55:13 +00:00
pub fn map<U: Clone + Debug, F: Fn(T) -> U>(&self, f: F) -> Point<U> {
Point {
x: f(self.x.clone()),
y: f(self.y.clone()),
}
}
2023-09-19 19:19:22 +00:00
}
2023-10-03 19:03:29 +00:00
impl Point<Pixels> {
pub fn scale(&self, factor: f32) -> Point<ScaledPixels> {
Point {
x: self.x.scale(factor),
y: self.y.scale(factor),
}
}
}
2023-09-28 04:02:48 +00:00
impl<T, Rhs> Mul<Rhs> for Point<T>
where
2023-10-03 04:14:45 +00:00
T: Mul<Rhs, Output = T> + Clone + Debug,
2023-09-28 04:02:48 +00:00
Rhs: Clone + Debug,
{
2023-10-03 04:14:45 +00:00
type Output = Point<T>;
2023-09-28 03:14:09 +00:00
2023-09-28 04:02:48 +00:00
fn mul(self, rhs: Rhs) -> Self::Output {
Point {
x: self.x * rhs.clone(),
y: self.y * rhs,
2023-09-28 03:14:09 +00:00
}
}
}
2023-09-23 20:20:07 +00:00
impl<T: Clone + Debug + Mul<S, Output = T>, S: Clone> MulAssign<S> for Point<T> {
fn mul_assign(&mut self, rhs: S) {
self.x = self.x.clone() * rhs.clone();
self.y = self.y.clone() * rhs;
}
}
2023-09-20 03:55:49 +00:00
impl<T: Clone + Debug + Sub<Output = T>> SubAssign<Size<T>> for Point<T> {
fn sub_assign(&mut self, rhs: Size<T>) {
self.x = self.x.clone() - rhs.width;
self.y = self.y.clone() - rhs.height;
}
}
2023-09-21 18:18:09 +00:00
impl<T: Clone + Debug + Add<Output = T> + Copy> AddAssign<T> for Point<T> {
fn add_assign(&mut self, rhs: T) {
self.x = self.x.clone() + rhs;
self.y = self.y.clone() + rhs;
}
}
2023-09-27 23:51:12 +00:00
impl<T: Clone + Debug + Div<S, Output = T>, S: Clone> Div<S> for Point<T> {
type Output = Self;
fn div(self, rhs: S) -> Self::Output {
Self {
x: self.x / rhs.clone(),
y: self.y / rhs,
}
}
}
2023-10-04 15:59:29 +00:00
impl<T: Clone + Debug + cmp::PartialOrd> Point<T> {
2023-09-20 03:55:49 +00:00
pub fn max(&self, other: &Self) -> Self {
Point {
x: if self.x >= other.x {
self.x.clone()
} else {
other.x.clone()
},
y: if self.y >= other.y {
self.y.clone()
} else {
other.y.clone()
},
}
}
2023-10-04 15:59:29 +00:00
pub fn min(&self, other: &Self) -> Self {
Point {
x: if self.x <= other.x {
self.x.clone()
} else {
other.x.clone()
},
y: if self.y <= other.y {
self.y.clone()
} else {
other.y.clone()
},
}
}
2023-09-20 03:55:49 +00:00
}
2023-09-19 19:19:22 +00:00
impl<T: Clone + Debug> Clone for Point<T> {
fn clone(&self) -> Self {
Self {
x: self.x.clone(),
y: self.y.clone(),
}
}
}
2023-10-05 20:42:11 +00:00
#[derive(Refineable, Default, Clone, Copy, PartialEq, Div, Hash)]
2023-09-19 19:19:22 +00:00
#[refineable(debug)]
2023-09-23 15:10:33 +00:00
#[repr(C)]
2023-09-19 19:19:22 +00:00
pub struct Size<T: Clone + Debug> {
pub width: T,
pub height: T,
}
2023-09-20 02:55:13 +00:00
pub fn size<T: Clone + Debug>(width: T, height: T) -> Size<T> {
Size { width, height }
}
2023-09-21 02:28:32 +00:00
impl<T: Clone + Debug> Size<T> {
pub fn map<U: Clone + Debug, F: Fn(T) -> U>(&self, f: F) -> Size<U> {
Size {
width: f(self.width.clone()),
height: f(self.height.clone()),
}
}
}
2023-10-03 19:03:29 +00:00
impl Size<Pixels> {
pub fn scale(&self, factor: f32) -> Size<ScaledPixels> {
Size {
width: self.width.scale(factor),
height: self.height.scale(factor),
}
}
}
2023-10-03 04:14:45 +00:00
impl<T: Clone + Debug + Ord> Size<T> {
pub fn max(&self, other: &Self) -> Self {
Size {
width: if self.width >= other.width {
self.width.clone()
} else {
other.width.clone()
},
height: if self.height >= other.height {
self.height.clone()
} else {
other.height.clone()
},
}
}
}
2023-09-28 04:02:48 +00:00
impl<T, Rhs> Mul<Rhs> for Size<T>
where
T: Mul<Rhs, Output = Rhs> + Debug + Clone,
Rhs: Debug + Clone,
{
type Output = Size<Rhs>;
2023-09-28 03:14:09 +00:00
2023-09-28 04:02:48 +00:00
fn mul(self, rhs: Rhs) -> Self::Output {
Size {
width: self.width * rhs.clone(),
height: self.height * rhs,
2023-09-28 03:14:09 +00:00
}
}
}
2023-09-23 20:20:07 +00:00
impl<T: Clone + Debug + Mul<S, Output = T>, S: Clone> MulAssign<S> for Size<T> {
fn mul_assign(&mut self, rhs: S) {
self.width = self.width.clone() * rhs.clone();
self.height = self.height.clone() * rhs;
}
}
2023-10-03 04:14:45 +00:00
impl<T: Eq + Debug + Clone> Eq for Size<T> {}
2023-10-05 20:42:11 +00:00
impl<T: Clone + Debug> Debug for Size<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Size {{ {:?} × {:?} }}", self.width, self.height)
}
}
2023-10-05 02:35:20 +00:00
impl From<Size<Pixels>> for Size<GlobalPixels> {
fn from(size: Size<Pixels>) -> Self {
2023-09-20 20:32:55 +00:00
Size {
2023-10-05 02:35:20 +00:00
width: GlobalPixels(size.width.0),
height: GlobalPixels(size.height.0),
2023-09-20 20:32:55 +00:00
}
}
}
2023-09-19 19:19:22 +00:00
impl Size<Length> {
pub fn full() -> Self {
Self {
width: relative(1.).into(),
height: relative(1.).into(),
2023-09-19 19:19:22 +00:00
}
}
}
impl Size<DefiniteLength> {
pub fn zero() -> Self {
Self {
width: px(0.).into(),
height: px(0.).into(),
}
}
}
impl Size<Length> {
pub fn auto() -> Self {
Self {
width: Length::Auto,
height: Length::Auto,
}
}
}
2023-10-03 15:36:12 +00:00
#[derive(Refineable, Clone, Default, Debug, Eq, PartialEq)]
2023-09-19 19:19:22 +00:00
#[refineable(debug)]
2023-09-23 15:10:33 +00:00
#[repr(C)]
2023-09-19 19:19:22 +00:00
pub struct Bounds<T: Clone + Debug> {
pub origin: Point<T>,
pub size: Size<T>,
}
2023-10-04 02:04:17 +00:00
impl<T: Clone + Debug + Sub<Output = T>> Bounds<T> {
pub fn from_corners(upper_left: Point<T>, lower_right: Point<T>) -> Self {
let origin = Point {
x: upper_left.x.clone(),
y: upper_left.y.clone(),
};
let size = Size {
width: lower_right.x - upper_left.x,
height: lower_right.y - upper_left.y,
};
Bounds { origin, size }
}
}
2023-10-05 02:35:20 +00:00
impl<T: Clone + Debug + PartialOrd + Add<T, Output = T> + Sub<Output = T>> Bounds<T> {
pub fn intersects(&self, other: &Bounds<T>) -> bool {
let my_lower_right = self.lower_right();
let their_lower_right = other.lower_right();
self.origin.x < their_lower_right.x
&& my_lower_right.x > other.origin.x
&& self.origin.y < their_lower_right.y
&& my_lower_right.y > other.origin.y
}
2023-10-05 15:00:45 +00:00
pub fn dilate(&mut self, amount: T) {
self.origin.x = self.origin.x.clone() - amount.clone();
self.origin.y = self.origin.y.clone() - amount.clone();
let double_amount = amount.clone() + amount;
self.size.width = self.size.width.clone() + double_amount.clone();
self.size.height = self.size.height.clone() + double_amount;
}
2023-10-05 02:35:20 +00:00
}
2023-10-04 15:59:29 +00:00
impl<T: Clone + Debug + PartialOrd + Add<T, Output = T> + Sub<Output = T>> Bounds<T> {
pub fn intersect(&self, other: &Self) -> Self {
let upper_left = self.origin.max(&other.origin);
let lower_right = self.lower_right().min(&other.lower_right());
Self::from_corners(upper_left, lower_right)
}
}
2023-09-28 04:02:48 +00:00
impl<T, Rhs> Mul<Rhs> for Bounds<T>
2023-09-28 03:14:09 +00:00
where
2023-09-28 04:02:48 +00:00
T: Mul<Rhs, Output = Rhs> + Clone + Debug,
2023-10-03 04:14:45 +00:00
Point<T>: Mul<Rhs, Output = Point<Rhs>>,
2023-09-28 04:02:48 +00:00
Rhs: Clone + Debug,
2023-09-28 03:14:09 +00:00
{
2023-09-28 04:02:48 +00:00
type Output = Bounds<Rhs>;
2023-09-28 03:14:09 +00:00
2023-09-28 04:02:48 +00:00
fn mul(self, rhs: Rhs) -> Self::Output {
Bounds {
2023-09-28 03:14:09 +00:00
origin: self.origin * rhs.clone(),
size: self.size * rhs,
}
}
}
2023-09-24 23:12:59 +00:00
impl<T: Clone + Debug + Mul<S, Output = T>, S: Clone> MulAssign<S> for Bounds<T> {
fn mul_assign(&mut self, rhs: S) {
self.origin *= rhs.clone();
self.size *= rhs;
}
}
2023-09-27 23:51:12 +00:00
impl<T: Clone + Debug + Div<S, Output = T>, S: Clone> Div<S> for Bounds<T>
where
Size<T>: Div<S, Output = Size<T>>,
{
type Output = Self;
fn div(self, rhs: S) -> Self {
Self {
origin: self.origin / rhs.clone(),
size: self.size / rhs,
}
}
}
2023-09-23 20:20:07 +00:00
impl<T: Clone + Debug + Add<T, Output = T>> Bounds<T> {
2023-09-19 19:19:22 +00:00
pub fn upper_right(&self) -> Point<T> {
Point {
2023-09-23 20:20:07 +00:00
x: self.origin.x.clone() + self.size.width.clone(),
y: self.origin.y.clone(),
2023-09-19 19:19:22 +00:00
}
}
2023-09-20 03:55:49 +00:00
pub fn lower_right(&self) -> Point<T> {
Point {
2023-09-23 20:20:07 +00:00
x: self.origin.x.clone() + self.size.width.clone(),
y: self.origin.y.clone() + self.size.height.clone(),
2023-09-20 03:55:49 +00:00
}
}
2023-10-05 20:42:11 +00:00
pub fn lower_left(&self) -> Point<T> {
Point {
x: self.origin.x.clone(),
y: self.origin.y.clone() + self.size.height.clone(),
}
}
2023-09-20 03:55:49 +00:00
}
2023-09-23 20:20:07 +00:00
impl<T: Clone + Debug + PartialOrd + Add<T, Output = T>> Bounds<T> {
2023-09-20 03:55:49 +00:00
pub fn contains_point(&self, point: Point<T>) -> bool {
point.x >= self.origin.x
2023-09-23 20:20:07 +00:00
&& point.x <= self.origin.x.clone() + self.size.width.clone()
2023-09-20 03:55:49 +00:00
&& point.y >= self.origin.y
2023-09-23 20:20:07 +00:00
&& point.y <= self.origin.y.clone() + self.size.height.clone()
2023-09-20 03:55:49 +00:00
}
2023-09-27 23:51:12 +00:00
pub fn map<U: Clone + Debug, F: Fn(T) -> U>(&self, f: F) -> Bounds<U> {
Bounds {
origin: self.origin.map(&f),
size: self.size.map(f),
}
}
2023-09-19 19:19:22 +00:00
}
2023-10-03 19:03:29 +00:00
impl Bounds<Pixels> {
pub fn scale(&self, factor: f32) -> Bounds<ScaledPixels> {
Bounds {
origin: self.origin.scale(factor),
size: self.size.scale(factor),
}
}
}
2023-09-19 19:19:22 +00:00
impl<T: Clone + Debug + Copy> Copy for Bounds<T> {}
2023-10-03 15:36:12 +00:00
#[derive(Refineable, Clone, Default, Debug, Eq, PartialEq)]
2023-09-19 19:19:22 +00:00
#[refineable(debug)]
2023-09-23 15:10:33 +00:00
#[repr(C)]
2023-09-19 19:19:22 +00:00
pub struct Edges<T: Clone + Debug> {
pub top: T,
pub right: T,
pub bottom: T,
pub left: T,
}
2023-09-28 03:14:09 +00:00
impl<T: Clone + Debug + Mul<Output = T>> Mul for Edges<T> {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
Self {
top: self.top.clone() * rhs.top,
right: self.right.clone() * rhs.right,
bottom: self.bottom.clone() * rhs.bottom,
left: self.left.clone() * rhs.left,
}
}
}
2023-09-23 20:20:07 +00:00
impl<T: Clone + Debug + Mul<S, Output = T>, S: Clone> MulAssign<S> for Edges<T> {
fn mul_assign(&mut self, rhs: S) {
self.top = self.top.clone() * rhs.clone();
self.right = self.right.clone() * rhs.clone();
self.bottom = self.bottom.clone() * rhs.clone();
self.left = self.left.clone() * rhs.clone();
}
}
2023-09-22 22:31:26 +00:00
impl<T: Clone + Debug + Copy> Copy for Edges<T> {}
2023-10-02 19:16:10 +00:00
impl<T: Clone + Debug> Edges<T> {
pub fn map<U: Clone + Debug, F: Fn(&T) -> U>(&self, f: F) -> Edges<U> {
Edges {
top: f(&self.top),
right: f(&self.right),
bottom: f(&self.bottom),
left: f(&self.left),
}
}
pub fn any<F: Fn(&T) -> bool>(&self, predicate: F) -> bool {
predicate(&self.top)
|| predicate(&self.right)
|| predicate(&self.bottom)
|| predicate(&self.left)
}
}
2023-09-19 19:19:22 +00:00
impl Edges<Length> {
pub fn auto() -> Self {
Self {
top: Length::Auto,
right: Length::Auto,
bottom: Length::Auto,
left: Length::Auto,
}
}
pub fn zero() -> Self {
Self {
top: px(0.).into(),
right: px(0.).into(),
bottom: px(0.).into(),
left: px(0.).into(),
}
}
}
impl Edges<DefiniteLength> {
pub fn zero() -> Self {
Self {
top: px(0.).into(),
right: px(0.).into(),
bottom: px(0.).into(),
left: px(0.).into(),
}
}
}
impl Edges<AbsoluteLength> {
pub fn zero() -> Self {
Self {
top: px(0.).into(),
right: px(0.).into(),
bottom: px(0.).into(),
left: px(0.).into(),
}
}
pub fn to_pixels(&self, rem_size: Pixels) -> Edges<Pixels> {
Edges {
top: self.top.to_pixels(rem_size),
right: self.right.to_pixels(rem_size),
bottom: self.bottom.to_pixels(rem_size),
left: self.left.to_pixels(rem_size),
}
}
}
2023-10-05 13:30:47 +00:00
impl Edges<Pixels> {
pub fn scale(&self, factor: f32) -> Edges<ScaledPixels> {
Edges {
top: self.top.scale(factor),
right: self.right.scale(factor),
bottom: self.bottom.scale(factor),
left: self.left.scale(factor),
}
}
}
2023-10-03 15:36:12 +00:00
#[derive(Refineable, Clone, Default, Debug, Eq, PartialEq)]
2023-09-22 22:31:26 +00:00
#[refineable(debug)]
2023-09-23 15:10:33 +00:00
#[repr(C)]
2023-09-22 22:31:26 +00:00
pub struct Corners<T: Clone + Debug> {
pub top_left: T,
pub top_right: T,
pub bottom_right: T,
pub bottom_left: T,
}
2023-10-04 02:04:17 +00:00
impl Corners<AbsoluteLength> {
2023-10-05 13:30:47 +00:00
pub fn to_pixels(&self, size: Size<Pixels>, rem_size: Pixels) -> Corners<Pixels> {
let max = size.width.max(size.height) / 2.;
2023-10-04 02:04:17 +00:00
Corners {
2023-10-04 14:42:28 +00:00
top_left: self.top_left.to_pixels(rem_size).min(max),
top_right: self.top_right.to_pixels(rem_size).min(max),
bottom_right: self.bottom_right.to_pixels(rem_size).min(max),
bottom_left: self.bottom_left.to_pixels(rem_size).min(max),
2023-10-04 02:04:17 +00:00
}
}
}
impl Corners<Pixels> {
pub fn scale(&self, factor: f32) -> Corners<ScaledPixels> {
Corners {
top_left: self.top_left.scale(factor),
top_right: self.top_right.scale(factor),
bottom_right: self.bottom_right.scale(factor),
bottom_left: self.bottom_left.scale(factor),
}
}
}
2023-10-02 19:16:10 +00:00
impl<T: Clone + Debug> Corners<T> {
pub fn map<U: Clone + Debug, F: Fn(&T) -> U>(&self, f: F) -> Corners<U> {
Corners {
top_left: f(&self.top_left),
top_right: f(&self.top_right),
bottom_right: f(&self.bottom_right),
bottom_left: f(&self.bottom_left),
}
}
}
2023-09-28 03:14:09 +00:00
impl<T: Clone + Debug + Mul<Output = T>> Mul for Corners<T> {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
Self {
top_left: self.top_left.clone() * rhs.top_left,
top_right: self.top_right.clone() * rhs.top_right,
bottom_right: self.bottom_right.clone() * rhs.bottom_right,
bottom_left: self.bottom_left.clone() * rhs.bottom_left,
}
}
}
2023-09-23 20:20:07 +00:00
impl<T: Clone + Debug + Mul<S, Output = T>, S: Clone> MulAssign<S> for Corners<T> {
fn mul_assign(&mut self, rhs: S) {
self.top_left = self.top_left.clone() * rhs.clone();
self.top_right = self.top_right.clone() * rhs.clone();
self.bottom_right = self.bottom_right.clone() * rhs.clone();
self.bottom_left = self.bottom_left.clone() * rhs;
}
}
2023-09-22 22:31:26 +00:00
impl<T: Clone + Debug + Copy> Copy for Corners<T> {}
2023-10-03 16:04:45 +00:00
#[derive(Clone, Copy, Default, Add, AddAssign, Sub, SubAssign, Div, PartialEq, PartialOrd)]
2023-09-19 19:19:22 +00:00
#[repr(transparent)]
pub struct Pixels(pub(crate) f32);
2023-09-23 20:20:07 +00:00
impl Mul<f32> for Pixels {
type Output = Pixels;
fn mul(self, other: f32) -> Pixels {
Pixels(self.0 * other)
}
}
2023-09-28 04:02:48 +00:00
impl Mul<Pixels> for f32 {
type Output = Pixels;
fn mul(self, rhs: Pixels) -> Self::Output {
Pixels(self * rhs.0)
}
}
2023-10-02 21:43:03 +00:00
impl MulAssign<f32> for Pixels {
fn mul_assign(&mut self, other: f32) {
self.0 *= other;
}
}
2023-09-20 03:55:49 +00:00
impl Pixels {
pub fn round(&self) -> Self {
Self(self.0.round())
2023-09-20 02:55:13 +00:00
}
2023-10-03 19:03:29 +00:00
pub fn scale(&self, factor: f32) -> ScaledPixels {
ScaledPixels(self.0 * factor)
}
2023-09-20 02:55:13 +00:00
}
2023-09-19 19:19:22 +00:00
impl Mul<Pixels> for Pixels {
type Output = Pixels;
fn mul(self, rhs: Pixels) -> Self::Output {
Pixels(self.0 * rhs.0)
}
}
impl Eq for Pixels {}
impl Ord for Pixels {
2023-10-04 15:59:29 +00:00
fn cmp(&self, other: &Self) -> cmp::Ordering {
2023-09-19 19:19:22 +00:00
self.0.partial_cmp(&other.0).unwrap()
}
}
impl std::hash::Hash for Pixels {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.0.to_bits().hash(state);
}
}
2023-09-20 16:17:29 +00:00
impl From<f64> for Pixels {
2023-10-04 03:23:32 +00:00
fn from(pixels: f64) -> Self {
Pixels(pixels as f32)
2023-09-20 16:17:29 +00:00
}
}
2023-09-20 20:32:55 +00:00
impl From<f32> for Pixels {
2023-10-04 03:23:32 +00:00
fn from(pixels: f32) -> Self {
Pixels(pixels)
2023-09-20 20:32:55 +00:00
}
}
2023-09-19 19:19:22 +00:00
impl Debug for Pixels {
2023-10-05 20:42:11 +00:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2023-09-19 19:19:22 +00:00
write!(f, "{} px", self.0)
}
}
impl From<Pixels> for f32 {
fn from(pixels: Pixels) -> Self {
pixels.0
}
}
impl From<&Pixels> for f32 {
fn from(pixels: &Pixels) -> Self {
pixels.0
}
}
2023-09-20 03:55:49 +00:00
impl From<Pixels> for f64 {
fn from(pixels: Pixels) -> Self {
pixels.0 as f64
}
}
2023-09-23 21:56:40 +00:00
#[derive(
2023-10-03 21:17:45 +00:00
Add, AddAssign, Clone, Copy, Default, Div, Eq, Hash, Ord, PartialEq, PartialOrd, Sub, SubAssign,
2023-09-23 21:56:40 +00:00
)]
#[repr(transparent)]
2023-10-03 19:03:29 +00:00
pub struct DevicePixels(pub(crate) i32);
2023-09-23 21:56:40 +00:00
2023-10-03 04:14:45 +00:00
impl DevicePixels {
pub fn to_bytes(&self, bytes_per_pixel: u8) -> u32 {
2023-10-03 19:03:29 +00:00
self.0 as u32 * bytes_per_pixel as u32
2023-10-03 04:14:45 +00:00
}
}
2023-10-05 20:42:11 +00:00
impl fmt::Debug for DevicePixels {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2023-10-03 21:17:45 +00:00
write!(f, "{} px (device)", self.0)
}
}
2023-10-03 19:03:29 +00:00
impl From<DevicePixels> for i32 {
2023-09-23 21:56:40 +00:00
fn from(device_pixels: DevicePixels) -> Self {
device_pixels.0
}
}
2023-10-03 19:03:29 +00:00
impl From<i32> for DevicePixels {
2023-10-04 03:23:32 +00:00
fn from(device_pixels: i32) -> Self {
DevicePixels(device_pixels)
}
}
impl From<u32> for DevicePixels {
fn from(device_pixels: u32) -> Self {
DevicePixels(device_pixels as i32)
}
}
impl From<DevicePixels> for u32 {
fn from(device_pixels: DevicePixels) -> Self {
device_pixels.0 as u32
2023-09-23 21:56:40 +00:00
}
}
2023-10-03 04:14:45 +00:00
impl From<DevicePixels> for u64 {
fn from(device_pixels: DevicePixels) -> Self {
device_pixels.0 as u64
}
}
impl From<u64> for DevicePixels {
2023-10-04 03:23:32 +00:00
fn from(device_pixels: u64) -> Self {
DevicePixels(device_pixels as i32)
2023-10-03 19:03:29 +00:00
}
}
#[derive(Clone, Copy, Default, Add, AddAssign, Sub, SubAssign, Div, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct ScaledPixels(pub(crate) f32);
2023-10-03 20:25:29 +00:00
impl ScaledPixels {
pub fn floor(&self) -> Self {
Self(self.0.floor())
}
2023-10-04 13:03:21 +00:00
pub fn ceil(&self) -> Self {
Self(self.0.ceil())
}
2023-10-03 20:25:29 +00:00
}
2023-10-03 19:03:29 +00:00
impl Eq for ScaledPixels {}
impl Debug for ScaledPixels {
2023-10-05 20:42:11 +00:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2023-10-03 19:03:29 +00:00
write!(f, "{} px (scaled)", self.0)
}
}
impl From<ScaledPixels> for DevicePixels {
fn from(scaled: ScaledPixels) -> Self {
DevicePixels(scaled.0.ceil() as i32)
2023-10-03 04:14:45 +00:00
}
}
2023-10-03 19:52:10 +00:00
impl From<DevicePixels> for ScaledPixels {
fn from(device: DevicePixels) -> Self {
ScaledPixels(device.0 as f32)
}
}
2023-10-05 20:42:11 +00:00
impl From<ScaledPixels> for f64 {
fn from(scaled_pixels: ScaledPixels) -> Self {
scaled_pixels.0 as f64
}
}
2023-10-05 02:35:20 +00:00
#[derive(Clone, Copy, Default, Add, AddAssign, Sub, SubAssign, Div, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct GlobalPixels(pub(crate) f32);
impl Debug for GlobalPixels {
2023-10-05 20:42:11 +00:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2023-10-05 02:35:20 +00:00
write!(f, "{} px (global coordinate space)", self.0)
}
}
impl From<GlobalPixels> for f64 {
fn from(global_pixels: GlobalPixels) -> Self {
global_pixels.0 as f64
}
}
impl From<f64> for GlobalPixels {
fn from(global_pixels: f64) -> Self {
GlobalPixels(global_pixels as f32)
}
}
2023-09-19 19:19:22 +00:00
#[derive(Clone, Copy, Default, Add, Sub, Mul, Div)]
pub struct Rems(f32);
impl Mul<Pixels> for Rems {
type Output = Pixels;
fn mul(self, other: Pixels) -> Pixels {
Pixels(self.0 * other.0)
}
}
impl Debug for Rems {
2023-10-05 20:42:11 +00:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2023-09-19 19:19:22 +00:00
write!(f, "{} rem", self.0)
}
}
#[derive(Clone, Copy, Debug)]
pub enum AbsoluteLength {
Pixels(Pixels),
Rems(Rems),
}
2023-10-02 19:16:10 +00:00
impl AbsoluteLength {
pub fn is_zero(&self) -> bool {
match self {
AbsoluteLength::Pixels(px) => px.0 == 0.,
AbsoluteLength::Rems(rems) => rems.0 == 0.,
}
}
}
2023-09-19 19:19:22 +00:00
impl From<Pixels> for AbsoluteLength {
fn from(pixels: Pixels) -> Self {
AbsoluteLength::Pixels(pixels)
}
}
impl From<Rems> for AbsoluteLength {
fn from(rems: Rems) -> Self {
AbsoluteLength::Rems(rems)
}
}
impl AbsoluteLength {
pub fn to_pixels(&self, rem_size: Pixels) -> Pixels {
match self {
AbsoluteLength::Pixels(pixels) => *pixels,
AbsoluteLength::Rems(rems) => *rems * rem_size,
}
}
}
impl Default for AbsoluteLength {
fn default() -> Self {
px(0.).into()
}
}
/// A non-auto length that can be defined in pixels, rems, or percent of parent.
#[derive(Clone, Copy)]
pub enum DefiniteLength {
Absolute(AbsoluteLength),
/// A fraction of the parent's size between 0 and 1.
Fraction(f32),
}
2023-09-28 05:05:39 +00:00
impl DefiniteLength {
pub fn to_pixels(&self, base_size: AbsoluteLength, rem_size: Pixels) -> Pixels {
match self {
DefiniteLength::Absolute(size) => size.to_pixels(rem_size),
DefiniteLength::Fraction(fraction) => match base_size {
AbsoluteLength::Pixels(px) => px * *fraction,
AbsoluteLength::Rems(rems) => rems * rem_size * *fraction,
},
}
}
}
2023-09-19 19:19:22 +00:00
impl Debug for DefiniteLength {
2023-10-05 20:42:11 +00:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2023-09-19 19:19:22 +00:00
match self {
DefiniteLength::Absolute(length) => Debug::fmt(length, f),
DefiniteLength::Fraction(fract) => write!(f, "{}%", (fract * 100.0) as i32),
}
}
}
impl From<Pixels> for DefiniteLength {
fn from(pixels: Pixels) -> Self {
Self::Absolute(pixels.into())
}
}
impl From<Rems> for DefiniteLength {
fn from(rems: Rems) -> Self {
Self::Absolute(rems.into())
}
}
impl From<AbsoluteLength> for DefiniteLength {
fn from(length: AbsoluteLength) -> Self {
Self::Absolute(length)
}
}
impl Default for DefiniteLength {
fn default() -> Self {
Self::Absolute(AbsoluteLength::default())
}
}
/// A length that can be defined in pixels, rems, percent of parent, or auto.
#[derive(Clone, Copy)]
pub enum Length {
Definite(DefiniteLength),
Auto,
}
impl Debug for Length {
2023-10-05 20:42:11 +00:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2023-09-19 19:19:22 +00:00
match self {
Length::Definite(definite_length) => write!(f, "{:?}", definite_length),
Length::Auto => write!(f, "auto"),
}
}
}
pub fn relative(fraction: f32) -> DefiniteLength {
2023-09-19 19:19:22 +00:00
DefiniteLength::Fraction(fraction).into()
}
2023-09-28 05:05:39 +00:00
/// Returns the Golden Ratio, i.e. `~(1.0 + sqrt(5.0)) / 2.0`.
pub fn phi() -> DefiniteLength {
relative(1.61803398875)
}
2023-09-19 19:19:22 +00:00
pub fn rems(rems: f32) -> Rems {
2023-09-20 02:55:13 +00:00
Rems(rems)
2023-09-19 19:19:22 +00:00
}
pub fn px(pixels: f32) -> Pixels {
2023-09-20 02:55:13 +00:00
Pixels(pixels)
2023-09-19 19:19:22 +00:00
}
pub fn auto() -> Length {
Length::Auto
}
impl From<Pixels> for Length {
fn from(pixels: Pixels) -> Self {
Self::Definite(pixels.into())
}
}
impl From<Rems> for Length {
fn from(rems: Rems) -> Self {
Self::Definite(rems.into())
}
}
impl From<DefiniteLength> for Length {
fn from(length: DefiniteLength) -> Self {
Self::Definite(length)
}
}
impl From<AbsoluteLength> for Length {
fn from(length: AbsoluteLength) -> Self {
Self::Definite(length.into())
}
}
impl Default for Length {
fn default() -> Self {
Self::Definite(DefiniteLength::default())
}
}
impl From<()> for Length {
fn from(_: ()) -> Self {
Self::Definite(DefiniteLength::default())
}
}
2023-10-03 19:52:10 +00:00
pub trait IsZero {
fn is_zero(&self) -> bool;
}
impl IsZero for DevicePixels {
fn is_zero(&self) -> bool {
self.0 == 0
}
}
impl IsZero for ScaledPixels {
fn is_zero(&self) -> bool {
self.0 == 0.
}
}
impl IsZero for Pixels {
fn is_zero(&self) -> bool {
self.0 == 0.
}
}
impl IsZero for Rems {
fn is_zero(&self) -> bool {
self.0 == 0.
}
}
impl IsZero for AbsoluteLength {
fn is_zero(&self) -> bool {
match self {
AbsoluteLength::Pixels(pixels) => pixels.is_zero(),
AbsoluteLength::Rems(rems) => rems.is_zero(),
}
}
}
impl IsZero for DefiniteLength {
fn is_zero(&self) -> bool {
match self {
DefiniteLength::Absolute(length) => length.is_zero(),
DefiniteLength::Fraction(fraction) => *fraction == 0.,
}
}
}
impl IsZero for Length {
fn is_zero(&self) -> bool {
match self {
Length::Definite(length) => length.is_zero(),
Length::Auto => false,
}
}
}
impl<T: IsZero + Debug + Clone> IsZero for Point<T> {
fn is_zero(&self) -> bool {
self.x.is_zero() && self.y.is_zero()
}
}
impl<T: IsZero + Debug + Clone> IsZero for Size<T> {
fn is_zero(&self) -> bool {
2023-10-04 03:23:32 +00:00
self.width.is_zero() || self.height.is_zero()
2023-10-03 19:52:10 +00:00
}
}
impl<T: IsZero + Debug + Clone> IsZero for Bounds<T> {
fn is_zero(&self) -> bool {
2023-10-04 14:42:28 +00:00
self.size.is_zero()
}
}
impl<T: IsZero + Debug + Clone> IsZero for Corners<T> {
fn is_zero(&self) -> bool {
self.top_left.is_zero()
&& self.top_right.is_zero()
&& self.bottom_right.is_zero()
&& self.bottom_left.is_zero()
2023-10-03 19:52:10 +00:00
}
}
2023-10-05 02:35:20 +00:00
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_bounds_intersects() {
let bounds1 = Bounds {
origin: Point { x: 0.0, y: 0.0 },
size: Size {
width: 5.0,
height: 5.0,
},
};
let bounds2 = Bounds {
origin: Point { x: 4.0, y: 4.0 },
size: Size {
width: 5.0,
height: 5.0,
},
};
let bounds3 = Bounds {
origin: Point { x: 10.0, y: 10.0 },
size: Size {
width: 5.0,
height: 5.0,
},
};
// Test Case 1: Intersecting bounds
assert_eq!(bounds1.intersects(&bounds2), true);
// Test Case 2: Non-Intersecting bounds
assert_eq!(bounds1.intersects(&bounds3), false);
// Test Case 3: Bounds intersecting with themselves
assert_eq!(bounds1.intersects(&bounds1), true);
}
}