pub struct PxPosition {
pub x: Px,
pub y: Px,
}
Expand description
A 2D position in physical pixel space.
This type represents a position with x and y coordinates in physical pixel space. Physical pixels correspond directly to screen pixels and are used internally by the rendering system.
§Coordinate System
- Origin (0, 0) is at the top-left corner
- X-axis increases to the right
- Y-axis increases downward
- Negative coordinates are supported for scrolling and off-screen positioning
§Examples
use tessera_ui::px::{Px, PxPosition};
// Create a position
let position = PxPosition::new(Px::new(100), Px::new(200));
// Offset the position
let offset_position = position.offset(Px::new(10), Px::new(-5));
// Calculate distance between positions
let other_position = PxPosition::new(Px::new(103), Px::new(196));
let distance = position.distance_to(other_position);
// Arithmetic operations
let sum = position + other_position;
let diff = position - other_position;
Fields§
§x: Px
The x-coordinate in physical pixels
y: Px
The y-coordinate in physical pixels
Implementations§
Source§impl PxPosition
impl PxPosition
Sourcepub const fn new(x: Px, y: Px) -> Self
pub const fn new(x: Px, y: Px) -> Self
Creates a new position from x and y coordinates.
§Arguments
x
- The x-coordinate in physical pixelsy
- The y-coordinate in physical pixels
§Examples
use tessera_ui::px::{Px, PxPosition};
let position = PxPosition::new(Px::new(100), Px::new(200));
assert_eq!(position.x, Px::new(100));
assert_eq!(position.y, Px::new(200));
Sourcepub fn offset(self, dx: Px, dy: Px) -> Self
pub fn offset(self, dx: Px, dy: Px) -> Self
Offsets the position by the given deltas.
§Panics
This function may panic on overflow in debug builds.
§Arguments
dx
- The x-axis offset in physical pixelsdy
- The y-axis offset in physical pixels
§Examples
use tessera_ui::px::{Px, PxPosition};
let position = PxPosition::new(Px::new(10), Px::new(20));
let offset_position = position.offset(Px::new(5), Px::new(-3));
assert_eq!(offset_position, PxPosition::new(Px::new(15), Px::new(17)));
Sourcepub fn saturating_offset(self, dx: Px, dy: Px) -> Self
pub fn saturating_offset(self, dx: Px, dy: Px) -> Self
Offsets the position with saturating arithmetic.
This prevents overflow by clamping the result to the valid coordinate range.
§Arguments
dx
- The x-axis offset in physical pixelsdy
- The y-axis offset in physical pixels
§Examples
use tessera_ui::px::{Px, PxPosition};
let position = PxPosition::new(Px::new(10), Px::new(20));
let offset_position = position.saturating_offset(Px::new(5), Px::new(-3));
assert_eq!(offset_position, PxPosition::new(Px::new(15), Px::new(17)));
// Prevents overflow
let max_position = PxPosition::new(Px::new(i32::MAX), Px::new(i32::MAX));
let safe_offset = max_position.saturating_offset(Px::new(1), Px::new(1));
assert_eq!(safe_offset, max_position);
Sourcepub fn distance_to(self, other: Self) -> f32
pub fn distance_to(self, other: Self) -> f32
Calculates the Euclidean distance to another position.
§Arguments
other
- The other position to calculate distance to
§Returns
The distance as a floating-point value
§Examples
use tessera_ui::px::{Px, PxPosition};
let pos1 = PxPosition::new(Px::new(0), Px::new(0));
let pos2 = PxPosition::new(Px::new(3), Px::new(4));
assert_eq!(pos1.distance_to(pos2), 5.0);
Sourcepub fn to_f32_arr2(self) -> [f32; 2]
pub fn to_f32_arr2(self) -> [f32; 2]
Sourcepub fn to_f32_arr3(self) -> [f32; 3]
pub fn to_f32_arr3(self) -> [f32; 3]
Sourcepub fn from_f32_arr2(arr: [f32; 2]) -> Self
pub fn from_f32_arr2(arr: [f32; 2]) -> Self
Sourcepub fn from_f32_arr3(arr: [f32; 3]) -> Self
pub fn from_f32_arr3(arr: [f32; 3]) -> Self
Creates a position from a 3D f32 array, ignoring the z component.
§Arguments
arr
- An array[x, y, z]
where only x and y are used
§Examples
use tessera_ui::px::{Px, PxPosition};
let position = PxPosition::from_f32_arr3([10.5, 20.7, 30.9]);
assert_eq!(position, PxPosition::new(Px::new(10), Px::new(20)));
Sourcepub fn to_f64_arr2(self) -> [f64; 2]
pub fn to_f64_arr2(self) -> [f64; 2]
Sourcepub fn to_f64_arr3(self) -> [f64; 3]
pub fn to_f64_arr3(self) -> [f64; 3]
Sourcepub fn from_f64_arr2(arr: [f64; 2]) -> Self
pub fn from_f64_arr2(arr: [f64; 2]) -> Self
Sourcepub fn from_f64_arr3(arr: [f64; 3]) -> Self
pub fn from_f64_arr3(arr: [f64; 3]) -> Self
Creates a position from a 3D f64 array, ignoring the z component.
§Arguments
arr
- An array[x, y, z]
where only x and y are used
§Examples
use tessera_ui::px::{Px, PxPosition};
let position = PxPosition::from_f64_arr3([10.5, 20.7, 30.9]);
assert_eq!(position, PxPosition::new(Px::new(10), Px::new(20)));
Trait Implementations§
Source§impl Add for PxPosition
impl Add for PxPosition
Source§impl Clone for PxPosition
impl Clone for PxPosition
Source§fn clone(&self) -> PxPosition
fn clone(&self) -> PxPosition
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for PxPosition
impl Debug for PxPosition
Source§impl From<PxPosition> for [Px; 2]
impl From<PxPosition> for [Px; 2]
Source§fn from(pos: PxPosition) -> Self
fn from(pos: PxPosition) -> Self
Source§impl From<PxPosition> for [i32; 2]
impl From<PxPosition> for [i32; 2]
Source§fn from(pos: PxPosition) -> Self
fn from(pos: PxPosition) -> Self
Source§impl From<PxPosition> for [u32; 2]
impl From<PxPosition> for [u32; 2]
Source§fn from(pos: PxPosition) -> Self
fn from(pos: PxPosition) -> Self
Source§impl From<PxPosition> for PhysicalPosition<i32>
impl From<PxPosition> for PhysicalPosition<i32>
Source§fn from(pos: PxPosition) -> Self
fn from(pos: PxPosition) -> Self
Source§impl From<PxPosition> for Position
impl From<PxPosition> for Position
Source§fn from(pos: PxPosition) -> Self
fn from(pos: PxPosition) -> Self
Source§impl Hash for PxPosition
impl Hash for PxPosition
Source§impl PartialEq for PxPosition
impl PartialEq for PxPosition
Source§impl Sub for PxPosition
impl Sub for PxPosition
impl Copy for PxPosition
impl Eq for PxPosition
impl StructuralPartialEq for PxPosition
Auto Trait Implementations§
impl Freeze for PxPosition
impl RefUnwindSafe for PxPosition
impl Send for PxPosition
impl Sync for PxPosition
impl Unpin for PxPosition
impl UnwindSafe for PxPosition
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,
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§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
.§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
.§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.§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.§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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