theme: add custom theme layered
This commit is contained in:
+112
-83
@@ -130,6 +130,16 @@ impl ThemeVariant {
|
||||
}
|
||||
}
|
||||
|
||||
/// A named control size class.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub enum Size {
|
||||
Small,
|
||||
/// The default.
|
||||
#[default]
|
||||
Medium,
|
||||
Large,
|
||||
}
|
||||
|
||||
/// The resolved theme, installed as the app-wide global.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct Theme {
|
||||
@@ -161,6 +171,63 @@ pub struct Theme {
|
||||
|
||||
impl Global for Theme {}
|
||||
|
||||
impl Theme {
|
||||
/// Reads the theme global. Panics before `init`.
|
||||
pub fn global(cx: &App) -> &Self {
|
||||
cx.global::<Self>()
|
||||
}
|
||||
|
||||
fn from_resolved(resolved: ResolvedTheme) -> Self {
|
||||
Self {
|
||||
variant: resolved.variant,
|
||||
colors: resolved.colors,
|
||||
material: resolved.material,
|
||||
ramps: resolved.ramps,
|
||||
radius: resolved.radius,
|
||||
borders: resolved.borders,
|
||||
spacing: resolved.spacing,
|
||||
elevations: resolved.elevations,
|
||||
motion: resolved.motion,
|
||||
scrollbar: resolved.scrollbar,
|
||||
titlebar_height: resolved.titlebar_height,
|
||||
root_font_size: resolved.root_font_size,
|
||||
mono_font: resolved.mono_font,
|
||||
sizes: resolved.sizes,
|
||||
accessibility: resolved.accessibility,
|
||||
window_background: resolved.window_background,
|
||||
}
|
||||
}
|
||||
|
||||
/// Installs the engine global, its platform watchers, and the initial resolved theme.
|
||||
pub fn init(cx: &mut App) {
|
||||
let mut engine = ThemeEngine {
|
||||
source: source::platform_source(),
|
||||
mode_override: None,
|
||||
preset: None,
|
||||
custom: None,
|
||||
};
|
||||
engine.source.install_watchers(cx.to_async());
|
||||
cx.set_global(engine);
|
||||
reapply(cx);
|
||||
}
|
||||
|
||||
/// Synchronizes the theme from the window's appearance.
|
||||
pub fn sync_from_window(window: &mut gpui::Window, cx: &mut App) {
|
||||
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) {
|
||||
let theme = cx.global::<Theme>();
|
||||
window.set_rem_size(theme.root_font_size);
|
||||
}
|
||||
}
|
||||
|
||||
/// Corner radii components paint from; grows one class at a time.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct Radius {
|
||||
@@ -408,16 +475,6 @@ impl Default for Scrollbar {
|
||||
}
|
||||
}
|
||||
|
||||
/// A named control size class.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub enum Size {
|
||||
Small,
|
||||
/// The default.
|
||||
#[default]
|
||||
Medium,
|
||||
Large,
|
||||
}
|
||||
|
||||
/// The control metrics for one size class.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct ControlMetrics {
|
||||
@@ -592,6 +649,28 @@ impl SizeTable {
|
||||
}
|
||||
}
|
||||
|
||||
/// User customizations layered over the resolved theme.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
||||
pub struct CustomTheme {
|
||||
pub root_font_size: Option<Pixels>,
|
||||
pub sm_radius: Option<Pixels>,
|
||||
pub control_radius: Option<Pixels>,
|
||||
pub overlay_radius: Option<Pixels>,
|
||||
pub popover_elevation: Option<Elevation>,
|
||||
pub dialog_elevation: Option<Elevation>,
|
||||
pub control_elevation: Option<Elevation>,
|
||||
}
|
||||
|
||||
/// The engine's knobs and its platform source.
|
||||
struct ThemeEngine {
|
||||
source: Box<dyn source::ThemeSource>,
|
||||
mode_override: Option<ThemeVariant>,
|
||||
preset: Option<(&'static str, ThemeDefinition)>,
|
||||
custom: Option<CustomTheme>,
|
||||
}
|
||||
|
||||
impl Global for ThemeEngine {}
|
||||
|
||||
/// Reads the resolved theme global from any app context.
|
||||
pub trait ActiveTheme {
|
||||
fn theme(&self) -> &Theme;
|
||||
@@ -603,89 +682,26 @@ impl ActiveTheme for App {
|
||||
}
|
||||
}
|
||||
|
||||
impl Theme {
|
||||
/// Reads the theme global. Panics before `init`.
|
||||
pub fn global(cx: &App) -> &Self {
|
||||
cx.global::<Self>()
|
||||
}
|
||||
|
||||
fn from_resolved(resolved: ResolvedTheme) -> Self {
|
||||
Self {
|
||||
variant: resolved.variant,
|
||||
colors: resolved.colors,
|
||||
material: resolved.material,
|
||||
ramps: resolved.ramps,
|
||||
radius: resolved.radius,
|
||||
borders: resolved.borders,
|
||||
spacing: resolved.spacing,
|
||||
elevations: resolved.elevations,
|
||||
motion: resolved.motion,
|
||||
scrollbar: resolved.scrollbar,
|
||||
titlebar_height: resolved.titlebar_height,
|
||||
root_font_size: resolved.root_font_size,
|
||||
mono_font: resolved.mono_font,
|
||||
sizes: resolved.sizes,
|
||||
accessibility: resolved.accessibility,
|
||||
window_background: resolved.window_background,
|
||||
}
|
||||
}
|
||||
|
||||
/// Installs the engine global, its platform watchers, and the initial resolved theme.
|
||||
pub fn init(cx: &mut App) {
|
||||
let mut engine = ThemeEngine {
|
||||
source: source::platform_source(),
|
||||
mode_override: None,
|
||||
preset: None,
|
||||
};
|
||||
engine.source.install_watchers(cx.to_async());
|
||||
cx.set_global(engine);
|
||||
reapply(cx);
|
||||
}
|
||||
|
||||
/// 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 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) {
|
||||
let theme = cx.global::<Theme>();
|
||||
window.set_rem_size(theme.root_font_size);
|
||||
}
|
||||
}
|
||||
|
||||
/// The engine's knobs and its platform source.
|
||||
struct ThemeEngine {
|
||||
source: Box<dyn source::ThemeSource>,
|
||||
mode_override: Option<ThemeVariant>,
|
||||
preset: Option<(&'static str, ThemeDefinition)>,
|
||||
}
|
||||
|
||||
impl Global for ThemeEngine {}
|
||||
|
||||
/// Reads the system, resolves through the current knobs,
|
||||
/// installs the theme global, and repaints every window.
|
||||
/// Reapplies the theme to the application.
|
||||
pub(crate) fn reapply(cx: &mut App) {
|
||||
let (preset, mode_override, system) = {
|
||||
let (preset, mode_override, custom, system) = {
|
||||
let engine = cx.global::<ThemeEngine>();
|
||||
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 custom = engine.custom;
|
||||
let variant = mode_override
|
||||
.or(preset.as_ref().and_then(|def| def.mode()))
|
||||
.unwrap_or(system_appearance);
|
||||
(preset, mode_override, engine.source.snapshot(variant))
|
||||
(
|
||||
preset,
|
||||
mode_override,
|
||||
custom,
|
||||
engine.source.snapshot(variant),
|
||||
)
|
||||
};
|
||||
|
||||
let resolved = resolve::resolve(&system, preset.as_ref(), mode_override);
|
||||
let resolved = resolve::resolve(&system, preset.as_ref(), mode_override, custom);
|
||||
|
||||
cx.set_reduce_motion(resolved.accessibility.reduce_motion);
|
||||
cx.set_global(Theme::from_resolved(resolved));
|
||||
@@ -717,12 +733,25 @@ pub fn set_preset(name: Option<&str>, cx: &mut App) -> anyhow::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Installs the user's custom theme, or `None` to follow the current scheme.
|
||||
pub fn set_custom_theme(custom: Option<CustomTheme>, cx: &mut App) {
|
||||
let engine = cx.global_mut::<ThemeEngine>();
|
||||
engine.custom = custom;
|
||||
reapply(cx);
|
||||
}
|
||||
|
||||
/// The variant the mode override forces, if any.
|
||||
pub fn mode_override(cx: &App) -> Option<ThemeVariant> {
|
||||
let engine = cx.global::<ThemeEngine>();
|
||||
engine.mode_override
|
||||
}
|
||||
|
||||
/// The installed custom theme, if any.
|
||||
pub fn custom_theme(cx: &App) -> Option<CustomTheme> {
|
||||
let engine = cx.global::<ThemeEngine>();
|
||||
engine.custom
|
||||
}
|
||||
|
||||
/// The active preset name, if one is installed.
|
||||
pub fn active_preset(cx: &App) -> Option<&'static str> {
|
||||
let engine = cx.global::<ThemeEngine>();
|
||||
|
||||
Reference in New Issue
Block a user