From b7d7981b682802b9127832ddc14a1b0cabc6c0ac Mon Sep 17 00:00:00 2001 From: Gabriel Donadel Dall'Agnol Date: Fri, 21 Jan 2022 18:57:10 -0300 Subject: [PATCH 1/4] feat: Exposes iOS prefersCrossFadeTransitions --- .../AccessibilityInfo/AccessibilityInfo.js | 25 +++++++++++++++++ .../NativeAccessibilityManager.js | 4 +++ React/CoreModules/RCTAccessibilityManager.h | 1 + React/CoreModules/RCTAccessibilityManager.mm | 28 +++++++++++++++++++ .../Accessibility/AccessibilityExample.js | 5 ++++ 5 files changed, 63 insertions(+) diff --git a/Libraries/Components/AccessibilityInfo/AccessibilityInfo.js b/Libraries/Components/AccessibilityInfo/AccessibilityInfo.js index dc13f6dcdcd40e..00efaa6db5cd2a 100644 --- a/Libraries/Components/AccessibilityInfo/AccessibilityInfo.js +++ b/Libraries/Components/AccessibilityInfo/AccessibilityInfo.js @@ -179,6 +179,31 @@ const AccessibilityInfo: AccessibilityInfoType = { }); }, + /** + * Query whether reduce motion and prefer cross-fade transitions settings are currently enabled. + * + * Returns a promise which resolves to a boolean. + * The result is `true` when prefer cross-fade transitions is enabled and `false` otherwise. + * + * See https://reactnative.dev/docs/accessibilityinfo#prefersCrossFadeTransitions + */ + prefersCrossFadeTransitions(): Promise { + return new Promise((resolve, reject) => { + if (Platform.OS === 'android') { + return Promise.resolve(false); + } else { + if (NativeAccessibilityManagerIOS != null) { + NativeAccessibilityManagerIOS.getCurrentPrefersCrossFadeTransitionsState( + resolve, + reject, + ); + } else { + reject(null); + } + } + }); + }, + /** * Query whether reduced transparency is currently enabled. * diff --git a/Libraries/Components/AccessibilityInfo/NativeAccessibilityManager.js b/Libraries/Components/AccessibilityInfo/NativeAccessibilityManager.js index d4c400ac3e99c3..17766c03a41a06 100644 --- a/Libraries/Components/AccessibilityInfo/NativeAccessibilityManager.js +++ b/Libraries/Components/AccessibilityInfo/NativeAccessibilityManager.js @@ -28,6 +28,10 @@ export interface Spec extends TurboModule { onSuccess: (isReduceMotionEnabled: boolean) => void, onError: (error: Object) => void, ) => void; + +getCurrentPrefersCrossFadeTransitionsState: ( + onSuccess: (prefersCrossFadeTransitions: boolean) => void, + onError: (error: Object) => void, + ) => void; +getCurrentReduceTransparencyState: ( onSuccess: (isReduceTransparencyEnabled: boolean) => void, onError: (error: Object) => void, diff --git a/React/CoreModules/RCTAccessibilityManager.h b/React/CoreModules/RCTAccessibilityManager.h index 77f7388aacc551..707d5766dd4c53 100644 --- a/React/CoreModules/RCTAccessibilityManager.h +++ b/React/CoreModules/RCTAccessibilityManager.h @@ -23,6 +23,7 @@ extern NSString *const RCTAccessibilityManagerDidUpdateMultiplierNotification; / @property (nonatomic, assign) BOOL isGrayscaleEnabled; @property (nonatomic, assign) BOOL isInvertColorsEnabled; @property (nonatomic, assign) BOOL isReduceMotionEnabled; +@property (nonatomic, assign) BOOL prefersCrossFadeTransitions; @property (nonatomic, assign) BOOL isReduceTransparencyEnabled; @property (nonatomic, assign) BOOL isVoiceOverEnabled; diff --git a/React/CoreModules/RCTAccessibilityManager.mm b/React/CoreModules/RCTAccessibilityManager.mm index 33b0501f694396..bdeb808d44d1eb 100644 --- a/React/CoreModules/RCTAccessibilityManager.mm +++ b/React/CoreModules/RCTAccessibilityManager.mm @@ -76,6 +76,13 @@ - (instancetype)init name:UIAccessibilityReduceMotionStatusDidChangeNotification object:nil]; + if (@available(iOS 14.0, *)) { + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(prefersCrossFadeTransitionsStatusDidChange:) + name:UIAccessibilityPrefersCrossFadeTransitionsStatusDidChangeNotification + object:nil]; + } + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reduceTransparencyStatusDidChange:) name:UIAccessibilityReduceTransparencyStatusDidChangeNotification @@ -91,6 +98,7 @@ - (instancetype)init _isGrayscaleEnabled = UIAccessibilityIsGrayscaleEnabled(); _isInvertColorsEnabled = UIAccessibilityIsInvertColorsEnabled(); _isReduceMotionEnabled = UIAccessibilityIsReduceMotionEnabled(); + _prefersCrossFadeTransitions = UIAccessibilityPrefersCrossFadeTransitions(); _isReduceTransparencyEnabled = UIAccessibilityIsReduceTransparencyEnabled(); _isVoiceOverEnabled = UIAccessibilityIsVoiceOverRunning(); } @@ -169,6 +177,19 @@ - (void)reduceMotionStatusDidChange:(__unused NSNotification *)notification } } +- (void)prefersCrossFadeTransitionsStatusDidChange:(__unused NSNotification *)notification +{ + BOOL newPrefersCrossFadeTransitionsEnabled = UIAccessibilityPrefersCrossFadeTransitions(); + if (_prefersCrossFadeTransitions != newPrefersCrossFadeTransitionsEnabled) { + _prefersCrossFadeTransitions = newPrefersCrossFadeTransitionsEnabled; +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + [[_moduleRegistry moduleForName:"EventDispatcher"] sendDeviceEventWithName:@"prefersCrossFadeTransitionsChanged" + body:@(_prefersCrossFadeTransitions)]; +#pragma clang diagnostic pop + } +} + - (void)reduceTransparencyStatusDidChange:(__unused NSNotification *)notification { BOOL newReduceTransparencyEnabled = UIAccessibilityIsReduceTransparencyEnabled(); @@ -358,6 +379,13 @@ static void setMultipliers( onSuccess(@[ @(_isReduceMotionEnabled) ]); } +RCT_EXPORT_METHOD(getCurrentPrefersCrossFadeTransitionsState + : (RCTResponseSenderBlock)onSuccess onError + : (__unused RCTResponseSenderBlock)onError) +{ + onSuccess(@[ @(_prefersCrossFadeTransitions) ]); +} + RCT_EXPORT_METHOD(getCurrentReduceTransparencyState : (RCTResponseSenderBlock)onSuccess onError : (__unused RCTResponseSenderBlock)onError) diff --git a/packages/rn-tester/js/examples/Accessibility/AccessibilityExample.js b/packages/rn-tester/js/examples/Accessibility/AccessibilityExample.js index 271a3db44bc7af..e3c6e333300ea5 100644 --- a/packages/rn-tester/js/examples/Accessibility/AccessibilityExample.js +++ b/packages/rn-tester/js/examples/Accessibility/AccessibilityExample.js @@ -1183,6 +1183,11 @@ class DisplayOptionsStatusExample extends React.Component<{}> { optionChecker={AccessibilityInfo.isReduceMotionEnabled} notification={'reduceMotionChanged'} /> + Date: Thu, 17 Feb 2022 19:42:33 -0300 Subject: [PATCH 2/4] chore: remove UIAccessibilityPrefersCrossFadeTransitionsStatusDidChangeNotification selector --- React/CoreModules/RCTAccessibilityManager.mm | 28 +++++--------------- jest/setup.js | 1 + 2 files changed, 7 insertions(+), 22 deletions(-) diff --git a/React/CoreModules/RCTAccessibilityManager.mm b/React/CoreModules/RCTAccessibilityManager.mm index bdeb808d44d1eb..f8878748eaefe3 100644 --- a/React/CoreModules/RCTAccessibilityManager.mm +++ b/React/CoreModules/RCTAccessibilityManager.mm @@ -76,13 +76,6 @@ - (instancetype)init name:UIAccessibilityReduceMotionStatusDidChangeNotification object:nil]; - if (@available(iOS 14.0, *)) { - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(prefersCrossFadeTransitionsStatusDidChange:) - name:UIAccessibilityPrefersCrossFadeTransitionsStatusDidChangeNotification - object:nil]; - } - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reduceTransparencyStatusDidChange:) name:UIAccessibilityReduceTransparencyStatusDidChangeNotification @@ -98,7 +91,6 @@ - (instancetype)init _isGrayscaleEnabled = UIAccessibilityIsGrayscaleEnabled(); _isInvertColorsEnabled = UIAccessibilityIsInvertColorsEnabled(); _isReduceMotionEnabled = UIAccessibilityIsReduceMotionEnabled(); - _prefersCrossFadeTransitions = UIAccessibilityPrefersCrossFadeTransitions(); _isReduceTransparencyEnabled = UIAccessibilityIsReduceTransparencyEnabled(); _isVoiceOverEnabled = UIAccessibilityIsVoiceOverRunning(); } @@ -177,19 +169,6 @@ - (void)reduceMotionStatusDidChange:(__unused NSNotification *)notification } } -- (void)prefersCrossFadeTransitionsStatusDidChange:(__unused NSNotification *)notification -{ - BOOL newPrefersCrossFadeTransitionsEnabled = UIAccessibilityPrefersCrossFadeTransitions(); - if (_prefersCrossFadeTransitions != newPrefersCrossFadeTransitionsEnabled) { - _prefersCrossFadeTransitions = newPrefersCrossFadeTransitionsEnabled; -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - [[_moduleRegistry moduleForName:"EventDispatcher"] sendDeviceEventWithName:@"prefersCrossFadeTransitionsChanged" - body:@(_prefersCrossFadeTransitions)]; -#pragma clang diagnostic pop - } -} - - (void)reduceTransparencyStatusDidChange:(__unused NSNotification *)notification { BOOL newReduceTransparencyEnabled = UIAccessibilityIsReduceTransparencyEnabled(); @@ -383,7 +362,12 @@ static void setMultipliers( : (RCTResponseSenderBlock)onSuccess onError : (__unused RCTResponseSenderBlock)onError) { - onSuccess(@[ @(_prefersCrossFadeTransitions) ]); + + if (@available(iOS 14.0, *)) { + onSuccess(@[ @(UIAccessibilityPrefersCrossFadeTransitions()) ]); + } else { + onSuccess(@[ @(false) ]); + } } RCT_EXPORT_METHOD(getCurrentReduceTransparencyState diff --git a/jest/setup.js b/jest/setup.js index 258948e851e4ed..e422cb64460d95 100644 --- a/jest/setup.js +++ b/jest/setup.js @@ -128,6 +128,7 @@ jest isGrayscaleEnabled: jest.fn(), isInvertColorsEnabled: jest.fn(), isReduceMotionEnabled: jest.fn(), + prefersCrossFadeTransitions: jest.fn(), isReduceTransparencyEnabled: jest.fn(), isScreenReaderEnabled: jest.fn(() => Promise.resolve(false)), setAccessibilityFocus: jest.fn(), From d289d8f7c29c9f2416bc61173384ceb6caf4d69f Mon Sep 17 00:00:00 2001 From: Gabriel Donadel Dall'Agnol Date: Fri, 19 Aug 2022 19:43:34 -0300 Subject: [PATCH 3/4] chore: Update getCurrentPrefersCrossFadeTransitionsState method to be optional --- Libraries/Components/AccessibilityInfo/AccessibilityInfo.js | 5 ++++- .../AccessibilityInfo/NativeAccessibilityManager.js | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Libraries/Components/AccessibilityInfo/AccessibilityInfo.js b/Libraries/Components/AccessibilityInfo/AccessibilityInfo.js index 00efaa6db5cd2a..1f116ddcf1795e 100644 --- a/Libraries/Components/AccessibilityInfo/AccessibilityInfo.js +++ b/Libraries/Components/AccessibilityInfo/AccessibilityInfo.js @@ -192,7 +192,10 @@ const AccessibilityInfo: AccessibilityInfoType = { if (Platform.OS === 'android') { return Promise.resolve(false); } else { - if (NativeAccessibilityManagerIOS != null) { + if ( + NativeAccessibilityManagerIOS?.getCurrentPrefersCrossFadeTransitionsState != + null + ) { NativeAccessibilityManagerIOS.getCurrentPrefersCrossFadeTransitionsState( resolve, reject, diff --git a/Libraries/Components/AccessibilityInfo/NativeAccessibilityManager.js b/Libraries/Components/AccessibilityInfo/NativeAccessibilityManager.js index 17766c03a41a06..a8dfc7147e1ffb 100644 --- a/Libraries/Components/AccessibilityInfo/NativeAccessibilityManager.js +++ b/Libraries/Components/AccessibilityInfo/NativeAccessibilityManager.js @@ -28,7 +28,7 @@ export interface Spec extends TurboModule { onSuccess: (isReduceMotionEnabled: boolean) => void, onError: (error: Object) => void, ) => void; - +getCurrentPrefersCrossFadeTransitionsState: ( + +getCurrentPrefersCrossFadeTransitionsState?: ( onSuccess: (prefersCrossFadeTransitions: boolean) => void, onError: (error: Object) => void, ) => void; From 5c81481d224d1247e19747213a2bb3a0d0ec8bc1 Mon Sep 17 00:00:00 2001 From: Gabriel Donadel Dall'Agnol Date: Sat, 20 Aug 2022 16:06:58 -0300 Subject: [PATCH 4/4] feat: Add prefersCrossFadeTransitions to flow --- .../AccessibilityInfo/AccessibilityInfo.flow.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Libraries/Components/AccessibilityInfo/AccessibilityInfo.flow.js b/Libraries/Components/AccessibilityInfo/AccessibilityInfo.flow.js index 8b38c926507b6c..60c3ec53cd3f44 100644 --- a/Libraries/Components/AccessibilityInfo/AccessibilityInfo.flow.js +++ b/Libraries/Components/AccessibilityInfo/AccessibilityInfo.flow.js @@ -85,6 +85,16 @@ export interface AccessibilityInfo { */ isReduceMotionEnabled: () => Promise; + /** + * Query whether reduce motion and prefer cross-fade transitions settings are currently enabled. + * + * Returns a promise which resolves to a boolean. + * The result is `true` when prefer cross-fade transitions is enabled and `false` otherwise. + * + * See https://reactnative.dev/docs/accessibilityinfo#prefersCrossFadeTransitions + */ + prefersCrossFadeTransitions: () => Promise; + /** * Query whether reduced transparency is currently enabled. *