Skip to main content

cranpose_ui/modifier/
resolved_modifiers.rs

1use cranpose_ui_layout::{Alignment, HorizontalAlignment, IntrinsicSize, VerticalAlignment};
2
3use super::{Color, EdgeInsets, Point, RoundedCornerShape};
4
5/// A background fill: its color and, when the corners are rounded, their shape.
6#[derive(Clone, Copy, Debug, PartialEq)]
7pub struct ResolvedBackground {
8    pub(super) color: Color,
9    pub(super) shape: Option<RoundedCornerShape>,
10}
11
12impl ResolvedBackground {
13    /// Creates a background of `color` with the corner `shape`, or square corners when `shape` is
14    /// `None`.
15    pub fn new(color: Color, shape: Option<RoundedCornerShape>) -> Self {
16        Self { color, shape }
17    }
18
19    /// Returns the fill color.
20    pub fn color(&self) -> Color {
21        self.color
22    }
23
24    /// Returns the corner shape, or `None` for square corners.
25    pub fn shape(&self) -> Option<RoundedCornerShape> {
26        self.shape
27    }
28
29    /// Replaces the corner shape. `None` gives square corners.
30    pub fn set_shape(&mut self, shape: Option<RoundedCornerShape>) {
31        self.shape = shape;
32    }
33}
34
35/// The padding, layout properties and offset that a modifier chain resolves to.
36#[derive(Clone, Copy, Debug, PartialEq, Default)]
37pub struct ResolvedModifiers {
38    pub(super) padding: EdgeInsets,
39    pub(super) layout: LayoutProperties,
40    pub(super) offset: Point,
41}
42
43impl ResolvedModifiers {
44    /// Returns the padding on each edge, in logical pixels.
45    pub fn padding(&self) -> EdgeInsets {
46        self.padding
47    }
48
49    /// Returns the size, alignment and weight settings of the chain.
50    pub fn layout_properties(&self) -> LayoutProperties {
51        self.layout
52    }
53
54    /// Returns the offset of the content, in logical pixels.
55    pub fn offset(&self) -> Point {
56        self.offset
57    }
58
59    pub(crate) fn set_padding(&mut self, padding: EdgeInsets) {
60        self.padding = padding;
61    }
62
63    pub(crate) fn set_layout_properties(&mut self, layout: LayoutProperties) {
64        self.layout = layout;
65    }
66
67    pub(crate) fn set_offset(&mut self, offset: Point) {
68        self.offset = offset;
69    }
70}
71
72#[derive(Clone, Copy, Debug, Default, PartialEq)]
73pub enum DimensionConstraint {
74    #[default]
75    Unspecified,
76    Points(f32),
77    Fraction(f32),
78    Intrinsic(IntrinsicSize),
79}
80
81#[derive(Clone, Copy, Debug, Default, PartialEq)]
82pub struct LayoutWeight {
83    pub weight: f32,
84    pub fill: bool,
85}
86
87#[derive(Clone, Copy, Debug, Default, PartialEq)]
88pub struct LayoutProperties {
89    pub(super) padding: EdgeInsets,
90    pub(super) width: DimensionConstraint,
91    pub(super) height: DimensionConstraint,
92    pub(super) min_width: Option<f32>,
93    pub(super) min_height: Option<f32>,
94    pub(super) max_width: Option<f32>,
95    pub(super) max_height: Option<f32>,
96    pub(super) weight: Option<LayoutWeight>,
97    pub(super) box_alignment: Option<Alignment>,
98    pub(super) column_alignment: Option<HorizontalAlignment>,
99    pub(super) row_alignment: Option<VerticalAlignment>,
100    pub(super) row_baseline: bool,
101}
102
103impl LayoutProperties {
104    pub fn padding(&self) -> EdgeInsets {
105        self.padding
106    }
107
108    pub fn width(&self) -> DimensionConstraint {
109        self.width
110    }
111
112    pub fn height(&self) -> DimensionConstraint {
113        self.height
114    }
115
116    pub fn min_width(&self) -> Option<f32> {
117        self.min_width
118    }
119
120    pub fn min_height(&self) -> Option<f32> {
121        self.min_height
122    }
123
124    pub fn max_width(&self) -> Option<f32> {
125        self.max_width
126    }
127
128    pub fn max_height(&self) -> Option<f32> {
129        self.max_height
130    }
131
132    pub fn weight(&self) -> Option<LayoutWeight> {
133        self.weight
134    }
135
136    pub fn box_alignment(&self) -> Option<Alignment> {
137        self.box_alignment
138    }
139
140    pub fn column_alignment(&self) -> Option<HorizontalAlignment> {
141        self.column_alignment
142    }
143
144    pub fn row_alignment(&self) -> Option<VerticalAlignment> {
145        self.row_alignment
146    }
147
148    /// Whether the parent Row aligns this child's first baseline with its siblings.
149    pub fn row_baseline(&self) -> bool {
150        self.row_baseline
151    }
152}