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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
//! [`ExpansionTile`]: a tile that opens to show more.
//!
//! It is a [`crate::ListTile`] with a chevron and a body underneath — which is what the
//! reference's is, and the reason it is built out of one here rather than painting a
//! header of its own. Everything a tile can carry, this carries: something in front, a
//! subtitle, a trailing widget of your own in place of the chevron, the tile's
//! measurements and its colours.
use std::cell::{OnceCell, RefCell};
use frus_core::{BorderRadius, Color, Insets, Rect, Scene, ShapeBorder};
use frus_layout::{FlexDirection, Style};
use crate::icons::Icons;
use crate::interaction::Status;
use crate::theme::Theme;
use crate::widget::Widget;
use crate::{Container, Icon, ListTile};
/// Which side the chevron sits on — the reference's `ListTileControlAffinity`.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub enum ControlAffinity {
/// In front of the title, where a checkbox would go. What a file tree wants: the
/// chevrons line up down the left and the indentation reads as a hierarchy.
Leading,
/// After it, at the end of the row. The reference's default, and this one's.
#[default]
Trailing,
}
/// The chevron's side, matching the reference's `expand_more`.
const CHEVRON: f32 = 24.0;
/// The room kept round what a tile hides while it is showing. Nothing above it, so the
/// body sits against the row; sixteen on the other three sides.
const CHILDREN_PADDING: Insets = Insets::new(0.0, 16.0, 16.0, 16.0);
/// A tile that opens to show more.
///
/// **Controlled**, like everything else here: it is told whether it is open and emits a
/// message asking to change, rather than keeping the answer to itself. That is what lets
/// a column of them behave as an accordion — the application decides only one is open —
/// without the widget knowing anything about its siblings.
///
/// ```
/// # use frus_widgets::{ExpansionTile, Text};
/// # #[derive(Clone)] enum Msg { Toggle }
/// ExpansionTile::new("Delivery", true, Msg::Toggle)
/// .subtitle("Arrives Thursday")
/// .content(Text::new("Two parcels, one signature."));
/// ```
pub struct ExpansionTile<Msg = crate::callback::Callback> {
title: String,
/// A header **widget** in place of the title line; see
/// [`ExpansionTile::title_child`].
title_widget: RefCell<Option<Box<dyn Widget<Msg>>>>,
open: bool,
on_toggle: Msg,
subtitle: Option<String>,
leading: RefCell<Option<Box<dyn Widget<Msg>>>>,
trailing: RefCell<Option<Box<dyn Widget<Msg>>>>,
content: RefCell<Option<Box<dyn Widget<Msg>>>>,
show_trailing_icon: bool,
affinity: ControlAffinity,
dense: bool,
tile_padding: Option<Insets>,
/// **An `Option`, not an `Insets`.** A bare `Insets` cannot tell *nothing was said*
/// from *zero on every side*, so a theme rung under it could never fire {em} the same
/// trap `AccessibilityOverrides` was built to avoid in milestone 407.
children_padding: Option<Insets>,
background: Option<Color>,
collapsed_background: Option<Color>,
text_color: Option<Color>,
collapsed_text_color: Option<Color>,
icon_color: Option<Color>,
collapsed_icon_color: Option<Color>,
shape: Option<ShapeBorder>,
collapsed_shape: Option<ShapeBorder>,
/// `[tile]`, or `[tile, body]` when it is open and has one. Assembled on the first
/// walk, as [`crate::ListTile`] assembles its row, so that the order the builders
/// were called in cannot change what comes out.
built: OnceCell<Vec<Box<dyn Widget<Msg>>>>,
}
impl<Msg: Clone + 'static> ExpansionTile<Msg> {
/// A tile with a title, whether it is open, and the message that asks to change that.
pub fn new(title: impl Into<String>, open: bool, on_toggle: Msg) -> Self {
Self {
title: title.into(),
title_widget: RefCell::new(None),
open,
on_toggle,
subtitle: None,
leading: RefCell::new(None),
trailing: RefCell::new(None),
content: RefCell::new(None),
show_trailing_icon: true,
affinity: ControlAffinity::default(),
dense: false,
tile_padding: None,
children_padding: None,
background: None,
collapsed_background: None,
text_color: None,
collapsed_text_color: None,
icon_color: None,
collapsed_icon_color: None,
shape: None,
collapsed_shape: None,
built: OnceCell::new(),
}
}
/// The first line as a **widget**, for a header the tile would not have made — a name
/// with a badge beside it, a row of chips. The reference's `headerBuilder`.
///
/// The chevron, the tap target and the row around it stay the tile's: what this
/// replaces is the line of text, not the row it sits in.
pub fn title_child(self, title: impl Widget<Msg> + 'static) -> Self {
self.title_child_boxed(Box::new(title))
}
/// The same, taking an **already boxed** widget — what a list of panels has to hand.
pub fn title_child_boxed(self, title: Box<dyn Widget<Msg>>) -> Self {
*self.title_widget.borrow_mut() = Some(title);
self
}
/// The body, shown only while the tile is open.
///
/// It is not built when the tile is shut — the reference's `maintainState` is what
/// asks for the other behaviour, and there is nothing here that a closed subtree
/// would be keeping alive.
pub fn content(self, content: impl Widget<Msg> + 'static) -> Self {
*self.content.borrow_mut() = Some(Box::new(content));
self
}
/// A second line under the title.
pub fn subtitle(mut self, subtitle: impl Into<String>) -> Self {
self.subtitle = Some(subtitle.into());
self
}
/// What goes in front of the title: an icon, an avatar, a checkbox.
///
/// With [`ControlAffinity::Leading`] the chevron takes this place, and whatever is
/// given here is dropped rather than fought over — the reference does the same, and
/// two things claiming one slot is a bug the layout cannot report.
pub fn leading(self, leading: impl Widget<Msg> + 'static) -> Self {
*self.leading.borrow_mut() = Some(Box::new(leading));
self
}
/// A widget of your own at the end of the row, **in place of** the chevron: a badge,
/// a count, a switch. It replaces the chevron rather than joining it.
pub fn trailing(self, trailing: impl Widget<Msg> + 'static) -> Self {
*self.trailing.borrow_mut() = Some(Box::new(trailing));
self
}
/// Hides the chevron altogether, for a tile whose open state is read from something
/// else on the row.
pub fn show_trailing_icon(mut self, show: bool) -> Self {
self.show_trailing_icon = show;
self
}
/// Which side the chevron sits on; the end of the row by default.
pub fn control_affinity(mut self, affinity: ControlAffinity) -> Self {
self.affinity = affinity;
self
}
/// A **dense** tile: the same shape, less room around it.
pub fn dense(mut self) -> Self {
self.dense = true;
self
}
/// The room inside the tile row itself.
pub fn tile_padding(mut self, padding: Insets) -> Self {
self.tile_padding = Some(padding);
self
}
/// The room around the body, inside the tile's width.
pub fn children_padding(mut self, padding: Insets) -> Self {
self.children_padding = Some(padding);
self
}
/// The row's background **while open**.
pub fn background_color(mut self, color: Color) -> Self {
self.background = Some(color);
self
}
/// The row's background while shut.
pub fn collapsed_background_color(mut self, color: Color) -> Self {
self.collapsed_background = Some(color);
self
}
/// The title's colour while open.
pub fn text_color(mut self, color: Color) -> Self {
self.text_color = Some(color);
self
}
/// The title's colour while shut.
pub fn collapsed_text_color(mut self, color: Color) -> Self {
self.collapsed_text_color = Some(color);
self
}
/// The chevron's colour while open.
pub fn icon_color(mut self, color: Color) -> Self {
self.icon_color = Some(color);
self
}
/// The chevron's colour while shut.
pub fn collapsed_icon_color(mut self, color: Color) -> Self {
self.collapsed_icon_color = Some(color);
self
}
/// **What shape the row is while the tile is open**, over the theme's and the
/// framework's. The row is a [`ListTile`](crate::ListTile), which has taken a shape
/// since milestone 457 — its surface **and** its ink, so a rounded tile does not
/// splash square corners.
#[must_use]
pub fn shape(mut self, shape: ShapeBorder) -> Self {
self.shape = Some(shape);
self
}
/// And while it is shut. A tile that says nothing here is **not** given
/// [`shape`](Self::shape): the reference keeps the two apart
/// (`expansion_tile_theme.dart:55`), and square-when-shut, rounded-when-open is a
/// design rather than an oversight.
#[must_use]
pub fn collapsed_shape(mut self, shape: ShapeBorder) -> Self {
self.collapsed_shape = Some(shape);
self
}
/// The shorthand for a rounded rectangle on **both** states, which is what an
/// application usually means.
#[must_use]
pub fn radius(self, radius: impl Into<BorderRadius>) -> Self {
let shape = ShapeBorder::rounded(radius.into());
self.shape(shape).collapsed_shape(shape)
}
/// The chevron, pointing down when the tile is open and along the reading direction
/// when it is shut — the same pair the section header drew before this was a tile.
fn chevron(&self, theme: &Theme) -> Option<Box<dyn Widget<Msg>>> {
if !self.show_trailing_icon {
return None;
}
let t = &theme.widgets.expansion_tile;
let color = if self.open {
self.icon_color.or(t.icon_color)
} else {
self.collapsed_icon_color.or(t.collapsed_icon_color)
}
.unwrap_or(theme.scheme.on_surface_variant);
let name = if self.open {
Icons::EXPAND_MORE
} else {
Icons::CHEVRON_RIGHT
};
Some(Box::new(Icon::new(name).size(CHEVRON).color(color)) as Box<dyn Widget<Msg>>)
}
/// Assembles the row and, when it is open, the body under it.
fn assemble(&self, theme: &Theme) -> Vec<Box<dyn Widget<Msg>>> {
let chevron = self.chevron(theme);
let given_leading = self.leading.borrow_mut().take();
let given_trailing = self.trailing.borrow_mut().take();
// The chevron claims one slot, and whichever slot it claims wins: a widget given
// for the same side is dropped rather than stacked, which is what the reference
// does and the only answer a row of fixed slots can give.
let (leading, trailing) = match self.affinity {
ControlAffinity::Leading => (chevron.or(given_leading), given_trailing),
ControlAffinity::Trailing => (given_leading, given_trailing.or(chevron)),
};
let mut tile = ListTile::new().on_tap(self.on_toggle.clone());
tile = match self.title_widget.borrow_mut().take() {
Some(header) => tile.title_child(crate::ConstrainedBox::new_boxed(header)),
None => tile.title(self.title.clone()),
};
if let Some(subtitle) = &self.subtitle {
tile = tile.subtitle(subtitle.clone());
}
if let Some(leading) = leading {
tile = tile.leading(crate::ConstrainedBox::new_boxed(leading));
}
if let Some(trailing) = trailing {
tile = tile.trailing(crate::ConstrainedBox::new_boxed(trailing));
}
if self.dense {
tile = tile.dense();
}
let t = &theme.widgets.expansion_tile;
if let Some(padding) = self.tile_padding.or(t.tile_padding) {
tile = tile.padding(padding);
}
if let Some(color) = if self.open {
self.background.or(t.background)
} else {
self.collapsed_background.or(t.collapsed_background)
} {
tile = tile.tile_color(color);
}
if let Some(color) = if self.open {
self.text_color.or(t.text_color)
} else {
self.collapsed_text_color.or(t.collapsed_text_color)
} {
let mut style = theme.text.body_large;
style.color = Some(color);
tile = tile.title_style(style);
}
// The two shapes stay apart on the way down as they do in the theme: a tile that
// named only one of them gets that one in that state and the framework's in the
// other.
if let Some(shape) = if self.open {
self.shape.or(t.shape)
} else {
self.collapsed_shape.or(t.collapsed_shape)
} {
tile = tile.shape(shape);
}
let mut out: Vec<Box<dyn Widget<Msg>>> = vec![Box::new(tile)];
if self.open {
if let Some(content) = self.content.borrow_mut().take() {
let pad = self
.children_padding
.or(t.children_padding)
.unwrap_or(CHILDREN_PADDING);
out.push(Box::new(
Container::new()
.padding_each(pad.top, pad.right, pad.bottom, pad.left)
.child(crate::ConstrainedBox::new_boxed(content)),
));
}
}
out
}
}
impl<Msg: Clone + 'static> Widget<Msg> for ExpansionTile<Msg> {
fn style(&self) -> Style {
Style {
// **No** width of its own: a cross axis left at `Auto` is stretched by the
// column above it, and an explicit percentage would overrule that stretch
// and resolve against a parent that is not definite yet — which comes out
// as a hug, with the chevron against the last letter of the title.
flex_direction: FlexDirection::Column,
..Default::default()
}
}
/// Assembled on the way down, under the theme the tile actually sits in: it reaches
/// for text styles and colours, and a tile built at construction time inside a
/// [`crate::Themed`] subtree would come out in the wrong palette.
fn build_themed(&self, theme: &Theme) {
self.built.get_or_init(|| self.assemble(theme));
}
fn children(&self) -> &[Box<dyn Widget<Msg>>] {
self.built.get().map(|v| &v[..]).unwrap_or(&[])
}
fn paint(&self, _bounds: Rect, _status: Status, _theme: &Theme, _scene: &mut Scene) {}
fn on_click(&self) -> Option<Msg> {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{build_ui, Runtime, Size, Text};
use frus_core::{Color, Primitive};
#[derive(Clone, Debug, PartialEq)]
enum Msg {
Toggle,
Other,
}
/// A tile as the walk sees it: assembled under a theme, children resolved.
fn assembled(tile: &ExpansionTile<Msg>) -> &[Box<dyn Widget<Msg>>] {
Widget::build_themed(tile, &Theme::default());
Widget::children(tile)
}
/// The row's surface and its corners, as the tile actually paints them.
fn row_of(tile: &ExpansionTile<Msg>, theme: &Theme) -> (Color, frus_core::BorderRadius) {
let ui = build_ui(tile, Size::new(300.0, 300.0), &Runtime::default(), theme);
fn find(primitives: &[frus_core::Primitive]) -> Option<(Color, frus_core::BorderRadius)> {
for p in primitives {
match p {
frus_core::Primitive::Rect {
color,
radius,
border_width,
..
} if *border_width == 0.0 && color.a > 0.0 => return Some((*color, *radius)),
frus_core::Primitive::Layer { primitives, .. } => {
if let Some(found) = find(primitives) {
return Some(found);
}
}
_ => {}
}
}
None
}
find(ui.scene().primitives()).expect("the row paints a surface")
}
/// **Every property was a builder and none of them was a theme.** A settings screen
/// is a column of these, so an application that wants its open sections tinted said
/// so on each of them — the same gap `ListTile` had until milestone 458, on the
/// widget that is usually built *out of* list tiles.
#[test]
fn a_theme_answers_for_every_expansion_tile() {
let mut theme = Theme::default();
theme.widgets.expansion_tile.background = Some(Color::rgb(0.2, 0.4, 0.6));
theme.widgets.expansion_tile.collapsed_background = Some(Color::rgb(0.6, 0.4, 0.2));
theme.widgets.expansion_tile.shape = Some(ShapeBorder::rounded(7.0));
theme.widgets.expansion_tile.collapsed_shape = Some(ShapeBorder::rounded(3.0));
let open = ExpansionTile::new("Delivery", true, Msg::Toggle);
assert_eq!(
row_of(&open, &theme),
(
Color::rgb(0.2, 0.4, 0.6),
frus_core::BorderRadius::uniform(7.0)
),
"the open answers"
);
let shut = ExpansionTile::new("Delivery", false, Msg::Toggle);
assert_eq!(
row_of(&shut, &theme),
(
Color::rgb(0.6, 0.4, 0.2),
frus_core::BorderRadius::uniform(3.0)
),
"and the shut ones, which are a different set and not a fallback"
);
// The tile outranks it.
let told = ExpansionTile::new("Delivery", true, Msg::Toggle)
.background_color(Color::rgb(0.9, 0.9, 0.9))
.shape(ShapeBorder::rounded(1.0));
assert_eq!(
row_of(&told, &theme),
(
Color::rgb(0.9, 0.9, 0.9),
frus_core::BorderRadius::uniform(1.0)
)
);
}
/// **A shut tile does not borrow the open one's shape.** The reference keeps `shape`
/// and `collapsedShape` apart (`expansion_tile_theme.dart:55`), and square-when-shut,
/// rounded-when-open is a design rather than an oversight — the same reasoning as
/// the drawer's `end_shape` in milestone 456.
#[test]
fn a_shut_tile_does_not_borrow_the_open_one_s_shape() {
let mut theme = Theme::default();
theme.widgets.expansion_tile.shape = Some(ShapeBorder::rounded(9.0));
theme.widgets.expansion_tile.collapsed_background = Some(Color::rgb(0.5, 0.5, 0.5));
let shut = ExpansionTile::new("Delivery", false, Msg::Toggle);
assert_eq!(
row_of(&shut, &theme).1,
frus_core::BorderRadius::ZERO,
"an open tile's shape is not a shut one's"
);
// And `radius` is the shorthand that says both.
let both = ExpansionTile::new("Delivery", false, Msg::Toggle)
.collapsed_background_color(Color::rgb(0.5, 0.5, 0.5))
.radius(5.0);
assert_eq!(
row_of(&both, &Theme::default()).1,
frus_core::BorderRadius::uniform(5.0)
);
}
/// **`children_padding` could not be told to be zero.** It was a bare `Insets` with a
/// value baked in, so *nothing was said* and *zero on every side* were the same
/// answer, and a theme rung under it could never have fired. An `Option` is the fix,
/// and it is the trap `AccessibilityOverrides` was built to avoid in milestone 407.
#[test]
fn the_room_round_the_body_can_be_told_to_be_nothing() {
let theme = Theme::default();
let body_padding = |tile: &ExpansionTile<Msg>, theme: &Theme| {
// The children exist only once the tile has been walked under a theme, and a
// `OnceCell` means once: each case below builds its own tile.
Widget::<Msg>::build_themed(tile, theme);
let children = Widget::<Msg>::children(tile);
assert_eq!(children.len(), 2, "an open tile with a body");
children[1].style_themed(theme).padding
};
let plain = ExpansionTile::new("Delivery", true, Msg::Toggle).content(Text::new("Two"));
assert_eq!(body_padding(&plain, &theme), CHILDREN_PADDING);
let mut themed = Theme::default();
themed.widgets.expansion_tile.children_padding = Some(Insets::uniform(4.0));
let from_theme =
ExpansionTile::new("Delivery", true, Msg::Toggle).content(Text::new("Two"));
assert_eq!(body_padding(&from_theme, &themed), Insets::uniform(4.0));
let none = ExpansionTile::new("Delivery", true, Msg::Toggle)
.content(Text::new("Two"))
.children_padding(Insets::ZERO);
assert_eq!(
body_padding(&none, &themed),
Insets::ZERO,
"zero is an answer, not a silence"
);
}
/// Shut, only the row is built — the body is not a subtree kept alive out of sight.
#[test]
fn a_shut_tile_builds_only_its_row() {
let tile = ExpansionTile::new("Title", false, Msg::Toggle).content(Text::new("hidden"));
assert_eq!(assembled(&tile).len(), 1);
}
/// Open, the body follows the row and the row asks to close.
#[test]
fn an_open_tile_carries_its_body_and_the_row_toggles() {
let tile = ExpansionTile::new("Title", true, Msg::Toggle).content(Text::new("visible"));
let children = assembled(&tile);
assert_eq!(children.len(), 2);
assert_eq!(children[0].on_click(), Some(Msg::Toggle));
}
/// It is a `ListTile`, so it is the reference's height rather than a header's guess.
#[test]
fn the_row_is_a_list_tile() {
let one = ExpansionTile::<Msg>::new("Title", false, Msg::Toggle);
Widget::build_themed(&one, &Theme::default());
let row = &Widget::children(&one)[0];
assert_eq!(
row.style_themed(&Theme::default()).min_height,
frus_layout::Dimension::Length(crate::LIST_TILE_HEIGHTS[0]),
"one line"
);
let two = ExpansionTile::<Msg>::new("Title", false, Msg::Toggle).subtitle("Second line");
Widget::build_themed(&two, &Theme::default());
let row = &Widget::children(&two)[0];
assert_eq!(
row.style_themed(&Theme::default()).min_height,
frus_layout::Dimension::Length(crate::LIST_TILE_HEIGHTS[1]),
"two lines"
);
}
/// The number of slots the row filled, leading first.
fn slots(tile: &ExpansionTile<Msg>) -> usize {
let row = &assembled(tile)[0];
// ListTile assembles a single row; its children are the filled slots.
row.build_themed(&Theme::default());
row.children()
.first()
.map(|inner| inner.children().len())
.unwrap_or(0)
}
/// A chevron on its own fills the trailing slot; a leading widget adds a second.
#[test]
fn the_chevron_takes_the_trailing_slot() {
let bare = ExpansionTile::<Msg>::new("Title", false, Msg::Toggle);
let with_leading =
ExpansionTile::<Msg>::new("Title", false, Msg::Toggle).leading(Text::new("*"));
assert_eq!(slots(&with_leading), slots(&bare) + 1);
}
/// A trailing widget **replaces** the chevron: a row whose end carries a switch is
/// not also carrying an arrow.
#[test]
fn a_trailing_widget_replaces_the_chevron() {
let bare = ExpansionTile::<Msg>::new("Title", false, Msg::Toggle);
let replaced =
ExpansionTile::<Msg>::new("Title", false, Msg::Toggle).trailing(Text::new("3"));
assert_eq!(slots(&replaced), slots(&bare), "one end slot either way");
}
/// Asked for nothing at the end, there is one slot fewer than with a chevron.
#[test]
fn the_chevron_can_be_hidden() {
let bare = ExpansionTile::<Msg>::new("Title", false, Msg::Toggle);
let none = ExpansionTile::<Msg>::new("Title", false, Msg::Toggle).show_trailing_icon(false);
assert_eq!(slots(&none) + 1, slots(&bare));
}
/// Leading affinity moves the chevron to the front, and the widget that wanted that
/// slot is dropped rather than stacked — two widgets in one slot is a bug a row of
/// fixed slots cannot report.
#[test]
fn leading_affinity_gives_the_chevron_the_front_slot() {
let bare = ExpansionTile::<Msg>::new("Title", false, Msg::Toggle)
.control_affinity(ControlAffinity::Leading);
let contested = ExpansionTile::<Msg>::new("Title", false, Msg::Toggle)
.control_affinity(ControlAffinity::Leading)
.leading(Text::new("*"));
assert_eq!(
slots(&contested),
slots(&bare),
"the chevron keeps the slot"
);
}
/// The colours are asked for, not assumed — and the open and shut states differ.
#[test]
fn the_row_takes_the_colour_it_was_given() {
const OPEN: Color = Color::rgb(0.0, 0.6, 0.3);
const SHUT: Color = Color::rgb(0.9, 0.2, 0.1);
let painted = |open: bool| {
let tile = ExpansionTile::<Msg>::new("Title", open, Msg::Toggle)
.background_color(OPEN)
.collapsed_background_color(SHUT);
let ui = build_ui(
&tile,
Size::new(300.0, 200.0),
&Runtime::default(),
&Theme::default(),
);
ui.scene().primitives().iter().any(|p| match p {
Primitive::Rect { color, .. } => *color == if open { OPEN } else { SHUT },
_ => false,
})
};
assert!(painted(true), "the open background is painted");
assert!(
painted(false),
"and the shut one, which is a different colour"
);
}
/// The message is the caller's, not a shape the widget invented.
#[test]
fn the_row_emits_the_message_it_was_handed() {
let tile = ExpansionTile::new("Title", false, Msg::Other);
assert_eq!(assembled(&tile)[0].on_click(), Some(Msg::Other));
}
}