Skip to main content

rust_glass/
theme.rs

1//! Appearance and accessibility settings shared by every widget.
2
3use crate::color::Color;
4use crate::material::Material;
5
6/// Appearance and accessibility settings. Change them through
7/// [`Context::theme`](crate::Context::theme) at any time.
8#[derive(Clone, Debug, PartialEq)]
9pub struct Theme {
10    /// Dark appearance for content. Small glass elements still switch between
11    /// light and dark on their own, depending on what is behind them.
12    pub dark: bool,
13    /// Color of prominent buttons, selections and focus rings.
14    pub accent: Color,
15    /// How tinted glass is, from 0 (clearest) to 1 (fully tinted). This is
16    /// the system wide transparency setting of iOS 27. The material presets
17    /// are tuned for [`Theme::DEFAULT_GLASS_TINT`].
18    pub glass_tint: f32,
19    /// Lets small glass elements switch between light and dark with what is
20    /// behind them.
21    pub adaptive: bool,
22    /// Makes glass nearly opaque.
23    pub reduce_transparency: bool,
24    /// Replaces bouncy springs with short, calm ones and turns off stretching.
25    pub reduce_motion: bool,
26    /// Strengthens outlines and edges.
27    pub increase_contrast: bool,
28
29    /// Material of controls and bars.
30    pub regular: Material,
31    /// Material of clear buttons over media.
32    pub clear: Material,
33    /// Material of menus, sheets, windows and panels.
34    pub thick: Material,
35    /// Material of a lifted slider, switch or segment thumb.
36    pub lens: Material,
37    /// Material of the lifted tab bar selection.
38    pub blob: Material,
39    /// Material that bends the tab labels under the lifted selection.
40    pub blob_labels: Material,
41
42    /// Height of buttons and fields, in points.
43    pub control_height: f32,
44    /// Default gap between widgets, in points.
45    pub spacing: f32,
46    /// Font size of body text.
47    pub body_size: f32,
48    /// Font size of captions.
49    pub caption_size: f32,
50    /// Font size of headings.
51    pub title_size: f32,
52}
53
54impl Default for Theme {
55    fn default() -> Self {
56        Self {
57            dark: false,
58            accent: Color::BLUE,
59            glass_tint: Theme::DEFAULT_GLASS_TINT,
60            adaptive: true,
61            reduce_transparency: false,
62            reduce_motion: false,
63            increase_contrast: false,
64            regular: Material::REGULAR,
65            clear: Material::CLEAR,
66            thick: Material::THICK,
67            lens: Material::LENS,
68            blob: Material::BLOB,
69            blob_labels: Material::BLOB_LABELS,
70            control_height: 36.0,
71            spacing: 10.0,
72            body_size: 14.0,
73            caption_size: 12.0,
74            title_size: 24.0,
75        }
76    }
77}
78
79impl Theme {
80    /// The default of [`Theme::glass_tint`], between clear and tinted.
81    pub const DEFAULT_GLASS_TINT: f32 = 0.35;
82
83    /// How far the tint setting is past the default, from 0 to 1.
84    pub(crate) fn tint_above_default(&self) -> f32 {
85        if self.reduce_transparency {
86            1.0
87        } else {
88            ((self.glass_tint - Self::DEFAULT_GLASS_TINT) / (1.0 - Self::DEFAULT_GLASS_TINT)).clamp(0.0, 1.0)
89        }
90    }
91
92    /// How milky the lifted tab bar selection is, from 0 to 1.
93    pub(crate) fn tab_blob_veil(&self) -> f32 {
94        if self.reduce_transparency { 1.0 } else { self.glass_tint.clamp(0.0, 1.0) * 0.75 }
95    }
96    /// Returns the color of text on the window background.
97    pub fn text_color(&self) -> Color {
98        if self.dark { Color::gray(0.97) } else { Color::gray(0.04) }
99    }
100    /// Returns the window background color.
101    pub fn background_color(&self) -> Color {
102        if self.dark { Color::gray(0.07) } else { Color::gray(0.95) }
103    }
104}
105
106/// What a widget is drawn on. It decides the colors of labels and fills.
107///
108/// Read it from [`Ui::surface`](crate::Ui::surface) when you draw your own
109/// widget, so it looks right on content and on glass.
110#[derive(Clone, Copy, Debug, PartialEq)]
111pub enum Surface {
112    /// Directly on app content.
113    Content {
114        /// The content has the dark appearance.
115        dark: bool,
116    },
117    /// On a glass element.
118    Glass {
119        /// How dark the glass is, from 0 (light) to 1 (dark).
120        dark: f32,
121    },
122}
123
124impl Surface {
125    /// Returns how dark the surface is, from 0 (light) to 1 (dark).
126    pub fn darkness(&self) -> f32 {
127        match self {
128            Surface::Content { dark } => *dark as u32 as f32,
129            Surface::Glass { dark } => *dark,
130        }
131    }
132    /// Returns `true` on glass.
133    pub fn is_glass(&self) -> bool {
134        matches!(self, Surface::Glass { .. })
135    }
136    /// Returns the color of primary text.
137    pub fn text_color(&self) -> Color {
138        Color::gray(0.04).lerp(Color::gray(0.99), self.darkness())
139    }
140    /// Returns the color of less important text.
141    pub fn secondary_color(&self) -> Color {
142        Color::black(0.52).lerp(Color::white(0.62), self.darkness())
143    }
144    /// Returns the color of hints and disabled text.
145    pub fn tertiary_color(&self) -> Color {
146        Color::black(0.30).lerp(Color::white(0.36), self.darkness())
147    }
148    /// Returns the fill of tracks, wells and hovered rows.
149    pub fn fill_color(&self) -> Color {
150        Color::black(0.085).lerp(Color::white(0.13), self.darkness())
151    }
152    /// Returns a stronger fill, for selections and switches that are off.
153    pub fn strong_fill_color(&self) -> Color {
154        Color::black(0.15).lerp(Color::white(0.22), self.darkness())
155    }
156    /// Returns the color of separator lines.
157    pub fn separator_color(&self) -> Color {
158        Color::black(0.10).lerp(Color::white(0.14), self.darkness())
159    }
160    /// Returns the color of a thumb at rest.
161    pub fn thumb_color(&self) -> Color {
162        Color::WHITE.lerp(Color::gray(0.96), self.darkness())
163    }
164}