Hitbox

Struct Hitbox 

Source
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: HitboxId

A 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: HitboxBehavior

Flags that specify hitbox behavior.

Implementations§

Source§

impl Hitbox

Source

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.

Source

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>>§

Source

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 another Bounds to 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 bounds
Source

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

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

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

pub fn extend(&self, amount: Edges<T>) -> Bounds<T>

Extends the bounds different amounts in each direction.

Source

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.

Source

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 another Bounds to 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 },
});
Source

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 another Bounds to 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

pub fn space_within(&self, outer: &Self) -> Edges<T>

Computes the space available within outer bounds.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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 });
Source

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 });
Source

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 });
Source

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

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 a Point<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));
Source

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 another Bounds that 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));
Source

pub fn map<U>(&self, f: impl Fn(T) -> U) -> Bounds<U>
where U: Clone + Debug + Default + PartialEq,

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 type T and returns a value of type U.
§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 },
});
Source

pub fn localize(&self, point: &Point<T>) -> Option<Point<T>>

Convert a point to the coordinate space defined by this Bounds

Source

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.

Source

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§

Source§

impl Clone for Hitbox

Source§

fn clone(&self) -> Hitbox

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Hitbox

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Deref for Hitbox

Source§

type Target = Bounds<Pixels>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert 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>

Convert 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)

Convert &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)

Convert &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
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more