1use crate::theme::LiquidColors;
7use cranpose_ui::current_density;
8use cranpose_ui::Modifier;
9use cranpose_ui_graphics::{
10 Color, GraphicsLayer, LayerShape, RenderEffect, RoundedCornerShape, RuntimeShader,
11 GLASS_ACTIVITY_UNIFORM, GLASS_BLUR_RADIUS_UNIFORM, GLASS_DISPERSION_UNIFORM,
12 GLASS_EFFECT_DENSITY_UNIFORM, GLASS_FOLD_DEPTH_UNIFORM, GLASS_MENISCUS_ABSORPTION_UNIFORM,
13 GLASS_OPTICAL_ZOOM_UNIFORM, GLASS_REFRACTION_CURVE_UNIFORM, GLASS_RESTING_TINT_UNIFORM,
14 GLASS_TRANSMISSION_REFRACTION_UNIFORM, LIQUID_GLASS_WGSL,
15};
16use std::rc::Rc;
17
18const CAPSULE_CLIP_RADIUS: f32 = 1.0e6;
21
22const CAPSULE_SHADER_RADIUS: f32 = -1.0;
25
26const WCKSRD_OPTICAL_BLUR_RADIUS_PX: f32 = 2.0;
30
31#[derive(Clone, Copy, Debug, PartialEq, Default)]
33pub enum LiquidShape {
34 #[default]
36 Capsule,
37 RoundedRect(f32),
39 Circle,
41}
42
43impl LiquidShape {
44 pub fn clip_shape(&self) -> RoundedCornerShape {
46 match self {
47 LiquidShape::Capsule | LiquidShape::Circle => {
48 RoundedCornerShape::uniform(CAPSULE_CLIP_RADIUS)
49 }
50 LiquidShape::RoundedRect(radius) => RoundedCornerShape::uniform(*radius),
51 }
52 }
53
54 pub fn layer_shape(&self) -> LayerShape {
56 LayerShape::Rounded(self.clip_shape())
57 }
58
59 fn shader_radius_px(&self, density: f32) -> f32 {
61 match self {
62 LiquidShape::Capsule | LiquidShape::Circle => CAPSULE_SHADER_RADIUS,
63 LiquidShape::RoundedRect(radius) => radius * density,
64 }
65 }
66}
67
68#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
71pub enum GlassVariant {
72 #[default]
74 Regular,
75 Clear,
77 Lens,
81}
82
83#[derive(Clone, Copy, Debug, PartialEq)]
87pub struct GlassShadow {
88 pub color: Color,
89 pub radius: f32,
90 pub offset_y: f32,
91 pub spread: f32,
92}
93
94impl GlassShadow {
95 pub fn new(color: Color, radius: f32, offset_y: f32, spread: f32) -> Self {
96 Self {
97 color,
98 radius: radius.max(0.0),
99 offset_y,
100 spread,
101 }
102 }
103}
104
105#[derive(Clone, Debug, PartialEq, Default)]
108pub struct GlassDynamics {
109 pub activity: Option<f32>,
113 pub resting_tint: Option<Color>,
116 pub highlight_boost: f32,
118 pub saturation_boost: f32,
121 pub tint_alpha_multiplier: Option<f32>,
124 pub morph: Option<GlassMorph>,
127 pub touch: Option<(f32, f32, f32)>,
131}
132
133fn foreground_is_dark(foreground: Color) -> bool {
134 let foreground_luma =
135 0.2126 * foreground.r() + 0.7152 * foreground.g() + 0.0722 * foreground.b();
136 foreground_luma < 0.5
137}
138
139fn boost_tint_saturation(tint: Color, boost: f32) -> Color {
140 if boost.abs() <= f32::EPSILON {
141 return tint;
142 }
143 let luma = 0.2126 * tint.r() + 0.7152 * tint.g() + 0.0722 * tint.b();
144 let saturation = (1.0 + boost).max(0.0);
145 Color::rgba(
146 (luma + (tint.r() - luma) * saturation).clamp(0.0, 1.0),
147 (luma + (tint.g() - luma) * saturation).clamp(0.0, 1.0),
148 (luma + (tint.b() - luma) * saturation).clamp(0.0, 1.0),
149 tint.a(),
150 )
151}
152
153pub(crate) fn neutral_surface_tint(foreground: Color, light_alpha: f32, dark_alpha: f32) -> Color {
154 if foreground_is_dark(foreground) {
155 Color::BLACK.with_alpha(light_alpha.clamp(0.0, 1.0))
156 } else {
157 Color::WHITE.with_alpha(dark_alpha.clamp(0.0, 1.0))
158 }
159}
160
161pub(crate) fn neutral_surface_lift(foreground: Color, light_lift: f32, dark_lift: f32) -> f32 {
162 if foreground_is_dark(foreground) {
163 light_lift
164 } else {
165 dark_lift
166 }
167}
168
169#[derive(Clone, Debug, PartialEq, Default)]
180pub struct GlassMorph {
181 pub node_size: (f32, f32),
187 pub primary: (f32, f32, f32, f32, f32),
188 pub shapes: Vec<(f32, f32, f32, f32, f32)>,
190 pub glue: f32,
192 pub wobble_amplitude: f32,
194 pub wobble_phase: f32,
195 pub bulge_amplitude: f32,
199 pub bulge_direction: f32,
200 pub ellipse_blend: f32,
204 pub deformation: Option<GlassDeformation>,
208}
209
210#[derive(Clone, Copy, Debug, PartialEq)]
214pub struct GlassDeformation {
215 axis: (f32, f32),
216 along: f32,
217}
218
219impl GlassDeformation {
220 pub fn incompressible(axis: (f32, f32), along: f32) -> Self {
221 let length = (axis.0 * axis.0 + axis.1 * axis.1).sqrt();
222 let axis = if length > f32::EPSILON {
223 (axis.0 / length, axis.1 / length)
224 } else {
225 (1.0, 0.0)
226 };
227 Self {
228 axis,
229 along: along.max(f32::EPSILON),
230 }
231 }
232
233 pub fn axis(self) -> (f32, f32) {
234 self.axis
235 }
236
237 pub fn along(self) -> f32 {
238 self.along
239 }
240
241 pub fn across(self) -> f32 {
242 1.0 / self.along
243 }
244}
245
246impl GlassMorph {
247 pub const MAX_SHAPES: usize = 8;
249}
250
251#[derive(Clone, Debug, PartialEq)]
254pub struct Glass {
255 pub variant: GlassVariant,
256 pub shape: LiquidShape,
257 pub tint: Option<Color>,
259 pub blur_radius: Option<f32>,
261 pub saturation: Option<f32>,
263 pub refraction_depth: f32,
265 pub refraction_curve: f32,
267 pub dispersion: f32,
269 pub transmission_refraction: f32,
272 pub meniscus_absorption: f32,
275 pub fold_depth: f32,
279 pub optical_zoom: f32,
283 pub highlight: f32,
285 pub lift: Option<f32>,
288 pub shadow: bool,
290 pub shadow_style: Option<GlassShadow>,
292 pub clip: bool,
295 pub foreground: Option<Color>,
298 pub adaptive_frost: f32,
300}
301
302impl Glass {
303 pub fn regular() -> Self {
304 Self {
305 variant: GlassVariant::Regular,
306 shape: LiquidShape::Capsule,
307 tint: None,
308 blur_radius: None,
309 saturation: None,
310 refraction_depth: 0.34,
311 refraction_curve: 0.25,
312 dispersion: 0.0,
313 transmission_refraction: 1.0,
314 meniscus_absorption: 1.0,
315 fold_depth: 0.0,
316 optical_zoom: 1.0,
317 highlight: 0.9,
318 lift: None,
319 shadow: true,
320 shadow_style: None,
321 clip: true,
322 foreground: None,
323 adaptive_frost: 0.65,
324 }
325 }
326
327 pub fn clear() -> Self {
328 Self {
329 variant: GlassVariant::Clear,
330 ..Self::regular()
331 }
332 }
333
334 pub fn lens() -> Self {
338 Self {
339 variant: GlassVariant::Lens,
340 shape: LiquidShape::Capsule,
341 tint: Some(Color::rgba(1.0, 1.0, 1.0, 0.07)),
342 blur_radius: None,
343 saturation: None,
344 refraction_depth: 0.34,
345 refraction_curve: 1.0,
346 dispersion: 0.30,
347 transmission_refraction: 1.0,
348 meniscus_absorption: 1.0,
349 fold_depth: 0.0,
350 optical_zoom: 1.0,
351 highlight: 1.15,
352 lift: None,
353 shadow: true,
354 shadow_style: None,
355 clip: true,
356 foreground: None,
357 adaptive_frost: 0.0,
358 }
359 }
360
361 pub fn shape(mut self, shape: LiquidShape) -> Self {
362 self.shape = shape;
363 self
364 }
365
366 pub fn tint(mut self, tint: Color) -> Self {
367 self.tint = Some(tint);
368 self
369 }
370
371 pub fn blur_radius(mut self, radius_dp: f32) -> Self {
372 self.blur_radius = Some(radius_dp);
373 self
374 }
375
376 pub fn saturation(mut self, saturation: f32) -> Self {
377 self.saturation = Some(saturation);
378 self
379 }
380
381 pub fn refraction_depth(mut self, fraction: f32) -> Self {
382 self.refraction_depth = fraction.clamp(0.0, 2.0);
383 self
384 }
385
386 pub fn refraction_curve(mut self, curve: f32) -> Self {
387 self.refraction_curve = curve.clamp(0.05, 1.0);
388 self
389 }
390
391 pub fn dispersion(mut self, strength: f32) -> Self {
392 self.dispersion = strength.clamp(0.0, 1.0);
393 self
394 }
395
396 pub fn transmission_refraction(mut self, strength: f32) -> Self {
397 self.transmission_refraction = strength.clamp(0.0, 1.0);
398 self
399 }
400
401 pub fn meniscus_absorption(mut self, strength: f32) -> Self {
402 self.meniscus_absorption = strength.clamp(0.0, 1.0);
403 self
404 }
405
406 pub fn fold_depth(mut self, depth_dp: f32) -> Self {
408 self.fold_depth = depth_dp.max(0.0);
409 self
410 }
411
412 pub fn optical_zoom(mut self, zoom: f32) -> Self {
414 self.optical_zoom = zoom.max(1.0);
415 self
416 }
417
418 pub fn highlight(mut self, highlight: f32) -> Self {
419 self.highlight = highlight;
420 self
421 }
422
423 pub fn lift(mut self, lift: f32) -> Self {
426 self.lift = Some(lift);
427 self
428 }
429
430 pub fn adaptive_frost(mut self, foreground: Color, strength: f32) -> Self {
431 self.foreground = Some(foreground);
432 self.adaptive_frost = strength.clamp(0.0, 1.0);
433 self
434 }
435
436 pub fn shadow(mut self, shadow: bool) -> Self {
437 self.shadow = shadow;
438 self
439 }
440
441 pub fn shadow_style(mut self, shadow: GlassShadow) -> Self {
442 self.shadow_style = Some(shadow);
443 self
444 }
445
446 pub fn no_clip(mut self) -> Self {
449 self.clip = false;
450 self
451 }
452
453 fn default_blur_radius(&self) -> f32 {
454 match self.variant {
455 GlassVariant::Regular => 8.0,
456 GlassVariant::Clear => 3.0,
457 GlassVariant::Lens => 0.0,
458 }
459 }
460
461 fn default_saturation(&self) -> f32 {
462 match self.variant {
463 GlassVariant::Regular => 1.5,
464 GlassVariant::Clear => 1.25,
465 GlassVariant::Lens => 1.0,
466 }
467 }
468
469 pub(crate) fn resolve(&self, colors: &LiquidColors) -> ResolvedGlass {
471 let lift = self.lift.unwrap_or(match (self.variant, colors.is_dark) {
478 (GlassVariant::Regular, false) => 0.42,
479 (GlassVariant::Regular, true) => -0.38,
480 (GlassVariant::Clear, false) => 0.12,
481 (GlassVariant::Clear, true) => -0.12,
482 (GlassVariant::Lens, false) => 0.10,
483 (GlassVariant::Lens, true) => -0.08,
484 });
485 let foreground = self.foreground.unwrap_or(colors.label);
486 let shadow = self.shadow_style.unwrap_or_else(|| {
487 GlassShadow::new(
488 Color::BLACK.with_alpha(match (self.variant, colors.is_dark) {
489 (GlassVariant::Lens, false) => 0.14,
490 (GlassVariant::Lens, true) => 0.28,
491 (_, false) => 0.16,
492 (_, true) => 0.5,
493 }),
494 if self.variant == GlassVariant::Lens {
495 10.0
496 } else {
497 22.0
498 },
499 if self.variant == GlassVariant::Lens {
500 3.0
501 } else {
502 8.0
503 },
504 if self.variant == GlassVariant::Lens {
505 -6.0
506 } else {
507 -2.0
508 },
509 )
510 });
511 ResolvedGlass {
512 shape: self.shape,
513 tint: self.tint.unwrap_or(colors.glass_tint),
514 blur_radius_dp: self
515 .blur_radius
516 .unwrap_or_else(|| self.default_blur_radius()),
517 saturation: self.saturation.unwrap_or_else(|| self.default_saturation()),
518 refraction_depth: self.refraction_depth,
519 refraction_curve: self.refraction_curve,
520 dispersion: self.dispersion,
521 transmission_refraction: self.transmission_refraction,
522 meniscus_absorption: self.meniscus_absorption,
523 fold_depth: self.fold_depth,
524 optical_zoom: self.optical_zoom,
525 highlight: self.highlight,
526 lift,
527 contrast: if self.variant == GlassVariant::Lens {
528 1.0
529 } else {
530 1.03
531 },
532 shadow: self.shadow,
533 clip: self.clip,
534 foreground_luma: 0.2126 * foreground.r()
535 + 0.7152 * foreground.g()
536 + 0.0722 * foreground.b(),
537 adaptive_frost: self.adaptive_frost,
538 rim_style: if self.variant == GlassVariant::Lens {
539 1.0
540 } else {
541 0.0
542 },
543 shadow_color: shadow.color,
544 shadow_radius: shadow.radius,
545 shadow_offset_y: shadow.offset_y,
546 shadow_spread: shadow.spread,
547 }
548 }
549}
550
551impl Default for Glass {
552 fn default() -> Self {
553 Self::regular()
554 }
555}
556
557#[derive(Clone, Debug, PartialEq)]
560pub(crate) struct ResolvedGlass {
561 pub shape: LiquidShape,
562 pub tint: Color,
563 pub blur_radius_dp: f32,
564 pub saturation: f32,
565 pub refraction_depth: f32,
566 pub refraction_curve: f32,
567 pub dispersion: f32,
568 pub transmission_refraction: f32,
569 pub meniscus_absorption: f32,
570 pub fold_depth: f32,
571 pub optical_zoom: f32,
572 pub highlight: f32,
573 pub lift: f32,
574 pub contrast: f32,
575 pub shadow: bool,
576 pub clip: bool,
577 pub rim_style: f32,
580 pub foreground_luma: f32,
581 pub adaptive_frost: f32,
582 pub shadow_color: Color,
583 pub shadow_radius: f32,
586 pub shadow_offset_y: f32,
587 pub shadow_spread: f32,
588}
589
590impl ResolvedGlass {
591 pub(crate) fn backdrop_effect(&self, density: f32, dynamics: GlassDynamics) -> RenderEffect {
595 self.runtime_effect(density, dynamics, false)
596 }
597
598 fn content_mask_effect(&self, density: f32, dynamics: GlassDynamics) -> RenderEffect {
599 self.runtime_effect(density, dynamics, true)
600 }
601
602 fn runtime_effect(
603 &self,
604 density: f32,
605 dynamics: GlassDynamics,
606 content_mask: bool,
607 ) -> RenderEffect {
608 let density = density.max(f32::EPSILON);
609 let activity = dynamics
610 .activity
611 .filter(|value| value.is_finite())
612 .unwrap_or(1.0)
613 .clamp(0.0, 1.0);
614 let mut shader = RuntimeShader::new(LIQUID_GLASS_WGSL);
615 if let Some(morph) = dynamics.morph.as_ref() {
616 let (node_w, node_h) = morph.node_size;
622 let (cx, cy, w, h, radius) = morph.primary;
623 shader.set_float2(0, node_w.max(1.0), node_h.max(1.0));
624 shader.set_float2(2, cx, cy);
625 shader.set_float2(4, w, h);
626 shader.set_float(6, radius);
627 let count = morph.shapes.len().min(GlassMorph::MAX_SHAPES);
628 shader.set_float(30, count as f32);
629 for (index, (sx, sy, sw, sh, sr)) in morph.shapes.iter().take(count).enumerate() {
630 let base = 36 + index * 5;
631 shader.set_float(base, *sx);
632 shader.set_float(base + 1, *sy);
633 shader.set_float(base + 2, *sw);
634 shader.set_float(base + 3, *sh);
635 shader.set_float(base + 4, *sr);
636 }
637 shader.set_float(31, morph.glue);
638 shader.set_float(32, morph.wobble_amplitude * activity);
639 shader.set_float(33, morph.wobble_phase);
640 shader.set_float(26, morph.bulge_amplitude * activity);
641 shader.set_float(27, morph.bulge_direction);
642 shader.set_float(110, morph.ellipse_blend.clamp(0.0, 1.0) * activity);
643 if let Some(deformation) = morph.deformation {
644 let axis = deformation.axis();
645 let along = 1.0 + (deformation.along() - 1.0) * activity;
646 shader.set_float2(106, axis.0, axis.1);
647 shader.set_float(108, along);
648 shader.set_float(109, 1.0 / along);
649 } else {
650 shader.set_float2(106, 1.0, 0.0);
651 shader.set_float2(108, 1.0, 1.0);
652 }
653 } else {
655 shader.set_float2(0, 0.0, 0.0);
658 shader.set_float(6, self.shape.shader_radius_px(density));
659 }
660 shader.set_float(9, self.refraction_depth * activity);
661 shader.set_float(
662 GLASS_REFRACTION_CURVE_UNIFORM,
663 self.refraction_curve * activity,
664 );
665 shader.set_float(GLASS_DISPERSION_UNIFORM, self.dispersion * activity);
666 shader.set_float(
667 GLASS_TRANSMISSION_REFRACTION_UNIFORM,
668 self.transmission_refraction * activity,
669 );
670 shader.set_float(GLASS_MENISCUS_ABSORPTION_UNIFORM, self.meniscus_absorption);
671 if self.fold_depth > 0.0 {
672 shader.set_float(GLASS_FOLD_DEPTH_UNIFORM, self.fold_depth);
673 }
674 if self.optical_zoom > 1.0 {
675 shader.set_float(
676 GLASS_OPTICAL_ZOOM_UNIFORM,
677 1.0 + (self.optical_zoom - 1.0) * activity,
678 );
679 }
680 if let Some((touch_x, touch_y, intensity)) = dynamics.touch {
681 shader.set_float(118, touch_x);
682 shader.set_float(119, touch_y);
683 shader.set_float(120, intensity.clamp(0.0, 1.0));
684 }
685 shader.set_float(GLASS_EFFECT_DENSITY_UNIFORM, density);
686 shader.set_float(
687 11,
688 (self.highlight + dynamics.highlight_boost).clamp(0.0, 2.0) * activity,
689 );
690 let dynamic_tint = boost_tint_saturation(self.tint, dynamics.saturation_boost);
691 let dynamic_tint_alpha = (dynamic_tint.a()
692 * dynamics
693 .tint_alpha_multiplier
694 .unwrap_or(1.0)
695 .clamp(0.0, 2.0)
696 * activity)
697 .clamp(0.0, 1.0);
698 shader.set_float4(
699 14,
700 dynamic_tint.r(),
701 dynamic_tint.g(),
702 dynamic_tint.b(),
703 dynamic_tint_alpha,
704 );
705 let saturation = (self.saturation + dynamics.saturation_boost).max(0.0);
706 shader.set_float(18, 1.0 + (saturation - 1.0) * activity);
707 shader.set_float(20, self.lift * activity);
708 shader.set_float(21, 0.5 * activity);
709 shader.set_float2(22, 0.0, 1.0);
710 shader.set_float(24, 1.0 + (self.contrast - 1.0) * activity);
711 shader.set_float(28, self.rim_style * activity);
712 let requested_blur_radius_px = if content_mask {
713 0.0
714 } else {
715 self.blur_radius_dp * density * activity
716 };
717 let wcksrd_blur_radius = requested_blur_radius_px.min(WCKSRD_OPTICAL_BLUR_RADIUS_PX);
718 let gaussian_blur_radius = (requested_blur_radius_px - wcksrd_blur_radius).max(0.0);
719 shader.set_float(GLASS_BLUR_RADIUS_UNIFORM, wcksrd_blur_radius);
720 shader.set_float(GLASS_ACTIVITY_UNIFORM, activity);
721 let resting_tint = dynamics.resting_tint.unwrap_or(Color::TRANSPARENT);
722 shader.set_float4(
723 GLASS_RESTING_TINT_UNIFORM,
724 resting_tint.r(),
725 resting_tint.g(),
726 resting_tint.b(),
727 resting_tint.a(),
728 );
729 shader.set_float(112, if content_mask { 1.0 } else { 0.0 });
730 shader.set_float(91, self.adaptive_frost * activity);
731 shader.set_float(97, self.foreground_luma);
732 let dynamic_shadow = !self.clip && self.shadow;
733 shader.set_float(
734 102,
735 if dynamic_shadow {
736 self.shadow_color.a() * 0.55 * activity
737 } else {
738 0.0
739 },
740 );
741 shader.set_float(103, self.shadow_radius);
742 shader.set_float(104, self.shadow_offset_y);
743 shader.set_float(105, self.shadow_spread);
744 let morph_pad = dynamics
748 .morph
749 .as_ref()
750 .map(|morph| {
751 let (px, py, pw, ph, _) = morph.primary;
752 let (left, top) = (px - pw * 0.5, py - ph * 0.5);
753 let (right, bottom) = (px + pw * 0.5, py + ph * 0.5);
754 let mut shape_reach = 0.0f32;
755 for (sx, sy, sw, sh, _) in &morph.shapes {
756 let reach_x = ((sx + sw * 0.5) - right)
757 .max(left - (sx - sw * 0.5))
758 .max(0.0);
759 let reach_y = ((sy + sh * 0.5) - bottom)
760 .max(top - (sy - sh * 0.5))
761 .max(0.0);
762 shape_reach = shape_reach.max(reach_x.max(reach_y));
763 }
764 let glue_pad = if morph.shapes.is_empty() {
765 0.0
766 } else {
767 morph.glue * 2.0
768 };
769 morph.wobble_amplitude * 2.0 + morph.bulge_amplitude + shape_reach + glue_pad
770 })
771 .unwrap_or(0.0);
772 shader.set_input_padding(self.input_padding() + morph_pad + wcksrd_blur_radius / density);
775 if dynamics.morph.is_some() {
779 let shadow_reach = if dynamic_shadow {
780 self.shadow_radius + self.shadow_offset_y.abs() + self.shadow_spread.max(0.0)
781 } else {
782 0.0
783 };
784 shader.set_output_padding(morph_pad + shadow_reach + 4.0);
785 }
786
787 let optical_effect = RenderEffect::runtime_shader(shader);
788 if gaussian_blur_radius > f32::EPSILON {
789 RenderEffect::blur(gaussian_blur_radius).then(optical_effect)
790 } else {
791 optical_effect
792 }
793 }
794
795 fn input_padding(&self) -> f32 {
799 2.0
804 }
805}
806
807pub trait LiquidModifierExt {
809 fn glass_effect(self, glass: Glass) -> Modifier;
815
816 fn glass_effect_with(
820 self,
821 glass: Glass,
822 dynamics: impl Fn() -> GlassDynamics + 'static,
823 ) -> Modifier;
824}
825
826impl LiquidModifierExt for Modifier {
827 fn glass_effect(self, glass: Glass) -> Modifier {
828 self.glass_effect_with(glass, GlassDynamics::default)
829 }
830
831 fn glass_effect_with(
832 self,
833 glass: Glass,
834 dynamics: impl Fn() -> GlassDynamics + 'static,
835 ) -> Modifier {
836 let colors = crate::theme::liquid_colors();
837 let resolved = Rc::new(glass.resolve(&colors));
838 let shape = resolved.shape;
839
840 let mut modifier = self;
841 if resolved.shadow && resolved.clip {
842 let shadow_color = resolved.shadow_color;
843 let (radius, offset_y, spread) = (
844 resolved.shadow_radius,
845 resolved.shadow_offset_y,
846 resolved.shadow_spread,
847 );
848 modifier = modifier.drop_shadow(shape.layer_shape(), move |scope| {
849 scope.radius = radius;
850 scope.spread = spread;
851 scope.offset.y = offset_y;
852 scope.color = shadow_color;
853 scope.cutout = true;
856 });
857 }
858
859 let layer_resolved = Rc::clone(&resolved);
860 let clip = resolved.clip;
861 modifier.graphics_layer(move || {
862 let density = current_density();
863 let frame = dynamics();
864 let render_effect = (!clip && frame.morph.is_some())
865 .then(|| layer_resolved.content_mask_effect(density, frame.clone()));
866 GraphicsLayer {
867 backdrop_effect: Some(layer_resolved.backdrop_effect(density, frame)),
868 render_effect,
869 shape: shape.layer_shape(),
870 clip,
871 ..Default::default()
872 }
873 })
874 }
875}
876
877#[cfg(test)]
878mod tests {
879 use super::*;
880
881 fn light_colors() -> LiquidColors {
882 LiquidColors::light(Color::from_rgb_u8(0, 122, 255))
883 }
884
885 fn terminal_shader(effect: RenderEffect) -> RuntimeShader {
886 match effect {
887 RenderEffect::Shader { shader } => shader,
888 RenderEffect::Chain { second, .. } => terminal_shader(*second),
889 effect => panic!("expected runtime shader, got {effect:?}"),
890 }
891 }
892
893 #[test]
894 fn liquid_shape_builds_matching_clip_and_layer_shapes() {
895 for shape in [
896 LiquidShape::Capsule,
897 LiquidShape::Circle,
898 LiquidShape::RoundedRect(12.0),
899 ] {
900 assert_eq!(shape.layer_shape(), LayerShape::Rounded(shape.clip_shape()));
901 }
902 }
903
904 #[test]
905 fn glass_shadow_clamps_negative_radius() {
906 let shadow = GlassShadow::new(Color::BLACK, -2.0, 3.0, -1.0);
907 assert_eq!(shadow.radius, 0.0);
908 assert_eq!(shadow.offset_y, 3.0);
909 assert_eq!(shadow.spread, -1.0);
910 }
911
912 #[test]
913 fn incompressible_deformation_normalizes_axis_and_conserves_area() {
914 let deformation = GlassDeformation::incompressible((3.0, 4.0), 1.25);
915 assert_eq!(deformation.axis(), (0.6, 0.8));
916 assert_eq!(deformation.along(), 1.25);
917 assert!((deformation.along() * deformation.across() - 1.0).abs() < 1.0e-6);
918 assert_eq!(
919 GlassDeformation::incompressible((0.0, 0.0), 0.0).axis(),
920 (1.0, 0.0)
921 );
922 }
923
924 #[test]
925 fn glass_builders_clamp_physical_inputs() {
926 let glass = Glass::lens()
927 .shape(LiquidShape::Circle)
928 .tint(Color::BLACK)
929 .blur_radius(-2.0)
930 .saturation(1.2)
931 .refraction_depth(3.0)
932 .refraction_curve(2.0)
933 .dispersion(2.0)
934 .transmission_refraction(2.0)
935 .meniscus_absorption(2.0)
936 .highlight(0.4)
937 .lift(-0.2)
938 .adaptive_frost(Color::WHITE, 2.0)
939 .shadow(false)
940 .no_clip();
941 assert_eq!(glass.shape, LiquidShape::Circle);
942 assert_eq!(glass.tint, Some(Color::BLACK));
943 assert_eq!(glass.blur_radius, Some(-2.0));
944 assert_eq!(glass.saturation, Some(1.2));
945 assert_eq!(glass.refraction_depth, 2.0);
946 assert_eq!(glass.refraction_curve, 1.0);
947 assert_eq!(glass.dispersion, 1.0);
948 assert_eq!(glass.transmission_refraction, 1.0);
949 assert_eq!(glass.meniscus_absorption, 1.0);
950 assert_eq!(glass.highlight, 0.4);
951 assert_eq!(glass.lift, Some(-0.2));
952 assert_eq!(glass.adaptive_frost, 1.0);
953 assert!(!glass.shadow);
954 assert!(!glass.clip);
955 }
956
957 #[test]
958 fn material_variants_resolve_distinct_frost_levels() {
959 let regular = Glass::regular().resolve(&light_colors());
960 let clear = Glass::clear().resolve(&light_colors());
961 let lens = Glass::lens().resolve(&light_colors());
962 assert!(regular.blur_radius_dp > clear.blur_radius_dp);
963 assert!(clear.blur_radius_dp > lens.blur_radius_dp);
964 assert!(regular.saturation > clear.saturation);
965 assert_eq!(lens.rim_style, 1.0);
966 assert_eq!(regular.refraction_curve, 0.25);
967 assert_eq!(lens.refraction_curve, 1.0);
968 assert_eq!(regular.dispersion, 0.0);
969 assert_eq!(lens.dispersion, 0.30);
970 }
971
972 #[test]
973 fn neutral_surface_helpers_follow_foreground_polarity() {
974 assert_eq!(
975 neutral_surface_tint(Color::BLACK, 0.08, 0.10),
976 Color::BLACK.with_alpha(0.08)
977 );
978 assert_eq!(
979 neutral_surface_tint(Color::WHITE, 0.08, 0.10),
980 Color::WHITE.with_alpha(0.10)
981 );
982 assert_eq!(neutral_surface_lift(Color::BLACK, 0.7, -0.3), 0.7);
983 assert_eq!(neutral_surface_lift(Color::WHITE, 0.7, -0.3), -0.3);
984 }
985
986 #[test]
987 fn dynamic_saturation_reaches_the_material_tint() {
988 let resting = Color::from_rgb_u8(0, 199, 208);
989 let raised = boost_tint_saturation(resting, 0.55);
990 assert_eq!(raised.r(), 0.0);
991 assert!(raised.g() > resting.g());
992 assert!(raised.b() > resting.b());
993 assert_eq!(raised.a(), resting.a());
994 }
995
996 #[test]
997 fn resolved_material_packs_wcksrd_and_dynamic_tint() {
998 let resolved = Glass::lens()
999 .refraction_depth(0.72)
1000 .refraction_curve(0.8)
1001 .dispersion(0.42)
1002 .transmission_refraction(0.35)
1003 .meniscus_absorption(0.3)
1004 .blur_radius(3.0)
1005 .tint(Color::BLACK.with_alpha(0.8))
1006 .resolve(&light_colors());
1007 let effect = resolved.backdrop_effect(
1008 2.0,
1009 GlassDynamics {
1010 highlight_boost: 0.2,
1011 saturation_boost: 0.35,
1012 tint_alpha_multiplier: Some(0.25),
1013 ..Default::default()
1014 },
1015 );
1016 let RenderEffect::Chain { first, .. } = &effect else {
1017 panic!("macroscopic frost must precede the wcKSRD optical pass");
1018 };
1019 assert!(matches!(
1020 first.as_ref(),
1021 RenderEffect::Blur {
1022 radius_x: 4.0,
1023 radius_y: 4.0,
1024 ..
1025 }
1026 ));
1027 let shader = terminal_shader(effect);
1028 assert_eq!(shader.uniforms()[9], 0.72);
1029 assert_eq!(shader.uniforms()[GLASS_REFRACTION_CURVE_UNIFORM], 0.8);
1030 assert_eq!(shader.uniforms()[GLASS_DISPERSION_UNIFORM], 0.42);
1031 assert_eq!(
1032 shader.uniforms()[GLASS_TRANSMISSION_REFRACTION_UNIFORM],
1033 0.35
1034 );
1035 assert_eq!(shader.uniforms()[GLASS_MENISCUS_ABSORPTION_UNIFORM], 0.3);
1036 assert_eq!(shader.uniforms()[11], resolved.highlight + 0.2);
1037 assert_eq!(shader.uniforms()[18], resolved.saturation + 0.35);
1038 assert!((shader.uniforms()[17] - 0.2).abs() < 1.0e-6);
1039 assert_eq!(
1040 shader.uniforms()[GLASS_BLUR_RADIUS_UNIFORM],
1041 WCKSRD_OPTICAL_BLUR_RADIUS_PX
1042 );
1043 assert_eq!(shader.uniforms()[GLASS_EFFECT_DENSITY_UNIFORM], 2.0);
1044 }
1045
1046 #[test]
1047 fn raised_tint_density_stays_premultiplied_alpha_safe() {
1048 let effect = Glass::lens()
1049 .tint(Color::WHITE.with_alpha(0.8))
1050 .resolve(&light_colors())
1051 .backdrop_effect(
1052 1.0,
1053 GlassDynamics {
1054 tint_alpha_multiplier: Some(2.0),
1055 ..Default::default()
1056 },
1057 );
1058 assert_eq!(terminal_shader(effect).uniforms()[17], 1.0);
1059 }
1060
1061 #[test]
1062 fn optical_activity_reaches_identity_without_removing_the_glass_geometry() {
1063 let resolved = Glass::lens()
1064 .refraction_depth(0.72)
1065 .refraction_curve(0.8)
1066 .dispersion(0.42)
1067 .blur_radius(3.0)
1068 .saturation(1.4)
1069 .highlight(0.6)
1070 .lift(0.2)
1071 .tint(Color::BLACK.with_alpha(0.8))
1072 .no_clip()
1073 .resolve(&light_colors());
1074 let morph = GlassMorph {
1075 node_size: (120.0, 72.0),
1076 primary: (60.0, 36.0, 96.0, 52.0, -1.0),
1077 wobble_amplitude: 3.0,
1078 bulge_amplitude: 4.0,
1079 deformation: Some(GlassDeformation::incompressible((1.0, 0.0), 1.25)),
1080 ..Default::default()
1081 };
1082
1083 let RenderEffect::Shader { shader } = resolved.backdrop_effect(
1084 2.0,
1085 GlassDynamics {
1086 activity: Some(0.0),
1087 morph: Some(morph),
1088 ..Default::default()
1089 },
1090 ) else {
1091 panic!("material must resolve to the shared wcKSRD shader");
1092 };
1093 let uniforms = shader.uniforms();
1094 assert_eq!(uniforms[GLASS_ACTIVITY_UNIFORM], 0.0);
1095 assert_eq!(uniforms[9], 0.0);
1096 assert_eq!(uniforms[GLASS_REFRACTION_CURVE_UNIFORM], 0.0);
1097 assert_eq!(uniforms[GLASS_DISPERSION_UNIFORM], 0.0);
1098 assert_eq!(uniforms[GLASS_TRANSMISSION_REFRACTION_UNIFORM], 0.0);
1099 assert_eq!(uniforms[11], 0.0);
1100 assert_eq!(uniforms[17], 0.0);
1101 assert_eq!(uniforms[18], 1.0);
1102 assert_eq!(uniforms[20], 0.0);
1103 assert_eq!(uniforms[21], 0.0);
1104 assert_eq!(uniforms[24], 1.0);
1105 assert_eq!(uniforms[28], 0.0);
1106 assert_eq!(uniforms[GLASS_BLUR_RADIUS_UNIFORM], 0.0);
1107 assert_eq!(uniforms[91], 0.0);
1108 assert_eq!(uniforms[102], 0.0);
1109 assert_eq!(
1110 &uniforms[GLASS_RESTING_TINT_UNIFORM..GLASS_RESTING_TINT_UNIFORM + 4],
1111 &[0.0, 0.0, 0.0, 0.0]
1112 );
1113 assert_eq!(uniforms[32], 0.0);
1114 assert_eq!(uniforms[26], 0.0);
1115 assert_eq!(&uniforms[108..110], &[1.0, 1.0]);
1116 assert_eq!(&uniforms[2..6], &[60.0, 36.0, 96.0, 52.0]);
1117 }
1118
1119 #[test]
1120 fn resting_surface_tint_survives_zero_optical_activity() {
1121 let tint = Color::BLACK.with_alpha(0.11);
1122 let RenderEffect::Shader { shader } = Glass::lens()
1123 .no_clip()
1124 .resolve(&light_colors())
1125 .backdrop_effect(
1126 1.0,
1127 GlassDynamics {
1128 activity: Some(0.0),
1129 resting_tint: Some(tint),
1130 ..Default::default()
1131 },
1132 )
1133 else {
1134 panic!("resting surface must use the shared wcKSRD shader");
1135 };
1136 assert_eq!(
1137 &shader.uniforms()[GLASS_RESTING_TINT_UNIFORM..GLASS_RESTING_TINT_UNIFORM + 4],
1138 &[tint.r(), tint.g(), tint.b(), tint.a()]
1139 );
1140 assert_eq!(shader.uniforms()[GLASS_ACTIVITY_UNIFORM], 0.0);
1141 }
1142
1143 #[test]
1144 fn full_optical_activity_preserves_the_resolved_material() {
1145 let resolved = Glass::lens()
1146 .refraction_depth(0.72)
1147 .refraction_curve(0.8)
1148 .dispersion(0.42)
1149 .blur_radius(3.0)
1150 .resolve(&light_colors());
1151 let shader = terminal_shader(resolved.backdrop_effect(
1152 2.0,
1153 GlassDynamics {
1154 activity: Some(1.0),
1155 ..Default::default()
1156 },
1157 ));
1158 let uniforms = shader.uniforms();
1159 assert_eq!(uniforms[GLASS_ACTIVITY_UNIFORM], 1.0);
1160 assert_eq!(uniforms[9], 0.72);
1161 assert_eq!(uniforms[GLASS_REFRACTION_CURVE_UNIFORM], 0.8);
1162 assert_eq!(uniforms[GLASS_DISPERSION_UNIFORM], 0.42);
1163 assert_eq!(uniforms[GLASS_TRANSMISSION_REFRACTION_UNIFORM], 1.0);
1164 assert_eq!(
1165 uniforms[GLASS_BLUR_RADIUS_UNIFORM],
1166 WCKSRD_OPTICAL_BLUR_RADIUS_PX
1167 );
1168 }
1169
1170 #[test]
1171 fn morph_geometry_and_incompressible_strain_are_packed() {
1172 let deformation = GlassDeformation::incompressible((0.0, 2.0), 1.25);
1173 let morph = GlassMorph {
1174 node_size: (78.0, 59.0),
1175 primary: (39.0, 29.5, 58.0, 39.0, -1.0),
1176 shapes: vec![(70.0, 29.5, 40.0, 40.0, -1.0)],
1177 glue: 8.0,
1178 wobble_amplitude: 1.0,
1179 wobble_phase: 0.5,
1180 bulge_amplitude: 2.0,
1181 bulge_direction: 0.25,
1182 ellipse_blend: 0.3,
1183 deformation: Some(deformation),
1184 };
1185 let RenderEffect::Shader { shader } = Glass::lens()
1186 .no_clip()
1187 .resolve(&light_colors())
1188 .backdrop_effect(
1189 1.0,
1190 GlassDynamics {
1191 morph: Some(morph),
1192 ..Default::default()
1193 },
1194 )
1195 else {
1196 panic!("morph must use the shared wcKSRD shader");
1197 };
1198 let uniforms = shader.uniforms();
1199 assert_eq!(&uniforms[0..6], &[78.0, 59.0, 39.0, 29.5, 58.0, 39.0]);
1200 assert_eq!(uniforms[30], 1.0);
1201 assert_eq!(&uniforms[106..110], &[0.0, 1.0, 1.25, 0.8]);
1202 assert_eq!(uniforms[110], 0.3);
1203 assert!(shader.output_padding() > 0.0);
1204 }
1205}