Skip to content

Commit

Permalink
Fix Appearance module to observe application appearance (facebook#1340)
Browse files Browse the repository at this point in the history
This fixes the `Appearance.getColorScheme()` API to return the application-level appearance instead of the last appearance of any root view, which breaks down in multi-window applications.

In Messenger Desktop we always respect the OS appearance for main and setting windows, but force a darkTheme in the calling UI.
W/o this change returning from the calling UI would result in a wrong appearance setting on light theme OS setting

Co-authored-by: Scott Kyle <skyle@fb.com>
  • Loading branch information
christophpurrer and appden committed Aug 10, 2022
1 parent 24acd40 commit c3e847a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
9 changes: 0 additions & 9 deletions React/Base/RCTRootView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
26 changes: 21 additions & 5 deletions React/CoreModules/RCTAppearance.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit c3e847a

Please sign in to comment.