feat: unify theme system (#7)

Reviewed-on: #7
This commit was merged in pull request #7.
This commit is contained in:
2026-11-16 12:58:32 +00:00
parent 745a01d7af
commit aed3601c89
47 changed files with 888 additions and 1875 deletions
+4 -89
View File
@@ -1,14 +1,12 @@
//! macOS Adapter
//!
//! Reads AppKit's semantic colors in the effective appearance and watches
//! the system's color preferences.
//! Reads the system accent and watches the system's color preferences.
use objc2::rc::Retained;
use objc2::runtime::{AnyObject, NSObject};
use objc2::{AnyThread, DefinedClass, define_class, msg_send, sel};
use objc2_app_kit::{
NSAppearance, NSAppearanceNameAqua, NSAppearanceNameDarkAqua, NSColor, NSColorSpace,
NSWorkspace, NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification,
NSColor, NSColorSpace, NSWorkspace, NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification,
};
use objc2_foundation::{
NSDictionary, NSDistributedNotificationCenter, NSKeyValueObservingOptions, NSNotification,
@@ -20,24 +18,7 @@ use super::narrow_component;
use super::ThemeSource;
use super::signal::ReapplySignal;
use crate::ThemeVariant;
use crate::system::{AccessibilityPreferences, PartialPalette, SourceKind, SystemTheme};
/// Default control radius for macOS controls.
const CONTROL_RADIUS_PTS: f32 = 6.0;
/// The text field's intrinsic height at regular size, square or rounded bezel alike.
const INPUT_HEIGHT_PTS: f32 = 24.0;
/// AppKit's regular push button, measured via `NSButton::fittingSize`.
const CONTROL_HEIGHT_PTS: f32 = 24.0;
/// The `NSMenu` row height; 20 before the macOS 26 redesign.
const MENU_ITEM_HEIGHT_PTS: f32 = 24.0;
/// `NSSwitch`, measured via `fittingSize`.
const SWITCH_TRACK_HEIGHT_PTS: f32 = 24.0;
const SWITCH_TRACK_WIDTH_PTS: f32 = 54.0;
const SWITCH_KNOB_PTS: f32 = 20.0;
/// The standard titled window's titlebar, measured via `contentLayoutRect`.
const TITLEBAR_HEIGHT_PTS: f32 = 32.0;
/// The HIG's default body text on macOS is 13 pt (system font size 13 at 1x).
const ROOT_FONT_SIZE_PTS: f32 = 13.0;
use crate::system::{AccessibilityPreferences, SourceKind, SystemTheme};
// An observer that forwards distributed notifications and preference.
define_class!(
@@ -91,58 +72,6 @@ impl MacosSource {
a: narrow_component(converted.alphaComponent()),
}))
}
/// Resolves the semantic colors with `variant` as the current drawing appearance.
fn snapshot_in_appearance(&self, variant: ThemeVariant) -> PartialPalette {
let name = match variant {
ThemeVariant::Light => {
// SAFETY: extern statics exported by AppKit, always initialized.
unsafe { NSAppearanceNameAqua }
}
ThemeVariant::Dark => {
// SAFETY: as above.
unsafe { NSAppearanceNameDarkAqua }
}
};
let Some(appearance) = NSAppearance::appearanceNamed(name) else {
return self.read_palette();
};
let palette = std::cell::Cell::new(PartialPalette::default());
appearance.performAsCurrentDrawingAppearance(&block2::RcBlock::new(|| {
palette.set(self.read_palette());
}));
palette.into_inner()
}
fn read_palette(&self) -> PartialPalette {
PartialPalette {
background: Self::color_to_hsla(&NSColor::windowBackgroundColor()),
foreground: Self::color_to_hsla(&NSColor::labelColor()),
surface: Self::color_to_hsla(&NSColor::textBackgroundColor()),
surface_foreground: Self::color_to_hsla(&NSColor::labelColor()),
sidebar: Self::color_to_hsla(&NSColor::windowBackgroundColor()),
primary_foreground: Self::color_to_hsla(&NSColor::alternateSelectedControlTextColor()),
accent_foreground: Self::color_to_hsla(&NSColor::alternateSelectedControlTextColor()),
secondary: None,
secondary_foreground: Self::color_to_hsla(&NSColor::secondaryLabelColor()),
muted: None,
muted_hover: None,
muted_foreground: Self::color_to_hsla(&NSColor::secondaryLabelColor()),
destructive: Self::color_to_hsla(&NSColor::systemRedColor()),
success: None,
warning: None,
info: None,
border: Self::color_to_hsla(&NSColor::separatorColor()),
input: Self::color_to_hsla(&NSColor::controlBackgroundColor()),
ring: Self::color_to_hsla(&NSColor::keyboardFocusIndicatorColor()),
selection: Self::color_to_hsla(&NSColor::selectedTextBackgroundColor()),
overlay: None,
scrollbar_thumb: None,
}
}
}
impl ThemeSource for MacosSource {
@@ -155,26 +84,12 @@ impl ThemeSource for MacosSource {
reduce_transparency: workspace.accessibilityDisplayShouldReduceTransparency(),
};
let palette = self.snapshot_in_appearance(variant);
SystemTheme {
variant,
accent: Self::color_to_hsla(&NSColor::controlAccentColor()),
palette,
accessibility,
// macOS blurs what is behind the window natively.
window_background: Some(gpui::WindowBackgroundAppearance::Blurred),
control_radius: Some(gpui::px(CONTROL_RADIUS_PTS)),
overlay_radius: None,
sm_radius: None,
control_height: Some(gpui::px(CONTROL_HEIGHT_PTS)),
input_height: Some(gpui::px(INPUT_HEIGHT_PTS)),
menu_item_height: Some(gpui::px(MENU_ITEM_HEIGHT_PTS)),
switch_height: Some(gpui::px(SWITCH_TRACK_HEIGHT_PTS)),
switch_width: Some(gpui::px(SWITCH_TRACK_WIDTH_PTS)),
switch_knob: Some(gpui::px(SWITCH_KNOB_PTS)),
titlebar_height: Some(gpui::px(TITLEBAR_HEIGHT_PTS)),
root_font_size: Some(gpui::px(ROOT_FONT_SIZE_PTS)),
accessibility,
source: SourceKind::Macos,
}
}