2015-07-31 17:37:12 +03: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"
|
|
|
|
|
2017-05-26 13:07:49 +03:00
|
|
|
#import "RCTUIManager.h"
|
2015-10-02 04:07:06 +03:00
|
|
|
#import "RCTBridge.h"
|
2015-11-14 21:25:00 +03:00
|
|
|
#import "RCTConvert.h"
|
2015-10-02 04:07:06 +03:00
|
|
|
#import "RCTEventDispatcher.h"
|
2015-07-31 17:37:12 +03:00
|
|
|
#import "RCTLog.h"
|
|
|
|
|
|
|
|
NSString *const RCTAccessibilityManagerDidUpdateMultiplierNotification = @"RCTAccessibilityManagerDidUpdateMultiplierNotification";
|
|
|
|
|
2017-08-07 16:45:24 +03:00
|
|
|
static NSString *UIKitCategoryFromJSCategory(NSString *JSCategory)
|
2015-07-31 17:37:12 +03:00
|
|
|
{
|
|
|
|
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};
|
|
|
|
});
|
2017-08-07 16:45:24 +03:00
|
|
|
return map[JSCategory];
|
2015-07-31 17:37:12 +03:00
|
|
|
}
|
|
|
|
|
2017-08-07 16:45:24 +03:00
|
|
|
@interface RCTAccessibilityManager ()
|
|
|
|
|
|
|
|
@property (nonatomic, copy) NSString *contentSizeCategory;
|
|
|
|
@property (nonatomic, assign) CGFloat multiplier;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTAccessibilityManager
|
|
|
|
|
|
|
|
@synthesize bridge = _bridge;
|
|
|
|
@synthesize multipliers = _multipliers;
|
|
|
|
|
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
|
|
|
+ (BOOL)requiresMainQueueSetup
|
2015-07-31 17:37:12 +03:00
|
|
|
{
|
2017-08-07 16:45:24 +03:00
|
|
|
return YES;
|
2015-07-31 17:37:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)init
|
|
|
|
{
|
2015-11-14 21:25:00 +03:00
|
|
|
if ((self = [super init])) {
|
2015-11-25 14:09:00 +03:00
|
|
|
|
|
|
|
// TODO: can this be moved out of the startup path?
|
2015-07-31 17:37:12 +03:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(didReceiveNewContentSizeCategory:)
|
|
|
|
name:UIContentSizeCategoryDidChangeNotification
|
2017-04-01 13:16:52 +03:00
|
|
|
object:RCTSharedApplication()];
|
2015-10-02 04:07:06 +03:00
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(didReceiveNewVoiceOverStatus:)
|
|
|
|
name:UIAccessibilityVoiceOverStatusChanged
|
|
|
|
object:nil];
|
2017-08-07 16:45:24 +03:00
|
|
|
|
2017-05-30 14:35:52 +03:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(accessibilityAnnouncementDidFinish:)
|
|
|
|
name:UIAccessibilityAnnouncementDidFinishNotification
|
|
|
|
object:nil];
|
2015-10-02 04:07:06 +03:00
|
|
|
|
2017-04-01 13:16:52 +03:00
|
|
|
self.contentSizeCategory = RCTSharedApplication().preferredContentSizeCategory;
|
2015-10-02 04:07:06 +03:00
|
|
|
_isVoiceOverEnabled = UIAccessibilityIsVoiceOverRunning();
|
2015-07-31 17:37:12 +03:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)didReceiveNewContentSizeCategory:(NSNotification *)note
|
|
|
|
{
|
|
|
|
self.contentSizeCategory = note.userInfo[UIContentSizeCategoryNewValueKey];
|
|
|
|
}
|
|
|
|
|
2015-10-08 15:34:26 +03:00
|
|
|
- (void)didReceiveNewVoiceOverStatus:(__unused NSNotification *)notification
|
2015-10-02 04:07:06 +03:00
|
|
|
{
|
|
|
|
BOOL newIsVoiceOverEnabled = UIAccessibilityIsVoiceOverRunning();
|
|
|
|
if (_isVoiceOverEnabled != newIsVoiceOverEnabled) {
|
|
|
|
_isVoiceOverEnabled = newIsVoiceOverEnabled;
|
2016-05-25 14:17:35 +03:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
2015-10-02 04:07:06 +03:00
|
|
|
[_bridge.eventDispatcher sendDeviceEventWithName:@"voiceOverDidChange"
|
|
|
|
body:@(_isVoiceOverEnabled)];
|
2016-05-25 14:17:35 +03:00
|
|
|
#pragma clang diagnostic pop
|
2015-10-02 04:07:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-30 14:35:52 +03:00
|
|
|
- (void)accessibilityAnnouncementDidFinish:(__unused NSNotification *)notification
|
|
|
|
{
|
|
|
|
NSDictionary *userInfo = notification.userInfo;
|
|
|
|
// Response dictionary to populate the event with.
|
|
|
|
NSDictionary *response = @{@"announcement": userInfo[UIAccessibilityAnnouncementKeyStringValue],
|
|
|
|
@"success": userInfo[UIAccessibilityAnnouncementKeyWasSuccessful]};
|
2017-08-07 16:45:24 +03:00
|
|
|
|
2017-05-30 14:35:52 +03:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
|
|
|
[_bridge.eventDispatcher sendDeviceEventWithName:@"announcementDidFinish"
|
|
|
|
body:response];
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
}
|
|
|
|
|
2015-07-31 17:37:12 +03:00
|
|
|
- (void)setContentSizeCategory:(NSString *)contentSizeCategory
|
|
|
|
{
|
|
|
|
if (_contentSizeCategory != contentSizeCategory) {
|
|
|
|
_contentSizeCategory = [contentSizeCategory copy];
|
2015-08-07 12:32:00 +03:00
|
|
|
[self invalidateMultiplier];
|
2015-07-31 17:37:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-07 12:32:00 +03:00
|
|
|
- (void)invalidateMultiplier
|
|
|
|
{
|
|
|
|
self.multiplier = [self multiplierForContentSizeCategory:_contentSizeCategory];
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:RCTAccessibilityManagerDidUpdateMultiplierNotification object:self];
|
|
|
|
}
|
|
|
|
|
2015-07-31 17:37:12 +03: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-11-14 21:25:00 +03:00
|
|
|
- (void)setMultipliers:(NSDictionary<NSString *, NSNumber *> *)multipliers
|
2015-08-07 12:32:00 +03:00
|
|
|
{
|
|
|
|
if (_multipliers != multipliers) {
|
|
|
|
_multipliers = [multipliers copy];
|
|
|
|
[self invalidateMultiplier];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-14 21:25:00 +03:00
|
|
|
- (NSDictionary<NSString *, NSNumber *> *)multipliers
|
2015-07-31 17:37:12 +03:00
|
|
|
{
|
|
|
|
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-11-14 21:25:00 +03:00
|
|
|
NSMutableDictionary<NSString *, NSNumber *> *multipliers = [NSMutableDictionary new];
|
2015-07-31 17:37:12 +03:00
|
|
|
for (NSString *__nonnull JSCategory in JSMultipliers) {
|
2015-11-14 21:25:00 +03:00
|
|
|
NSNumber *m = [RCTConvert NSNumber:JSMultipliers[JSCategory]];
|
2017-08-07 16:45:24 +03:00
|
|
|
NSString *UIKitCategory = UIKitCategoryFromJSCategory(JSCategory);
|
2015-07-31 17:37:12 +03:00
|
|
|
multipliers[UIKitCategory] = m;
|
|
|
|
}
|
|
|
|
self.multipliers = multipliers;
|
|
|
|
}
|
|
|
|
|
2017-05-26 13:07:49 +03:00
|
|
|
RCT_EXPORT_METHOD(setAccessibilityFocus:(nonnull NSNumber *)reactTag)
|
|
|
|
{
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
UIView *view = [self.bridge.uiManager viewForReactTag:reactTag];
|
|
|
|
UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, view);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-30 14:35:52 +03:00
|
|
|
RCT_EXPORT_METHOD(announceForAccessibility:(NSString *)announcement)
|
|
|
|
{
|
|
|
|
UIAccessibilityPostNotification(UIAccessibilityAnnouncementNotification, announcement);
|
|
|
|
}
|
|
|
|
|
2015-07-31 17:37:12 +03:00
|
|
|
RCT_EXPORT_METHOD(getMultiplier:(RCTResponseSenderBlock)callback)
|
|
|
|
{
|
|
|
|
if (callback) {
|
|
|
|
callback(@[ @(self.multiplier) ]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-02 04:07:06 +03:00
|
|
|
RCT_EXPORT_METHOD(getCurrentVoiceOverState:(RCTResponseSenderBlock)callback
|
|
|
|
error:(__unused RCTResponseSenderBlock)error)
|
|
|
|
{
|
|
|
|
callback(@[@(_isVoiceOverEnabled)]);
|
|
|
|
}
|
|
|
|
|
2015-07-31 17:37:12 +03:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTBridge (RCTAccessibilityManager)
|
|
|
|
|
|
|
|
- (RCTAccessibilityManager *)accessibilityManager
|
|
|
|
{
|
2015-11-25 14:09:00 +03:00
|
|
|
return [self moduleForClass:[RCTAccessibilityManager class]];
|
2015-07-31 17:37:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|