Skip to repository content

tenant.openagents/omega

No repository description is available.

OpenAgents Git authority 2026-07-28T04:58:41.997Z Public web read
NIP-34 coordinate30617:7649603503856e5148d571eac2766b288a8ff1e9e35d380337a1d2b0015b4f92:omega
MaintainersHidden in public view
References2 branches · 1 tag
Read-only clonegit clone https://openagents.com/git/tenant.openagents/omega.git
Browse files

theme.rs

350 lines · 10.4 KB · rust
1#![deny(missing_docs)]
2
3//! # Theme
4//!
5//! This crate provides the theme system for Zed.
6//!
7//! ## Overview
8//!
9//! A theme is a collection of colors used to build a consistent appearance for UI components across the application.
10
11mod color_space;
12mod default_colors;
13mod fallback_themes;
14mod font_family_cache;
15mod icon_theme;
16mod icon_theme_schema;
17mod registry;
18mod scale;
19mod schema;
20mod styles;
21mod theme_settings_provider;
22mod ui_density;
23
24use std::sync::Arc;
25
26use gpui::BorrowAppContext;
27use gpui::Global;
28use gpui::{
29    App, AssetSource, Hsla, Pixels, SharedString, Styled, Tiling, WindowAppearance,
30    WindowBackgroundAppearance, px,
31};
32use serde::Deserialize;
33
34pub use crate::color_space::*;
35pub use crate::default_colors::*;
36pub use crate::fallback_themes::{apply_status_color_defaults, apply_theme_color_defaults};
37pub use crate::font_family_cache::*;
38pub use crate::icon_theme::*;
39pub use crate::icon_theme_schema::*;
40pub use crate::registry::*;
41pub use crate::scale::*;
42pub use crate::schema::*;
43pub use crate::styles::*;
44pub use crate::theme_settings_provider::*;
45pub use crate::ui_density::*;
46
47/// The name of the default dark theme.
48pub const DEFAULT_DARK_THEME: &str = "Aiur";
49
50/// Defines window border radius for platforms that use client side decorations.
51pub const CLIENT_SIDE_DECORATION_ROUNDING: Pixels = px(10.0);
52/// Defines window shadow size for platforms that use client side decorations.
53pub const CLIENT_SIDE_DECORATION_SHADOW: Pixels = px(10.0);
54
55/// Styling helpers for elements that follow client-side window decorations.
56pub trait ClientDecorationsExt: Styled {
57    /// Rounds each corner whose two adjacent edges are both untiled.
58    fn rounded_client_corners(mut self, tiling: Tiling) -> Self {
59        if !tiling.top && !tiling.left {
60            self = self.rounded_tl(CLIENT_SIDE_DECORATION_ROUNDING);
61        }
62        if !tiling.top && !tiling.right {
63            self = self.rounded_tr(CLIENT_SIDE_DECORATION_ROUNDING);
64        }
65        if !tiling.bottom && !tiling.left {
66            self = self.rounded_bl(CLIENT_SIDE_DECORATION_ROUNDING);
67        }
68        if !tiling.bottom && !tiling.right {
69            self = self.rounded_br(CLIENT_SIDE_DECORATION_ROUNDING);
70        }
71        self
72    }
73}
74
75impl<T: Styled> ClientDecorationsExt for T {}
76
77/// The appearance of the theme.
78#[derive(Debug, PartialEq, Clone, Copy, Deserialize)]
79pub enum Appearance {
80    /// A light appearance.
81    Light,
82    /// A dark appearance.
83    Dark,
84}
85
86impl Appearance {
87    /// Returns whether the appearance is light.
88    pub fn is_light(&self) -> bool {
89        match self {
90            Self::Light => true,
91            Self::Dark => false,
92        }
93    }
94}
95
96impl From<WindowAppearance> for Appearance {
97    fn from(value: WindowAppearance) -> Self {
98        match value {
99            WindowAppearance::Dark | WindowAppearance::VibrantDark => Self::Dark,
100            WindowAppearance::Light | WindowAppearance::VibrantLight => Self::Light,
101        }
102    }
103}
104
105/// Which themes should be loaded. This is used primarily for testing.
106pub enum LoadThemes {
107    /// Only load the base theme.
108    ///
109    /// No user themes will be loaded.
110    JustBase,
111
112    /// Load all of the built-in themes.
113    All(Box<dyn AssetSource>),
114}
115
116/// Initialize the theme system with default themes.
117///
118/// This sets up the [`ThemeRegistry`], [`FontFamilyCache`], [`SystemAppearance`],
119/// and [`GlobalTheme`] with the default dark theme. It does NOT load bundled
120/// themes from JSON or integrate with settings — use `theme_settings::init` for that.
121pub fn init(themes_to_load: LoadThemes, cx: &mut App) {
122    SystemAppearance::init(cx);
123    let assets = match themes_to_load {
124        LoadThemes::JustBase => Box::new(()) as Box<dyn AssetSource>,
125        LoadThemes::All(assets) => assets,
126    };
127    ThemeRegistry::set_global(assets, cx);
128    FontFamilyCache::init_global(cx);
129
130    let themes = ThemeRegistry::default_global(cx);
131    let theme = themes.get(DEFAULT_DARK_THEME).unwrap_or_else(|_| {
132        themes
133            .list()
134            .into_iter()
135            .next()
136            .map(|m| themes.get(&m.name).unwrap())
137            .unwrap()
138    });
139    let icon_theme = themes.default_icon_theme().unwrap();
140    cx.set_global(GlobalTheme { theme, icon_theme });
141}
142
143/// Implementing this trait allows accessing the active theme.
144pub trait ActiveTheme {
145    /// Returns the active theme.
146    fn theme(&self) -> &Arc<Theme>;
147}
148
149impl ActiveTheme for App {
150    fn theme(&self) -> &Arc<Theme> {
151        GlobalTheme::theme(self)
152    }
153}
154
155/// The appearance of the system.
156#[derive(Debug, Clone, Copy)]
157pub struct SystemAppearance(pub Appearance);
158
159impl std::ops::Deref for SystemAppearance {
160    type Target = Appearance;
161
162    fn deref(&self) -> &Self::Target {
163        &self.0
164    }
165}
166
167impl Default for SystemAppearance {
168    fn default() -> Self {
169        Self(Appearance::Dark)
170    }
171}
172
173#[derive(Default)]
174struct GlobalSystemAppearance(SystemAppearance);
175
176impl std::ops::DerefMut for GlobalSystemAppearance {
177    fn deref_mut(&mut self) -> &mut Self::Target {
178        &mut self.0
179    }
180}
181
182impl std::ops::Deref for GlobalSystemAppearance {
183    type Target = SystemAppearance;
184
185    fn deref(&self) -> &Self::Target {
186        &self.0
187    }
188}
189
190impl Global for GlobalSystemAppearance {}
191
192impl SystemAppearance {
193    /// Initializes the [`SystemAppearance`] for the application.
194    pub fn init(cx: &mut App) {
195        *cx.default_global::<GlobalSystemAppearance>() =
196            GlobalSystemAppearance(SystemAppearance(cx.window_appearance().into()));
197    }
198
199    /// Returns the global [`SystemAppearance`].
200    pub fn global(cx: &App) -> Self {
201        cx.global::<GlobalSystemAppearance>().0
202    }
203
204    /// Returns a mutable reference to the global [`SystemAppearance`].
205    pub fn global_mut(cx: &mut App) -> &mut Self {
206        cx.global_mut::<GlobalSystemAppearance>()
207    }
208}
209
210/// A theme family is a grouping of themes under a single name.
211///
212/// For example, the "One" theme family contains the "One Light" and "One Dark" themes.
213///
214/// It can also be used to package themes with many variants.
215///
216/// For example, the "Atelier" theme family contains "Cave", "Dune", "Estuary", "Forest", "Heath", etc.
217pub struct ThemeFamily {
218    /// The unique identifier for the theme family.
219    pub id: String,
220    /// The name of the theme family. This will be displayed in the UI, such as when adding or removing a theme family.
221    pub name: SharedString,
222    /// The author of the theme family.
223    pub author: SharedString,
224    /// The [Theme]s in the family.
225    pub themes: Vec<Theme>,
226    /// The color scales used by the themes in the family.
227    /// Note: This will be removed in the future.
228    pub scales: ColorScales,
229}
230
231/// A theme is the primary mechanism for defining the appearance of the UI.
232#[derive(Clone, Debug, PartialEq)]
233pub struct Theme {
234    /// The unique identifier for the theme.
235    pub id: String,
236    /// The name of the theme.
237    pub name: SharedString,
238    /// The appearance of the theme (light or dark).
239    pub appearance: Appearance,
240    /// The colors and other styles for the theme.
241    pub styles: ThemeStyles,
242}
243
244impl Theme {
245    /// Returns the [`SystemColors`] for the theme.
246    #[inline(always)]
247    pub fn system(&self) -> &SystemColors {
248        &self.styles.system
249    }
250
251    /// Returns the [`AccentColors`] for the theme.
252    #[inline(always)]
253    pub fn accents(&self) -> &AccentColors {
254        &self.styles.accents
255    }
256
257    /// Returns the [`PlayerColors`] for the theme.
258    #[inline(always)]
259    pub fn players(&self) -> &PlayerColors {
260        &self.styles.player
261    }
262
263    /// Returns the [`ThemeColors`] for the theme.
264    #[inline(always)]
265    pub fn colors(&self) -> &ThemeColors {
266        &self.styles.colors
267    }
268
269    /// Returns the [`SyntaxTheme`] for the theme.
270    #[inline(always)]
271    pub fn syntax(&self) -> &Arc<SyntaxTheme> {
272        &self.styles.syntax
273    }
274
275    /// Returns the [`StatusColors`] for the theme.
276    #[inline(always)]
277    pub fn status(&self) -> &StatusColors {
278        &self.styles.status
279    }
280
281    /// Returns the [`Appearance`] for the theme.
282    #[inline(always)]
283    pub fn appearance(&self) -> Appearance {
284        self.appearance
285    }
286
287    /// Returns the [`WindowBackgroundAppearance`] for the theme.
288    #[inline(always)]
289    pub fn window_background_appearance(&self) -> WindowBackgroundAppearance {
290        self.styles.window_background_appearance
291    }
292
293    /// Darkens the color by reducing its lightness.
294    /// The resulting lightness is clamped to ensure it doesn't go below 0.0.
295    ///
296    /// The first value darkens light appearance mode, the second darkens appearance dark mode.
297    ///
298    /// Note: This is a tentative solution and may be replaced with a more robust color system.
299    pub fn darken(&self, color: Hsla, light_amount: f32, dark_amount: f32) -> Hsla {
300        let amount = match self.appearance {
301            Appearance::Light => light_amount,
302            Appearance::Dark => dark_amount,
303        };
304        let mut hsla = color;
305        hsla.l = (hsla.l - amount).max(0.0);
306        hsla
307    }
308}
309
310/// Deserializes an icon theme from the given bytes.
311pub fn deserialize_icon_theme(bytes: &[u8]) -> anyhow::Result<IconThemeFamilyContent> {
312    let icon_theme_family: IconThemeFamilyContent = serde_json_lenient::from_slice(bytes)?;
313
314    Ok(icon_theme_family)
315}
316
317/// The active theme.
318pub struct GlobalTheme {
319    theme: Arc<Theme>,
320    icon_theme: Arc<IconTheme>,
321}
322impl Global for GlobalTheme {}
323
324impl GlobalTheme {
325    /// Creates a new [`GlobalTheme`] with the given theme and icon theme.
326    pub fn new(theme: Arc<Theme>, icon_theme: Arc<IconTheme>) -> Self {
327        Self { theme, icon_theme }
328    }
329
330    /// Updates the active theme.
331    pub fn update_theme(cx: &mut App, theme: Arc<Theme>) {
332        cx.update_global::<Self, _>(|this, _| this.theme = theme);
333    }
334
335    /// Updates the active icon theme.
336    pub fn update_icon_theme(cx: &mut App, icon_theme: Arc<IconTheme>) {
337        cx.update_global::<Self, _>(|this, _| this.icon_theme = icon_theme);
338    }
339
340    /// Returns the active theme.
341    pub fn theme(cx: &App) -> &Arc<Theme> {
342        &cx.global::<Self>().theme
343    }
344
345    /// Returns the active icon theme.
346    pub fn icon_theme(cx: &App) -> &Arc<IconTheme> {
347        &cx.global::<Self>().icon_theme
348    }
349}
350
Served at tenant.openagents/omega Member data and write actions are omitted.