2015-07-31 14:37:12 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTAccessibilityManager.h"
|
|
|
|
|
|
|
|
#import "RCTLog.h"
|
|
|
|
|
|
|
|
NSString *const RCTAccessibilityManagerDidUpdateMultiplierNotification = @"RCTAccessibilityManagerDidUpdateMultiplierNotification";
|
|
|
|
|
|
|
|
@interface RCTAccessibilityManager ()
|
|
|
|
|
|
|
|
@property (nonatomic, copy) NSString *contentSizeCategory;
|
|
|
|
@property (nonatomic, assign) CGFloat multiplier;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTAccessibilityManager
|
|
|
|
|
|
|
|
@synthesize bridge = _bridge;
|
2015-08-07 09:32:00 +00:00
|
|
|
@synthesize multipliers = _multipliers;
|
2015-07-31 14:37:12 +00:00
|
|
|
|
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
|
|
|
+ (NSDictionary *)JSToUIKitMap
|
|
|
|
{
|
|
|
|
static NSDictionary *map = nil;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
map = @{@"extraSmall": UIContentSizeCategoryExtraSmall,
|
|
|
|
@"small": UIContentSizeCategorySmall,
|
|
|
|
@"medium": UIContentSizeCategoryMedium,
|
|
|
|
@"large": UIContentSizeCategoryLarge,
|
|
|
|
@"extraLarge": UIContentSizeCategoryExtraLarge,
|
|
|
|
@"extraExtraLarge": UIContentSizeCategoryExtraExtraLarge,
|
|
|
|
@"extraExtraExtraLarge": UIContentSizeCategoryExtraExtraExtraLarge,
|
|
|
|
@"accessibilityMedium": UIContentSizeCategoryAccessibilityMedium,
|
|
|
|
@"accessibilityLarge": UIContentSizeCategoryAccessibilityLarge,
|
|
|
|
@"accessibilityExtraLarge": UIContentSizeCategoryAccessibilityExtraLarge,
|
|
|
|
@"accessibilityExtraExtraLarge": UIContentSizeCategoryAccessibilityExtraExtraLarge,
|
|
|
|
@"accessibilityExtraExtraExtraLarge": UIContentSizeCategoryAccessibilityExtraExtraExtraLarge};
|
|
|
|
});
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSString *)UIKitCategoryFromJSCategory:(NSString *)JSCategory
|
|
|
|
{
|
|
|
|
return self.JSToUIKitMap[JSCategory];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)init
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self) {
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(didReceiveNewContentSizeCategory:)
|
|
|
|
name:UIContentSizeCategoryDidChangeNotification
|
|
|
|
object:[UIApplication sharedApplication]];
|
2015-08-24 10:14:33 +00:00
|
|
|
self.contentSizeCategory = [UIApplication sharedApplication].preferredContentSizeCategory;
|
2015-07-31 14:37:12 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)didReceiveNewContentSizeCategory:(NSNotification *)note
|
|
|
|
{
|
|
|
|
self.contentSizeCategory = note.userInfo[UIContentSizeCategoryNewValueKey];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setContentSizeCategory:(NSString *)contentSizeCategory
|
|
|
|
{
|
|
|
|
if (_contentSizeCategory != contentSizeCategory) {
|
|
|
|
_contentSizeCategory = [contentSizeCategory copy];
|
2015-08-07 09:32:00 +00:00
|
|
|
[self invalidateMultiplier];
|
2015-07-31 14:37:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-07 09:32:00 +00:00
|
|
|
- (void)invalidateMultiplier
|
|
|
|
{
|
|
|
|
self.multiplier = [self multiplierForContentSizeCategory:_contentSizeCategory];
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:RCTAccessibilityManagerDidUpdateMultiplierNotification object:self];
|
|
|
|
}
|
|
|
|
|
2015-07-31 14:37:12 +00:00
|
|
|
- (CGFloat)multiplierForContentSizeCategory:(NSString *)category
|
|
|
|
{
|
|
|
|
NSNumber *m = self.multipliers[category];
|
|
|
|
if (m.doubleValue <= 0.0) {
|
|
|
|
RCTLogError(@"Can't determinte multiplier for category %@. Using 1.0.", category);
|
|
|
|
m = @1.0;
|
|
|
|
}
|
|
|
|
return m.doubleValue;
|
|
|
|
}
|
|
|
|
|
2015-08-07 09:32:00 +00:00
|
|
|
- (void)setMultipliers:(NSDictionary *)multipliers
|
|
|
|
{
|
|
|
|
if (_multipliers != multipliers) {
|
|
|
|
_multipliers = [multipliers copy];
|
|
|
|
[self invalidateMultiplier];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 14:37:12 +00:00
|
|
|
- (NSDictionary *)multipliers
|
|
|
|
{
|
|
|
|
if (_multipliers == nil) {
|
|
|
|
_multipliers = @{UIContentSizeCategoryExtraSmall: @0.823,
|
|
|
|
UIContentSizeCategorySmall: @0.882,
|
|
|
|
UIContentSizeCategoryMedium: @0.941,
|
|
|
|
UIContentSizeCategoryLarge: @1.0,
|
|
|
|
UIContentSizeCategoryExtraLarge: @1.118,
|
|
|
|
UIContentSizeCategoryExtraExtraLarge: @1.235,
|
|
|
|
UIContentSizeCategoryExtraExtraExtraLarge: @1.353,
|
|
|
|
UIContentSizeCategoryAccessibilityMedium: @1.786,
|
|
|
|
UIContentSizeCategoryAccessibilityLarge: @2.143,
|
|
|
|
UIContentSizeCategoryAccessibilityExtraLarge: @2.643,
|
|
|
|
UIContentSizeCategoryAccessibilityExtraExtraLarge: @3.143,
|
|
|
|
UIContentSizeCategoryAccessibilityExtraExtraExtraLarge: @3.571};
|
|
|
|
}
|
|
|
|
return _multipliers;
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(setAccessibilityContentSizeMultipliers:(NSDictionary *)JSMultipliers)
|
|
|
|
{
|
2015-08-17 14:35:34 +00:00
|
|
|
NSMutableDictionary *multipliers = [NSMutableDictionary new];
|
2015-07-31 14:37:12 +00:00
|
|
|
for (NSString *__nonnull JSCategory in JSMultipliers) {
|
|
|
|
NSNumber *m = JSMultipliers[JSCategory];
|
|
|
|
NSString *UIKitCategory = [self.class UIKitCategoryFromJSCategory:JSCategory];
|
|
|
|
multipliers[UIKitCategory] = m;
|
|
|
|
}
|
|
|
|
self.multipliers = multipliers;
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(getMultiplier:(RCTResponseSenderBlock)callback)
|
|
|
|
{
|
|
|
|
if (callback) {
|
|
|
|
callback(@[ @(self.multiplier) ]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTBridge (RCTAccessibilityManager)
|
|
|
|
|
|
|
|
- (RCTAccessibilityManager *)accessibilityManager
|
|
|
|
{
|
|
|
|
return self.modules[RCTBridgeModuleNameForClass([RCTAccessibilityManager class])];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|