theme: extend live theme change watching on macos and windows
This commit is contained in:
@@ -4,19 +4,21 @@
|
||||
//! the system's color preferences.
|
||||
|
||||
use objc2::rc::Retained;
|
||||
use objc2::runtime::{NSObjectProtocol, ProtocolObject};
|
||||
use objc2::runtime::{AnyObject, NSObject};
|
||||
use objc2::{AnyThread, DefinedClass, define_class, msg_send, sel};
|
||||
use objc2_app_kit::{
|
||||
NSAppearance, NSAppearanceNameAqua, NSAppearanceNameDarkAqua, NSColor, NSColorSpace,
|
||||
NSWorkspace,
|
||||
NSWorkspace, NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification,
|
||||
};
|
||||
use objc2_foundation::{
|
||||
NSDistributedNotificationCenter, NSNotification, NSNotificationName, NSOperationQueue,
|
||||
NSDictionary, NSDistributedNotificationCenter, NSKeyValueObservingOptions, NSNotification,
|
||||
NSNotificationName, NSObjectNSKeyValueObserverRegistration, NSString, NSUserDefaults,
|
||||
};
|
||||
use std::ptr::NonNull;
|
||||
|
||||
use super::narrow_component;
|
||||
|
||||
use super::ThemeSource;
|
||||
use super::signal::ReapplySignal;
|
||||
use crate::ThemeVariant;
|
||||
use crate::system::{AccessibilityPreferences, PartialPalette, SourceKind, SystemTheme};
|
||||
|
||||
@@ -37,9 +39,44 @@ 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;
|
||||
|
||||
// An observer that forwards distributed notifications and preference.
|
||||
define_class!(
|
||||
#[unsafe(super(NSObject))]
|
||||
#[ivars = async_channel::Sender<()>]
|
||||
struct ThemeObserver;
|
||||
|
||||
impl ThemeObserver {
|
||||
#[unsafe(method(themeDidChange:))]
|
||||
fn theme_did_change(&self, _notification: &NSNotification) {
|
||||
ReapplySignal::poke(self.ivars());
|
||||
}
|
||||
|
||||
#[unsafe(method(observeValueForKeyPath:ofObject:change:context:))]
|
||||
fn observe_value_for_key_path(
|
||||
&self,
|
||||
_key_path: &NSString,
|
||||
_object: Option<&AnyObject>,
|
||||
_change: Option<&NSDictionary<NSString, AnyObject>>,
|
||||
_context: *mut core::ffi::c_void,
|
||||
) {
|
||||
ReapplySignal::poke(self.ivars());
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
impl ThemeObserver {
|
||||
fn new(reapply: async_channel::Sender<()>) -> Retained<Self> {
|
||||
let this = Self::alloc().set_ivars(reapply);
|
||||
unsafe { msg_send![super(this), init] }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub(crate) struct MacosSource {
|
||||
_observers: Vec<Retained<ProtocolObject<dyn NSObjectProtocol>>>,
|
||||
/// The notification centers track, but do not retain, the observer.
|
||||
_observer: Option<Retained<ThemeObserver>>,
|
||||
/// Keeps the reapply loop alive.
|
||||
_signal: Option<ReapplySignal>,
|
||||
}
|
||||
|
||||
impl MacosSource {
|
||||
@@ -142,34 +179,62 @@ impl ThemeSource for MacosSource {
|
||||
}
|
||||
}
|
||||
|
||||
/// Fires on accent and appearance changes (System Settings > Appearance)
|
||||
/// and re-runs the resolve pipeline through `async_cx`.
|
||||
/// Re-runs the resolve pipeline when the system's theme changes.
|
||||
fn install_watchers(&mut self, async_cx: gpui::AsyncApp) {
|
||||
let center = NSDistributedNotificationCenter::defaultCenter();
|
||||
let queue = NSOperationQueue::mainQueue();
|
||||
let signal = ReapplySignal::install(&async_cx);
|
||||
let observer = ThemeObserver::new(signal.sender());
|
||||
|
||||
let center = NSDistributedNotificationCenter::defaultCenter();
|
||||
for name in [
|
||||
"AppleColorPreferencesChangedNotification",
|
||||
"AppleInterfaceThemeChangedNotification",
|
||||
] {
|
||||
let async_cx = async_cx.clone();
|
||||
let block = block2::RcBlock::new(move |_notification: NonNull<NSNotification>| {
|
||||
async_cx.update(crate::reapply);
|
||||
});
|
||||
let name = NSNotificationName::from_str(name);
|
||||
// SAFETY: the observer token is retained for the source's lifetime,
|
||||
// the block is sendable (AsyncApp is Clone + 'static)
|
||||
// and the main queue runs it outside any App borrow.
|
||||
let observer = unsafe {
|
||||
center.addObserverForName_object_queue_usingBlock(
|
||||
// SAFETY: the observer is retained past every delivery.
|
||||
unsafe {
|
||||
center.addObserver_selector_name_object(
|
||||
&observer,
|
||||
sel!(themeDidChange:),
|
||||
Some(&name),
|
||||
None,
|
||||
Some(&queue),
|
||||
&block,
|
||||
)
|
||||
};
|
||||
self._observers.push(observer);
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let defaults = NSUserDefaults::standardUserDefaults();
|
||||
for key in [
|
||||
"AppleAccentColor",
|
||||
"AppleAquaColorVariant",
|
||||
"AppleInterfaceStyle",
|
||||
] {
|
||||
let key = NSString::from_str(key);
|
||||
// SAFETY: as above
|
||||
unsafe {
|
||||
defaults.addObserver_forKeyPath_options_context(
|
||||
&observer,
|
||||
&key,
|
||||
NSKeyValueObservingOptions::empty(),
|
||||
core::ptr::null_mut(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let workspace_center = NSWorkspace::sharedWorkspace().notificationCenter();
|
||||
let accessibility_name =
|
||||
unsafe { NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification };
|
||||
|
||||
// SAFETY: as above.
|
||||
unsafe {
|
||||
workspace_center.addObserver_selector_name_object(
|
||||
&observer,
|
||||
sel!(themeDidChange:),
|
||||
Some(accessibility_name),
|
||||
None,
|
||||
);
|
||||
}
|
||||
|
||||
self._observer = Some(observer);
|
||||
self._signal = Some(signal);
|
||||
}
|
||||
|
||||
fn label(&self) -> &'static str {
|
||||
|
||||
Reference in New Issue
Block a user