Support passing escape item to TouchBar constructor

This commit is contained in:
Kevin Sawicki 2017-04-03 15:12:57 -07:00
parent b24b4212c5
commit 414540bfcb
3 changed files with 28 additions and 9 deletions

View file

@ -4,9 +4,11 @@
Process: [Main](../tutorial/quick-start.md#main-process)
### `new TouchBar(items)` _Experimental_
### `new TouchBar(options)` _Experimental_
* `items` ([TouchBarButton](touch-bar-button.md) | [TouchBarColorPicker](touch-bar-color-picker.md) | [TouchBarGroup](touch-bar-group.md) | [TouchBarLabel](touch-bar-label.md) | [TouchBarPopover](touch-bar-popover.md) | [TouchBarScrubber](touch-bar-scrubber.md) | [TouchBarSegmentedControl](touch-bar-segmented-control.md) | [TouchBarSlider](touch-bar-slider.md) | [TouchBarSpacer](touch-bar-spacer.md))[]
* `options` - Object
* `items` ([TouchBarButton](touch-bar-button.md) | [TouchBarColorPicker](touch-bar-color-picker.md) | [TouchBarGroup](touch-bar-group.md) | [TouchBarLabel](touch-bar-label.md) | [TouchBarPopover](touch-bar-popover.md) | [TouchBarScrubber](touch-bar-scrubber.md) | [TouchBarSegmentedControl](touch-bar-segmented-control.md) | [TouchBarSlider](touch-bar-slider.md) | [TouchBarSpacer](touch-bar-spacer.md))[]
* `escapeItem` ([TouchBarButton](touch-bar-button.md) | [TouchBarColorPicker](touch-bar-color-picker.md) | [TouchBarGroup](touch-bar-group.md) | [TouchBarLabel](touch-bar-label.md) | [TouchBarPopover](touch-bar-popover.md) | [TouchBarScrubber](touch-bar-scrubber.md) | [TouchBarSegmentedControl](touch-bar-segmented-control.md) | [TouchBarSlider](touch-bar-slider.md) | [TouchBarSpacer](touch-bar-spacer.md)) (optional)
Creates a new touch bar with the specified items. Use
`BrowserWindow.setTouchBar` to add the `TouchBar` to a window.

View file

@ -20,17 +20,29 @@ class TouchBar extends EventEmitter {
touchBar._addToWindow(window)
}
constructor (items) {
constructor (options) {
super()
if (options == null) {
throw new Error('Must specify options object as first argument')
}
let {items, escapeItem} = options
// FIXME Support array as first argument, remove in 2.0
if (Array.isArray(options)) {
items = options
escapeItem = null
}
if (!Array.isArray(items)) {
throw new Error('Must specify items array as first argument')
items = []
}
this.windowListeners = {}
this.items = {}
this.ordereredItems = []
this.escapeItem = null
this.escapeItem = escapeItem
this.changeListener = (item) => {
this.emit('change', item.id, item.type)
}

View file

@ -6,23 +6,28 @@ const {TouchBarButton, TouchBarColorPicker, TouchBarGroup} = TouchBar
const {TouchBarLabel, TouchBarPopover, TouchBarScrubber, TouchBarSegmentedControl, TouchBarSlider, TouchBarSpacer} = TouchBar
describe('TouchBar module', function () {
it('throws an error when created without an items array', function () {
it('throws an error when created without an options object', function () {
assert.throws(() => {
const touchBar = new TouchBar()
touchBar.toString()
}, /Must specify items array as first argument/)
}, /Must specify options object as first argument/)
})
it('throws an error when created with invalid items', function () {
assert.throws(() => {
const touchBar = new TouchBar([1, true, {}, []])
const touchBar = new TouchBar({items: [1, true, {}, []]})
touchBar.toString()
}, /Each item must be an instance of TouchBarItem/)
})
it('throws an error when an invalid escape item is set', function () {
assert.throws(() => {
const touchBar = new TouchBar([])
const touchBar = new TouchBar({items: [], escapeItem: 'esc'})
touchBar.toString()
}, /Escape item must be an instance of TouchBarItem/)
assert.throws(() => {
const touchBar = new TouchBar({items: []})
touchBar.escapeItem = 'esc'
}, /Escape item must be an instance of TouchBarItem/)
})