Skip to main content

cranpose_ui/
density.rs

1//! Integral layout, the way Compose does it.
2//!
3//! Cranpose's layout is continuous: [`Constraints`](cranpose_ui_layout::Constraints)
4//! is four `f32`, a child is placed at an `f32` offset, and nothing in
5//! `cranpose-ui-layout` rounds. Compose's is integral — `Dp.roundToPx()` turns
6//! every padding, minimum size and spacing into an `Int` before anything is
7//! measured, and `Placeable.placeAt` takes an `IntOffset`.
8//!
9//! That difference is not cosmetic. Half a pixel of drift moves a line of
10//! text's antialiasing onto the next row, and it puts a soft edge everywhere
11//! the Compose build draws a crisp one. A widget that must draw a crisp edge
12//! therefore rounds its own sizes and offsets through [`Density`] rather than
13//! trusting the ambient float arithmetic. The Wear widgets do this throughout;
14//! the rest of the library does not, which is where a soft edge still comes
15//! from.
16//!
17//! One length unit runs through all of it: a Cranpose layout point is a dp.
18//! `Density` converts to and from device pixels and does the rounding on
19//! the pixel side, which is the only side where rounding means anything.
20
21use cranpose_core::{CompositionLocal, CompositionLocalProvider, compositionLocalOf};
22
23use crate::{
24    font_scale::FontScaleCurve,
25    render_state::{current_density, current_font_scale_curve},
26    round_scaling_list::round_to_px,
27};
28
29/// The device pixel grid a layout measures against: how many device pixels
30/// a layout point is worth, and how the platform converts a text size.
31///
32/// This is Compose's `Density`, and [`local_density`] is its `LocalDensity`.
33///
34/// Read it in a composable through [`density`], which subscribes to the
35/// composition local rather than to process-wide state, so a subtree given its
36/// own density recomposes only what read it. Tests and goldens construct one
37/// directly so a measurement does not depend on the machine it runs on.
38#[derive(Clone, Copy, Debug, PartialEq)]
39pub struct Density {
40    density: f32,
41    font_scale: FontScaleCurve,
42}
43
44impl Density {
45    /// The grid the host installed on this shell.
46    ///
47    /// This is the composition local's default, not the way a composable should
48    /// read the density -- use [`density`] for that, so a subtree provided its
49    /// own grid is honoured.
50    pub fn from_host() -> Self {
51        // A composition can run without a shell -- a test, a golden, a headless
52        // measurement -- and asking the render state for a grid there panics.
53        // The unit grid is the honest answer when no host has stated one.
54        if crate::render_state::has_current_app_context() {
55            Self::with_curve(current_density(), current_font_scale_curve())
56        } else {
57            Self::default()
58        }
59    }
60
61    /// A grid stated outright, for a test or a golden. The setting is taken as
62    /// a plain multiplier, which is what it is wherever the platform reports no
63    /// conversion of its own.
64    pub fn new(density: f32, font_scale: f32) -> Self {
65        let font_scale = if font_scale.is_finite() && font_scale > 0.0 {
66            font_scale
67        } else {
68            1.0
69        };
70        Self::with_curve(density, FontScaleCurve::linear(font_scale))
71    }
72
73    /// A grid whose text size follows the platform's own `Sp` conversion rather
74    /// than a multiplier. See [`crate::font_scale`].
75    pub fn with_curve(density: f32, font_scale: FontScaleCurve) -> Self {
76        Self {
77            density: if density.is_finite() && density > 0.0 {
78                density
79            } else {
80                1.0
81            },
82            font_scale,
83        }
84    }
85
86    /// Device pixels per layout point.
87    pub fn density(self) -> f32 {
88        self.density
89    }
90
91    /// The user's text size setting.
92    pub fn font_scale(self) -> f32 {
93        self.font_scale.scale()
94    }
95
96    /// The conversion the platform performs for a size in `Sp`.
97    pub fn font_scale_curve(self) -> FontScaleCurve {
98        self.font_scale
99    }
100
101    /// `Dp.roundToPx()`, expressed back in points: a dp length snapped to the
102    /// whole device pixel Compose would give it.
103    pub fn dp(self, value: f32) -> f32 {
104        round_to_px(value, self.density)
105    }
106
107    /// A text size in scale-independent pixels, snapped the same way.
108    ///
109    /// Anything that must not move when the user changes their text size is
110    /// measured with [`Density::dp`] instead — that is the whole difference
111    /// between the two.
112    pub fn sp(self, value: f32) -> f32 {
113        self.dp(self.font_scale.sp_to_dp(value))
114    }
115
116    /// A length snapped to the nearest whole device pixel, exact halves up.
117    pub fn round(self, value: f32) -> f32 {
118        round_to_px(value, self.density)
119    }
120
121    /// A length snapped down to a whole device pixel.
122    pub fn floor(self, value: f32) -> f32 {
123        if value.is_finite() {
124            (value * self.density).floor() / self.density
125        } else {
126            value
127        }
128    }
129
130    /// A length snapped up to a whole device pixel.
131    ///
132    /// This is what a line box does with its own height, and what a container
133    /// does with a content size it must not clip.
134    pub fn ceil(self, value: f32) -> f32 {
135        if value.is_finite() {
136            (value * self.density).ceil() / self.density
137        } else {
138            value
139        }
140    }
141
142    /// Points to device pixels.
143    pub fn to_px(self, points: f32) -> f32 {
144        points * self.density
145    }
146
147    /// Centres `content` in `available` the way Compose does: on a whole pixel.
148    ///
149    /// `Arrangement.Center` measures every child in whole pixels and rounds the
150    /// leading gap before placing any of them, so a column whose content is an
151    /// odd number of pixels tall starts on a pixel boundary rather than across
152    /// one. Halving in floats instead is the single easiest way to soften every
153    /// edge in a row.
154    pub fn centre(self, available: f32, content: f32) -> f32 {
155        self.round((available - content) * 0.5)
156    }
157}
158
159impl Default for Density {
160    fn default() -> Self {
161        Self::new(1.0, 1.0)
162    }
163}
164
165/// The production [`cranpose_ui_layout::MeasureScope`]: a grid captured at
166/// composition time, carried through measurement.
167///
168/// `MeasurePolicy::measure` runs with this as its scope, the way Compose's
169/// `MeasurePolicy.measure` runs with `MeasureScope` as its receiver. The grid
170/// is the [`LayoutNode`](crate::widgets::nodes::LayoutNode)'s own
171/// [`Density`] -- read at composition time by the `Layout` composable from
172/// [`density`], not the host default -- so a subtree given a different grid
173/// through [`ProvideDensity`] is measured on that grid.
174#[derive(Clone, Copy, Debug, PartialEq)]
175pub struct DensityMeasureScope {
176    density: Density,
177}
178
179impl DensityMeasureScope {
180    /// Wraps a captured [`Density`] as a measurement scope.
181    pub fn new(density: Density) -> Self {
182        Self { density }
183    }
184}
185
186impl cranpose_ui_layout::MeasureScope for DensityMeasureScope {
187    fn density(&self) -> f32 {
188        self.density.density()
189    }
190
191    fn font_scale(&self) -> f32 {
192        self.density.font_scale()
193    }
194}
195
196/// The [`CompositionLocal`] carrying the device pixel grid.
197///
198/// Compose's `LocalDensity`. Its default is whatever grid the host installed on
199/// this shell, so an application that never provides one still measures against
200/// the real screen; providing one scopes a different grid to a subtree, which
201/// is what a preview, a scaled container or a density-specific golden needs.
202pub fn local_density() -> CompositionLocal<Density> {
203    thread_local! {
204        static LOCAL: std::cell::RefCell<Option<CompositionLocal<Density>>> =
205            const { std::cell::RefCell::new(None) };
206    }
207    LOCAL.with(|cell| {
208        cell.borrow_mut()
209            .get_or_insert_with(|| compositionLocalOf(Density::from_host))
210            .clone()
211    })
212}
213
214/// The device pixel grid in force here.
215pub fn density() -> Density {
216    local_density().current()
217}
218
219/// Runs `content` on `density`.
220///
221/// Compose's `CompositionLocalProvider(LocalDensity provides ...)`. A preview, a
222/// scaled container or a golden that must not depend on the machine it runs on
223/// states its grid here instead of moving the whole shell onto it.
224#[allow(non_snake_case)]
225#[track_caller]
226pub fn ProvideDensity(density: Density, content: impl FnOnce()) {
227    CompositionLocalProvider(vec![local_density().provides(density)], content);
228}
229
230#[cfg(test)]
231mod tests {
232    use super::*;
233
234    #[test]
235    fn a_surviving_density_scope_keeps_its_entry_when_a_sibling_leaves() {
236        use std::{cell::Cell, rc::Rc};
237
238        use cranpose_core::{Composition, MemoryApplier, MutableState, location_key};
239
240        let mut composition = Composition::new(MemoryApplier::new());
241        let runtime = composition.runtime_handle();
242        let with_leading = MutableState::with_runtime(true, runtime.clone());
243        let survivor = MutableState::with_runtime(3.0_f32, runtime);
244        let seen = Rc::new(Cell::new(0.0_f32));
245
246        #[cranpose_macros::composable]
247        #[allow(non_snake_case)]
248        fn Reader(seen: Rc<Cell<f32>>) {
249            seen.set(density().density());
250        }
251
252        fn tree(with_leading: bool, survivor: f32, seen: Rc<Cell<f32>>) {
253            if with_leading {
254                ProvideDensity(Density::new(2.0, 1.0), || {});
255            }
256            ProvideDensity(Density::new(survivor, 1.0), move || Reader(seen));
257        }
258
259        #[cranpose_macros::composable]
260        #[allow(non_snake_case)]
261        fn Panel(
262            with_leading: MutableState<bool>,
263            survivor: MutableState<f32>,
264            seen: Rc<Cell<f32>>,
265        ) {
266            tree(with_leading.value(), survivor.value(), seen);
267        }
268
269        let key = location_key(file!(), line!(), column!());
270        {
271            let seen = Rc::clone(&seen);
272            composition
273                .render(key, move || Panel(with_leading, survivor, Rc::clone(&seen)))
274                .expect("initial composition");
275        }
276        assert_eq!(seen.get(), 3.0);
277
278        with_leading.set_value(false);
279        while composition
280            .process_invalid_scopes()
281            .expect("drop the leading density scope")
282        {}
283        survivor.set_value(4.0);
284        while composition
285            .process_invalid_scopes()
286            .expect("change the surviving density")
287        {}
288        assert_eq!(
289            seen.get(),
290            4.0,
291            "the surviving density scope must keep its entry and its reader's \
292             subscription when the sibling leaves"
293        );
294    }
295
296    /// Density was a value read from the shell, so a subtree could not be
297    /// measured on a different grid. Reading it through the local is what lets a
298    /// preview or a golden state its own grid without moving the whole shell.
299    #[test]
300    fn a_provided_density_reaches_the_content_and_ends_with_it() {
301        use std::{cell::Cell, rc::Rc};
302
303        use cranpose_core::{Composition, MemoryApplier, location_key};
304
305        let mut composition = Composition::new(MemoryApplier::new());
306        let outer = Rc::new(Cell::new(0.0_f32));
307        let inside = Rc::new(Cell::new(0.0_f32));
308        let after = Rc::new(Cell::new(0.0_f32));
309
310        let key = location_key(file!(), line!(), column!());
311        {
312            let (outer, inside, after) = (Rc::clone(&outer), Rc::clone(&inside), Rc::clone(&after));
313            let mut render = move || {
314                outer.set(density().density());
315                ProvideDensity(Density::new(3.0, 1.0), || inside.set(density().density()));
316                after.set(density().density());
317            };
318            composition.render(key, &mut render).expect("render");
319        }
320
321        assert_eq!(inside.get(), 3.0, "the subtree reads the grid it was given");
322        assert_ne!(
323            outer.get(),
324            3.0,
325            "the provision does not escape upwards out of its content"
326        );
327        assert_eq!(
328            after.get(),
329            outer.get(),
330            "nor does it leak into what follows it"
331        );
332    }
333
334    #[test]
335    fn a_dp_length_lands_on_a_whole_device_pixel() {
336        let density = Density::new(2.0, 1.0);
337        assert_eq!(density.dp(13.3), 13.5);
338        assert_eq!(density.to_px(density.dp(13.3)), 27.0);
339        // An exact half goes up, as Kotlin's `roundToInt` does.
340        assert_eq!(density.dp(0.25), 0.5);
341    }
342
343    #[test]
344    fn a_text_size_moves_with_the_font_scale_and_a_dp_length_does_not() {
345        let density = Density::new(2.0, 1.24);
346        assert_eq!(density.dp(15.0), 15.0);
347        // 15sp at 1.24 is 18.6dp = 37.2px, which is 37px.
348        assert_eq!(density.to_px(density.sp(15.0)), 37.0);
349    }
350
351    #[test]
352    fn centring_puts_the_leading_gap_on_a_pixel_boundary() {
353        let density = Density::new(2.0, 1.0);
354        // 104px of room, 93px of content: 5.5px above, which rounds up to a
355        // whole 6px rather than landing across a pixel boundary.
356        let leading = density.centre(52.0, 46.5);
357        assert_eq!(density.to_px(leading), 6.0);
358    }
359
360    #[test]
361    fn floor_and_ceil_move_whole_pixels_only() {
362        let density = Density::new(2.0, 1.0);
363        assert_eq!(density.floor(13.3), 13.0);
364        assert_eq!(density.ceil(13.3), 13.5);
365        assert_eq!(density.ceil(13.5), 13.5);
366    }
367
368    #[test]
369    fn a_nonsense_grid_falls_back_to_one_rather_than_dividing_by_zero() {
370        let density = Density::new(0.0, f32::NAN);
371        assert_eq!(density.density(), 1.0);
372        assert_eq!(density.font_scale(), 1.0);
373        assert_eq!(density.dp(3.7), 4.0);
374    }
375}