iced_aw/style/
selection_list.rs

1//! Selection List
2//!
3//! *This API requires the following crate features to be activated: `selection_list`*
4
5use super::{Status, StyleFn};
6use iced_core::{Background, Color, Theme};
7
8/// The appearance of a menu.
9#[derive(Debug, Clone, Copy)]
10pub struct Style {
11    /// The List Label Text Color
12    pub text_color: Color,
13    /// The background
14    pub background: Background,
15    /// The container Border width
16    pub border_width: f32,
17    /// The container Border color
18    pub border_color: Color,
19}
20
21/// The Catalog of a [`Badge`](crate::widget::selection_list::SelectionList).
22pub trait Catalog {
23    ///Style for the trait to use.
24    type Class<'a>;
25
26    /// The default class produced by the [`Catalog`].
27    fn default<'a>() -> Self::Class<'a>;
28
29    /// The [`Style`] of a class with the given status.
30    fn style(&self, class: &Self::Class<'_>, status: Status) -> Style;
31}
32
33impl Catalog for Theme {
34    type Class<'a> = StyleFn<'a, Self, Style>;
35
36    fn default<'a>() -> Self::Class<'a> {
37        Box::new(primary)
38    }
39
40    fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
41        class(self, status)
42    }
43}
44
45/// The primary theme of a [`Badge`](crate::widget::selection_list::SelectionList).
46#[must_use]
47pub fn primary(theme: &Theme, status: Status) -> Style {
48    let palette = theme.extended_palette();
49
50    let base = Style {
51        text_color: palette.background.base.text,
52        background: palette.background.base.color.into(),
53        border_width: 1.0,
54        border_color: palette.background.weak.color,
55    };
56
57    match status {
58        Status::Hovered => Style {
59            text_color: palette.primary.base.text,
60            background: palette.primary.base.color.into(),
61            ..base
62        },
63        Status::Selected => Style {
64            text_color: palette.primary.strong.text,
65            background: palette.primary.strong.color.into(),
66            ..base
67        },
68        _ => base,
69    }
70}