1use waft_embedder::{Brightness, Color};
4use waft_foundation::App;
5use waft_widgets::BuildContext;
6
7use crate::colors::Colors;
8use crate::theme::Theme;
9
10pub struct ElevationOverlay;
16
17impl ElevationOverlay {
18 #[allow(deprecated)]
31 pub fn apply_surface_tint(color: Color, surface_tint: Option<Color>, elevation: f64) -> Color {
32 if let Some(surface_tint) = surface_tint
33 && surface_tint != Colors::TRANSPARENT
34 {
35 return Color::alpha_blend(
36 surface_tint.with_opacity(Self::surface_tint_opacity_for_elevation(elevation)),
37 color,
38 );
39 }
40 color
41 }
42
43 fn surface_tint_opacity_for_elevation(elevation: f64) -> f64 {
48 if elevation < SURFACE_TINT_ELEVATION_OPACITIES[0].elevation {
49 return SURFACE_TINT_ELEVATION_OPACITIES[0].opacity;
51 }
52
53 let mut index = 0;
55 while elevation >= SURFACE_TINT_ELEVATION_OPACITIES[index].elevation {
56 if elevation == SURFACE_TINT_ELEVATION_OPACITIES[index].elevation
58 || index + 1 == SURFACE_TINT_ELEVATION_OPACITIES.len()
59 {
60 return SURFACE_TINT_ELEVATION_OPACITIES[index].opacity;
61 }
62 index += 1;
63 }
64
65 let lower = &SURFACE_TINT_ELEVATION_OPACITIES[index - 1];
67 let upper = &SURFACE_TINT_ELEVATION_OPACITIES[index];
68 let t = (elevation - lower.elevation) / (upper.elevation - lower.elevation);
69 lower.opacity + t * (upper.opacity - lower.opacity)
70 }
71
72 #[allow(deprecated)]
103 pub fn apply_overlay(
104 app: &mut App,
105 context: BuildContext,
106 color: Color,
107 elevation: f64,
108 ) -> Color {
109 let theme = Theme::of(app, context);
110 if elevation > 0.0
111 && theme.apply_elevation_overlay_color
112 && theme.brightness() == Brightness::Dark
113 && color.with_opacity(1.0) == theme.color_scheme.surface().with_opacity(1.0)
114 {
115 return Self::color_with_overlay(color, theme.color_scheme.on_surface(), elevation);
116 }
117 color
118 }
119
120 pub fn overlay_color(app: &mut App, context: BuildContext, elevation: f64) -> Color {
132 let theme = Theme::of(app, context);
133 Self::overlay_color_of(theme.color_scheme.on_surface(), elevation)
134 }
135
136 pub fn color_with_overlay(surface: Color, overlay: Color, elevation: f64) -> Color {
148 Color::alpha_blend(Self::overlay_color_of(overlay, elevation), surface)
149 }
150
151 #[allow(deprecated)]
153 fn overlay_color_of(color: Color, elevation: f64) -> Color {
154 let opacity = (4.5 * (elevation + 1.0).ln() + 2.0) / 100.0;
158 color.with_opacity(opacity)
159 }
160}
161
162struct ElevationOpacity {
164 elevation: f64,
165 opacity: f64,
166}
167
168impl ElevationOpacity {
169 const fn new(elevation: f64, opacity: f64) -> ElevationOpacity {
170 ElevationOpacity { elevation, opacity }
171 }
172}
173
174const SURFACE_TINT_ELEVATION_OPACITIES: [ElevationOpacity; 6] = [
177 ElevationOpacity::new(0.0, 0.0), ElevationOpacity::new(1.0, 0.05), ElevationOpacity::new(3.0, 0.08), ElevationOpacity::new(6.0, 0.11), ElevationOpacity::new(8.0, 0.12), ElevationOpacity::new(12.0, 0.14), ];
184
185#[cfg(test)]
186mod tests {
187 use std::rc::Rc;
188
189 use waft_widgets::IntoWidget;
190
191 use waft_widgets::testing::{WidgetTester, test_widgets};
192
193 use super::*;
194 use crate::drawer_theme::tests::probe;
195 use crate::theme_data::ThemeData;
196
197 const BASE_COLOR: Color = Color::new(0xff888888);
198 const SURFACE_TINT_COLOR: Color = Color::new(0xff44ccff);
199
200 #[allow(deprecated)]
201 fn overlay_with_opacity(opacity: f64) -> Color {
202 Color::alpha_blend(SURFACE_TINT_COLOR.with_opacity(opacity), BASE_COLOR)
203 }
204
205 fn tinted(elevation: f64) -> Color {
206 ElevationOverlay::apply_surface_tint(BASE_COLOR, Some(SURFACE_TINT_COLOR), elevation)
207 }
208
209 #[test]
210 fn apply_surface_tint_with_null_surface_tint_returns_given_color() {
211 let result = ElevationOverlay::apply_surface_tint(BASE_COLOR, None, 42.0);
212
213 assert_eq!(result, Color::new(0xFF888888));
214 }
215
216 #[test]
217 fn apply_surface_tint_with_exact_elevation_levels_uses_the_right_opacity_overlay() {
218 assert_eq!(tinted(0.0), overlay_with_opacity(0.0));
220 assert_eq!(tinted(1.0), overlay_with_opacity(0.05));
222 assert_eq!(tinted(3.0), overlay_with_opacity(0.08));
224 assert_eq!(tinted(6.0), overlay_with_opacity(0.11));
226 assert_eq!(tinted(8.0), overlay_with_opacity(0.12));
228 assert_eq!(tinted(12.0), overlay_with_opacity(0.14));
230 }
231
232 #[test]
233 fn apply_surface_tint_with_elevation_lower_than_level_0_should_have_no_overlay() {
234 assert_eq!(tinted(-42.0), overlay_with_opacity(0.0));
235 }
236
237 #[test]
238 fn apply_surface_tint_with_elevation_higher_than_level_5_should_have_no_level_5_overlay() {
239 assert_eq!(tinted(42.0), overlay_with_opacity(0.14));
241 }
242
243 #[test]
244 fn apply_surface_tint_with_elevation_between_two_levels_should_interpolate_the_opacity() {
245 assert_eq!(tinted(9.2), overlay_with_opacity(0.126));
250 assert_eq!(tinted(10.0), overlay_with_opacity(0.13));
252 assert_eq!(tinted(10.4), overlay_with_opacity(0.132));
254 }
255
256 #[test]
257 fn apply_surface_tint_with_a_transparent_surface_tint_returns_given_color() {
258 let result =
259 ElevationOverlay::apply_surface_tint(BASE_COLOR, Some(Colors::TRANSPARENT), 6.0);
260
261 assert_eq!(result, BASE_COLOR);
262 }
263
264 fn dark_theme_with_overlay(app: &App) -> ThemeData {
265 ThemeData::builder()
266 .brightness(Brightness::Dark)
267 .use_material3(false)
268 .apply_elevation_overlay_color(true)
269 .build(app)
270 }
271
272 fn overlay_results_under(
273 tester: &mut WidgetTester,
274 theme_of: impl Fn(&App) -> ThemeData + 'static,
275 color_of: impl Fn(&ThemeData) -> Color + 'static,
276 elevation: f64,
277 ) -> (Color, Color, Rc<ThemeData>) {
278 probe(
279 tester,
280 move |app, child| Theme::new(theme_of(app), child).into_widget(),
281 move |app, context| {
282 let theme = Theme::of(app, context);
283 let color = color_of(&theme);
284 (
285 ElevationOverlay::apply_overlay(app, context, color, elevation),
286 ElevationOverlay::overlay_color(app, context, elevation),
287 theme,
288 )
289 },
290 )
291 }
292
293 #[test]
294 fn apply_overlay_overlays_the_surface_color_in_a_dark_theme() {
295 test_widgets(|tester| {
296 let (applied, overlay, theme) = overlay_results_under(
297 tester,
298 dark_theme_with_overlay,
299 |theme| theme.color_scheme.surface(),
300 4.0,
301 );
302
303 let on_surface = theme.color_scheme.on_surface();
304 assert_eq!(
305 applied,
306 ElevationOverlay::color_with_overlay(theme.color_scheme.surface(), on_surface, 4.0)
307 );
308 assert_ne!(applied, theme.color_scheme.surface());
309 assert_eq!(overlay, ElevationOverlay::overlay_color_of(on_surface, 4.0));
310 });
311 }
312
313 #[test]
314 fn apply_overlay_leaves_other_colors_and_light_themes_and_zero_elevation_alone() {
315 test_widgets(|tester| {
316 let (other_color, _, theme) = overlay_results_under(
317 tester,
318 dark_theme_with_overlay,
319 |_| Color::new(0xff123456),
320 4.0,
321 );
322 assert_eq!(other_color, Color::new(0xff123456));
323 assert_eq!(theme.brightness(), Brightness::Dark);
324
325 let (light, _, light_theme) = overlay_results_under(
326 tester,
327 |app| ThemeData::light(app, Some(false)),
328 |theme| theme.color_scheme.surface(),
329 4.0,
330 );
331 assert_eq!(light, light_theme.color_scheme.surface());
332
333 let (flat, _, dark_theme) = overlay_results_under(
334 tester,
335 dark_theme_with_overlay,
336 |theme| theme.color_scheme.surface(),
337 0.0,
338 );
339 assert_eq!(flat, dark_theme.color_scheme.surface());
340 });
341 }
342
343 #[test]
344 #[allow(deprecated)]
345 fn color_with_overlay_uses_the_dark_theme_opacity_curve() {
346 let opacity = (4.5 * 2.0_f64.ln() + 2.0) / 100.0;
347
348 assert_eq!(
349 ElevationOverlay::color_with_overlay(BASE_COLOR, SURFACE_TINT_COLOR, 1.0),
350 Color::alpha_blend(SURFACE_TINT_COLOR.with_opacity(opacity), BASE_COLOR)
351 );
352 }
353}