Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Match the macOS app appearance to the selected theme (#58902)
Jason Lee8e4e5a39eed9
diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rsindex 65a5331142d..18b64ad9658 100644--- a/crates/gpui/src/app.rs+++ b/crates/gpui/src/app.rs@@ -1325,6 +1325,20 @@ impl App {self.platform.window_appearance()}+ /// Overrides the appearance (light/dark) applied to the app's windows, independent of+ /// the OS-wide setting. Pass `None` to clear the override and follow the system again.+ /// The current value is reported by [`App::window_appearance`].+ ///+ /// On macOS this sets the underlying `NSApplication.appearance`, which controls the+ /// native window chrome (the window border and titlebar) of every window. Use this+ /// when the app uses a dark theme while the system is in light mode (or vice versa)+ /// so the window edges render to match the theme. While an appearance is forced,+ /// windows stop tracking system light/dark changes; pass `None` to resume following+ /// the system. On other platforms this is a no-op.+ pub fn set_window_appearance(&self, appearance: Option<WindowAppearance>) {+ self.platform.set_window_appearance(appearance);+ }+/// Returns the window button layout configuration when supported.pub fn button_layout(&self) -> Option<WindowButtonLayout> {self.platform.button_layout()diff --git a/crates/gpui/src/platform.rs b/crates/gpui/src/platform.rsindex 20600c3eb51..d6b2c1acdf5 100644--- a/crates/gpui/src/platform.rs+++ b/crates/gpui/src/platform.rs@@ -167,6 +167,16 @@ pub trait Platform: 'static {/// Returns the appearance of the application's windows.fn window_appearance(&self) -> WindowAppearance;+ /// Overrides the appearance (light/dark) applied to the app's windows, independent+ /// of the OS-wide setting. Pass `None` to clear the override and follow the system+ /// again. The override is reflected by [`Platform::window_appearance`].+ ///+ /// Currently only implemented on macOS, where it sets `NSApplication.appearance` so+ /// the native window chrome (the window border and titlebar) of every window matches+ /// a dark app theme even when the system is in light mode (or vice versa). A no-op on+ /// other platforms.+ fn set_window_appearance(&self, _appearance: Option<WindowAppearance>) {}+/// Returns the window button layout configuration when supported.fn button_layout(&self) -> Option<WindowButtonLayout> {Nonediff --git a/crates/gpui_macos/src/platform.rs b/crates/gpui_macos/src/platform.rsindex b25067935e5..c9bd3076006 100644--- a/crates/gpui_macos/src/platform.rs+++ b/crates/gpui_macos/src/platform.rs@@ -7,9 +7,10 @@ use anyhow::{Context as _, anyhow};use block::ConcreteBlock;use cocoa::{appkit::{- NSApplication, NSApplicationActivationPolicy::NSApplicationActivationPolicyRegular,- NSControl as _, NSEventModifierFlags, NSMenu, NSMenuItem, NSModalResponse, NSOpenPanel,- NSSavePanel, NSVisualEffectState, NSVisualEffectView, NSWindow,+ NSAppearanceNameVibrantDark, NSAppearanceNameVibrantLight, NSApplication,+ NSApplicationActivationPolicy::NSApplicationActivationPolicyRegular, NSControl as _,+ NSEventModifierFlags, NSMenu, NSMenuItem, NSModalResponse, NSOpenPanel, NSSavePanel,+ NSVisualEffectState, NSVisualEffectView, NSWindow,},base::{BOOL, NO, YES, id, nil, selector},foundation::{@@ -677,6 +678,29 @@ impl Platform for MacPlatform {}}+ fn set_window_appearance(&self, appearance: Option<WindowAppearance>) {+ unsafe {+ let app: id = msg_send![APP_CLASS, sharedApplication];+ // `None` clears the override by setting a nil appearance, so the app+ // falls back to tracking the system-wide light/dark setting.+ let ns_appearance: id = match appearance {+ None => nil,+ Some(appearance) => {+ let name: id = match appearance {+ WindowAppearance::Light => crate::window_appearance::NSAppearanceNameAqua,+ WindowAppearance::Dark => {+ crate::window_appearance::NSAppearanceNameDarkAqua+ }+ WindowAppearance::VibrantLight => NSAppearanceNameVibrantLight,+ WindowAppearance::VibrantDark => NSAppearanceNameVibrantDark,+ };+ msg_send![class!(NSAppearance), appearanceNamed: name]+ }+ };+ let _: () = msg_send![app, setAppearance: ns_appearance];+ }+ }+fn open_url(&self, url: &str) {unsafe {let ns_url = NSURL::alloc(nil).initWithString_(ns_string(url));diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rsindex 76d27fb2316..c5db036e503 100644--- a/crates/zed/src/zed.rs+++ b/crates/zed/src/zed.rs@@ -434,6 +434,7 @@ pub fn initialize_workspace(app_state: Arc<AppState>, cx: &mut App) {.detach();init_cursor_hide_mode(cx);+ init_app_appearance(cx);init_reduce_motion(cx);cx.observe_new(|_multi_workspace: &mut MultiWorkspace, window, cx| {@@ -2051,6 +2052,24 @@ fn init_cursor_hide_mode(cx: &mut App) {cx.observe_global::<SettingsStore>(apply).detach();}+fn init_app_appearance(cx: &mut App) {+ // Force the native window chrome (border + titlebar) to match the selected theme.+ // `System` follows the OS (no override); any other theme forces its appearance, so a+ // dark theme doesn't render a light window border when the system is in light mode.+ let apply = |cx: &mut App| {+ let appearance = match ThemeSettings::get_global(cx).theme.mode() {+ Some(theme_settings::ThemeAppearanceMode::System) => None,+ _ => Some(match cx.theme().appearance() {+ theme::Appearance::Light => gpui::WindowAppearance::Light,+ theme::Appearance::Dark => gpui::WindowAppearance::Dark,+ }),+ };+ cx.set_window_appearance(appearance);+ };+ apply(cx);+ cx.observe_global::<SettingsStore>(apply).detach();+}+#[derive(Copy, Clone, Debug, settings::RegisterSetting)]struct ReduceMotionSetting(settings::ReduceMotionMode);