cosmic/widget/button/
style.rs

1// Copyright 2023 System76 <info@system76.com>
2// SPDX-License-Identifier: MPL-2.0
3
4//! Change the apperance of a button.
5use iced_core::{Background, Color, Vector, border::Radius};
6
7use crate::theme::THEME;
8
9/// The appearance of a button.
10#[must_use]
11#[derive(Debug, Clone, Copy)]
12pub struct Style {
13    /// The amount of offset to apply to the shadow of the button.
14    pub shadow_offset: Vector,
15
16    /// The [`Background`] of the button.
17    pub background: Option<Background>,
18
19    /// The [`Background`] overlay of the button.
20    pub overlay: Option<Background>,
21
22    /// The border radius of the button.
23    pub border_radius: Radius,
24
25    /// The border width of the button.
26    pub border_width: f32,
27
28    /// The border [`Color`] of the button.
29    pub border_color: Color,
30
31    /// An outline placed around the border.
32    pub outline_width: f32,
33
34    /// The [`Color`] of the outline.
35    pub outline_color: Color,
36
37    /// The icon [`Color`] of the button.
38    pub icon_color: Option<Color>,
39
40    /// The text [`Color`] of the button.
41    pub text_color: Option<Color>,
42}
43
44impl Style {
45    // TODO: `Radius` is not `const fn` compatible.
46    pub fn new() -> Self {
47        let rad_0 = THEME.lock().unwrap().cosmic().corner_radii.radius_0;
48        Self {
49            shadow_offset: Vector::new(0.0, 0.0),
50            background: None,
51            border_radius: Radius::from(rad_0),
52            border_width: 0.0,
53            border_color: Color::TRANSPARENT,
54            outline_width: 0.0,
55            outline_color: Color::TRANSPARENT,
56            icon_color: None,
57            text_color: None,
58            overlay: None,
59        }
60    }
61}
62
63impl std::default::Default for Style {
64    fn default() -> Self {
65        Self::new()
66    }
67}
68
69// TODO update to match other styles
70/// A set of rules that dictate the style of a button.
71pub trait Catalog {
72    /// The supported style of the [`StyleSheet`].
73    type Class: Default;
74
75    /// Produces the active [`Appearance`] of a button.
76    fn active(&self, focused: bool, selected: bool, style: &Self::Class) -> Style;
77
78    /// Produces the disabled [`Appearance`] of a button.
79    fn disabled(&self, style: &Self::Class) -> Style;
80
81    /// [`Appearance`] when the button is the target of a DND operation.
82    fn drop_target(&self, style: &Self::Class) -> Style {
83        self.hovered(false, false, style)
84    }
85
86    /// Produces the hovered [`Appearance`] of a button.
87    fn hovered(&self, focused: bool, selected: bool, style: &Self::Class) -> Style;
88
89    /// Produces the pressed [`Appearance`] of a button.
90    fn pressed(&self, focused: bool, selected: bool, style: &Self::Class) -> Style;
91
92    /// Background color of the selection indicator
93    fn selection_background(&self) -> Background;
94}