theme: add transparency knob for material fill
This commit is contained in:
+126
-2
@@ -1,6 +1,6 @@
|
||||
use crate::definition::ThemeDefinition;
|
||||
use crate::ramps::{AccentShades, Oklch, contrast_foreground, derive_shades, mix};
|
||||
use crate::system::SystemTheme;
|
||||
use crate::system::{SourceKind, SystemTheme};
|
||||
use crate::{
|
||||
Borders, ColorTokens, CustomTheme, Elevations, MaterialTokens, Motion, Radius, Scrollbar,
|
||||
SizeTable, Spacing, ThemeVariant, Typography,
|
||||
@@ -198,8 +198,14 @@ pub(crate) fn resolve(
|
||||
),
|
||||
};
|
||||
|
||||
// The knob scales the fill over a platform-stated blur.
|
||||
let transparency = custom
|
||||
.transparency
|
||||
.map(|level| level.min(100))
|
||||
.filter(|_| system.source != SourceKind::Windows && system.window_background.is_some());
|
||||
|
||||
// Reduced transparency forces the window opaque.
|
||||
let window_background = if system.accessibility.reduce_transparency {
|
||||
let window_background = if system.accessibility.reduce_transparency || transparency == Some(0) {
|
||||
WindowBackgroundAppearance::Opaque
|
||||
} else {
|
||||
system
|
||||
@@ -303,6 +309,7 @@ pub(crate) fn resolve(
|
||||
ramps.primary.base,
|
||||
variant,
|
||||
window_background != WindowBackgroundAppearance::Opaque,
|
||||
transparency,
|
||||
);
|
||||
|
||||
let platform_elevations = Elevations::for_platform(system.source, variant);
|
||||
@@ -638,6 +645,123 @@ mod tests {
|
||||
assert_eq!(resolved.material.selection, resolved.ramps.primary.base);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_custom_transparency_level_scales_the_material_fill() {
|
||||
let mut stated = system(ThemeVariant::Dark, PartialPalette::default(), None);
|
||||
stated.window_background = Some(WindowBackgroundAppearance::Blurred);
|
||||
let custom = CustomTheme {
|
||||
transparency: Some(80),
|
||||
..CustomTheme::default()
|
||||
};
|
||||
let resolved = resolve(&stated, None, None, Some(custom));
|
||||
|
||||
assert_eq!(
|
||||
resolved.window_background,
|
||||
WindowBackgroundAppearance::Blurred
|
||||
);
|
||||
assert!((resolved.material.background.a - 0.32).abs() < 1e-6);
|
||||
|
||||
// Levels past 100 clamp, bottoming out at the dark floor.
|
||||
let clamped = resolve(
|
||||
&stated,
|
||||
None,
|
||||
None,
|
||||
Some(CustomTheme {
|
||||
transparency: Some(150),
|
||||
..CustomTheme::default()
|
||||
}),
|
||||
);
|
||||
assert!((clamped.material.background.a - 0.15).abs() < 1e-6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn light_mode_keeps_a_heavier_fill_at_full_transparency() {
|
||||
let mut light_system = system(ThemeVariant::Light, PartialPalette::default(), None);
|
||||
light_system.window_background = Some(WindowBackgroundAppearance::Blurred);
|
||||
let mut dark_system = system(ThemeVariant::Dark, PartialPalette::default(), None);
|
||||
dark_system.window_background = Some(WindowBackgroundAppearance::Blurred);
|
||||
let maxed = CustomTheme {
|
||||
transparency: Some(100),
|
||||
..CustomTheme::default()
|
||||
};
|
||||
let light = resolve(&light_system, None, None, Some(maxed));
|
||||
let dark = resolve(&dark_system, None, None, Some(maxed));
|
||||
|
||||
assert!((light.material.background.a - 0.45).abs() < 1e-6);
|
||||
assert!((dark.material.background.a - 0.15).abs() < 1e-6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transparency_zero_opens_an_opaque_window() {
|
||||
let mut stated = system(ThemeVariant::Dark, PartialPalette::default(), None);
|
||||
stated.window_background = Some(WindowBackgroundAppearance::Blurred);
|
||||
let resolved = resolve(
|
||||
&stated,
|
||||
None,
|
||||
None,
|
||||
Some(CustomTheme {
|
||||
transparency: Some(0),
|
||||
..CustomTheme::default()
|
||||
}),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolved.window_background,
|
||||
WindowBackgroundAppearance::Opaque
|
||||
);
|
||||
assert_eq!(resolved.material.background, resolved.colors.background);
|
||||
assert_eq!(resolved.material.hover, resolved.colors.muted);
|
||||
assert_eq!(resolved.material.selection, resolved.ramps.primary.base);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_transparency_knob_skips_windows_and_platforms_without_a_material() {
|
||||
let mut windows = system(ThemeVariant::Dark, PartialPalette::default(), None);
|
||||
windows.source = SourceKind::Windows;
|
||||
windows.window_background = Some(WindowBackgroundAppearance::MicaBackdrop);
|
||||
let resolved_windows = resolve(
|
||||
&windows,
|
||||
None,
|
||||
None,
|
||||
Some(CustomTheme {
|
||||
transparency: Some(100),
|
||||
..CustomTheme::default()
|
||||
}),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolved_windows.window_background,
|
||||
WindowBackgroundAppearance::MicaBackdrop
|
||||
);
|
||||
// The knob's value is ignored on Windows; the default drives the fill.
|
||||
let unset = resolve(&windows, None, None, None);
|
||||
assert_eq!(
|
||||
resolved_windows.material.background,
|
||||
unset.material.background
|
||||
);
|
||||
|
||||
let mut unstated = system(ThemeVariant::Dark, PartialPalette::default(), None);
|
||||
unstated.window_background = None;
|
||||
let resolved_unstated = resolve(
|
||||
&unstated,
|
||||
None,
|
||||
None,
|
||||
Some(CustomTheme {
|
||||
transparency: Some(100),
|
||||
..CustomTheme::default()
|
||||
}),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolved_unstated.window_background,
|
||||
WindowBackgroundAppearance::Opaque
|
||||
);
|
||||
assert_eq!(
|
||||
resolved_unstated.material.background,
|
||||
resolved_unstated.colors.background
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overlay_is_translucent_and_overridable() {
|
||||
let light = resolve(
|
||||
|
||||
Reference in New Issue
Block a user