Struct Dp

Source
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

Source

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

pub const fn new(value: f64) -> Self

Creates a new Dp instance with the specified value.

This is a const function, allowing Dp values to be created at compile time.

§Arguments
  • value - The dp value as a floating-point number
§Examples
use tessera_ui::Dp;

const BUTTON_HEIGHT: Dp = Dp::new(48.0);
let padding = Dp::new(16.0);
Source

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
Source

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

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.

Source

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

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.

Source

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

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

Trait Implementations§

Source§

impl Clone for Dp

Source§

fn clone(&self) -> Dp

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 Dp

Source§

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

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

impl Default for Dp

Source§

fn default() -> Dp

Returns the “default value” for a type. Read more
Source§

impl From<Dp> for DimensionValue

Source§

fn from(value: Dp) -> Self

Converts a Dp value to a DimensionValue::Fixed.

Source§

impl From<Dp> for Px

Source§

fn from(dp: Dp) -> Self

Converts to this type from the input type.
Source§

impl From<Px> for Dp

Source§

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 - A Px 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
Source§

impl From<f64> for Dp

Source§

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 PartialEq for Dp

Source§

fn eq(&self, other: &Dp) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Dp

Source§

fn partial_cmp(&self, other: &Dp) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Copy for Dp

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Returns a reference to the concrete type as &dyn Any.
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
§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

§

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

§

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

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

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

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

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

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

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

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

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

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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

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

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
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.
§

impl<T> Upcast<T> for T

§

fn upcast(&self) -> Option<&T>

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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

impl<T> WasmNotSend for T
where T: Send,

§

impl<T> WasmNotSendSync for T
where T: WasmNotSend + WasmNotSync,

§

impl<T> WasmNotSync for T
where T: Sync,