theme_settings_provider.rs

 1use gpui::{App, Font, Global, Pixels};
 2
 3use crate::UiDensity;
 4
 5/// Trait for providing theme-related settings (fonts, font sizes, UI density)
 6/// without coupling to the concrete settings infrastructure.
 7///
 8/// A concrete implementation is registered as a global by the `theme_settings` crate.
 9pub trait ThemeSettingsProvider: Send + Sync + 'static {
10    /// Returns the font used for UI elements.
11    fn ui_font<'a>(&'a self, cx: &'a App) -> &'a Font;
12
13    /// Returns the font used for buffers and the terminal.
14    fn buffer_font<'a>(&'a self, cx: &'a App) -> &'a Font;
15
16    /// Returns the UI font size in pixels.
17    fn ui_font_size(&self, cx: &App) -> Pixels;
18
19    /// Returns the buffer font size in pixels.
20    fn buffer_font_size(&self, cx: &App) -> Pixels;
21
22    /// Returns the current UI density setting.
23    fn ui_density(&self, cx: &App) -> UiDensity;
24}
25
26struct GlobalThemeSettingsProvider(Box<dyn ThemeSettingsProvider>);
27
28impl Global for GlobalThemeSettingsProvider {}
29
30/// Registers the global [`ThemeSettingsProvider`] implementation.
31///
32/// This should be called during application initialization by the crate
33/// that owns the concrete theme settings (e.g. `theme_settings`).
34pub fn set_theme_settings_provider(provider: Box<dyn ThemeSettingsProvider>, cx: &mut App) {
35    cx.set_global(GlobalThemeSettingsProvider(provider));
36}
37
38/// Returns the global [`ThemeSettingsProvider`].
39///
40/// Panics if no provider has been registered via [`set_theme_settings_provider`].
41pub fn theme_settings(cx: &App) -> &dyn ThemeSettingsProvider {
42    &*cx.global::<GlobalThemeSettingsProvider>().0
43}