forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ios dark mode detection (keybase#19340)
- Loading branch information
1 parent
838c602
commit f6b30d7
Showing
16 changed files
with
300 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
// Entry point to the chrome part of the app: ORDER IS IMPORTANT | ||
import '../../util/user-timings' | ||
import 'react-hot-loader' | ||
import {_setSystemIsDarkMode} from '../../styles/dark-mode' | ||
import {_setSystemIsDarkMode, _setSystemSupported} from '../../styles/dark-mode' | ||
import {isDarwin} from '../../constants/platform' | ||
import * as SafeElectron from '../../util/safe-electron.desktop' | ||
|
||
_setSystemIsDarkMode(isDarwin && SafeElectron.getSystemPreferences().isDarkMode()) | ||
_setSystemSupported(isDarwin) | ||
require('./main2.desktop') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,20 @@ | ||
// @flow | ||
// React-native tooling assumes this file is here, so we just require our real entry point | ||
import './app/globals.native' | ||
import {NativeModules} from 'react-native' | ||
import {_setSystemIsDarkMode, _setSystemSupported} from './styles/dark-mode' | ||
|
||
// Load storybook or the app | ||
if (__STORYBOOK__) { | ||
const load = require('./storybook/index.native').default | ||
load() | ||
} else { | ||
const NativeAppearance = NativeModules.Appearance | ||
|
||
if (NativeAppearance) { | ||
_setSystemIsDarkMode(NativeAppearance.initialColorScheme === 'dark') | ||
_setSystemSupported(NativeAppearance.supported === '1') | ||
} | ||
const {load} = require('./app/index.native') | ||
load() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// A placeholder implementation for RN's https://github.com/facebook/react-native/commit/63fa3f21c5ab308def450bffb22054241a8842ef | ||
// Appearance.h | ||
// Keybase | ||
// | ||
// Created by Chris Nojima on 9/9/19. | ||
// Copyright © 2019 Keybase. All rights reserved. | ||
// | ||
|
||
#ifndef Appearance_h | ||
#define Appearance_h | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
#import <React/RCTBridgeModule.h> | ||
#import <React/RCTEventEmitter.h> | ||
|
||
NSString *const RCTUserInterfaceStyleDidChangeNotification = @"RCTUserInterfaceStyleDidChangeNotification"; | ||
|
||
@interface Appearance : RCTEventEmitter <RCTBridgeModule> | ||
@end | ||
|
||
#endif /* Appearance_h */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// | ||
// Appearance.m | ||
// Keybase | ||
// | ||
// Created by Chris Nojima on 9/9/19. | ||
// Copyright © 2019 Keybase. All rights reserved. | ||
// | ||
|
||
#import "Appearance.h" | ||
|
||
#import <React/RCTEventEmitter.h> | ||
|
||
NSString *const RCTAppearanceColorSchemeLight = @"light"; | ||
NSString *const RCTAppearanceColorSchemeDark = @"dark"; | ||
|
||
static NSString *RCTColorSchemePreference(UITraitCollection *traitCollection) | ||
{ | ||
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0 | ||
if (@available(iOS 13.0, *)) { | ||
static NSDictionary *appearances; | ||
static dispatch_once_t onceToken; | ||
|
||
dispatch_once(&onceToken, ^{ | ||
appearances = @{ | ||
@(UIUserInterfaceStyleLight): RCTAppearanceColorSchemeLight, | ||
@(UIUserInterfaceStyleDark): RCTAppearanceColorSchemeDark | ||
}; | ||
}); | ||
|
||
traitCollection = traitCollection ?: [UITraitCollection currentTraitCollection]; | ||
return appearances[@(traitCollection.userInterfaceStyle)] ?: nil; | ||
} | ||
#endif | ||
|
||
return nil; | ||
} | ||
|
||
@interface Appearance () | ||
@end | ||
|
||
@implementation Appearance | ||
|
||
RCT_EXPORT_MODULE(Appearance) | ||
|
||
+ (BOOL)requiresMainQueueSetup | ||
{ | ||
return YES; | ||
} | ||
|
||
- (dispatch_queue_t)methodQueue | ||
{ | ||
return dispatch_get_main_queue(); | ||
} | ||
|
||
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSString *, getColorScheme) | ||
{ | ||
return RCTColorSchemePreference(nil); | ||
} | ||
|
||
- (void)appearanceChanged:(NSNotification *)notification | ||
{ | ||
NSDictionary *userInfo = [notification userInfo]; | ||
UITraitCollection *traitCollection = nil; | ||
if (userInfo) { | ||
traitCollection = userInfo[@"traitCollection"]; | ||
} | ||
[self sendEventWithName:@"appearanceChanged" body:@{@"colorScheme": RCTColorSchemePreference(traitCollection)}]; | ||
} | ||
|
||
#pragma mark - RCTEventEmitter | ||
|
||
- (NSArray<NSString *> *)supportedEvents | ||
{ | ||
return @[@"appearanceChanged"]; | ||
} | ||
|
||
- (void)startObserving | ||
{ | ||
if (@available(iOS 13.0, *)) { | ||
[[NSNotificationCenter defaultCenter] addObserver:self | ||
selector:@selector(appearanceChanged:) | ||
name:RCTUserInterfaceStyleDidChangeNotification | ||
object:nil]; | ||
} | ||
} | ||
|
||
- (void)stopObserving | ||
{ | ||
if (@available(iOS 13.0, *)) { | ||
[[NSNotificationCenter defaultCenter] removeObserver:self]; | ||
} | ||
} | ||
|
||
- (NSDictionary *)constantsToExport { | ||
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0 | ||
return @{ @"initialColorScheme": RCTColorSchemePreference(nil), | ||
@"supported": @"1" | ||
}; | ||
#else | ||
return @{ @"initialColorScheme": @"light", | ||
@"supported": @"0" | ||
}; | ||
#endif | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// AppearanceRootView.h | ||
// Keybase | ||
// | ||
// Created by Chris Nojima on 9/9/19. | ||
// Copyright © 2019 Keybase. All rights reserved. | ||
// | ||
|
||
#import <React/RCTRootView.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface AppearanceRootView : RCTRootView | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// AppearanceRootView.m | ||
// Keybase | ||
// | ||
// Created by Chris Nojima on 9/9/19. | ||
// Copyright © 2019 Keybase. All rights reserved. | ||
// | ||
|
||
#import "AppearanceRootView.h" | ||
|
||
@implementation AppearanceRootView | ||
|
||
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \ | ||
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0 | ||
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection | ||
{ | ||
NSString * RCTUserInterfaceStyleDidChangeNotification = @"RCTUserInterfaceStyleDidChangeNotification"; | ||
[super traitCollectionDidChange:previousTraitCollection]; | ||
|
||
if (@available(iOS 13.0, *)) { | ||
if ([previousTraitCollection hasDifferentColorAppearanceComparedToTraitCollection:self.traitCollection]) { | ||
[[NSNotificationCenter defaultCenter] postNotificationName:RCTUserInterfaceStyleDidChangeNotification | ||
object:self | ||
userInfo:@{@"traitCollection": self.traitCollection}]; | ||
} | ||
} | ||
} | ||
#endif | ||
|
||
@end |
Oops, something went wrong.