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