Skip to main content

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