Skip to content

Commit

Permalink
themes: Make background colors partly transparent by default (#24151)
Browse files Browse the repository at this point in the history
Certain themes define the `created` and `deleted` status colors, but not
`created_background` and `deleted_background`. Previously, Zed would use
`created` and `deleted` colors, and apply a hard-coded opacity change,
but *not* use `created_background` and `deleted_background`, but that
behavior was inadvertently changed in
#22994.

This PR restores the old behavior as a fallback. If a theme defines a
status color, but not the corresponding background color, we'll use a
75% transparent version of the foreground color as a fallback.

Release Notes:

- Fixed an issue in certain themes where diffs would render with the
wrong red and green colors for deletions and insertions.
  • Loading branch information
maxbrunsfeld committed Feb 3, 2025
1 parent 092261a commit 623b6a2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
25 changes: 23 additions & 2 deletions crates/theme/src/fallback_themes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use std::sync::Arc;
use gpui::{hsla, FontStyle, FontWeight, HighlightStyle, Hsla, WindowBackgroundAppearance};

use crate::{
default_color_scales, AccentColors, Appearance, PlayerColors, StatusColors, SyntaxTheme,
SystemColors, Theme, ThemeColors, ThemeFamily, ThemeStyles,
default_color_scales, AccentColors, Appearance, PlayerColors, StatusColors,
StatusColorsRefinement, SyntaxTheme, SystemColors, Theme, ThemeColors, ThemeFamily,
ThemeStyles,
};

/// The default theme family for Zed.
Expand All @@ -21,6 +22,26 @@ pub fn zed_default_themes() -> ThemeFamily {
}
}

// If a theme customizes a foreground version of a status color, but does not
// customize the background color, then use a partly-transparent version of the
// foreground color for the background color.
pub(crate) fn apply_status_color_defaults(status: &mut StatusColorsRefinement) {
for (fg_color, bg_color) in [
(&status.deleted, &mut status.deleted_background),
(&status.created, &mut status.created_background),
(&status.modified, &mut status.modified_background),
(&status.conflict, &mut status.conflict_background),
(&status.error, &mut status.error_background),
(&status.hidden, &mut status.hidden_background),
] {
if bg_color.is_none() {
if let Some(fg_color) = fg_color {
*bg_color = Some(fg_color.opacity(0.25));
}
}
}
}

pub(crate) fn zed_default_dark() -> Theme {
let bg = hsla(215. / 360., 12. / 100., 15. / 100., 1.);
let editor = hsla(220. / 360., 12. / 100., 18. / 100., 1.);
Expand Down
6 changes: 3 additions & 3 deletions crates/theme/src/styles/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ impl StatusColors {
conflict_background: red().dark().step_9(),
conflict_border: red().dark().step_9(),
created: grass().dark().step_9(),
created_background: grass().dark().step_9(),
created_background: grass().dark().step_9().opacity(0.25),
created_border: grass().dark().step_9(),
deleted: red().dark().step_9(),
deleted_background: red().dark().step_9(),
deleted_background: red().dark().step_9().opacity(0.25),
deleted_border: red().dark().step_9(),
error: red().dark().step_9(),
error_background: red().dark().step_9(),
Expand All @@ -123,7 +123,7 @@ impl StatusColors {
info_background: blue().dark().step_9(),
info_border: blue().dark().step_9(),
modified: yellow().dark().step_9(),
modified_background: yellow().dark().step_9(),
modified_background: yellow().dark().step_9().opacity(0.25),
modified_border: yellow().dark().step_9(),
predictive: neutral().dark_alpha().step_9(),
predictive_background: neutral().dark_alpha().step_9(),
Expand Down
5 changes: 4 additions & 1 deletion crates/theme/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use std::sync::Arc;

use ::settings::Settings;
use anyhow::Result;
use fallback_themes::apply_status_color_defaults;
use fs::Fs;
use gpui::{
px, App, AssetSource, HighlightStyle, Hsla, Pixels, Refineable, SharedString, WindowAppearance,
Expand Down Expand Up @@ -155,7 +156,9 @@ impl ThemeFamily {
AppearanceContent::Light => StatusColors::light(),
AppearanceContent::Dark => StatusColors::dark(),
};
refined_status_colors.refine(&theme.style.status_colors_refinement());
let mut status_colors_refinement = theme.style.status_colors_refinement();
apply_status_color_defaults(&mut status_colors_refinement);
refined_status_colors.refine(&status_colors_refinement);

let mut refined_player_colors = match theme.appearance {
AppearanceContent::Light => PlayerColors::light(),
Expand Down

0 comments on commit 623b6a2

Please sign in to comment.