pub struct Hitbox {
pub id: HitboxId,
pub bounds: Bounds<Pixels>,
pub content_mask: ContentMask<Pixels>,
pub behavior: HitboxBehavior,
}Expand description
A rectangular region that potentially blocks hitboxes inserted prior. See Window::insert_hitbox for more details.
Fields§
§id: HitboxIdA unique identifier for the hitbox.
bounds: Bounds<Pixels>The bounds of the hitbox.
content_mask: ContentMask<Pixels>The content mask when the hitbox was inserted.
behavior: HitboxBehaviorFlags that specify hitbox behavior.
Implementations§
Source§impl Hitbox
impl Hitbox
Sourcepub fn is_hovered(&self, window: &Window) -> bool
pub fn is_hovered(&self, window: &Window) -> bool
Checks if the hitbox is currently hovered. Except when handling ScrollWheelEvent, this is
typically what you want when determining whether to handle mouse events or paint hover
styles.
This can return false even when the hitbox contains the mouse, if a hitbox in front of
this sets HitboxBehavior::BlockMouse (InteractiveElement::occlude) or
HitboxBehavior::BlockMouseExceptScroll (InteractiveElement::block_mouse_except_scroll).
Handling of ScrollWheelEvent should typically use should_handle_scroll instead.
Concretely, this is due to use-cases like overlays that cause the elements under to be
non-interactive while still allowing scrolling. More abstractly, this is because
is_hovered is about element interactions directly under the mouse - mouse moves, clicks,
hover styling, etc. In contrast, scrolling is about finding the current outer scrollable
container.
Sourcepub fn should_handle_scroll(&self, window: &Window) -> bool
pub fn should_handle_scroll(&self, window: &Window) -> bool
Checks if the hitbox contains the mouse and should handle scroll events. Typically this
should only be used when handling ScrollWheelEvent, and otherwise is_hovered should be
used. See the documentation of Hitbox::is_hovered for details about this distinction.
This can return false even when the hitbox contains the mouse, if a hitbox in front of
this sets HitboxBehavior::BlockMouse (InteractiveElement::occlude).
Methods from Deref<Target = Bounds<Pixels>>§
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 boundsSourcepub 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 });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);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 },
});Sourcepub fn extend(&self, amount: Edges<T>) -> Bounds<T>
pub fn extend(&self, amount: Edges<T>) -> Bounds<T>
Extends the bounds different amounts in each direction.
Sourcepub fn inset(&self, amount: T) -> Self
pub fn inset(&self, amount: T) -> Self
Inset the bounds by a specified amount. Equivalent to dilate with the amount negated.
Note that this may panic if T does not support negative values.
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 },
});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.
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 });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 localize(&self, point: &Point<T>) -> Option<Point<T>>
pub fn localize(&self, point: &Point<T>) -> Option<Point<T>>
Convert a point to the coordinate space defined by this Bounds
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
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.
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)
},
});Trait Implementations§
Auto Trait Implementations§
impl Freeze for Hitbox
impl RefUnwindSafe for Hitbox
impl Send for Hitbox
impl Sync for Hitbox
impl Unpin for Hitbox
impl UnwindSafe for Hitbox
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<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 more