diff --git a/React/Base/RCTRootView.m b/React/Base/RCTRootView.m index 66f7b1eaf472fe..d76325d083d71d 100644 --- a/React/Base/RCTRootView.m +++ b/React/Base/RCTRootView.m @@ -431,15 +431,6 @@ - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection RCTUserInterfaceStyleDidChangeNotificationTraitCollectionKey : self.traitCollection, }]; } -#else // [TODO(macOS GH#774) -- (void)viewDidChangeEffectiveAppearance { - [[NSNotificationCenter defaultCenter] postNotificationName:RCTUserInterfaceStyleDidChangeNotification - object:self - userInfo:@{ - RCTUserInterfaceStyleDidChangeNotificationTraitCollectionKey: self.effectiveAppearance, - }]; - -} #endif // ]TODO(macOS GH#774) diff --git a/React/CoreModules/RCTAppearance.mm b/React/CoreModules/RCTAppearance.mm index 40f4094d62c292..bac91467a8e885 100644 --- a/React/CoreModules/RCTAppearance.mm +++ b/React/CoreModules/RCTAppearance.mm @@ -137,21 +137,26 @@ - (instancetype)init return _currentColorScheme; } +#if !TARGET_OS_OSX // TODO(macOS GH#774) - (void)appearanceChanged:(NSNotification *)notification { NSDictionary *userInfo = [notification userInfo]; -#if !TARGET_OS_OSX // TODO(macOS GH#774) UITraitCollection *traitCollection = nil; if (userInfo) { traitCollection = userInfo[RCTUserInterfaceStyleDidChangeNotificationTraitCollectionKey]; } NSString *newColorScheme = RCTColorSchemePreference(traitCollection); #else // [TODO(macOS GH#774) - NSAppearance *appearance = nil; - if (userInfo) { - appearance = userInfo[RCTUserInterfaceStyleDidChangeNotificationTraitCollectionKey]; +- (void)observeValueForKeyPath:(NSString *)keyPath + ofObject:(id)object + change:(NSDictionary *)change + context:(void *)context +{ + if (object != NSApp || ![keyPath isEqualToString:@"effectiveAppearance"]) { + [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; + return; } - NSString *newColorScheme = RCTColorSchemePreference(appearance); + NSString *newColorScheme = RCTColorSchemePreference(nil); #endif // ]TODO(macOS GH#774) if (![_currentColorScheme isEqualToString:newColorScheme]) { _currentColorScheme = newColorScheme; @@ -168,19 +173,30 @@ - (void)appearanceChanged:(NSNotification *)notification - (void)startObserving { +#if !TARGET_OS_OSX // [TODO(macOS GH#774) if (@available(iOS 13.0, *)) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appearanceChanged:) name:RCTUserInterfaceStyleDidChangeNotification object:nil]; } +#else + [NSApp addObserver:self + forKeyPath:@"effectiveAppearance" + options:NSKeyValueObservingOptionNew + context:nil]; +#endif // ]TODO(macOS GH#774) } - (void)stopObserving { +#if !TARGET_OS_OSX // [TODO(macOS GH#774) if (@available(iOS 13.0, *)) { [[NSNotificationCenter defaultCenter] removeObserver:self]; } +#else + [NSApp removeObserver:self forKeyPath:@"effectiveAppearance" context:nil]; +#endif // ]TODO(macOS GH#774) } @end