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
78
79
//! Row widget implementation
use cranpose_core::NodeId;
use cranpose_ui_layout::{LinearArrangement, VerticalAlignment};
use super::layout::compose_layout;
use crate::{composable, layout::policies::FlexMeasurePolicy, modifier::Modifier};
/// Specification for Row layout behavior.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct RowSpec {
/// How children are spaced along the horizontal axis. Defaults to `Start`.
pub horizontal_arrangement: LinearArrangement,
/// How children align on the vertical axis. Defaults to `Top`.
pub vertical_alignment: VerticalAlignment,
}
impl RowSpec {
/// Creates a spec with start arrangement and top alignment.
pub fn new() -> Self {
Self::default()
}
/// Sets how children are spaced along the horizontal axis.
pub fn horizontal_arrangement(mut self, arrangement: LinearArrangement) -> Self {
self.horizontal_arrangement = arrangement;
self
}
/// Sets how children align on the vertical axis.
pub fn vertical_alignment(mut self, alignment: VerticalAlignment) -> Self {
self.vertical_alignment = alignment;
self
}
}
impl Default for RowSpec {
fn default() -> Self {
Self {
horizontal_arrangement: LinearArrangement::Start,
vertical_alignment: VerticalAlignment::Top,
}
}
}
/// Places children from left to right, or right to left for an RTL layout.
/// `spec` sets horizontal space and vertical alignment.
///
/// # Example
///
/// ```rust
/// use cranpose_ui::*;
///
/// #[composable]
/// fn Summary() {
/// Row(
/// Modifier::empty().fill_max_width(),
/// RowSpec::default().horizontal_arrangement(LinearArrangement::SpaceBetween),
/// || {
/// Text("Name", Modifier::empty(), TextStyle::default());
/// Text("Value", Modifier::empty(), TextStyle::default());
/// },
/// );
/// }
/// ```
#[composable]
pub fn Row<F>(modifier: Modifier, spec: RowSpec, content: F) -> NodeId
where
F: FnMut() + 'static,
{
let density = crate::density::density();
let mut policy = FlexMeasurePolicy::row(
spec.horizontal_arrangement,
spec.vertical_alignment,
density.density(),
);
policy.layout_direction = crate::layout_direction();
compose_layout(modifier, policy, density, content)
}