Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) Microsoft Corporation.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#if TARGET_OS_OSX // [macOS
#import <AppKit/AppKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface RCTAppearanceProxy : NSObject

+ (instancetype)sharedInstance;

/*
* Property to access the current appearance.
* Thread safe.
*/
@property (nonatomic, readonly) NSAppearance *currentAppearance;

- (void)startObservingAppearance;

@end

NS_ASSUME_NONNULL_END
#endif // macOS]
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright (c) Microsoft Corporation.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#if TARGET_OS_OSX // [macOS
#import "RCTAppearanceProxy.h"

#import <React/RCTConstants.h>
#import <React/RCTUtils.h>

#import <mutex>

@implementation RCTAppearanceProxy {
BOOL _isObserving;
std::mutex _mutex;
NSAppearance *_currentAppearance;
}

+ (instancetype)sharedInstance
{
static RCTAppearanceProxy *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [RCTAppearanceProxy new];
});
return sharedInstance;
}

- (instancetype)init
{
self = [super init];
if (self) {
_isObserving = NO;
_currentAppearance = [NSApp effectiveAppearance];
}
return self;
}

- (void)startObservingAppearance
{
RCTAssertMainQueue();
std::lock_guard<std::mutex> lock(_mutex);
if (!_isObserving) {
_isObserving = YES;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_appearanceDidChange:)
name:RCTUserInterfaceStyleDidChangeNotification
object:nil];
}
}

- (NSAppearance *)currentAppearance
{
{
std::lock_guard<std::mutex> lock(_mutex);
if (_isObserving) {
return _currentAppearance;
}
}

__block NSAppearance *appearance = nil;
if (RCTIsMainQueue()) {
appearance = [NSApp effectiveAppearance];
} else {
dispatch_sync(dispatch_get_main_queue(), ^{
appearance = [NSApp effectiveAppearance];
});
}
return appearance;
}

- (void)_appearanceDidChange:(NSNotification *)notification
{
std::lock_guard<std::mutex> lock(_mutex);

NSDictionary *userInfo = [notification userInfo];
if (userInfo) {
NSAppearance *appearance = userInfo[RCTUserInterfaceStyleDidChangeNotificationAppearanceKey];
if (appearance != nil) {
_currentAppearance = appearance;
return;
}
}

_currentAppearance = [NSApp effectiveAppearance];
}

@end
#endif // macOS]
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#import "RCTKeyWindowValuesProxy.h"
#import "RCTTraitCollectionProxy.h"
#import "RCTWindowSafeAreaProxy.h"
#if TARGET_OS_OSX // [macOS
#import "RCTAppearanceProxy.h"
#endif // macOS]

void RCTInitializeUIKitProxies(void)
{
Expand All @@ -19,7 +22,9 @@ void RCTInitializeUIKitProxies(void)
#if !TARGET_OS_OSX // [macOS]
[[RCTTraitCollectionProxy sharedInstance] startObservingTraitCollection];
[[RCTInitialAccessibilityValuesProxy sharedInstance] recordAccessibilityValues];
#endif // [macOS]
#else // [macOS
[[RCTAppearanceProxy sharedInstance] startObservingAppearance];
#endif // macOS]
[[RCTKeyWindowValuesProxy sharedInstance] startObservingWindowSizeIfNecessary];
});
}
43 changes: 24 additions & 19 deletions packages/react-native/React/CoreModules/RCTAppearance.mm
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@

#import "CoreModulesPlugins.h"

#if TARGET_OS_OSX // [macOS
#import <React/RCTUtils.h>

#import "RCTAppearanceProxy.h"
#endif // macOS]

using namespace facebook::react;

NSString *const RCTAppearanceColorSchemeLight = @"light";
Expand Down Expand Up @@ -119,7 +125,7 @@ - (instancetype)init
UITraitCollection *traitCollection = [RCTTraitCollectionProxy sharedInstance].currentTraitCollection;
_currentColorScheme = RCTColorSchemePreference(traitCollection);
#else // [macOS
NSAppearance *appearance = RCTSharedApplication().appearance;
NSAppearance *appearance = [RCTAppearanceProxy sharedInstance].currentAppearance;
_currentColorScheme = RCTColorSchemePreference(appearance);
#endif // macOS]
[[NSNotificationCenter defaultCenter] addObserver:self
Expand All @@ -134,7 +140,11 @@ - (instancetype)init

+ (BOOL)requiresMainQueueSetup
{
#if !TARGET_OS_OSX // [macOS]
return NO;
#else // [macOS
return YES;
#endif // macOS]
}

- (dispatch_queue_t)methodQueue
Expand All @@ -160,13 +170,15 @@ - (dispatch_queue_t)methodQueue
window.overrideUserInterfaceStyle = userInterfaceStyle;
}
#else // [macOS
NSAppearance *appearance = nil;
if ([style isEqualToString:@"light"]) {
appearance = [NSAppearance appearanceNamed:NSAppearanceNameAqua];
} else if ([style isEqualToString:@"dark"]) {
appearance = [NSAppearance appearanceNamed:NSAppearanceNameDarkAqua];
}
RCTSharedApplication().appearance = appearance;
RCTExecuteOnMainQueue(^{
NSAppearance *appearance = nil;
if ([style isEqualToString:@"light"]) {
appearance = [NSAppearance appearanceNamed:NSAppearanceNameAqua];
} else if ([style isEqualToString:@"dark"]) {
appearance = [NSAppearance appearanceNamed:NSAppearanceNameDarkAqua];
}
RCTSharedApplication().appearance = appearance;
});
#endif // macOS]
}

Expand All @@ -177,10 +189,7 @@ - (dispatch_queue_t)methodQueue
UITraitCollection *traitCollection = [RCTTraitCollectionProxy sharedInstance].currentTraitCollection;
_currentColorScheme = RCTColorSchemePreference(traitCollection);
#else // [macOS
__block NSAppearance *appearance = nil;
RCTUnsafeExecuteOnMainQueueSync(^{
appearance = RCTKeyWindow().appearance;
});
NSAppearance *appearance = [RCTAppearanceProxy sharedInstance].currentAppearance;
_currentColorScheme = RCTColorSchemePreference(appearance);
#endif // macOS]
}
Expand All @@ -190,23 +199,19 @@ - (dispatch_queue_t)methodQueue

- (void)appearanceChanged:(NSNotification *)notification
{
#if !TARGET_OS_OSX // [macOS
NSDictionary *userInfo = [notification userInfo];
#if !TARGET_OS_OSX // [macOS]
UITraitCollection *traitCollection = nil;
if (userInfo) {
traitCollection = userInfo[RCTUserInterfaceStyleDidChangeNotificationTraitCollectionKey];
}
NSString *newColorScheme = RCTColorSchemePreference(traitCollection);
#else // [macOS
NSAppearance *appearance = nil;
if (userInfo) {
appearance = userInfo[RCTUserInterfaceStyleDidChangeNotificationAppearanceKey];
}
NSString *newColorScheme = RCTColorSchemePreference(appearance);
NSString *newColorScheme = RCTColorSchemePreference([RCTAppearanceProxy sharedInstance].currentAppearance);
#endif // macOS]
if (![_currentColorScheme isEqualToString:newColorScheme]) {
_currentColorScheme = newColorScheme;
[self sendEventWithName:@"appearanceChanged" body:@{@"colorScheme" : newColorScheme}];
[self sendEventWithName:@"appearanceChanged" body:@{ @"colorScheme" : newColorScheme }];
}
}

Expand Down
4 changes: 4 additions & 0 deletions packages/react-native/React/CoreModules/RCTDeviceInfo.mm
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ - (instancetype)init

+ (BOOL)requiresMainQueueSetup
{
#if !TARGET_OS_OSX // [macOS]
return NO;
#else // [macOS
return YES;
#endif // macOS]
}

- (dispatch_queue_t)methodQueue
Expand Down
Loading