iced_aw/style/
color_picker.rs

1//! Use a color picker as an input element for picking colors.
2//!
3//! *This API requires the following crate features to be activated: `color_picker`*
4
5use super::{Status, StyleFn};
6use iced_core::{Background, Color, Theme};
7
8/// The appearance of a [`ColorPicker`](crate::widget::ColorPicker).
9#[derive(Clone, Copy, Debug)]
10pub struct Style {
11    /// The background of the [`ColorPicker`](crate::widget::ColorPicker).
12    pub background: Background,
13
14    /// The border radius of the [`ColorPicker`](crate::widget::ColorPicker).
15    pub border_radius: f32,
16
17    /// The border with of the [`ColorPicker`](crate::widget::ColorPicker).
18    pub border_width: f32,
19
20    /// The border color of the [`ColorPicker`](crate::widget::ColorPicker).
21    pub border_color: Color,
22
23    /// The border radius of the bars of the [`ColorPicker`](crate::widget::ColorPicker).
24    pub bar_border_radius: f32,
25
26    /// The border width of the bars of the [`ColorPicker`](crate::widget::ColorPicker).
27    pub bar_border_width: f32,
28
29    /// The border color of the bars of the [`ColorPicker`](crate::widget::ColorPicker).
30    pub bar_border_color: Color,
31}
32
33/// The Catalog of a [`ColorPicker`](crate::widget::ColorPicker).
34pub trait Catalog {
35    ///Style for the trait to use.
36    type Class<'a>;
37
38    /// The default class produced by the [`Catalog`].
39    fn default<'a>() -> Self::Class<'a>;
40
41    /// The [`Style`] of a class with the given status.
42    fn style(&self, class: &Self::Class<'_>, status: Status) -> Style;
43}
44
45impl Catalog for Theme {
46    type Class<'a> = StyleFn<'a, Self, Style>;
47
48    fn default<'a>() -> Self::Class<'a> {
49        Box::new(primary)
50    }
51
52    fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
53        class(self, status)
54    }
55}
56
57/// The primary theme of a [`Badge`](crate::widget::badge::Badge).
58#[must_use]
59pub fn primary(theme: &Theme, status: Status) -> Style {
60    let palette = theme.extended_palette();
61    let foreground = theme.palette();
62
63    let base = Style {
64        background: palette.background.base.color.into(),
65        border_radius: 15.0,
66        border_width: 1.0,
67        border_color: foreground.text,
68        bar_border_radius: 5.0,
69        bar_border_width: 1.0,
70        bar_border_color: foreground.text,
71    };
72
73    match status {
74        Status::Focused => Style {
75            border_color: palette.background.strong.color,
76            bar_border_color: palette.background.strong.color,
77            ..base
78        },
79        _ => base,
80    }
81}