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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use crate::prelude::*;
use ribir_core::prelude::*;
#[derive(Declare, Default)]
pub struct Lists {
#[declare(default = false)]
divider: bool,
}
#[derive(Clone, PartialEq)]
pub enum EdgePosition {
Frist,
Last,
None,
}
#[derive(Clone, Declare)]
pub struct ListItemStyle {
#[declare(default = false)]
pub divider: bool,
pub edge: EdgePosition,
}
impl ComposeStyle for ListItemStyle {
type Host = Widget;
#[inline]
fn compose_style(_this: Stateful<Self>, host: Self::Host) -> Widget
where
Self: Sized,
{
host
}
}
pub struct HeadlineText(pub CowArc<str>);
impl HeadlineText {
pub fn new(v: impl Into<CowArc<str>>) -> Self { HeadlineText(v.into()) }
}
pub struct SupportingText(pub CowArc<str>);
impl SupportingText {
pub fn new(v: impl Into<CowArc<str>>) -> Self { SupportingText(v.into()) }
}
#[derive(Default, Declare)]
pub struct ListItem;
#[derive(Template)]
pub struct ListItemTemplate {
headline_text: HeadlineText,
supporting_text: Option<SupportingText>,
leading: Option<WidgetOf<Leading>>,
trailing: Option<WidgetOf<Trailing>>,
}
impl ComposeChild for ListItem {
type Child = ListItemTemplate;
fn compose_child(_this: State<Self>, child: Self::Child) -> Widget {
let ListItemTemplate {
headline_text,
supporting_text,
leading,
trailing,
} = child;
widget! {
init ctx => {
let surface_variant = Brush::Color(Palette::of(ctx).on_surface_variant());
let body_text_style = TypographyTheme::of(ctx).body1.text.clone();
}
Row {
DynWidget {
dyns: leading.map(|w| w.child)
}
Expanded {
flex: 1.,
Column {
DynWidget {
dyns: widget! {
Text {
text: headline_text.0,
style: TextStyle {
foreground: surface_variant,
..body_text_style
}
}
}
}
DynWidget {
dyns: supporting_text.map(|text| {
widget! {
Text {
text: text.0
}
}
})
}
}
}
DynWidget {
dyns: trailing.map(|w| w.child)
}
}
}
}
}
impl ComposeChild for Lists {
type Child = Vec<Widget>;
fn compose_child(this: State<Self>, children: Self::Child) -> Widget {
let last_idx = children.len() - 1;
widget! {
states {
this: this.into_readonly()
}
Column {
DynWidget {
dyns: children.into_iter().enumerate().map(move |(idx, w)| {
let edge = if idx == 0 {
EdgePosition::Frist
} else if idx == last_idx {
EdgePosition::Last
} else {
EdgePosition::None
};
widget! {
ListItemStyle {
divider: this.divider,
edge,
DynWidget {
dyns: w
}
}
}
})
}
}
}
}
}