logiguard fork v3: full patch set on verified 8c74db0 tree

Includes prior-session patches (carry forward so the app compiles):
  - crates/gpui/build.rs: cross-compile manifest fix
  - crates/gpui/src/platform.rs: PlatformWindow::activate_with_token trait method
  - crates/gpui/src/window.rs: Window::activate_with_token public API
  - crates/gpui_linux/src/linux/wayland/window.rs: WaylandWindow::activate_with_token + activate() keyboard-serial fix

Plus the focus-serial fix:
  - serial.rs: SerialKind::KeyboardEnter
  - client.rs: store wl_keyboard.enter serial; latest_serial_of()

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Mohamad Khani
2026-07-14 01:52:12 +03:30
commit b9819977a5
3984 changed files with 1487015 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
[package]
name = "theme_settings"
version = "0.1.0"
edition.workspace = true
publish.workspace = true
license = "GPL-3.0-or-later"
[lints]
workspace = true
[features]
default = []
test-support = ["gpui/test-support", "settings/test-support", "theme/test-support"]
[lib]
path = "src/theme_settings.rs"
doctest = false
[dependencies]
anyhow.workspace = true
collections.workspace = true
gpui.workspace = true
gpui_util.workspace = true
log.workspace = true
palette = { workspace = true, default-features = false, features = ["std"] }
refineable.workspace = true
schemars.workspace = true
serde.workspace = true
serde_json.workspace = true
serde_json_lenient.workspace = true
settings.workspace = true
theme.workspace = true
uuid.workspace = true
[dev-dependencies]
gpui = { workspace = true, features = ["test-support"] }
settings = { workspace = true, features = ["test-support"] }

View File

@@ -0,0 +1 @@
../../LICENSE-GPL

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,666 @@
#![allow(missing_docs)]
use crate::schema::{status_colors_refinement, syntax_overrides, theme_colors_refinement};
use crate::{merge_accent_colors, merge_player_colors};
use collections::HashMap;
use gpui::{
App, Context, Font, FontFallbacks, FontStyle, Global, Pixels, SharedString, Subscription,
Window, px,
};
use refineable::Refineable;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
pub use settings::{FontFamilyName, IconThemeName, ThemeAppearanceMode, ThemeName};
use settings::{IntoGpui, RegisterSetting, Settings, SettingsContent};
use std::sync::Arc;
use theme::{Appearance, DEFAULT_ICON_THEME_NAME, SyntaxTheme, Theme, UiDensity};
const MIN_FONT_SIZE: Pixels = px(6.0);
const MAX_FONT_SIZE: Pixels = px(100.0);
const MIN_LINE_HEIGHT: f32 = 1.0;
pub(crate) fn ui_density_from_settings(val: settings::UiDensity) -> UiDensity {
match val {
settings::UiDensity::Compact => UiDensity::Compact,
settings::UiDensity::Default => UiDensity::Default,
settings::UiDensity::Comfortable => UiDensity::Comfortable,
}
}
pub fn appearance_to_mode(appearance: Appearance) -> ThemeAppearanceMode {
match appearance {
Appearance::Light => ThemeAppearanceMode::Light,
Appearance::Dark => ThemeAppearanceMode::Dark,
}
}
/// Customizable settings for the UI and theme system.
#[derive(Clone, PartialEq, RegisterSetting)]
pub struct ThemeSettings {
/// The UI font size. Determines the size of text in the UI,
/// as well as the size of a [gpui::Rems] unit.
///
/// Changing this will impact the size of all UI elements.
ui_font_size: Pixels,
/// The font used for UI elements.
pub ui_font: Font,
/// The font size used for buffers, and the terminal.
///
/// The terminal font size can be overridden using it's own setting.
buffer_font_size: Pixels,
/// The font used for buffers, and the terminal.
///
/// The terminal font family can be overridden using it's own setting.
pub buffer_font: Font,
/// The agent font size. Determines the size of text in the agent panel. Falls back to the UI font size if unset.
agent_ui_font_size: Option<Pixels>,
/// The agent buffer font size. Determines the size of user messages in the agent panel.
agent_buffer_font_size: Option<Pixels>,
/// The font family to use for rendering in the markdown preview.
/// Falls back to the UI font family if unset.
markdown_preview_font_family: Option<SharedString>,
/// The theme to use for the markdown preview.
/// Falls back to the main editor theme if unset.
pub markdown_preview_theme: Option<ThemeSelection>,
/// The line height for buffers, and the terminal.
///
/// Changing this may affect the spacing of some UI elements.
///
/// The terminal font family can be overridden using it's own setting.
pub buffer_line_height: BufferLineHeight,
/// The current theme selection.
pub theme: ThemeSelection,
/// Manual overrides for the active theme.
///
/// Note: This setting is still experimental. See [this tracking issue](https://github.com/zed-industries/zed/issues/18078)
pub experimental_theme_overrides: Option<settings::ThemeStyleContent>,
/// Manual overrides per theme
pub theme_overrides: HashMap<String, settings::ThemeStyleContent>,
/// The current icon theme selection.
pub icon_theme: IconThemeSelection,
/// The density of the UI.
/// Note: This setting is still experimental. See [this tracking issue](
pub ui_density: UiDensity,
/// The amount of fading applied to unnecessary code.
pub unnecessary_code_fade: f32,
}
/// Returns the name of the default theme for the given [`Appearance`].
pub fn default_theme(appearance: Appearance) -> &'static str {
match appearance {
Appearance::Light => settings::DEFAULT_LIGHT_THEME,
Appearance::Dark => settings::DEFAULT_DARK_THEME,
}
}
#[derive(Default)]
struct BufferFontSize(Pixels);
impl Global for BufferFontSize {}
#[derive(Default)]
pub(crate) struct UiFontSize(Pixels);
impl Global for UiFontSize {}
/// In-memory override for the UI font size in the agent panel.
#[derive(Default)]
pub struct AgentUiFontSize(Pixels);
impl Global for AgentUiFontSize {}
/// In-memory override for the buffer font size in the agent panel.
#[derive(Default)]
pub struct AgentBufferFontSize(Pixels);
impl Global for AgentBufferFontSize {}
/// Represents the selection of a theme, which can be either static or dynamic.
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[serde(untagged)]
pub enum ThemeSelection {
/// A static theme selection, represented by a single theme name.
Static(ThemeName),
/// A dynamic theme selection, which can change based the [ThemeMode].
Dynamic {
/// The mode used to determine which theme to use.
#[serde(default)]
mode: ThemeAppearanceMode,
/// The theme to use for light mode.
light: ThemeName,
/// The theme to use for dark mode.
dark: ThemeName,
},
}
impl From<settings::ThemeSelection> for ThemeSelection {
fn from(selection: settings::ThemeSelection) -> Self {
match selection {
settings::ThemeSelection::Static(theme) => ThemeSelection::Static(theme),
settings::ThemeSelection::Dynamic { mode, light, dark } => {
ThemeSelection::Dynamic { mode, light, dark }
}
}
}
}
impl ThemeSelection {
/// Returns the theme name for the selected [ThemeMode].
pub fn name(&self, system_appearance: Appearance) -> ThemeName {
match self {
Self::Static(theme) => theme.clone(),
Self::Dynamic { mode, light, dark } => match mode {
ThemeAppearanceMode::Light => light.clone(),
ThemeAppearanceMode::Dark => dark.clone(),
ThemeAppearanceMode::System => match system_appearance {
Appearance::Light => light.clone(),
Appearance::Dark => dark.clone(),
},
},
}
}
/// Returns the [ThemeMode] for the [ThemeSelection].
pub fn mode(&self) -> Option<ThemeAppearanceMode> {
match self {
ThemeSelection::Static(_) => None,
ThemeSelection::Dynamic { mode, .. } => Some(*mode),
}
}
}
/// Represents the selection of an icon theme, which can be either static or dynamic.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum IconThemeSelection {
/// A static icon theme selection, represented by a single icon theme name.
Static(IconThemeName),
/// A dynamic icon theme selection, which can change based on the [`ThemeMode`].
Dynamic {
/// The mode used to determine which theme to use.
mode: ThemeAppearanceMode,
/// The icon theme to use for light mode.
light: IconThemeName,
/// The icon theme to use for dark mode.
dark: IconThemeName,
},
}
impl From<settings::IconThemeSelection> for IconThemeSelection {
fn from(selection: settings::IconThemeSelection) -> Self {
match selection {
settings::IconThemeSelection::Static(theme) => IconThemeSelection::Static(theme),
settings::IconThemeSelection::Dynamic { mode, light, dark } => {
IconThemeSelection::Dynamic { mode, light, dark }
}
}
}
}
impl IconThemeSelection {
/// Returns the icon theme name based on the given [`Appearance`].
pub fn name(&self, system_appearance: Appearance) -> IconThemeName {
match self {
Self::Static(theme) => theme.clone(),
Self::Dynamic { mode, light, dark } => match mode {
ThemeAppearanceMode::Light => light.clone(),
ThemeAppearanceMode::Dark => dark.clone(),
ThemeAppearanceMode::System => match system_appearance {
Appearance::Light => light.clone(),
Appearance::Dark => dark.clone(),
},
},
}
}
/// Returns the [`ThemeMode`] for the [`IconThemeSelection`].
pub fn mode(&self) -> Option<ThemeAppearanceMode> {
match self {
IconThemeSelection::Static(_) => None,
IconThemeSelection::Dynamic { mode, .. } => Some(*mode),
}
}
}
/// Sets the theme for the given appearance to the theme with the specified name.
///
/// The caller should make sure that the [`Appearance`] matches the theme associated with the name.
///
/// If the current [`ThemeAppearanceMode`] is set to [`System`] and the user's system [`Appearance`]
/// is different than the new theme's [`Appearance`], this function will update the
/// [`ThemeAppearanceMode`] to the new theme's appearance in order to display the new theme.
///
/// [`System`]: ThemeAppearanceMode::System
pub fn set_theme(
current: &mut SettingsContent,
theme_name: impl Into<Arc<str>>,
theme_appearance: Appearance,
system_appearance: Appearance,
) {
let theme_name = ThemeName(theme_name.into());
let Some(selection) = current.theme.theme.as_mut() else {
current.theme.theme = Some(settings::ThemeSelection::Static(theme_name));
return;
};
match selection {
settings::ThemeSelection::Static(theme) => {
*theme = theme_name;
}
settings::ThemeSelection::Dynamic { mode, light, dark } => {
match theme_appearance {
Appearance::Light => *light = theme_name,
Appearance::Dark => *dark = theme_name,
}
let should_update_mode =
!(mode == &ThemeAppearanceMode::System && theme_appearance == system_appearance);
if should_update_mode {
*mode = appearance_to_mode(theme_appearance);
}
}
}
}
/// Sets the icon theme for the given appearance to the icon theme with the specified name.
pub fn set_icon_theme(
current: &mut SettingsContent,
icon_theme_name: IconThemeName,
appearance: Appearance,
) {
if let Some(selection) = current.theme.icon_theme.as_mut() {
let icon_theme_to_update = match selection {
settings::IconThemeSelection::Static(theme) => theme,
settings::IconThemeSelection::Dynamic { mode, light, dark } => match mode {
ThemeAppearanceMode::Light => light,
ThemeAppearanceMode::Dark => dark,
ThemeAppearanceMode::System => match appearance {
Appearance::Light => light,
Appearance::Dark => dark,
},
},
};
*icon_theme_to_update = icon_theme_name;
} else {
current.theme.icon_theme = Some(settings::IconThemeSelection::Static(icon_theme_name));
}
}
/// Sets the mode for the theme.
pub fn set_mode(content: &mut SettingsContent, mode: ThemeAppearanceMode) {
let theme = content.theme.as_mut();
if let Some(selection) = theme.theme.as_mut() {
match selection {
settings::ThemeSelection::Static(_) => {
*selection = settings::ThemeSelection::Dynamic {
mode: ThemeAppearanceMode::System,
light: ThemeName(settings::DEFAULT_LIGHT_THEME.into()),
dark: ThemeName(settings::DEFAULT_DARK_THEME.into()),
};
}
settings::ThemeSelection::Dynamic {
mode: mode_to_update,
..
} => *mode_to_update = mode,
}
} else {
theme.theme = Some(settings::ThemeSelection::Dynamic {
mode,
light: ThemeName(settings::DEFAULT_LIGHT_THEME.into()),
dark: ThemeName(settings::DEFAULT_DARK_THEME.into()),
});
}
if let Some(selection) = theme.icon_theme.as_mut() {
match selection {
settings::IconThemeSelection::Static(icon_theme) => {
*selection = settings::IconThemeSelection::Dynamic {
mode,
light: icon_theme.clone(),
dark: icon_theme.clone(),
};
}
settings::IconThemeSelection::Dynamic {
mode: mode_to_update,
..
} => *mode_to_update = mode,
}
} else {
theme.icon_theme = Some(settings::IconThemeSelection::Static(IconThemeName(
DEFAULT_ICON_THEME_NAME.into(),
)));
}
}
/// The buffer's line height.
#[derive(Clone, Copy, Debug, PartialEq, Default)]
pub enum BufferLineHeight {
/// A less dense line height.
#[default]
Comfortable,
/// The default line height.
Standard,
/// A custom line height, where 1.0 is the font's height. Must be at least 1.0.
Custom(f32),
}
impl From<settings::BufferLineHeight> for BufferLineHeight {
fn from(value: settings::BufferLineHeight) -> Self {
match value {
settings::BufferLineHeight::Comfortable => BufferLineHeight::Comfortable,
settings::BufferLineHeight::Standard => BufferLineHeight::Standard,
settings::BufferLineHeight::Custom(line_height) => {
BufferLineHeight::Custom(line_height)
}
}
}
}
impl BufferLineHeight {
/// Returns the value of the line height.
pub fn value(&self) -> f32 {
match self {
BufferLineHeight::Comfortable => 1.618,
BufferLineHeight::Standard => 1.3,
BufferLineHeight::Custom(line_height) => *line_height,
}
}
}
impl ThemeSettings {
/// Returns the buffer font size.
pub fn buffer_font_size(&self, cx: &App) -> Pixels {
let font_size = cx
.try_global::<BufferFontSize>()
.map(|size| size.0)
.unwrap_or(self.buffer_font_size);
clamp_font_size(font_size)
}
/// Returns the UI font size.
pub fn ui_font_size(&self, cx: &App) -> Pixels {
let font_size = cx
.try_global::<UiFontSize>()
.map(|size| size.0)
.unwrap_or(self.ui_font_size);
clamp_font_size(font_size)
}
/// Returns the agent panel font size. Falls back to the UI font size if unset.
pub fn agent_ui_font_size(&self, cx: &App) -> Pixels {
cx.try_global::<AgentUiFontSize>()
.map(|size| size.0)
.or(self.agent_ui_font_size)
.map(clamp_font_size)
.unwrap_or_else(|| self.ui_font_size(cx))
}
/// Returns the agent panel buffer font size.
pub fn agent_buffer_font_size(&self, cx: &App) -> Pixels {
cx.try_global::<AgentBufferFontSize>()
.map(|size| size.0)
.or(self.agent_buffer_font_size)
.map(clamp_font_size)
.unwrap_or_else(|| self.buffer_font_size(cx))
}
/// Returns the font family to use in the markdown preview,
/// falling back to the UI font family when unset.
pub fn markdown_preview_font_family(&self) -> &SharedString {
self.markdown_preview_font_family
.as_ref()
.unwrap_or(&self.ui_font.family)
}
/// Returns the buffer font size, read from the settings.
///
/// The real buffer font size is stored in-memory, to support temporary font size changes.
/// Use [`Self::buffer_font_size`] to get the real font size.
pub fn buffer_font_size_settings(&self) -> Pixels {
self.buffer_font_size
}
/// Returns the UI font size, read from the settings.
///
/// The real UI font size is stored in-memory, to support temporary font size changes.
/// Use [`Self::ui_font_size`] to get the real font size.
pub fn ui_font_size_settings(&self) -> Pixels {
self.ui_font_size
}
/// Returns the agent font size, read from the settings.
///
/// The real agent font size is stored in-memory, to support temporary font size changes.
/// Use [`Self::agent_ui_font_size`] to get the real font size.
pub fn agent_ui_font_size_settings(&self) -> Option<Pixels> {
self.agent_ui_font_size
}
/// Returns the agent buffer font size, read from the settings.
///
/// The real agent buffer font size is stored in-memory, to support temporary font size changes.
/// Use [`Self::agent_buffer_font_size`] to get the real font size.
pub fn agent_buffer_font_size_settings(&self) -> Option<Pixels> {
self.agent_buffer_font_size
}
/// Returns the buffer's line height.
pub fn line_height(&self) -> f32 {
f32::max(self.buffer_line_height.value(), MIN_LINE_HEIGHT)
}
/// Applies the theme overrides, if there are any, to the current theme.
pub fn apply_theme_overrides(&self, mut arc_theme: Arc<Theme>) -> Arc<Theme> {
if let Some(experimental_theme_overrides) = &self.experimental_theme_overrides {
let mut theme = (*arc_theme).clone();
ThemeSettings::modify_theme(&mut theme, experimental_theme_overrides);
arc_theme = Arc::new(theme);
}
if let Some(theme_overrides) = self.theme_overrides.get(arc_theme.name.as_ref()) {
let mut theme = (*arc_theme).clone();
ThemeSettings::modify_theme(&mut theme, theme_overrides);
arc_theme = Arc::new(theme);
}
arc_theme
}
fn modify_theme(base_theme: &mut Theme, theme_overrides: &settings::ThemeStyleContent) {
if let Some(window_background_appearance) = theme_overrides.window_background_appearance {
base_theme.styles.window_background_appearance =
window_background_appearance.into_gpui();
}
let status_color_refinement = status_colors_refinement(&theme_overrides.status);
let theme_color_refinement = theme_colors_refinement(
&theme_overrides.colors,
&status_color_refinement,
base_theme.appearance.is_light(),
);
base_theme.styles.colors.refine(&theme_color_refinement);
base_theme.styles.status.refine(&status_color_refinement);
merge_player_colors(&mut base_theme.styles.player, &theme_overrides.players);
merge_accent_colors(&mut base_theme.styles.accents, &theme_overrides.accents);
base_theme.styles.syntax = SyntaxTheme::merge(
base_theme.styles.syntax.clone(),
syntax_overrides(theme_overrides),
);
}
}
/// Observe changes to the adjusted buffer font size.
pub fn observe_buffer_font_size_adjustment<V: 'static>(
cx: &mut Context<V>,
f: impl 'static + Fn(&mut V, &mut Context<V>),
) -> Subscription {
cx.observe_global::<BufferFontSize>(f)
}
/// Gets the font size, adjusted by the difference between the current buffer font size and the one set in the settings.
pub fn adjusted_font_size(size: Pixels, cx: &App) -> Pixels {
let adjusted_font_size =
if let Some(BufferFontSize(adjusted_size)) = cx.try_global::<BufferFontSize>() {
let buffer_font_size = ThemeSettings::get_global(cx).buffer_font_size;
let delta = *adjusted_size - buffer_font_size;
size + delta
} else {
size
};
clamp_font_size(adjusted_font_size)
}
/// Adjusts the buffer font size, without persisting the result in the settings.
/// This will be effective until the app is restarted.
pub fn adjust_buffer_font_size(cx: &mut App, f: impl FnOnce(Pixels) -> Pixels) {
let buffer_font_size = ThemeSettings::get_global(cx).buffer_font_size;
let adjusted_size = cx
.try_global::<BufferFontSize>()
.map_or(buffer_font_size, |adjusted_size| adjusted_size.0);
cx.set_global(BufferFontSize(clamp_font_size(f(adjusted_size))));
cx.refresh_windows();
}
/// Resets the buffer font size to the default value.
pub fn reset_buffer_font_size(cx: &mut App) {
if cx.has_global::<BufferFontSize>() {
cx.remove_global::<BufferFontSize>();
cx.refresh_windows();
}
}
#[allow(missing_docs)]
pub fn setup_ui_font(window: &mut Window, cx: &mut App) -> gpui::Font {
let (ui_font, ui_font_size) = {
let theme_settings = ThemeSettings::get_global(cx);
let font = theme_settings.ui_font.clone();
(font, theme_settings.ui_font_size(cx))
};
window.set_rem_size(ui_font_size);
ui_font
}
/// Sets the adjusted UI font size.
pub fn adjust_ui_font_size(cx: &mut App, f: impl FnOnce(Pixels) -> Pixels) {
let ui_font_size = ThemeSettings::get_global(cx).ui_font_size(cx);
let adjusted_size = cx
.try_global::<UiFontSize>()
.map_or(ui_font_size, |adjusted_size| adjusted_size.0);
cx.set_global(UiFontSize(clamp_font_size(f(adjusted_size))));
cx.refresh_windows();
}
/// Resets the UI font size to the default value.
pub fn reset_ui_font_size(cx: &mut App) {
if cx.has_global::<UiFontSize>() {
cx.remove_global::<UiFontSize>();
cx.refresh_windows();
}
}
/// Sets the adjusted font size of agent responses in the agent panel.
pub fn adjust_agent_ui_font_size(cx: &mut App, f: impl FnOnce(Pixels) -> Pixels) {
let agent_ui_font_size = ThemeSettings::get_global(cx).agent_ui_font_size(cx);
let adjusted_size = cx
.try_global::<AgentUiFontSize>()
.map_or(agent_ui_font_size, |adjusted_size| adjusted_size.0);
cx.set_global(AgentUiFontSize(clamp_font_size(f(adjusted_size))));
cx.refresh_windows();
}
/// Resets the agent response font size in the agent panel to the default value.
pub fn reset_agent_ui_font_size(cx: &mut App) {
if cx.has_global::<AgentUiFontSize>() {
cx.remove_global::<AgentUiFontSize>();
cx.refresh_windows();
}
}
/// Sets the adjusted font size of user messages in the agent panel.
pub fn adjust_agent_buffer_font_size(cx: &mut App, f: impl FnOnce(Pixels) -> Pixels) {
let agent_buffer_font_size = ThemeSettings::get_global(cx).agent_buffer_font_size(cx);
let adjusted_size = cx
.try_global::<AgentBufferFontSize>()
.map_or(agent_buffer_font_size, |adjusted_size| adjusted_size.0);
cx.set_global(AgentBufferFontSize(clamp_font_size(f(adjusted_size))));
cx.refresh_windows();
}
/// Resets the user message font size in the agent panel to the default value.
pub fn reset_agent_buffer_font_size(cx: &mut App) {
if cx.has_global::<AgentBufferFontSize>() {
cx.remove_global::<AgentBufferFontSize>();
cx.refresh_windows();
}
}
/// Ensures font size is within the valid range.
pub fn clamp_font_size(size: Pixels) -> Pixels {
size.clamp(MIN_FONT_SIZE, MAX_FONT_SIZE)
}
fn font_fallbacks_from_settings(
fallbacks: Option<Vec<settings::FontFamilyName>>,
) -> Option<FontFallbacks> {
fallbacks.map(|fallbacks| {
FontFallbacks::from_fonts(
fallbacks
.into_iter()
.map(|font_family| font_family.0.to_string())
.collect(),
)
})
}
impl settings::Settings for ThemeSettings {
fn from_settings(content: &settings::SettingsContent) -> Self {
let content = &content.theme;
let theme_selection: ThemeSelection = content.theme.clone().unwrap().into();
let icon_theme_selection: IconThemeSelection = content.icon_theme.clone().unwrap().into();
Self {
ui_font_size: clamp_font_size(content.ui_font_size.unwrap().into_gpui()),
ui_font: Font {
family: content.ui_font_family.as_ref().unwrap().0.clone().into(),
features: content.ui_font_features.clone().unwrap().into_gpui(),
fallbacks: font_fallbacks_from_settings(content.ui_font_fallbacks.clone()),
weight: content.ui_font_weight.unwrap().into_gpui(),
style: Default::default(),
},
buffer_font: Font {
family: content
.buffer_font_family
.as_ref()
.unwrap()
.0
.clone()
.into(),
features: content.buffer_font_features.clone().unwrap().into_gpui(),
fallbacks: font_fallbacks_from_settings(content.buffer_font_fallbacks.clone()),
weight: content.buffer_font_weight.unwrap().into_gpui(),
style: FontStyle::default(),
},
buffer_font_size: clamp_font_size(content.buffer_font_size.unwrap().into_gpui()),
buffer_line_height: content.buffer_line_height.unwrap().into(),
agent_ui_font_size: content.agent_ui_font_size.map(|s| s.into_gpui()),
agent_buffer_font_size: content.agent_buffer_font_size.map(|s| s.into_gpui()),
markdown_preview_font_family: content
.markdown_preview_font_family
.as_ref()
.map(|f| f.0.clone().into()),
markdown_preview_theme: content
.markdown_preview_theme
.clone()
.map(ThemeSelection::from),
theme: theme_selection,
experimental_theme_overrides: content.experimental_theme_overrides.clone(),
theme_overrides: content.theme_overrides.clone(),
icon_theme: icon_theme_selection,
ui_density: ui_density_from_settings(content.ui_density.unwrap_or_default()),
unnecessary_code_fade: content.unnecessary_code_fade.unwrap().0.clamp(0.0, 0.9),
}
}
}

View File

@@ -0,0 +1,428 @@
#![deny(missing_docs)]
//! # Theme Settings
//!
//! This crate provides theme settings integration for Zed,
//! bridging the theme system with the settings infrastructure.
mod schema;
mod settings;
use std::sync::Arc;
use ::settings::{IntoGpui, Settings, SettingsStore};
use anyhow::{Context as _, Result};
use gpui::{App, Font, HighlightStyle, Pixels, Refineable, px};
use gpui_util::ResultExt;
use theme::{
AccentColors, Appearance, AppearanceContent, DEFAULT_DARK_THEME, DEFAULT_ICON_THEME_NAME,
GlobalTheme, LoadThemes, PlayerColor, PlayerColors, StatusColors, SyntaxTheme,
SystemAppearance, SystemColors, Theme, ThemeColors, ThemeFamily, ThemeRegistry,
ThemeSettingsProvider, ThemeStyles, default_color_scales, try_parse_color,
};
pub use crate::schema::{
FontStyleContent, FontWeightContent, HighlightStyleContent, StatusColorsContent,
ThemeColorsContent, ThemeContent, ThemeFamilyContent, ThemeStyleContent,
WindowBackgroundContent, status_colors_refinement, syntax_overrides, theme_colors_refinement,
};
use crate::settings::adjust_buffer_font_size;
pub use crate::settings::{
AgentBufferFontSize, AgentUiFontSize, BufferLineHeight, FontFamilyName, IconThemeName,
IconThemeSelection, ThemeAppearanceMode, ThemeName, ThemeSelection, ThemeSettings,
adjust_agent_buffer_font_size, adjust_agent_ui_font_size, adjust_ui_font_size,
adjusted_font_size, appearance_to_mode, clamp_font_size, default_theme,
observe_buffer_font_size_adjustment, reset_agent_buffer_font_size, reset_agent_ui_font_size,
reset_buffer_font_size, reset_ui_font_size, set_icon_theme, set_mode, set_theme, setup_ui_font,
};
pub use theme::UiDensity;
struct ThemeSettingsProviderImpl;
impl ThemeSettingsProvider for ThemeSettingsProviderImpl {
fn ui_font<'a>(&'a self, cx: &'a App) -> &'a Font {
&ThemeSettings::get_global(cx).ui_font
}
fn buffer_font<'a>(&'a self, cx: &'a App) -> &'a Font {
&ThemeSettings::get_global(cx).buffer_font
}
fn ui_font_size(&self, cx: &App) -> Pixels {
ThemeSettings::get_global(cx).ui_font_size(cx)
}
fn buffer_font_size(&self, cx: &App) -> Pixels {
ThemeSettings::get_global(cx).buffer_font_size(cx)
}
fn ui_density(&self, cx: &App) -> UiDensity {
ThemeSettings::get_global(cx).ui_density
}
}
/// Initialize the theme system with settings integration.
///
/// This is the full initialization for the application. It calls [`theme::init`]
/// and then wires up settings observation for theme/font changes.
pub fn init(themes_to_load: LoadThemes, cx: &mut App) {
let load_user_themes = matches!(&themes_to_load, LoadThemes::All(_));
theme::init(themes_to_load, cx);
theme::set_theme_settings_provider(Box::new(ThemeSettingsProviderImpl), cx);
if load_user_themes {
let registry = ThemeRegistry::global(cx);
load_bundled_themes(&registry);
}
let theme = configured_theme(cx);
let icon_theme = configured_icon_theme(cx);
GlobalTheme::update_theme(cx, theme);
GlobalTheme::update_icon_theme(cx, icon_theme);
let settings = ThemeSettings::get_global(cx);
let mut prev_buffer_font_size_settings = settings.buffer_font_size_settings();
let mut prev_ui_font_size_settings = settings.ui_font_size_settings();
let mut prev_agent_ui_font_size_settings = settings.agent_ui_font_size_settings();
let mut prev_agent_buffer_font_size_settings = settings.agent_buffer_font_size_settings();
let mut prev_theme_name = settings.theme.name(SystemAppearance::global(cx).0);
let mut prev_icon_theme_name = settings.icon_theme.name(SystemAppearance::global(cx).0);
let mut prev_theme_overrides = (
settings.experimental_theme_overrides.clone(),
settings.theme_overrides.clone(),
);
cx.observe_global::<SettingsStore>(move |cx| {
let settings = ThemeSettings::get_global(cx);
let buffer_font_size_settings = settings.buffer_font_size_settings();
let ui_font_size_settings = settings.ui_font_size_settings();
let agent_ui_font_size_settings = settings.agent_ui_font_size_settings();
let agent_buffer_font_size_settings = settings.agent_buffer_font_size_settings();
let theme_name = settings.theme.name(SystemAppearance::global(cx).0);
let icon_theme_name = settings.icon_theme.name(SystemAppearance::global(cx).0);
let theme_overrides = (
settings.experimental_theme_overrides.clone(),
settings.theme_overrides.clone(),
);
if buffer_font_size_settings != prev_buffer_font_size_settings {
prev_buffer_font_size_settings = buffer_font_size_settings;
reset_buffer_font_size(cx);
}
if ui_font_size_settings != prev_ui_font_size_settings {
prev_ui_font_size_settings = ui_font_size_settings;
reset_ui_font_size(cx);
}
if agent_ui_font_size_settings != prev_agent_ui_font_size_settings {
prev_agent_ui_font_size_settings = agent_ui_font_size_settings;
reset_agent_ui_font_size(cx);
}
if agent_buffer_font_size_settings != prev_agent_buffer_font_size_settings {
prev_agent_buffer_font_size_settings = agent_buffer_font_size_settings;
reset_agent_buffer_font_size(cx);
}
if theme_name != prev_theme_name || theme_overrides != prev_theme_overrides {
prev_theme_name = theme_name;
prev_theme_overrides = theme_overrides;
reload_theme(cx);
}
if icon_theme_name != prev_icon_theme_name {
prev_icon_theme_name = icon_theme_name;
reload_icon_theme(cx);
}
})
.detach();
}
fn configured_theme(cx: &mut App) -> Arc<Theme> {
let themes = ThemeRegistry::default_global(cx);
let theme_settings = ThemeSettings::get_global(cx);
let system_appearance = SystemAppearance::global(cx);
let theme_name = theme_settings.theme.name(*system_appearance);
let theme = match themes.get(&theme_name.0) {
Ok(theme) => theme,
Err(err) => {
if themes.extensions_loaded() {
log::error!("{err}");
}
themes
.get(default_theme(*system_appearance))
.unwrap_or_else(|_| themes.get(DEFAULT_DARK_THEME).unwrap())
}
};
theme_settings.apply_theme_overrides(theme)
}
fn configured_icon_theme(cx: &mut App) -> Arc<theme::IconTheme> {
let themes = ThemeRegistry::default_global(cx);
let theme_settings = ThemeSettings::get_global(cx);
let system_appearance = SystemAppearance::global(cx);
let icon_theme_name = theme_settings.icon_theme.name(*system_appearance);
match themes.get_icon_theme(&icon_theme_name.0) {
Ok(theme) => theme,
Err(err) => {
if themes.extensions_loaded() {
log::error!("{err}");
}
themes.get_icon_theme(DEFAULT_ICON_THEME_NAME).unwrap()
}
}
}
/// Reloads the current theme from settings.
pub fn reload_theme(cx: &mut App) {
let theme = configured_theme(cx);
GlobalTheme::update_theme(cx, theme);
cx.refresh_windows();
}
/// Reloads the current icon theme from settings.
pub fn reload_icon_theme(cx: &mut App) {
let icon_theme = configured_icon_theme(cx);
GlobalTheme::update_icon_theme(cx, icon_theme);
cx.refresh_windows();
}
/// Loads the themes bundled with the Zed binary into the registry.
pub fn load_bundled_themes(registry: &ThemeRegistry) {
let theme_paths = registry
.assets()
.list("themes/")
.expect("failed to list theme assets")
.into_iter()
.filter(|path| path.ends_with(".json"));
for path in theme_paths {
let Some(theme) = registry.assets().load(&path).log_err().flatten() else {
continue;
};
let Some(theme_family) = serde_json::from_slice(&theme)
.with_context(|| format!("failed to parse theme at path \"{path}\""))
.log_err()
else {
continue;
};
let refined = refine_theme_family(theme_family);
registry.insert_theme_families([refined]);
}
}
/// Loads a user theme from the given bytes into the registry.
pub fn load_user_theme(registry: &ThemeRegistry, bytes: &[u8]) -> Result<()> {
let theme = deserialize_user_theme(bytes)?;
let refined = refine_theme_family(theme);
registry.insert_theme_families([refined]);
Ok(())
}
/// Deserializes a user theme from the given bytes.
pub fn deserialize_user_theme(bytes: &[u8]) -> Result<ThemeFamilyContent> {
let theme_family: ThemeFamilyContent = serde_json_lenient::from_slice(bytes)?;
for theme in &theme_family.themes {
if theme
.style
.colors
.deprecated_scrollbar_thumb_background
.is_some()
{
log::warn!(
r#"Theme "{theme_name}" is using a deprecated style property: scrollbar_thumb.background. Use `scrollbar.thumb.background` instead."#,
theme_name = theme.name
)
}
}
Ok(theme_family)
}
/// Refines a [`ThemeFamilyContent`] and its [`ThemeContent`]s into a [`ThemeFamily`].
pub fn refine_theme_family(theme_family_content: ThemeFamilyContent) -> ThemeFamily {
let id = uuid::Uuid::new_v4().to_string();
let name = theme_family_content.name.clone();
let author = theme_family_content.author.clone();
let themes: Vec<Theme> = theme_family_content
.themes
.iter()
.map(|theme_content| refine_theme(theme_content))
.collect();
ThemeFamily {
id,
name: name.into(),
author: author.into(),
themes,
scales: default_color_scales(),
}
}
/// Refines a [`ThemeContent`] into a [`Theme`].
pub fn refine_theme(theme: &ThemeContent) -> Theme {
let appearance = match theme.appearance {
AppearanceContent::Light => Appearance::Light,
AppearanceContent::Dark => Appearance::Dark,
};
let mut refined_status_colors = match theme.appearance {
AppearanceContent::Light => StatusColors::light(),
AppearanceContent::Dark => StatusColors::dark(),
};
let mut status_colors_refinement = status_colors_refinement(&theme.style.status);
theme::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(),
AppearanceContent::Dark => PlayerColors::dark(),
};
merge_player_colors(&mut refined_player_colors, &theme.style.players);
let mut refined_theme_colors = match theme.appearance {
AppearanceContent::Light => ThemeColors::light(),
AppearanceContent::Dark => ThemeColors::dark(),
};
let mut theme_colors_refinement = theme_colors_refinement(
&theme.style.colors,
&status_colors_refinement,
theme.appearance == AppearanceContent::Light,
);
theme::apply_theme_color_defaults(&mut theme_colors_refinement, &refined_player_colors);
refined_theme_colors.refine(&theme_colors_refinement);
let mut refined_accent_colors = match theme.appearance {
AppearanceContent::Light => AccentColors::light(),
AppearanceContent::Dark => AccentColors::dark(),
};
merge_accent_colors(&mut refined_accent_colors, &theme.style.accents);
let syntax_highlights = theme.style.syntax.iter().map(|(syntax_token, highlight)| {
(
syntax_token.clone(),
HighlightStyle {
color: highlight
.color
.as_ref()
.and_then(|color| try_parse_color(color).ok()),
background_color: highlight
.background_color
.as_ref()
.and_then(|color| try_parse_color(color).ok()),
font_style: highlight.font_style.map(|s| s.into_gpui()),
font_weight: highlight.font_weight.map(|w| w.into_gpui()),
..Default::default()
},
)
});
let syntax_theme = Arc::new(SyntaxTheme::new(syntax_highlights));
let window_background_appearance = theme
.style
.window_background_appearance
.map(|w| w.into_gpui())
.unwrap_or_default();
Theme {
id: uuid::Uuid::new_v4().to_string(),
name: theme.name.clone().into(),
appearance,
styles: ThemeStyles {
system: SystemColors::default(),
window_background_appearance,
accents: refined_accent_colors,
colors: refined_theme_colors,
status: refined_status_colors,
player: refined_player_colors,
syntax: syntax_theme,
},
}
}
/// Merges player color overrides into the given [`PlayerColors`].
pub fn merge_player_colors(
player_colors: &mut PlayerColors,
user_player_colors: &[::settings::PlayerColorContent],
) {
if user_player_colors.is_empty() {
return;
}
for (idx, player) in user_player_colors.iter().enumerate() {
let cursor = player
.cursor
.as_ref()
.and_then(|color| try_parse_color(color).ok());
let background = player
.background
.as_ref()
.and_then(|color| try_parse_color(color).ok());
let selection = player
.selection
.as_ref()
.and_then(|color| try_parse_color(color).ok());
if let Some(player_color) = player_colors.0.get_mut(idx) {
*player_color = PlayerColor {
cursor: cursor.unwrap_or(player_color.cursor),
background: background.unwrap_or(player_color.background),
selection: selection.unwrap_or(player_color.selection),
};
} else {
player_colors.0.push(PlayerColor {
cursor: cursor.unwrap_or_default(),
background: background.unwrap_or_default(),
selection: selection.unwrap_or_default(),
});
}
}
}
/// Merges accent color overrides into the given [`AccentColors`].
pub fn merge_accent_colors(
accent_colors: &mut AccentColors,
user_accent_colors: &[::settings::AccentContent],
) {
if user_accent_colors.is_empty() {
return;
}
let colors = user_accent_colors
.iter()
.filter_map(|accent_color| {
accent_color
.0
.as_ref()
.and_then(|color| try_parse_color(color).ok())
})
.collect::<Vec<_>>();
if !colors.is_empty() {
accent_colors.0 = Arc::from(colors);
}
}
/// Increases the buffer font size by 1 pixel, without persisting the result in the settings.
/// This will be effective until the app is restarted.
pub fn increase_buffer_font_size(cx: &mut App) {
adjust_buffer_font_size(cx, |size| size + px(1.0));
}
/// Decreases the buffer font size by 1 pixel, without persisting the result in the settings.
/// This will be effective until the app is restarted.
pub fn decrease_buffer_font_size(cx: &mut App) {
adjust_buffer_font_size(cx, |size| size - px(1.0));
}