-
Notifications
You must be signed in to change notification settings - Fork 24.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
iOS: Introduce API for making screen reader announcements #14168
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,9 +18,11 @@ var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter'); | |
var AccessibilityManager = NativeModules.AccessibilityManager; | ||
|
||
var VOICE_OVER_EVENT = 'voiceOverDidChange'; | ||
var ANNOUNCEMENT_DID_FINISH_EVENT = 'announcementDidFinish'; | ||
|
||
type ChangeEventName = $Enum<{ | ||
change: string, | ||
announcementFinished: string | ||
}>; | ||
|
||
var _subscriptions = new Map(); | ||
|
@@ -97,15 +99,30 @@ var AccessibilityInfo = { | |
* - `change`: Fires when the state of the screen reader changes. The argument | ||
* to the event handler is a boolean. The boolean is `true` when a screen | ||
* reader is enabled and `false` otherwise. | ||
* - `announcementFinished`: iOS-only event. Fires when the screen reader has | ||
* finished making an announcement. The argument to the event handler is a dictionary | ||
* with these keys: | ||
* - `announcement`: The string announced by the screen reader. | ||
* - `success`: A boolean indicating whether the announcement was successfully made. | ||
*/ | ||
addEventListener: function ( | ||
eventName: ChangeEventName, | ||
handler: Function | ||
): Object { | ||
var listener = RCTDeviceEventEmitter.addListener( | ||
VOICE_OVER_EVENT, | ||
handler | ||
); | ||
var listener; | ||
|
||
if (eventName === 'change') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have to unify native event names with JS ones, this will drastically simplify this code fragment. |
||
listener = RCTDeviceEventEmitter.addListener( | ||
VOICE_OVER_EVENT, | ||
handler | ||
); | ||
} else if (eventName === 'announcementFinished') { | ||
listener = RCTDeviceEventEmitter.addListener( | ||
ANNOUNCEMENT_DID_FINISH_EVENT, | ||
handler | ||
); | ||
} | ||
|
||
_subscriptions.set(handler, listener); | ||
return { | ||
remove: AccessibilityInfo.removeEventListener.bind(null, eventName, handler), | ||
|
@@ -121,6 +138,15 @@ var AccessibilityInfo = { | |
AccessibilityManager.setAccessibilityFocus(reactTag); | ||
}, | ||
|
||
/** | ||
* iOS-Only. Post a string to be announced by the screen reader. | ||
*/ | ||
announceForAccessibility: function( | ||
announcement: string | ||
): void { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need |
||
AccessibilityManager.announceForAccessibility(announcement); | ||
}, | ||
|
||
/** | ||
* Remove an event handler. | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,11 @@ - (instancetype)init | |
selector:@selector(didReceiveNewVoiceOverStatus:) | ||
name:UIAccessibilityVoiceOverStatusChanged | ||
object:nil]; | ||
|
||
[[NSNotificationCenter defaultCenter] addObserver:self | ||
selector:@selector(accessibilityAnnouncementDidFinish:) | ||
name:UIAccessibilityAnnouncementDidFinishNotification | ||
object:nil]; | ||
|
||
self.contentSizeCategory = RCTSharedApplication().preferredContentSizeCategory; | ||
_isVoiceOverEnabled = UIAccessibilityIsVoiceOverRunning(); | ||
|
@@ -101,6 +106,20 @@ - (void)didReceiveNewVoiceOverStatus:(__unused NSNotification *)notification | |
} | ||
} | ||
|
||
- (void)accessibilityAnnouncementDidFinish:(__unused NSNotification *)notification | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove |
||
{ | ||
NSDictionary *userInfo = notification.userInfo; | ||
// Response dictionary to populate the event with. | ||
NSDictionary *response = @{@"announcement": userInfo[UIAccessibilityAnnouncementKeyStringValue], | ||
@"success": userInfo[UIAccessibilityAnnouncementKeyWasSuccessful]}; | ||
|
||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Wdeprecated-declarations" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, this is deprecated. We have to use typed event objects instead of strings. |
||
[_bridge.eventDispatcher sendDeviceEventWithName:@"announcementDidFinish" | ||
body:response]; | ||
#pragma clang diagnostic pop | ||
} | ||
|
||
- (void)setContentSizeCategory:(NSString *)contentSizeCategory | ||
{ | ||
if (_contentSizeCategory != contentSizeCategory) { | ||
|
@@ -171,6 +190,11 @@ - (void)setMultipliers:(NSDictionary<NSString *, NSNumber *> *)multipliers | |
}); | ||
} | ||
|
||
RCT_EXPORT_METHOD(announceForAccessibility:(NSString *)announcement) | ||
{ | ||
UIAccessibilityPostNotification(UIAccessibilityAnnouncementNotification, announcement); | ||
} | ||
|
||
RCT_EXPORT_METHOD(getMultiplier:(RCTResponseSenderBlock)callback) | ||
{ | ||
if (callback) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no-trailing-spaces: Trailing spaces not allowed.