theme: add transparency knob for material fill
This commit is contained in:
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
use gpui::{Hsla, hsla, rgb};
|
use gpui::{Hsla, hsla, rgb};
|
||||||
|
|
||||||
use crate::ThemeVariant;
|
use crate::{DEFAULT_TRANSPARENCY, MIN_MATERIAL_FILL_DARK, MIN_MATERIAL_FILL_LIGHT, ThemeVariant};
|
||||||
|
|
||||||
fn hex(hex: u32) -> Hsla {
|
fn hex(hex: u32) -> Hsla {
|
||||||
rgb(hex).into()
|
rgb(hex).into()
|
||||||
@@ -136,6 +136,7 @@ impl MaterialTokens {
|
|||||||
selection: Hsla,
|
selection: Hsla,
|
||||||
variant: ThemeVariant,
|
variant: ThemeVariant,
|
||||||
translucent: bool,
|
translucent: bool,
|
||||||
|
transparency: Option<u8>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
if !translucent {
|
if !translucent {
|
||||||
return Self {
|
return Self {
|
||||||
@@ -145,13 +146,16 @@ impl MaterialTokens {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Light tints heavier than dark: the blur carries less contrast there.
|
let level = transparency.unwrap_or(DEFAULT_TRANSPARENCY) as f32 / 100.0;
|
||||||
let background_alpha = match variant {
|
|
||||||
ThemeVariant::Light => 0.6,
|
let floor = match variant {
|
||||||
ThemeVariant::Dark => 0.5,
|
ThemeVariant::Light => MIN_MATERIAL_FILL_LIGHT,
|
||||||
|
ThemeVariant::Dark => MIN_MATERIAL_FILL_DARK,
|
||||||
};
|
};
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
background: colors.background.alpha(background_alpha),
|
// The fill eases toward a floor instead of zero.
|
||||||
|
background: colors.background.alpha(1.0 - level * (1.0 - floor)),
|
||||||
// A foreground wash keeps hover visible over any backdrop hue.
|
// A foreground wash keeps hover visible over any backdrop hue.
|
||||||
hover: colors.foreground.alpha(0.06),
|
hover: colors.foreground.alpha(0.06),
|
||||||
selection: selection.alpha(0.85),
|
selection: selection.alpha(0.85),
|
||||||
|
|||||||
@@ -104,6 +104,12 @@ pub const PIXEL_AVATAR_LIGHT_LIFT: f32 = 1.6;
|
|||||||
pub const PIXEL_AVATAR_LIGHT_CAP: f32 = 0.95;
|
pub const PIXEL_AVATAR_LIGHT_CAP: f32 = 0.95;
|
||||||
pub const PIXEL_AVATAR_DARK_FACTOR: f32 = 0.45;
|
pub const PIXEL_AVATAR_DARK_FACTOR: f32 = 0.45;
|
||||||
pub const PIXEL_AVATAR_DARK_FLOOR: f32 = 0.18;
|
pub const PIXEL_AVATAR_DARK_FLOOR: f32 = 0.18;
|
||||||
|
/// The default window material transparency, as a percent (0 opaque, 100 the lightest fill).
|
||||||
|
pub const DEFAULT_TRANSPARENCY: u8 = 94;
|
||||||
|
/// The light-mode floor of the material fill.
|
||||||
|
pub const MIN_MATERIAL_FILL_LIGHT: f32 = 0.45;
|
||||||
|
/// The dark-mode floor of the material fill.
|
||||||
|
pub const MIN_MATERIAL_FILL_DARK: f32 = 0.15;
|
||||||
|
|
||||||
/// Light or dark, resolved from the system appearance.
|
/// Light or dark, resolved from the system appearance.
|
||||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Deserialize)]
|
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Deserialize)]
|
||||||
@@ -664,6 +670,10 @@ pub struct CustomTheme {
|
|||||||
pub popover_elevation: Option<Elevation>,
|
pub popover_elevation: Option<Elevation>,
|
||||||
pub dialog_elevation: Option<Elevation>,
|
pub dialog_elevation: Option<Elevation>,
|
||||||
pub control_elevation: Option<Elevation>,
|
pub control_elevation: Option<Elevation>,
|
||||||
|
/// Window material transparency as a percent (0 opaque, 100 pure blur).
|
||||||
|
///
|
||||||
|
/// Note: windows platform is not supported
|
||||||
|
pub transparency: Option<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The engine's knobs and its platform source.
|
/// The engine's knobs and its platform source.
|
||||||
|
|||||||
+126
-2
@@ -1,6 +1,6 @@
|
|||||||
use crate::definition::ThemeDefinition;
|
use crate::definition::ThemeDefinition;
|
||||||
use crate::ramps::{AccentShades, Oklch, contrast_foreground, derive_shades, mix};
|
use crate::ramps::{AccentShades, Oklch, contrast_foreground, derive_shades, mix};
|
||||||
use crate::system::SystemTheme;
|
use crate::system::{SourceKind, SystemTheme};
|
||||||
use crate::{
|
use crate::{
|
||||||
Borders, ColorTokens, CustomTheme, Elevations, MaterialTokens, Motion, Radius, Scrollbar,
|
Borders, ColorTokens, CustomTheme, Elevations, MaterialTokens, Motion, Radius, Scrollbar,
|
||||||
SizeTable, Spacing, ThemeVariant, Typography,
|
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.
|
// 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
|
WindowBackgroundAppearance::Opaque
|
||||||
} else {
|
} else {
|
||||||
system
|
system
|
||||||
@@ -303,6 +309,7 @@ pub(crate) fn resolve(
|
|||||||
ramps.primary.base,
|
ramps.primary.base,
|
||||||
variant,
|
variant,
|
||||||
window_background != WindowBackgroundAppearance::Opaque,
|
window_background != WindowBackgroundAppearance::Opaque,
|
||||||
|
transparency,
|
||||||
);
|
);
|
||||||
|
|
||||||
let platform_elevations = Elevations::for_platform(system.source, variant);
|
let platform_elevations = Elevations::for_platform(system.source, variant);
|
||||||
@@ -638,6 +645,123 @@ mod tests {
|
|||||||
assert_eq!(resolved.material.selection, resolved.ramps.primary.base);
|
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]
|
#[test]
|
||||||
fn overlay_is_translucent_and_overridable() {
|
fn overlay_is_translucent_and_overridable() {
|
||||||
let light = resolve(
|
let light = resolve(
|
||||||
|
|||||||
Reference in New Issue
Block a user