pub struct Density { /* private fields */ }Expand description
The device pixel grid a layout measures against: how many device pixels a layout point is worth, and how the platform converts a text size.
This is Compose’s Density, and local_density is its LocalDensity.
Read it in a composable through density, which subscribes to the
composition local rather than to process-wide state, so a subtree given its
own density recomposes only what read it. Tests and goldens construct one
directly so a measurement does not depend on the machine it runs on.
Implementations§
Source§impl Density
impl Density
Sourcepub fn from_host() -> Self
pub fn from_host() -> Self
The grid the host installed on this shell.
This is the composition local’s default, not the way a composable should
read the density – use density for that, so a subtree provided its
own grid is honoured.
Sourcepub fn new(density: f32, font_scale: f32) -> Self
pub fn new(density: f32, font_scale: f32) -> Self
A grid stated outright, for a test or a golden. The setting is taken as a plain multiplier, which is what it is wherever the platform reports no conversion of its own.
Sourcepub fn with_curve(density: f32, font_scale: FontScaleCurve) -> Self
pub fn with_curve(density: f32, font_scale: FontScaleCurve) -> Self
A grid whose text size follows the platform’s own Sp conversion rather
than a multiplier. See crate::font_scale.
Sourcepub fn font_scale(self) -> f32
pub fn font_scale(self) -> f32
The user’s text size setting.
Sourcepub fn font_scale_curve(self) -> FontScaleCurve
pub fn font_scale_curve(self) -> FontScaleCurve
The conversion the platform performs for a size in Sp.
Sourcepub fn dp(self, value: f32) -> f32
pub fn dp(self, value: f32) -> f32
Dp.roundToPx(), expressed back in points: a dp length snapped to the
whole device pixel Compose would give it.
Sourcepub fn sp(self, value: f32) -> f32
pub fn sp(self, value: f32) -> f32
A text size in scale-independent pixels, snapped the same way.
Anything that must not move when the user changes their text size is
measured with Density::dp instead — that is the whole difference
between the two.
Sourcepub fn round(self, value: f32) -> f32
pub fn round(self, value: f32) -> f32
A length snapped to the nearest whole device pixel, exact halves up.
Sourcepub fn ceil(self, value: f32) -> f32
pub fn ceil(self, value: f32) -> f32
A length snapped up to a whole device pixel.
This is what a line box does with its own height, and what a container does with a content size it must not clip.
Sourcepub fn centre(self, available: f32, content: f32) -> f32
pub fn centre(self, available: f32, content: f32) -> f32
Centres content in available the way Compose does: on a whole pixel.
Arrangement.Center measures every child in whole pixels and rounds the
leading gap before placing any of them, so a column whose content is an
odd number of pixels tall starts on a pixel boundary rather than across
one. Halving in floats instead is the single easiest way to soften every
edge in a row.