iced_aw/style/
date_picker.rs

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