chore: audit components

This commit is contained in:
2026-11-10 15:06:16 +07:00
parent af2177c6c8
commit 03369f501e
92 changed files with 2744 additions and 1947 deletions
+116 -40
View File
@@ -20,7 +20,9 @@ pub use ramps::{AccentShades, Oklch};
pub use resolve::{Ramps, ResolvedTheme};
pub use system::{AccessibilityPreferences, PartialPalette, SourceKind, SystemTheme};
use gpui::{App, Global, Hsla, Pixels, WindowAppearance, WindowBackgroundAppearance, px};
use gpui::{
App, Global, Hsla, Pixels, SpringConfig, WindowAppearance, WindowBackgroundAppearance, hsla, px,
};
use std::time::Duration;
/// The hairline width of the control border every button and field paints.
@@ -46,7 +48,51 @@ pub const TRAFFIC_LIGHT_PAD: Pixels = px(78.0);
/// The width of a Windows caption button.
pub const WINDOWS_CAPTION_WIDTH: Pixels = px(36.0);
/// Windows paints its caption close-button hover red in every theme.
pub const WINDOWS_CLOSE_HOVER_HEX: u32 = 0xE81120FF;
pub const WINDOWS_CLOSE_HOVER: Hsla = hsla(0.986043, 0.863406, 0.488235, 1.0);
/// The close-button hover dimmed while pressed.
pub const WINDOWS_CLOSE_PRESSED: Hsla = hsla(0.986043, 0.863406, 0.488235, 0.8);
/// The close glyph's color over the red hover.
pub const WINDOWS_CLOSE_FOREGROUND: Hsla = gpui::white();
/// The width a sidebar opens at.
pub const SIDEBAR_DEFAULT_WIDTH: Pixels = px(240.0);
/// The narrowest a sidebar may be dragged.
pub const SIDEBAR_MIN_WIDTH: Pixels = px(160.0);
/// The widest a sidebar may be dragged.
pub const SIDEBAR_MAX_WIDTH: Pixels = px(480.0);
/// The size a pane starts at before its group has measured the extent.
pub const PANE_DEFAULT_SIZE: Pixels = px(200.0);
/// The floor a pane keeps while dragging unless the caller lowers it.
pub const PANE_MIN_SIZE: Pixels = px(48.0);
/// The hairline a resize seam paints on a shared boundary.
pub const HAIRLINE_WIDTH: Pixels = px(0.5);
/// How much of the window shadow's strength survives losing focus.
pub const INACTIVE_SHADOW_OPACITY: f32 = 0.7;
/// The spread of the keyboard focus ring.
pub const FOCUS_RING_SPREAD: Pixels = px(3.0);
/// How far a pushed alert slides in on entry.
pub const ENTER_SLIDE_DISTANCE: Pixels = px(24.0);
/// The widest a toast may grow.
pub const TOAST_MAX_WIDTH: Pixels = px(480.0);
/// One revolution of the spinner.
pub const SPINNER_PERIOD: Duration = Duration::from_millis(800);
/// One sweep of the indeterminate progress bar.
pub const PROGRESS_ANIMATION_PERIOD: Duration = Duration::from_millis(1500);
/// How long an autohide scrollbar thumb stays fully visible before fading.
pub const SCROLLBAR_AUTOHIDE_DELAY: Duration = Duration::from_millis(700);
/// How long the autohide scrollbar's fade runs.
pub const SCROLLBAR_FADE_DURATION: Duration = Duration::from_millis(250);
/// How much a change wash tints the rows it covers.
pub const WASH_ALPHA: f32 = 0.14;
/// The stronger wash under number gutters and intra-line word changes.
pub const WASH_ALPHA_STRONG: f32 = 0.34;
/// The subtle wash that sets the number gutter off from the code.
pub const WASH_ALPHA_GUTTER: f32 = 0.18;
/// The soft wash that marks the active code row.
pub const WASH_ALPHA_SOFT: f32 = 0.45;
/// How far a scrolling list renders past its viewport.
pub const CODE_OVERSCROLL_MAX: Pixels = px(400.0);
/// The spring every control's toggle and segment animations ride.
pub const CONTROL_SPRING: SpringConfig = SpringConfig::new(500., 35., 1.);
/// The monospace advance estimate as a share of the text size.
pub const MONO_ADVANCE_RATIO: f32 = 0.62;
/// How much a pixel avatar's tint washes over its background.
@@ -73,6 +119,15 @@ impl ThemeVariant {
WindowAppearance::Dark | WindowAppearance::VibrantDark => Self::Dark,
}
}
/// Classifies a surface by its lightness, for platforms that state no mode.
pub fn by_lightness(background: Hsla) -> Self {
if background.l >= 0.5 {
Self::Light
} else {
Self::Dark
}
}
}
/// The resolved theme, installed as the app-wide global.
@@ -587,21 +642,23 @@ impl Theme {
reapply(cx);
}
/// Re-resolves when a window's appearance differs from the resolved variant.
/// Re-resolves when no override is set and a window's appearance differs
/// from the resolved variant; an override pins the variant instead.
///
/// Wire to `Window::observe_window_appearance`.
pub fn sync_from_window(window: &mut gpui::Window, cx: &mut App) {
let variant = ThemeVariant::from_appearance(window.appearance());
if cx.global::<Theme>().variant != variant
|| cx.global::<ThemeEngine>().mode_override.is_none()
{
let window_variant = ThemeVariant::from_appearance(window.appearance());
let theme = cx.global::<Theme>();
let engine = cx.global::<ThemeEngine>();
if engine.mode_override.is_none() && theme.variant != window_variant {
reapply(cx);
}
}
/// Sizes the window's rem to the theme's root font size.
pub fn sync_window_rem_size(window: &mut gpui::Window, cx: &App) {
window.set_rem_size(cx.global::<Theme>().root_font_size);
let theme = cx.global::<Theme>();
window.set_rem_size(theme.root_font_size);
}
}
@@ -617,20 +674,17 @@ impl Global for ThemeEngine {}
/// Reads the system, resolves through the current knobs,
/// installs the theme global, and repaints every window.
pub(crate) fn reapply(cx: &mut App) {
let (system_appearance, preset, mode_override) = {
let (preset, mode_override, system) = {
let engine = cx.global::<ThemeEngine>();
(
ThemeVariant::from_appearance(cx.window_appearance()),
engine.preset.as_ref().map(|(_, def)| def.clone()),
engine.mode_override,
)
let system_appearance = ThemeVariant::from_appearance(cx.window_appearance());
let preset = engine.preset.as_ref().map(|(_, def)| def.clone());
let mode_override = engine.mode_override;
let variant = mode_override
.or(preset.as_ref().and_then(|def| def.mode()))
.unwrap_or(system_appearance);
(preset, mode_override, engine.source.snapshot(variant))
};
let variant = mode_override
.or(preset.as_ref().and_then(|def| def.mode))
.unwrap_or(system_appearance);
let system = cx.global::<ThemeEngine>().source.snapshot(variant);
let resolved = resolve::resolve(&system, preset.as_ref(), mode_override);
cx.set_reduce_motion(resolved.accessibility.reduce_motion);
@@ -640,41 +694,39 @@ pub(crate) fn reapply(cx: &mut App) {
/// Forces a variant, independent of the OS. `None` follows the system.
pub fn set_mode_override(variant: Option<ThemeVariant>, cx: &mut App) {
cx.global_mut::<ThemeEngine>().mode_override = variant;
let engine = cx.global_mut::<ThemeEngine>();
engine.mode_override = variant;
reapply(cx);
}
/// Installs a built-in preset, or `None` to follow the system again.
pub fn set_preset(name: Option<&str>, cx: &mut App) -> anyhow::Result<()> {
let preset = match name {
Some(name) => {
let def =
presets::builtin(name).ok_or_else(|| anyhow::anyhow!("unknown preset: {name}"))?;
let static_name = presets::preset_names()
.iter()
.copied()
.find(|candidate| *candidate == name)
.ok_or_else(|| anyhow::anyhow!("unknown preset: {name}"))?;
Some((static_name, def))
}
None => None,
};
cx.global_mut::<ThemeEngine>().preset = preset;
let preset = name
.map(|name| {
presets::builtin(name).ok_or_else(|| {
anyhow::anyhow!(
"unknown preset: {name}; available: {}",
presets::preset_names().collect::<Vec<_>>().join(", ")
)
})
})
.transpose()?;
let engine = cx.global_mut::<ThemeEngine>();
engine.preset = preset;
reapply(cx);
Ok(())
}
/// The variant the mode override forces, if any.
pub fn mode_override(cx: &App) -> Option<ThemeVariant> {
cx.global::<ThemeEngine>().mode_override
let engine = cx.global::<ThemeEngine>();
engine.mode_override
}
/// The active preset name, if one is installed.
pub fn active_preset(cx: &App) -> Option<&'static str> {
cx.global::<ThemeEngine>()
.preset
.as_ref()
.map(|(name, _)| *name)
let engine = cx.global::<ThemeEngine>();
engine.preset.as_ref().map(|(name, _)| *name)
}
/// Clears the mode override and preset; the theme follows the system again.
@@ -687,5 +739,29 @@ pub fn follow_system(cx: &mut App) {
/// Which adapter produced the current system snapshot.
pub fn detected_source(cx: &App) -> &'static str {
cx.global::<ThemeEngine>().source.label()
let engine = cx.global::<ThemeEngine>();
engine.source.label()
}
#[cfg(test)]
mod tests {
use super::*;
use gpui::Rgba;
#[test]
fn the_windows_close_colors_match_the_legacy_hex() {
let hover = Hsla::from(gpui::rgba(0xE81123FF));
let pressed = hover.opacity(0.8);
for (token, expected) in [
(WINDOWS_CLOSE_HOVER, hover),
(WINDOWS_CLOSE_PRESSED, pressed),
] {
let (token, expected) = (Rgba::from(token), Rgba::from(expected));
assert!((token.r - expected.r).abs() < 1e-4);
assert!((token.g - expected.g).abs() < 1e-4);
assert!((token.b - expected.b).abs() < 1e-4);
assert!((token.a - expected.a).abs() < 1e-4);
}
assert_eq!(WINDOWS_CLOSE_FOREGROUND, gpui::white());
}
}