Skip to main content

cranpose_ui/widgets/
padding_values.rs

1//! Compose's `PaddingValues`: four edge insets in reading order.
2
3use cranpose_ui_graphics::EdgeInsets;
4
5use crate::{
6    Modifier,
7    layout_direction::{LayoutDirection, layout_direction},
8};
9
10/// Padding on four edges, in reading order, such as a scaffold's insets for
11/// its content slot or a lazy list's content padding.
12///
13/// `start` and `end` are the reading-order edges: in a left-to-right layout
14/// `start` is the left edge, in a right-to-left layout it is the right one.
15#[derive(Clone, Copy, Debug, Default, PartialEq)]
16pub struct PaddingValues {
17    pub start: f32,
18    pub top: f32,
19    pub end: f32,
20    pub bottom: f32,
21}
22
23impl PaddingValues {
24    pub const fn new(start: f32, top: f32, end: f32, bottom: f32) -> Self {
25        Self {
26            start,
27            top,
28            end,
29            bottom,
30        }
31    }
32
33    /// The same padding on every edge.
34    pub const fn all(value: f32) -> Self {
35        Self::new(value, value, value, value)
36    }
37
38    /// `horizontal` on the start and end edges, `vertical` on the top and
39    /// bottom ones: Compose's `PaddingValues(horizontal, vertical)`.
40    pub const fn symmetric(horizontal: f32, vertical: f32) -> Self {
41        Self::new(horizontal, vertical, horizontal, vertical)
42    }
43
44    /// Applies these values in the composition's current reading order.
45    pub fn apply_to(self, modifier: Modifier) -> Modifier {
46        self.apply_to_in(modifier, layout_direction())
47    }
48
49    /// Applies these values against an explicit reading order.
50    pub fn apply_to_in(self, modifier: Modifier, direction: LayoutDirection) -> Modifier {
51        modifier.padding_relative_in(direction, self.start, self.top, self.end, self.bottom)
52    }
53
54    /// The physical left/top/right/bottom edges in `direction`.
55    pub fn physical(self, direction: LayoutDirection) -> EdgeInsets {
56        let (left, right) = direction.resolve(self.start, self.end);
57        EdgeInsets::from_components(left, self.top, right, self.bottom)
58    }
59
60    /// The larger of each edge, used to merge bar heights with window insets.
61    pub fn max(self, other: Self) -> Self {
62        Self::new(
63            self.start.max(other.start),
64            self.top.max(other.top),
65            self.end.max(other.end),
66            self.bottom.max(other.bottom),
67        )
68    }
69}