1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//! FlowRow widget implementation
use cranpose_core::NodeId;
use super::layout::Layout;
use crate::{composable, layout::policies::FlowRowMeasurePolicy, modifier::Modifier};
/// Specification for FlowRow layout behavior.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct FlowRowSpec {
/// Horizontal gap between adjacent children on the same line, in dp
/// (Compose: `horizontalArrangement = Arrangement.spacedBy(..)`).
pub main_axis_spacing: f32,
/// Vertical gap between consecutive lines, in dp
/// (Compose: `verticalArrangement = Arrangement.spacedBy(..)`).
pub cross_axis_spacing: f32,
}
impl FlowRowSpec {
pub fn new() -> Self {
Self::default()
}
pub fn main_axis_spacing(mut self, spacing: f32) -> Self {
self.main_axis_spacing = spacing;
self
}
pub fn cross_axis_spacing(mut self, spacing: f32) -> Self {
self.cross_axis_spacing = spacing;
self
}
}
impl Default for FlowRowSpec {
fn default() -> Self {
Self {
main_axis_spacing: 0.0,
cross_axis_spacing: 0.0,
}
}
}
/// Places children across a row and starts a new row when the available width is full.
/// The spec sets the space between children and between rows.
///
/// # Example
///
/// ```rust
/// use cranpose_ui::{
/// widgets::{FlowRow, FlowRowSpec},
/// *,
/// };
///
/// #[composable]
/// fn Topics() {
/// FlowRow(
/// Modifier::empty().fill_max_width(),
/// FlowRowSpec::new()
/// .main_axis_spacing(8.0)
/// .cross_axis_spacing(4.0),
/// || {
/// for label in ["Rust", "Compose", "Android"] {
/// Text(label, Modifier::empty().padding(8.0), TextStyle::default());
/// }
/// },
/// );
/// }
/// ```
#[composable]
pub fn FlowRow<F>(modifier: Modifier, spec: FlowRowSpec, content: F) -> NodeId
where
F: FnMut() + 'static,
{
let policy = FlowRowMeasurePolicy::new(spec.main_axis_spacing, spec.cross_axis_spacing);
Layout(modifier, policy, content)
}