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
//! Column widget implementation
use cranpose_core::NodeId;
use cranpose_ui_layout::{HorizontalAlignment, LinearArrangement};
use super::layout::compose_layout;
use crate::{composable, layout::policies::FlexMeasurePolicy, modifier::Modifier};
/// Specification for Column layout behavior.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ColumnSpec {
/// How children are spaced along the vertical axis. Defaults to `Start`.
pub vertical_arrangement: LinearArrangement,
/// How children align on the horizontal axis. Defaults to `Start`.
pub horizontal_alignment: HorizontalAlignment,
}
impl ColumnSpec {
/// Creates a spec with start arrangement and start alignment.
pub fn new() -> Self {
Self::default()
}
/// Sets how children are spaced along the vertical axis.
pub fn vertical_arrangement(mut self, arrangement: LinearArrangement) -> Self {
self.vertical_arrangement = arrangement;
self
}
/// Sets how children align on the horizontal axis.
pub fn horizontal_alignment(mut self, alignment: HorizontalAlignment) -> Self {
self.horizontal_alignment = alignment;
self
}
}
impl Default for ColumnSpec {
fn default() -> Self {
Self {
vertical_arrangement: LinearArrangement::Start,
horizontal_alignment: HorizontalAlignment::Start,
}
}
}
/// Places children from top to bottom. `spec` sets vertical space and horizontal alignment.
/// Use [`Row`](crate::Row) for a horizontal sequence.
///
/// # Example
///
/// ```rust
/// use cranpose_ui::*;
///
/// #[composable]
/// fn Details() {
/// Column(
/// Modifier::empty().padding(16.0),
/// ColumnSpec::default().vertical_arrangement(LinearArrangement::spaced_by(8.0)),
/// || {
/// Text("Title", Modifier::empty(), TextStyle::default());
/// Text("Subtitle", Modifier::empty(), TextStyle::default());
/// },
/// );
/// }
/// ```
#[composable]
pub fn Column<F>(modifier: Modifier, spec: ColumnSpec, content: F) -> NodeId
where
F: FnMut() + 'static,
{
let density = crate::density::density();
let mut policy = FlexMeasurePolicy::column(
spec.vertical_arrangement,
spec.horizontal_alignment,
density.density(),
);
policy.layout_direction = crate::layout_direction();
compose_layout(modifier, policy, density, content)
}