repose-material 0.26.8

Material components for Repose
Documentation
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
#![allow(non_snake_case)]

use std::rc::Rc;
use std::sync::atomic::{AtomicU64, Ordering};

use repose_core::*;
use repose_ui::{Box, Column, Row, Text, TextStyle, ViewExt, anim::animate_color};

use super::*;

/// Colors for [`ListItem`] -> matches Compose Material3 `ListItemColors` with
/// 4 state groups (default, disabled, selected, dragged) × 6 slots each.
#[derive(Clone, Debug)]
pub struct ListItemColors {
    pub container_color: Color,
    pub headline_color: Color,
    pub supporting_color: Color,
    pub overline_color: Color,
    pub leading_icon_color: Color,
    pub trailing_icon_color: Color,

    pub disabled_container_color: Color,
    pub disabled_headline_color: Color,
    pub disabled_supporting_color: Color,
    pub disabled_overline_color: Color,
    pub disabled_leading_icon_color: Color,
    pub disabled_trailing_icon_color: Color,

    pub selected_container_color: Color,
    pub selected_headline_color: Color,
    pub selected_supporting_color: Color,
    pub selected_overline_color: Color,
    pub selected_leading_icon_color: Color,
    pub selected_trailing_icon_color: Color,

    pub dragged_container_color: Color,
    pub dragged_headline_color: Color,
    pub dragged_supporting_color: Color,
    pub dragged_overline_color: Color,
    pub dragged_leading_icon_color: Color,
    pub dragged_trailing_icon_color: Color,
}

impl ListItemColors {
    pub fn container(&self, enabled: bool, selected: bool, dragged: bool) -> Color {
        if !enabled {
            self.disabled_container_color
        } else if dragged {
            self.dragged_container_color
        } else if selected {
            self.selected_container_color
        } else {
            self.container_color
        }
    }
    pub fn headline(&self, enabled: bool, selected: bool, dragged: bool) -> Color {
        if !enabled {
            self.disabled_headline_color
        } else if dragged {
            self.dragged_headline_color
        } else if selected {
            self.selected_headline_color
        } else {
            self.headline_color
        }
    }
    pub fn supporting(&self, enabled: bool, selected: bool, dragged: bool) -> Color {
        if !enabled {
            self.disabled_supporting_color
        } else if dragged {
            self.dragged_supporting_color
        } else if selected {
            self.selected_supporting_color
        } else {
            self.supporting_color
        }
    }
    pub fn overline(&self, enabled: bool, selected: bool, dragged: bool) -> Color {
        if !enabled {
            self.disabled_overline_color
        } else if dragged {
            self.dragged_overline_color
        } else if selected {
            self.selected_overline_color
        } else {
            self.overline_color
        }
    }
    pub fn leading_icon(&self, enabled: bool, selected: bool, dragged: bool) -> Color {
        if !enabled {
            self.disabled_leading_icon_color
        } else if dragged {
            self.dragged_leading_icon_color
        } else if selected {
            self.selected_leading_icon_color
        } else {
            self.leading_icon_color
        }
    }
    pub fn trailing_icon(&self, enabled: bool, selected: bool, dragged: bool) -> Color {
        if !enabled {
            self.disabled_trailing_icon_color
        } else if dragged {
            self.dragged_trailing_icon_color
        } else if selected {
            self.selected_trailing_icon_color
        } else {
            self.trailing_icon_color
        }
    }
}

impl Default for ListItemColors {
    fn default() -> Self {
        Self {
            container_color: Color::TRANSPARENT,
            headline_color: ListItemDefaults::headline_color(),
            supporting_color: ListItemDefaults::supporting_color(),
            overline_color: ListItemDefaults::overline_color(),
            leading_icon_color: ListItemDefaults::leading_icon_color(),
            trailing_icon_color: ListItemDefaults::trailing_icon_color(),
            disabled_container_color: ListItemDefaults::disabled_container_color(),
            disabled_headline_color: ListItemDefaults::disabled_headline_color(),
            disabled_supporting_color: ListItemDefaults::disabled_supporting_color(),
            disabled_overline_color: ListItemDefaults::disabled_overline_color(),
            disabled_leading_icon_color: ListItemDefaults::disabled_leading_icon_color(),
            disabled_trailing_icon_color: ListItemDefaults::disabled_trailing_icon_color(),
            selected_container_color: ListItemDefaults::selected_container_color(),
            selected_headline_color: ListItemDefaults::selected_headline_color(),
            selected_supporting_color: ListItemDefaults::selected_supporting_color(),
            selected_overline_color: ListItemDefaults::selected_overline_color(),
            selected_leading_icon_color: ListItemDefaults::selected_leading_icon_color(),
            selected_trailing_icon_color: ListItemDefaults::selected_trailing_icon_color(),
            dragged_container_color: ListItemDefaults::dragged_container_color(),
            dragged_headline_color: ListItemDefaults::dragged_headline_color(),
            dragged_supporting_color: ListItemDefaults::dragged_supporting_color(),
            dragged_overline_color: ListItemDefaults::dragged_overline_color(),
            dragged_leading_icon_color: ListItemDefaults::dragged_leading_icon_color(),
            dragged_trailing_icon_color: ListItemDefaults::dragged_trailing_icon_color(),
        }
    }
}

/// Configuration for [`ListItem`].
#[derive(Clone, Debug)]
pub struct ListItemConfig {
    pub modifier: Modifier,
    /// When false, renders disabled colors and suppresses clicks.
    pub enabled: bool,
    pub selected: bool,
    /// Renders the M3 dragged color/elevation roles (e.g. while reordering).
    pub dragged: bool,
    pub colors: ListItemColors,
    pub state_colors: StateColors,
    pub tonal_elevation: f32,
    /// Additional elevation applied while `dragged` (M3 drag lift, e.g. Level 4).
    pub dragged_elevation: f32,
    pub shadow_elevation: f32,
    pub shape_radius: f32,
    /// Per-corner radii `[BL, BR, TR, TL]`. When set, overrides `shape_radius`.
    pub shape_radii: Option<[f32; 4]>,
    pub horizontal_padding: f32,
    pub trailing_padding: f32,
    pub one_line_height: f32,
    pub two_line_height: f32,
    pub three_line_height: f32,
    pub interaction_source: Option<MutableInteractionSource>,
}

impl Default for ListItemConfig {
    fn default() -> Self {
        Self {
            modifier: Modifier::new(),
            enabled: true,
            selected: false,
            dragged: false,
            colors: ListItemColors::default(),
            state_colors: ListItemDefaults::state_colors_default(),
            tonal_elevation: 0.0,
            dragged_elevation: 0.0,
            shadow_elevation: 0.0,
            shape_radius: 0.0,
            shape_radii: None,
            horizontal_padding: ListItemDefaults::HORIZONTAL_PADDING,
            trailing_padding: ListItemDefaults::TRAILING_PADDING,
            one_line_height: ListItemDefaults::ONE_LINE_HEIGHT,
            two_line_height: ListItemDefaults::TWO_LINE_HEIGHT,
            three_line_height: ListItemDefaults::THREE_LINE_HEIGHT,
            interaction_source: None,
        }
    }
}

static LISTITEM_COUNTER: AtomicU64 = AtomicU64::new(0);

/// M3 List Item - a single row in a list with optional leading/trailing content,
/// overline text, and click handling.
pub fn ListItem(
    headline: impl Into<String>,
    supporting_text: Option<String>,
    overline_text: Option<String>,
    leading: Option<View>,
    trailing: Option<View>,
    on_click: Option<Rc<dyn Fn()>>,
    on_long_click: Option<Rc<dyn Fn()>>,
    config: ListItemConfig,
) -> View {
    let th = theme();
    let is_enabled = config.enabled;
    let is_selected = config.selected;
    let is_dragged = config.dragged;
    let c = &config.colors;
    let id = remember(|| LISTITEM_COUNTER.fetch_add(1, Ordering::Relaxed));
    let spec = th.motion.color;

    let hd_col = animate_color(
        format!("li_hd_{}", id),
        c.headline(is_enabled, is_selected, is_dragged),
        spec,
    );
    let sp_col = animate_color(
        format!("li_sp_{}", id),
        c.supporting(is_enabled, is_selected, is_dragged),
        spec,
    );
    let ol_col = animate_color(
        format!("li_ol_{}", id),
        c.overline(is_enabled, is_selected, is_dragged),
        spec,
    );
    let ld_col = animate_color(
        format!("li_ld_{}", id),
        c.leading_icon(is_enabled, is_selected, is_dragged),
        spec,
    );
    let tr_col = animate_color(
        format!("li_tr_{}", id),
        c.trailing_icon(is_enabled, is_selected, is_dragged),
        spec,
    );
    let bg = animate_color(
        format!("li_bg_{}", id),
        c.container(is_enabled, is_selected, is_dragged),
        spec,
    );

    let line_count = match (overline_text.is_some(), supporting_text.is_some()) {
        (true, true) => 3,
        (true, false) | (false, true) => 2,
        (false, false) => 1,
    };
    let min_h = match line_count {
        3 => config.three_line_height,
        2 => config.two_line_height,
        _ => config.one_line_height,
    };
    let top_bottom_padding = match line_count {
        3 => 12.0,
        _ => 8.0,
    };

    let vert_align = if min_h >= config.three_line_height {
        AlignItems::START
    } else {
        AlignItems::CENTER
    };

    let li_source: Rc<MutableInteractionSource> = config
        .interaction_source
        .clone()
        .map(Rc::new)
        .unwrap_or_else(|| remember(MutableInteractionSource::new));

    let mut modifier = Modifier::new()
        .min_width(200.0)
        .min_height(min_h)
        .background(bg);
    match config.shape_radii {
        Some(r) => modifier = modifier.clip_rounded_radii(r),
        None => modifier = modifier.clip_rounded(config.shape_radius),
    }
    modifier = modifier
        .state_colors(config.state_colors)
        .padding_values(PaddingValues {
            left: config.horizontal_padding,
            right: config.trailing_padding,
            top: top_bottom_padding,
            bottom: top_bottom_padding,
        })
        .align_items(vert_align)
        .interaction_source(&li_source)
        .then(config.modifier);

    if config.tonal_elevation > 0.0 || config.dragged_elevation > 0.0 {
        let dragged_elev = if config.dragged_elevation > 0.0 {
            config.dragged_elevation
        } else {
            config.tonal_elevation
        };
        modifier = modifier.state_elevation(StateElevation {
            default: config.tonal_elevation,
            hovered: config.tonal_elevation,
            pressed: config.tonal_elevation,
            dragged: dragged_elev,
            disabled: 0.0,
        });
    }
    if config.shadow_elevation > 0.0 {
        modifier = modifier.shadow(config.shadow_elevation, 0.0);
    }

    if on_click.is_some() || on_long_click.is_some() {
        modifier = modifier.clickable();
        if let Some(cb) = on_click {
            let cb = cb.clone();
            modifier = modifier.on_click(move || {
                if is_enabled {
                    cb();
                }
            });
        }
        if let Some(cb) = &on_long_click {
            let cb = cb.clone();
            modifier = modifier.on_long_click(move || {
                if is_enabled {
                    cb();
                }
            });
        }
    }

    let wrap_icon = |color: Color, v: View| -> View { with_content_color(color, move || v) };

    Row(modifier).child((
        leading
            .map(|v| {
                Box(Modifier::new().padding_values(PaddingValues {
                    left: 0.0,
                    right: 16.0,
                    top: 0.0,
                    bottom: 0.0,
                }))
                .child(wrap_icon(ld_col, v))
            })
            .unwrap_or(Box(Modifier::new())),
        Column(
            Modifier::new()
                .flex_grow(1.0)
                .justify_content(JustifyContent::CENTER),
        )
        .child((
            overline_text
                .map(|ot| {
                    Text(ot)
                        .color(ol_col)
                        .size(th.typography.label_small)
                        .single_line()
                })
                .unwrap_or(Box(Modifier::new())),
            Text(headline)
                .color(hd_col)
                .size(th.typography.body_large)
                .single_line(),
            supporting_text
                .map(|st| {
                    Text(st)
                        .color(sp_col)
                        .size(th.typography.body_medium)
                        .max_lines(2)
                        .overflow_ellipsize()
                })
                .unwrap_or(Box(Modifier::new())),
        )),
        trailing
            .map(|v| {
                Box(Modifier::new().padding_values(PaddingValues {
                    left: 16.0,
                    right: 0.0,
                    top: 0.0,
                    bottom: 0.0,
                }))
                .child(wrap_icon(tr_col, v))
            })
            .unwrap_or(Box(Modifier::new())),
    ))
}

/// M3 Selectable List Item -> single-selection variant with `selected` state and
/// `Role::RadioButton` semantics.
pub fn SelectableListItem(
    headline: impl Into<String>,
    selected: bool,
    supporting_text: Option<String>,
    overline_text: Option<String>,
    leading: Option<View>,
    trailing: Option<View>,
    on_click: Option<Rc<dyn Fn()>>,
    mut config: ListItemConfig,
) -> View {
    config.selected = selected;
    let mut m = Modifier::new().semantics(Semantics::new(Role::RadioButton));
    m = m.then(config.modifier);
    config.modifier = m;
    ListItem(
        headline,
        supporting_text,
        overline_text,
        leading,
        trailing,
        on_click,
        None,
        config,
    )
}

/// M3 Toggleable List Item -> multi-selection variant with `checked` state and
/// `Role::Checkbox` semantics. Clicking toggles the checked state.
pub fn ToggleableListItem(
    headline: impl Into<String>,
    checked: bool,
    on_checked_change: impl Fn(bool) + 'static,
    supporting_text: Option<String>,
    overline_text: Option<String>,
    leading: Option<View>,
    trailing: Option<View>,
    config: ListItemConfig,
) -> View {
    let mut cfg = config.clone();
    cfg.selected = checked;
    let cb = Rc::new(on_checked_change);
    let cb2 = cb.clone();
    let mut m = Modifier::new().semantics(Semantics::new(Role::Checkbox));
    m = m.then(cfg.modifier);
    cfg.modifier = m;
    ListItem(
        headline,
        supporting_text,
        overline_text,
        leading,
        trailing,
        Some(Rc::new(move || (cb2)(!checked))),
        None,
        cfg,
    )
}

/// Compute per-index corner radii `[BL, BR, TR, TL]` for a segmented list item.
fn segmented_item_radii(index: usize, count: usize, r: f32) -> [f32; 4] {
    if count <= 1 {
        [r, r, r, r]
    } else if index == 0 {
        [0.0, 0.0, r, r]
    } else if index == count - 1 {
        [r, r, 0.0, 0.0]
    } else {
        [0.0, 0.0, 0.0, 0.0]
    }
}

/// M3 Segmented List Item -> clickable variant with segmented (per-index) corner radii.
pub fn SegmentedListItem(
    index: usize,
    count: usize,
    headline: impl Into<String>,
    supporting_text: Option<String>,
    overline_text: Option<String>,
    leading: Option<View>,
    trailing: Option<View>,
    on_click: Option<Rc<dyn Fn()>>,
    mut config: ListItemConfig,
) -> View {
    config.shape_radii = Some(segmented_item_radii(index, count, config.shape_radius));
    ListItem(
        headline,
        supporting_text,
        overline_text,
        leading,
        trailing,
        on_click,
        None,
        config,
    )
}

/// M3 Segmented List Item -> single-selection variant.
pub fn SegmentedSelectableListItem(
    index: usize,
    count: usize,
    headline: impl Into<String>,
    selected: bool,
    supporting_text: Option<String>,
    overline_text: Option<String>,
    leading: Option<View>,
    trailing: Option<View>,
    on_click: Option<Rc<dyn Fn()>>,
    mut config: ListItemConfig,
) -> View {
    config.selected = selected;
    config.shape_radii = Some(segmented_item_radii(index, count, config.shape_radius));
    let mut m = Modifier::new().semantics(Semantics::new(Role::RadioButton));
    m = m.then(config.modifier);
    config.modifier = m;
    ListItem(
        headline,
        supporting_text,
        overline_text,
        leading,
        trailing,
        on_click,
        None,
        config,
    )
}

/// M3 Segmented List Item -> multi-selection (toggleable) variant.
pub fn SegmentedToggleableListItem(
    index: usize,
    count: usize,
    headline: impl Into<String>,
    checked: bool,
    on_checked_change: impl Fn(bool) + 'static,
    supporting_text: Option<String>,
    overline_text: Option<String>,
    leading: Option<View>,
    trailing: Option<View>,
    config: ListItemConfig,
) -> View {
    let mut cfg = config.clone();
    cfg.selected = checked;
    cfg.shape_radii = Some(segmented_item_radii(index, count, cfg.shape_radius));
    let cb2 = Rc::new(on_checked_change);
    let mut m = Modifier::new().semantics(Semantics::new(Role::Checkbox));
    m = m.then(cfg.modifier);
    cfg.modifier = m;
    ListItem(
        headline,
        supporting_text,
        overline_text,
        leading,
        trailing,
        Some(Rc::new(move || (cb2)(!checked))),
        None,
        cfg,
    )
}