iced_aw/style/
number_input.rs

1//! Display fields that can only be filled with numeric type.
2//!
3//! *This API requires the following crate features to be activated: `number_input`*
4
5use super::{Status, StyleFn};
6use iced::{widget, Background, Color, Theme};
7
8/// The appearance of a [`NumberInput`](crate::widget::number_input::NumberInput).
9#[derive(Clone, Copy, Debug)]
10pub struct Style {
11    /// The background of the [`NumberInput`](crate::widget::number_input::NumberInput).
12    pub button_background: Option<Background>,
13    /// The Color of the arrows of [`NumberInput`](crate::widget::number_input::NumberInput).
14    pub icon_color: Color,
15}
16
17impl Default for Style {
18    fn default() -> Self {
19        Self {
20            button_background: None,
21            icon_color: Color::BLACK,
22        }
23    }
24}
25
26/// The Catalog of a [`NumberInput`](crate::widget::number_input::NumberInput).
27pub trait Catalog {
28    ///Style for the trait to use.
29    type Class<'a>;
30
31    /// The default class produced by the [`Catalog`].
32    fn default<'a>() -> Self::Class<'a>;
33
34    /// The [`Style`] of a class with the given status.
35    fn style(&self, class: &Self::Class<'_>, status: Status) -> Style;
36}
37
38impl Catalog for Theme {
39    type Class<'a> = StyleFn<'a, Self, Style>;
40
41    fn default<'a>() -> Self::Class<'a> {
42        Box::new(primary)
43    }
44
45    fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
46        class(self, status)
47    }
48}
49
50/// The Extended Catalog of a [`NumberInput`](crate::widget::number_input::NumberInput).
51pub trait ExtendedCatalog:
52    widget::text_input::Catalog + widget::container::Catalog + widget::text::Catalog + self::Catalog
53{
54    /// The default class produced by the [`Catalog`].
55    #[must_use]
56    fn default_input<'a>() -> <Self as widget::text_input::Catalog>::Class<'a> {
57        <Self as widget::text_input::Catalog>::default()
58    }
59
60    /// The [`Style`] of a class with the given status.
61    fn style(&self, class: &<Self as self::Catalog>::Class<'_>, status: Status) -> Style;
62}
63
64impl ExtendedCatalog for Theme {
65    fn style(&self, class: &<Self as self::Catalog>::Class<'_>, status: Status) -> Style {
66        class(self, status)
67    }
68}
69
70/// The primary theme of a [`Badge`](crate::widget::badge::Badge).
71#[must_use]
72pub fn primary(theme: &Theme, status: Status) -> Style {
73    let palette = theme.extended_palette();
74    let base = Style {
75        button_background: Some(palette.primary.strong.color.into()),
76        icon_color: palette.primary.strong.text,
77    };
78
79    match status {
80        Status::Disabled => Style {
81            button_background: base.button_background.map(|bg| match bg {
82                Background::Color(color) => Background::Color(Color {
83                    a: color.a * 0.5,
84                    ..color
85                }),
86                Background::Gradient(grad) => Background::Gradient(grad),
87            }),
88            icon_color: Color {
89                a: base.icon_color.a * 0.5,
90                ..base.icon_color
91            },
92        },
93        _ => base,
94    }
95}