#[repr(C)]pub struct Bounds<T: Clone + Debug + Default + PartialEq> {
pub origin: Point<T>,
pub size: Size<T>,
}Expand description
Represents a rectangular area in a 2D space with an origin point and a size.
The Bounds struct is generic over a type T which represents the type of the coordinate system.
The origin is represented as a Point<T> which defines the top left corner of the rectangle,
and the size is represented as a Size<T> which defines the width and height of the rectangle.
§Examples
let origin = Point { x: 0, y: 0 };
let size = Size { width: 10, height: 20 };
let bounds = Bounds::new(origin, size);
assert_eq!(bounds.origin, origin);
assert_eq!(bounds.size, size);Fields§
§origin: Point<T>The origin point of this area.
size: Size<T>The size of the rectangle.
Implementations§
Source§impl Bounds<Pixels>
impl Bounds<Pixels>
Source§impl<T> Bounds<T>
impl<T> Bounds<T>
Sourcepub fn from_corners(top_left: Point<T>, bottom_right: Point<T>) -> Self
pub fn from_corners(top_left: Point<T>, bottom_right: Point<T>) -> Self
Constructs a Bounds from two corner points: the top left and bottom right corners.
This function calculates the origin and size of the Bounds based on the provided corner points.
The origin is set to the top left corner, and the size is determined by the difference between
the x and y coordinates of the bottom right and top left points.
§Arguments
top_left- APoint<T>representing the top left corner of the rectangle.bottom_right- APoint<T>representing the bottom right corner of the rectangle.
§Returns
Returns a Bounds<T> that encompasses the area defined by the two corner points.
§Examples
let top_left = Point { x: 0, y: 0 };
let bottom_right = Point { x: 10, y: 10 };
let bounds = Bounds::from_corners(top_left, bottom_right);
assert_eq!(bounds.origin, top_left);
assert_eq!(bounds.size.width, 10);
assert_eq!(bounds.size.height, 10);Source§impl<T> Bounds<T>
impl<T> Bounds<T>
Sourcepub fn centered_at(center: Point<T>, size: Size<T>) -> Self
pub fn centered_at(center: Point<T>, size: Size<T>) -> Self
Creates a new bounds centered at the given point.
Source§impl<T> Bounds<T>
impl<T> Bounds<T>
Sourcepub fn intersects(&self, other: &Bounds<T>) -> bool
pub fn intersects(&self, other: &Bounds<T>) -> bool
Checks if this Bounds intersects with another Bounds.
Two Bounds instances intersect if they overlap in the 2D space they occupy.
This method checks if there is any overlapping area between the two bounds.
§Arguments
other- A reference to anotherBoundsto check for intersection with.
§Returns
Returns true if there is any intersection between the two bounds, false otherwise.
§Examples
let bounds1 = Bounds {
origin: Point { x: 0, y: 0 },
size: Size { width: 10, height: 10 },
};
let bounds2 = Bounds {
origin: Point { x: 5, y: 5 },
size: Size { width: 10, height: 10 },
};
let bounds3 = Bounds {
origin: Point { x: 20, y: 20 },
size: Size { width: 10, height: 10 },
};
assert_eq!(bounds1.intersects(&bounds2), true); // Overlapping bounds
assert_eq!(bounds1.intersects(&bounds3), false); // Non-overlapping boundsSource§impl<T> Bounds<T>
impl<T> Bounds<T>
Sourcepub fn center(&self) -> Point<T>
pub fn center(&self) -> Point<T>
Returns the center point of the bounds.
Calculates the center by taking the origin’s x and y coordinates and adding half the width and height
of the bounds, respectively. The center is represented as a Point<T> where T is the type of the
coordinate system.
§Returns
A Point<T> representing the center of the bounds.
§Examples
let bounds = Bounds {
origin: Point { x: 0, y: 0 },
size: Size { width: 10, height: 20 },
};
let center = bounds.center();
assert_eq!(center, Point { x: 5, y: 10 });Source§impl<T> Bounds<T>
impl<T> Bounds<T>
Sourcepub fn half_perimeter(&self) -> T
pub fn half_perimeter(&self) -> T
Calculates the half perimeter of a rectangle defined by the bounds.
The half perimeter is calculated as the sum of the width and the height of the rectangle.
This method is generic over the type T which must implement the Sub trait to allow
calculation of the width and height from the bounds’ origin and size, as well as the Add trait
to sum the width and height for the half perimeter.
§Examples
let bounds = Bounds {
origin: Point { x: 0, y: 0 },
size: Size { width: 10, height: 20 },
};
let half_perimeter = bounds.half_perimeter();
assert_eq!(half_perimeter, 30);Source§impl<T> Bounds<T>
impl<T> Bounds<T>
Sourcepub fn dilate(&self, amount: T) -> Bounds<T>
pub fn dilate(&self, amount: T) -> Bounds<T>
Dilates the bounds by a specified amount in all directions.
This method expands the bounds by the given amount, increasing the size
and adjusting the origin so that the bounds grow outwards equally in all directions.
The resulting bounds will have its width and height increased by twice the amount
(since it grows in both directions), and the origin will be moved by -amount
in both the x and y directions.
§Arguments
amount- The amount by which to dilate the bounds.
§Examples
let mut bounds = Bounds {
origin: Point { x: 10, y: 10 },
size: Size { width: 10, height: 10 },
};
let expanded_bounds = bounds.dilate(5);
assert_eq!(expanded_bounds, Bounds {
origin: Point { x: 5, y: 5 },
size: Size { width: 20, height: 20 },
});Source§impl<T: PartialOrd + Add<T, Output = T> + Sub<Output = T> + Clone + Debug + Default + PartialEq> Bounds<T>
impl<T: PartialOrd + Add<T, Output = T> + Sub<Output = T> + Clone + Debug + Default + PartialEq> Bounds<T>
Sourcepub fn intersect(&self, other: &Self) -> Self
pub fn intersect(&self, other: &Self) -> Self
Calculates the intersection of two Bounds objects.
This method computes the overlapping region of two Bounds. If the bounds do not intersect,
the resulting Bounds will have a size with width and height of zero.
§Arguments
other- A reference to anotherBoundsto intersect with.
§Returns
Returns a Bounds representing the intersection area. If there is no intersection,
the returned Bounds will have a size with width and height of zero.
§Examples
let bounds1 = Bounds {
origin: Point { x: 0, y: 0 },
size: Size { width: 10, height: 10 },
};
let bounds2 = Bounds {
origin: Point { x: 5, y: 5 },
size: Size { width: 10, height: 10 },
};
let intersection = bounds1.intersect(&bounds2);
assert_eq!(intersection, Bounds {
origin: Point { x: 5, y: 5 },
size: Size { width: 5, height: 5 },
});Sourcepub fn union(&self, other: &Self) -> Self
pub fn union(&self, other: &Self) -> Self
Computes the union of two Bounds.
This method calculates the smallest Bounds that contains both the current Bounds and the other Bounds.
The resulting Bounds will have an origin that is the minimum of the origins of the two Bounds,
and a size that encompasses the furthest extents of both Bounds.
§Arguments
other- A reference to anotherBoundsto create a union with.
§Returns
Returns a Bounds representing the union of the two Bounds.
§Examples
let bounds1 = Bounds {
origin: Point { x: 0, y: 0 },
size: Size { width: 10, height: 10 },
};
let bounds2 = Bounds {
origin: Point { x: 5, y: 5 },
size: Size { width: 15, height: 15 },
};
let union_bounds = bounds1.union(&bounds2);
assert_eq!(union_bounds, Bounds {
origin: Point { x: 0, y: 0 },
size: Size { width: 20, height: 20 },
});Source§impl<T> Bounds<T>
impl<T> Bounds<T>
Sourcepub fn space_within(&self, outer: &Self) -> Edges<T>
pub fn space_within(&self, outer: &Self) -> Edges<T>
Computes the space available within outer bounds.
Source§impl<T> Bounds<T>
impl<T> Bounds<T>
Sourcepub fn top(&self) -> T
pub fn top(&self) -> T
Returns the top edge of the bounds.
§Returns
A value of type T representing the y-coordinate of the top edge of the bounds.
Sourcepub fn bottom(&self) -> T
pub fn bottom(&self) -> T
Returns the bottom edge of the bounds.
§Returns
A value of type T representing the y-coordinate of the bottom edge of the bounds.
Sourcepub fn left(&self) -> T
pub fn left(&self) -> T
Returns the left edge of the bounds.
§Returns
A value of type T representing the x-coordinate of the left edge of the bounds.
Sourcepub fn right(&self) -> T
pub fn right(&self) -> T
Returns the right edge of the bounds.
§Returns
A value of type T representing the x-coordinate of the right edge of the bounds.
Sourcepub fn top_right(&self) -> Point<T>
pub fn top_right(&self) -> Point<T>
Returns the top right corner point of the bounds.
§Returns
A Point<T> representing the top right corner of the bounds.
§Examples
let bounds = Bounds {
origin: Point { x: 0, y: 0 },
size: Size { width: 10, height: 20 },
};
let top_right = bounds.top_right();
assert_eq!(top_right, Point { x: 10, y: 0 });Sourcepub fn bottom_right(&self) -> Point<T>
pub fn bottom_right(&self) -> Point<T>
Returns the bottom right corner point of the bounds.
§Returns
A Point<T> representing the bottom right corner of the bounds.
§Examples
let bounds = Bounds {
origin: Point { x: 0, y: 0 },
size: Size { width: 10, height: 20 },
};
let bottom_right = bounds.bottom_right();
assert_eq!(bottom_right, Point { x: 10, y: 20 });Sourcepub fn bottom_left(&self) -> Point<T>
pub fn bottom_left(&self) -> Point<T>
Returns the bottom left corner point of the bounds.
§Returns
A Point<T> representing the bottom left corner of the bounds.
§Examples
let bounds = Bounds {
origin: Point { x: 0, y: 0 },
size: Size { width: 10, height: 20 },
};
let bottom_left = bounds.bottom_left();
assert_eq!(bottom_left, Point { x: 0, y: 20 });Sourcepub fn corner(&self, corner: Corner) -> Point<T>
pub fn corner(&self, corner: Corner) -> Point<T>
Returns the requested corner point of the bounds.
§Returns
A Point<T> representing the corner of the bounds requested by the parameter.
§Examples
use gpui::{Bounds, Corner, Point, Size};
let bounds = Bounds {
origin: Point { x: 0, y: 0 },
size: Size { width: 10, height: 20 },
};
let bottom_left = bounds.corner(Corner::BottomLeft);
assert_eq!(bottom_left, Point { x: 0, y: 20 });Source§impl<T> Bounds<T>
impl<T> Bounds<T>
Sourcepub fn contains(&self, point: &Point<T>) -> bool
pub fn contains(&self, point: &Point<T>) -> bool
Checks if the given point is within the bounds.
This method determines whether a point lies inside the rectangle defined by the bounds, including the edges. The point is considered inside if its x-coordinate is greater than or equal to the left edge and less than or equal to the right edge, and its y-coordinate is greater than or equal to the top edge and less than or equal to the bottom edge of the bounds.
§Arguments
point- A reference to aPoint<T>that represents the point to check.
§Returns
Returns true if the point is within the bounds, false otherwise.
§Examples
let bounds = Bounds {
origin: Point { x: 0, y: 0 },
size: Size { width: 10, height: 10 },
};
let inside_point = Point { x: 5, y: 5 };
let outside_point = Point { x: 15, y: 15 };
assert!(bounds.contains(&inside_point));
assert!(!bounds.contains(&outside_point));Sourcepub fn is_contained_within(&self, other: &Self) -> bool
pub fn is_contained_within(&self, other: &Self) -> bool
Checks if this bounds is completely contained within another bounds.
This method determines whether the current bounds is entirely enclosed by the given bounds. A bounds is considered to be contained within another if its origin (top-left corner) and its bottom-right corner are both contained within the other bounds.
§Arguments
other- A reference to anotherBoundsthat might contain this bounds.
§Returns
Returns true if this bounds is completely inside the other bounds, false otherwise.
§Examples
let outer_bounds = Bounds {
origin: Point { x: 0, y: 0 },
size: Size { width: 20, height: 20 },
};
let inner_bounds = Bounds {
origin: Point { x: 5, y: 5 },
size: Size { width: 10, height: 10 },
};
let overlapping_bounds = Bounds {
origin: Point { x: 15, y: 15 },
size: Size { width: 10, height: 10 },
};
assert!(inner_bounds.is_contained_within(&outer_bounds));
assert!(!overlapping_bounds.is_contained_within(&outer_bounds));Sourcepub fn map<U>(&self, f: impl Fn(T) -> U) -> Bounds<U>
pub fn map<U>(&self, f: impl Fn(T) -> U) -> Bounds<U>
Applies a function to the origin and size of the bounds, producing a new Bounds<U>.
This method allows for converting a Bounds<T> to a Bounds<U> by specifying a closure
that defines how to convert between the two types. The closure is applied to the origin and
size fields, resulting in new bounds of the desired type.
§Arguments
f- A closure that takes a value of typeTand returns a value of typeU.
§Returns
Returns a new Bounds<U> with the origin and size mapped by the provided function.
§Examples
let bounds = Bounds {
origin: Point { x: 10.0, y: 10.0 },
size: Size { width: 10.0, height: 20.0 },
};
let new_bounds = bounds.map(|value| value as f64 * 1.5);
assert_eq!(new_bounds, Bounds {
origin: Point { x: 15.0, y: 15.0 },
size: Size { width: 15.0, height: 30.0 },
});Sourcepub fn map_origin(self, f: impl Fn(T) -> T) -> Bounds<T>
pub fn map_origin(self, f: impl Fn(T) -> T) -> Bounds<T>
Applies a function to the origin of the bounds, producing a new Bounds with the new origin
§Examples
let bounds = Bounds {
origin: Point { x: 10.0, y: 10.0 },
size: Size { width: 10.0, height: 20.0 },
};
let new_bounds = bounds.map_origin(|value| value * 1.5);
assert_eq!(new_bounds, Bounds {
origin: Point { x: 15.0, y: 15.0 },
size: Size { width: 10.0, height: 20.0 },
});Sourcepub fn map_size(self, f: impl Fn(T) -> T) -> Bounds<T>
pub fn map_size(self, f: impl Fn(T) -> T) -> Bounds<T>
Applies a function to the origin of the bounds, producing a new Bounds with the new origin
§Examples
let bounds = Bounds {
origin: Point { x: 10.0, y: 10.0 },
size: Size { width: 10.0, height: 20.0 },
};
let new_bounds = bounds.map_size(|value| value * 1.5);
assert_eq!(new_bounds, Bounds {
origin: Point { x: 10.0, y: 10.0 },
size: Size { width: 15.0, height: 30.0 },
});Source§impl<T: PartialOrd + Clone + Debug + Default + PartialEq> Bounds<T>
Checks if the bounds represent an empty area.
impl<T: PartialOrd + Clone + Debug + Default + PartialEq> Bounds<T>
Checks if the bounds represent an empty area.
§Returns
Returns true if either the width or the height of the bounds is less than or equal to zero, indicating an empty area.
Source§impl Bounds<Pixels>
impl Bounds<Pixels>
Sourcepub fn scale(&self, factor: f32) -> Bounds<ScaledPixels>
pub fn scale(&self, factor: f32) -> Bounds<ScaledPixels>
Scales the bounds by a given factor, typically used to adjust for display scaling.
This method multiplies the origin and size of the bounds by the provided scaling factor,
resulting in a new Bounds<ScaledPixels> that is proportionally larger or smaller
depending on the scaling factor. This can be used to ensure that the bounds are properly
scaled for different display densities.
§Arguments
factor- The scaling factor to apply to the origin and size, typically the display’s scaling factor.
§Returns
Returns a new Bounds<ScaledPixels> that represents the scaled bounds.
§Examples
let bounds = Bounds {
origin: Point { x: Pixels::from(10.0), y: Pixels::from(20.0) },
size: Size { width: Pixels::from(30.0), height: Pixels::from(40.0) },
};
let display_scale_factor = 2.0;
let scaled_bounds = bounds.scale(display_scale_factor);
assert_eq!(scaled_bounds, Bounds {
origin: Point {
x: ScaledPixels::from(20.0),
y: ScaledPixels::from(40.0),
},
size: Size {
width: ScaledPixels::from(60.0),
height: ScaledPixels::from(80.0)
},
});Sourcepub fn to_device_pixels(self, factor: f32) -> Bounds<DevicePixels>
pub fn to_device_pixels(self, factor: f32) -> Bounds<DevicePixels>
Convert the bounds from logical pixels to physical pixels
Trait Implementations§
Source§impl<'de, T> Deserialize<'de> for Bounds<T>
impl<'de, T> Deserialize<'de> for Bounds<T>
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<T: Clone + Debug + Default + PartialEq + Display + Add<T, Output = T>> Display for Bounds<T>
impl<T: Clone + Debug + Default + PartialEq + Display + Add<T, Output = T>> Display for Bounds<T>
Source§impl<T: Clone + Debug + Default + PartialEq> From<BoundsRefinement<T>> for Bounds<T>
impl<T: Clone + Debug + Default + PartialEq> From<BoundsRefinement<T>> for Bounds<T>
Source§fn from(value: BoundsRefinement<T>) -> Self
fn from(value: BoundsRefinement<T>) -> Self
Source§impl From<Box2D<i32, UnknownUnit>> for Bounds<DevicePixels>
impl From<Box2D<i32, UnknownUnit>> for Bounds<DevicePixels>
Source§impl<T, S> MulAssign<S> for Bounds<T>
impl<T, S> MulAssign<S> for Bounds<T>
Source§fn mul_assign(&mut self, rhs: S)
fn mul_assign(&mut self, rhs: S)
*= operation. Read moreSource§impl<T: Clone + Debug + Default + PartialEq> Refineable for Bounds<T>
impl<T: Clone + Debug + Default + PartialEq> Refineable for Bounds<T>
type Refinement = BoundsRefinement<T>
Source§fn refine(&mut self, refinement: &Self::Refinement)
fn refine(&mut self, refinement: &Self::Refinement)
Source§fn refined(self, refinement: Self::Refinement) -> Self
fn refined(self, refinement: Self::Refinement) -> Self
self and calling
refine on it.Source§fn is_superset_of(&self, refinement: &Self::Refinement) -> bool
fn is_superset_of(&self, refinement: &Self::Refinement) -> bool
true if this instance would contain all values from the refinement. Read moreSource§fn subtract(&self, refinement: &Self::Refinement) -> Self::Refinement
fn subtract(&self, refinement: &Self::Refinement) -> Self::Refinement
impl<T: Copy + Clone + Debug + Default + PartialEq> Copy for Bounds<T>
impl<T: Eq + Clone + Debug + Default + PartialEq> Eq for Bounds<T>
impl<T: Clone + Debug + Default + PartialEq> StructuralPartialEq for Bounds<T>
Auto Trait Implementations§
impl<T> Freeze for Bounds<T>where
T: Freeze,
impl<T> RefUnwindSafe for Bounds<T>where
T: RefUnwindSafe,
impl<T> Send for Bounds<T>where
T: Send,
impl<T> Sync for Bounds<T>where
T: Sync,
impl<T> Unpin for Bounds<T>where
T: Unpin,
impl<T> UnwindSafe for Bounds<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().