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
//! Column widget implementation
#![allow(non_snake_case)]
use cranpose_core::NodeId;
use cranpose_ui_layout::{HorizontalAlignment, LinearArrangement};
use super::layout::Layout;
use crate::{composable, layout::policies::FlexMeasurePolicy, modifier::Modifier};
/// Specification for Column layout behavior.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ColumnSpec {
pub vertical_arrangement: LinearArrangement,
pub horizontal_alignment: HorizontalAlignment,
}
impl ColumnSpec {
pub fn new() -> Self {
Self::default()
}
pub fn vertical_arrangement(mut self, arrangement: LinearArrangement) -> Self {
self.vertical_arrangement = arrangement;
self
}
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,
}
}
}
/// A layout composable that places its children in a vertical sequence.
///
/// # When to use
/// Use `Column` to arrange items top-to-bottom. For horizontal arrangement, use [`Row`](crate::widgets::Row).
///
/// # Arguments
///
/// * `modifier` - Modifiers to apply to the column layout.
/// * `spec` - Configuration for vertical arrangement and horizontal alignment.
/// * `content` - The children composables to layout.
///
/// # Example
///
/// ```rust,ignore
/// Column(
/// Modifier::padding(16.0),
/// ColumnSpec::default().vertical_arrangement(LinearArrangement::spaced_by(8.0)),
/// || {
/// Text("Title", Modifier::empty());
/// Text("Subtitle", Modifier::empty());
/// }
/// );
/// ```
#[composable]
pub fn Column<F>(modifier: Modifier, spec: ColumnSpec, content: F) -> NodeId
where
F: FnMut() + 'static,
{
let policy = FlexMeasurePolicy::column(spec.vertical_arrangement, spec.horizontal_alignment);
Layout(modifier, policy, content)
}