pub struct Dp(pub f64);
Expand description
Density-independent pixels (dp) for UI scaling.
Dp
represents a length measurement that remains visually consistent across
different screen densities. This is essential for creating UIs that look the
same physical size on devices with varying pixel densities.
§Design Philosophy
The dp unit is inspired by Android’s density-independent pixel system and
provides a device-agnostic way to specify UI dimensions. When you specify
a button height of Dp(48.0)
, it will appear roughly the same physical
size on a low-DPI laptop screen and a high-DPI mobile device.
§Internal Representation
The Dp
struct wraps a single f64
value representing the dp measurement.
This value is converted to physical pixels using the global SCALE_FACTOR
when rendering operations require pixel-precise measurements.
§Examples
use tessera_ui::Dp;
// Common UI measurements in dp
let small_padding = Dp(8.0);
let medium_padding = Dp(16.0);
let button_height = Dp(48.0);
let large_spacing = Dp(32.0);
// Convert to pixels for rendering
let pixels = button_height.to_pixels_f32();
// Result depends on the current scale factor
§Arithmetic Operations
While Dp
doesn’t implement arithmetic operators directly, you can perform
operations on the inner value:
use tessera_ui::Dp;
let base_size = Dp(16.0);
let double_size = Dp(base_size.0 * 2.0);
let half_size = Dp(base_size.0 / 2.0);
Tuple Fields§
§0: f64
Implementations§
Source§impl Dp
impl Dp
Sourcepub const ZERO: Dp
pub const ZERO: Dp
A constant representing zero density-independent pixels.
§Examples
use tessera_ui::Dp;
let zero_dp = Dp::ZERO;
assert_eq!(zero_dp, Dp(0.0));
Sourcepub fn to_pixels_f64(&self) -> f64
pub fn to_pixels_f64(&self) -> f64
Converts this dp value to physical pixels as an f64
.
This method applies the current global scale factor to convert density-independent
pixels to physical pixels. The scale factor is read from SCALE_FACTOR
.
§Returns
The equivalent value in physical pixels as a 64-bit floating-point number. If the scale factor hasn’t been initialized, defaults to 1.0 (no scaling).
§Examples
use tessera_ui::Dp;
let dp_value = Dp(24.0);
let pixels = dp_value.to_pixels_f64();
// Result depends on the current scale factor
Sourcepub fn from_pixels_f64(value: f64) -> Self
pub fn from_pixels_f64(value: f64) -> Self
Creates a Dp
value from physical pixels specified as an f64
.
This method performs the inverse conversion of to_pixels_f64
,
converting physical pixels back to density-independent pixels using the current
global scale factor.
§Arguments
value
- The pixel value as a 64-bit floating-point number
§Returns
A new Dp
instance representing the equivalent dp value.
If the scale factor hasn’t been initialized, defaults to 1.0 (no scaling).
§Examples
use tessera_ui::Dp;
// Convert 96 pixels to dp (assuming 2.0 scale factor = 48 dp)
let dp_value = Dp::from_pixels_f64(96.0);
Sourcepub fn to_pixels_u32(&self) -> u32
pub fn to_pixels_u32(&self) -> u32
Converts this dp value to physical pixels as a u32
.
This method applies the current global scale factor and truncates the result to an unsigned 32-bit integer. This is commonly used for rendering operations that require integer pixel coordinates.
§Returns
The equivalent value in physical pixels as an unsigned 32-bit integer. The result is truncated (not rounded) from the floating-point calculation. If the scale factor hasn’t been initialized, defaults to 1.0 (no scaling).
§Examples
use tessera_ui::Dp;
let dp_value = Dp(24.5);
let pixels = dp_value.to_pixels_u32();
// With scale factor 2.0: 24.5 * 2.0 = 49.0 -> 49u32
§Note
Values are truncated, not rounded. For more precise control over rounding
behavior, use to_pixels_f64
and apply your preferred
rounding method.
Sourcepub fn from_pixels_u32(value: u32) -> Self
pub fn from_pixels_u32(value: u32) -> Self
Creates a Dp
value from physical pixels specified as a u32
.
This method converts an unsigned 32-bit integer pixel value to density-independent
pixels using the current global scale factor. The integer is first converted to
f64
for the calculation.
§Arguments
value
- The pixel value as an unsigned 32-bit integer
§Returns
A new Dp
instance representing the equivalent dp value.
If the scale factor hasn’t been initialized, defaults to 1.0 (no scaling).
§Examples
use tessera_ui::Dp;
// Convert 96 pixels to dp (assuming 2.0 scale factor = 48.0 dp)
let dp_value = Dp::from_pixels_u32(96);
Sourcepub fn to_pixels_f32(&self) -> f32
pub fn to_pixels_f32(&self) -> f32
Converts this dp value to physical pixels as an f32
.
This method applies the current global scale factor and converts the result
to a 32-bit floating-point number. This is commonly used for graphics APIs
that work with f32
coordinates.
§Returns
The equivalent value in physical pixels as a 32-bit floating-point number. If the scale factor hasn’t been initialized, defaults to 1.0 (no scaling).
§Examples
use tessera_ui::Dp;
let dp_value = Dp(24.0);
let pixels = dp_value.to_pixels_f32();
// With scale factor 1.5: 24.0 * 1.5 = 36.0f32
§Precision Note
Converting from f64
to f32
may result in precision loss for very large
or very precise values. For maximum precision, use to_pixels_f64
.
Sourcepub fn from_pixels_f32(value: f32) -> Self
pub fn from_pixels_f32(value: f32) -> Self
Creates a Dp
value from physical pixels specified as an f32
.
This method converts a 32-bit floating-point pixel value to density-independent
pixels using the current global scale factor. The f32
value is first converted
to f64
for internal calculations.
§Arguments
value
- The pixel value as a 32-bit floating-point number
§Returns
A new Dp
instance representing the equivalent dp value.
If the scale factor hasn’t been initialized, defaults to 1.0 (no scaling).
§Examples
use tessera_ui::Dp;
// Convert 36.0 pixels to dp (assuming 1.5 scale factor = 24.0 dp)
let dp_value = Dp::from_pixels_f32(36.0);
Sourcepub fn to_px(&self) -> Px
pub fn to_px(&self) -> Px
Converts this Dp
value to a Px
(physical pixels) value.
This method provides a convenient way to convert between the two pixel
types used in the Tessera framework. It applies the current scale factor
and creates a Px
instance from the result.
§Returns
A new Px
instance representing the equivalent physical pixel value.
§Examples
use tessera_ui::Dp;
let dp_value = Dp(24.0);
let px_value = dp_value.to_px();
// px_value now contains the scaled pixel equivalent
§See Also
Px::to_dp
- For the inverse conversionto_pixels_f32
- For directf32
pixel conversion
Trait Implementations§
Source§impl From<Dp> for DimensionValue
impl From<Dp> for DimensionValue
Source§impl From<Px> for Dp
impl From<Px> for Dp
Source§fn from(px: Px) -> Self
fn from(px: Px) -> Self
Creates a Dp
instance from a Px
(physical pixels) value.
This implementation enables seamless conversion between the two pixel types used in the Tessera framework. The conversion applies the inverse of the current scale factor to convert physical pixels back to density-independent pixels.
§Arguments
px
- APx
instance representing physical pixels
§Examples
use tessera_ui::{Dp, Px};
let px_value = Px::from_f32(48.0);
let dp_value: Dp = px_value.into();
// Or using From::from
let dp_value2 = Dp::from(px_value);
§See Also
to_px
- For the inverse conversionfrom_pixels_f64
- For direct pixel-to-dp conversion
Source§impl From<f64> for Dp
impl From<f64> for Dp
Source§fn from(value: f64) -> Self
fn from(value: f64) -> Self
Creates a Dp
instance from an f64
value.
This implementation allows for convenient conversion from floating-point
numbers to Dp
values using the into()
method or direct assignment
in contexts where type coercion occurs.
§Arguments
value
- The dp value as a 64-bit floating-point number
§Examples
use tessera_ui::Dp;
let dp1: Dp = 24.0.into();
let dp2 = Dp::from(16.0);
// In function calls that expect Dp
fn set_padding(padding: Dp) { /* ... */ }
set_padding(8.0.into());
Source§impl PartialOrd for Dp
impl PartialOrd for Dp
impl Copy for Dp
impl StructuralPartialEq for Dp
Auto Trait Implementations§
impl Freeze for Dp
impl RefUnwindSafe for Dp
impl Send for Dp
impl Sync for Dp
impl Unpin for Dp
impl UnwindSafe for Dp
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<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§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()
.