X Tutup
Skip to content

Commit c16a186

Browse files
codebytereJohn Kleinschmidt
authored andcommitted
feat: improve TouchBarButton accessibility (electron#20454)
1 parent 8da9a3c commit c16a186

File tree

4 files changed

+53
-32
lines changed

4 files changed

+53
-32
lines changed

docs/api/touch-bar-button.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,23 @@ Process: [Main](../tutorial/application-architecture.md#main-and-renderer-proces
88

99
* `options` Object
1010
* `label` String (optional) - Button text.
11+
* `accessibilityLabel` String (optional) - A short description of the button for use by screenreaders like VoiceOver.
1112
* `backgroundColor` String (optional) - Button background color in hex format,
1213
i.e `#ABCDEF`.
1314
* `icon` [NativeImage](native-image.md) | String (optional) - Button icon.
1415
* `iconPosition` String (optional) - Can be `left`, `right` or `overlay`. Defaults to `overlay`.
1516
* `click` Function (optional) - Function to call when the button is clicked.
1617

18+
When defining `accessibilityLabel`, ensure you have considered macOS [best practices](https://developer.apple.com/documentation/appkit/nsaccessibilitybutton/1524910-accessibilitylabel?language=objc).
19+
1720
### Instance Properties
1821

1922
The following properties are available on instances of `TouchBarButton`:
2023

24+
#### `touchBarButton.accessibilityLabel`
25+
26+
A `String` representing the description of the button to be read by a screen reader. Will only be read by screen readers if no label is set.
27+
2128
#### `touchBarButton.label`
2229

2330
A `String` representing the button's current text. Changing this value immediately updates the button

docs/api/touch-bar-label.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ Process: [Main](../tutorial/application-architecture.md#main-and-renderer-proces
88

99
* `options` Object
1010
* `label` String (optional) - Text to display.
11+
* `accessibilityLabel` String (optional) - A short description of the button for use by screenreaders like VoiceOver.
1112
* `textColor` String (optional) - Hex color of text, i.e `#ABCDEF`.
1213

14+
When defining `accessibilityLabel`, ensure you have considered macOS [best practices](https://developer.apple.com/documentation/appkit/nsaccessibilitybutton/1524910-accessibilitylabel?language=objc).
15+
1316
### Instance Properties
1417

1518
The following properties are available on instances of `TouchBarLabel`:
@@ -19,6 +22,10 @@ The following properties are available on instances of `TouchBarLabel`:
1922
A `String` representing the label's current text. Changing this value immediately updates the label in
2023
the touch bar.
2124

25+
#### `touchBarLabel.accessibilityLabel`
26+
27+
A `String` representing the description of the label to be read by a screen reader.
28+
2229
#### `touchBarLabel.textColor`
2330

2431
A `String` hex code representing the label's current text color. Changing this value immediately updates the

lib/browser/api/touch-bar.js

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,12 @@ TouchBar.TouchBarButton = class TouchBarButton extends TouchBarItem {
191191
super()
192192
if (config == null) config = {}
193193
this._addImmutableProperty('type', 'button')
194-
const { click, icon, iconPosition, label, backgroundColor } = config
195-
this._addLiveProperty('label', label)
196-
this._addLiveProperty('backgroundColor', backgroundColor)
197-
this._addLiveProperty('icon', icon)
198-
this._addLiveProperty('iconPosition', iconPosition)
199-
if (typeof click === 'function') {
194+
this._addLiveProperty('label', config.label)
195+
this._addLiveProperty('accessibilityLabel', config.accessibilityLabel)
196+
this._addLiveProperty('backgroundColor', config.backgroundColor)
197+
this._addLiveProperty('icon', config.icon)
198+
this._addLiveProperty('iconPosition', config.iconPosition)
199+
if (typeof config.click === 'function') {
200200
this._addImmutableProperty('onInteraction', () => {
201201
config.click()
202202
})
@@ -209,14 +209,13 @@ TouchBar.TouchBarColorPicker = class TouchBarColorPicker extends TouchBarItem {
209209
super()
210210
if (config == null) config = {}
211211
this._addImmutableProperty('type', 'colorpicker')
212-
const { availableColors, change, selectedColor } = config
213-
this._addLiveProperty('availableColors', availableColors)
214-
this._addLiveProperty('selectedColor', selectedColor)
212+
this._addLiveProperty('availableColors', config.availableColors)
213+
this._addLiveProperty('selectedColor', config.selectedColor)
215214

216-
if (typeof change === 'function') {
215+
if (typeof config.change === 'function') {
217216
this._addImmutableProperty('onInteraction', (details) => {
218217
this._selectedColor = details.color
219-
change(details.color)
218+
config.change(details.color)
220219
})
221220
}
222221
}
@@ -239,6 +238,7 @@ TouchBar.TouchBarLabel = class TouchBarLabel extends TouchBarItem {
239238
if (config == null) config = {}
240239
this._addImmutableProperty('type', 'label')
241240
this._addLiveProperty('label', config.label)
241+
this._addLiveProperty('accessibilityLabel', config.accessibilityLabel)
242242
this._addLiveProperty('textColor', config.textColor)
243243
}
244244
}
@@ -262,16 +262,15 @@ TouchBar.TouchBarSlider = class TouchBarSlider extends TouchBarItem {
262262
super()
263263
if (config == null) config = {}
264264
this._addImmutableProperty('type', 'slider')
265-
const { change, label, minValue, maxValue, value } = config
266-
this._addLiveProperty('label', label)
267-
this._addLiveProperty('minValue', minValue)
268-
this._addLiveProperty('maxValue', maxValue)
269-
this._addLiveProperty('value', value)
265+
this._addLiveProperty('label', config.label)
266+
this._addLiveProperty('minValue', config.minValue)
267+
this._addLiveProperty('maxValue', config.maxValue)
268+
this._addLiveProperty('value', config.value)
270269

271-
if (typeof change === 'function') {
270+
if (typeof config.change === 'function') {
272271
this._addImmutableProperty('onInteraction', (details) => {
273272
this._value = details.value
274-
change(details.value)
273+
config.change(details.value)
275274
})
276275
}
277276
}
@@ -290,17 +289,16 @@ TouchBar.TouchBarSegmentedControl = class TouchBarSegmentedControl extends Touch
290289
constructor (config) {
291290
super()
292291
if (config == null) config = {}
293-
const { segmentStyle, segments, selectedIndex, change, mode } = config
294292
this._addImmutableProperty('type', 'segmented_control')
295-
this._addLiveProperty('segmentStyle', segmentStyle)
296-
this._addLiveProperty('segments', segments || [])
297-
this._addLiveProperty('selectedIndex', selectedIndex)
298-
this._addLiveProperty('mode', mode)
293+
this._addLiveProperty('segmentStyle', config.segmentStyle)
294+
this._addLiveProperty('segments', config.segments || [])
295+
this._addLiveProperty('selectedIndex', config.selectedIndex)
296+
this._addLiveProperty('mode', config.mode)
299297

300-
if (typeof change === 'function') {
298+
if (typeof config.change === 'function') {
301299
this._addImmutableProperty('onInteraction', (details) => {
302300
this._selectedIndex = details.selectedIndex
303-
change(details.selectedIndex, details.isSelected)
301+
config.change(details.selectedIndex, details.isSelected)
304302
})
305303
}
306304
}
@@ -310,15 +308,16 @@ TouchBar.TouchBarScrubber = class TouchBarScrubber extends TouchBarItem {
310308
constructor (config) {
311309
super()
312310
if (config == null) config = {}
313-
const { items, selectedStyle, overlayStyle, showArrowButtons, continuous, mode } = config
314311
let { select, highlight } = config
315312
this._addImmutableProperty('type', 'scrubber')
316-
this._addLiveProperty('items', items)
317-
this._addLiveProperty('selectedStyle', selectedStyle || null)
318-
this._addLiveProperty('overlayStyle', overlayStyle || null)
319-
this._addLiveProperty('showArrowButtons', showArrowButtons || false)
320-
this._addLiveProperty('mode', mode || 'free')
321-
this._addLiveProperty('continuous', typeof continuous === 'undefined' ? true : continuous)
313+
this._addLiveProperty('items', config.items)
314+
this._addLiveProperty('selectedStyle', config.selectedStyle || null)
315+
this._addLiveProperty('overlayStyle', config.overlayStyle || null)
316+
this._addLiveProperty('showArrowButtons', config.showArrowButtons || false)
317+
this._addLiveProperty('mode', config.mode || 'free')
318+
319+
const cont = typeof config.continuous === 'undefined' ? true : config.continuous
320+
this._addLiveProperty('continuous', cont)
322321

323322
if (typeof select === 'function' || typeof highlight === 'function') {
324323
if (select == null) select = () => {}

shell/browser/ui/cocoa/atom_touch_bar.mm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,10 @@ - (void)updateButton:(NSCustomTouchBarItem*)item
363363
button.bezelColor = nil;
364364
}
365365

366+
std::string accessibilityLabel;
367+
settings.Get("accessibilityLabel", &accessibilityLabel);
368+
button.accessibilityLabel = base::SysUTF8ToNSString(accessibilityLabel);
369+
366370
std::string label;
367371
settings.Get("label", &label);
368372
button.title = base::SysUTF8ToNSString(label);
@@ -405,6 +409,10 @@ - (void)updateLabel:(NSCustomTouchBarItem*)item
405409
settings.Get("label", &label);
406410
text_field.stringValue = base::SysUTF8ToNSString(label);
407411

412+
std::string accessibilityLabel;
413+
settings.Get("accessibilityLabel", &accessibilityLabel);
414+
text_field.accessibilityLabel = base::SysUTF8ToNSString(accessibilityLabel);
415+
408416
std::string textColor;
409417
if (settings.Get("textColor", &textColor) && !textColor.empty()) {
410418
text_field.textColor = [self colorFromHexColorString:textColor];

0 commit comments

Comments
 (0)
X Tutup