diff --git a/docs/api/touch-bar.md b/docs/api/touch-bar.md index 54315d797733..a1d86794dd7b 100644 --- a/docs/api/touch-bar.md +++ b/docs/api/touch-bar.md @@ -14,16 +14,15 @@ Creates a new touch bar with the specified items. Use **Note:** The TouchBar API is currently experimental and may change or be removed in future Electron releases. -### Instance Methods +### Instance Properties -The following methods are available on instances of `TouchBar`: +The following properties are available on instances of `TouchBar`: -#### `touchBar.setEscapeItem([touchBarItem])` +#### `touchBar.escapeItem` -* `touchBarItem` (TouchBarButton | TouchBarColorPicker | TouchBarGroup | TouchBarLabel | TouchBarPopover | TouchBarScrubber | TouchBarSegmentedControl | TouchBarSlider | TouchBarSpacer) - (Optional) The touch bar item to replace the escape button with - -Replaces the "esc" button on the touchbar with the given TouchBarItem, if `touchBarItem` is not provided or is falsey the button is reset -to the "esc" button automatically. +The `TouchBarItem` that will replace the "esc" button on the touch bar when set. +Setting to `null` restores the default "esc" button. Changing this value +immediately updates the escape item in the touch bar. ## Examples diff --git a/lib/browser/api/touch-bar.js b/lib/browser/api/touch-bar.js index 2b95616f2217..cd806a5562be 100644 --- a/lib/browser/api/touch-bar.js +++ b/lib/browser/api/touch-bar.js @@ -2,8 +2,6 @@ const {EventEmitter} = require('events') let nextItemID = 1 -const DEFAULT_ESCAPE_ITEM = new EventEmitter() - class TouchBar extends EventEmitter { // Bind a touch bar to a window static _setOnWindow (touchBar, window) { @@ -32,7 +30,7 @@ class TouchBar extends EventEmitter { this.windowListeners = {} this.items = {} this.ordereredItems = [] - this.escapeItem = DEFAULT_ESCAPE_ITEM + this.escapeItem = null this.changeListener = (item) => { this.emit('change', item.id, item.type) } @@ -53,19 +51,24 @@ class TouchBar extends EventEmitter { }) } - setEscapeItem (item) { + set escapeItem (item) { if (item != null && !(item instanceof TouchBarItem)) { throw new Error('Escape item must be an instance of TouchBarItem') } - if (item == null) { - item = DEFAULT_ESCAPE_ITEM + if (this.escapeItem != null) { + this.escapeItem.removeListener('change', this.changeListener) + } + this._escapeItem = item + if (this.escapeItem != null) { + this.escapeItem.on('change', this.changeListener) } - this.escapeItem.removeListener('change', this.changeListener) - this.escapeItem = item - this.escapeItem.on('change', this.changeListener) this.emit('escape-item-change', item) } + get escapeItem () { + return this._escapeItem + } + _addToWindow (window) { const {id} = window @@ -80,7 +83,7 @@ class TouchBar extends EventEmitter { this.on('change', changeListener) const escapeItemListener = (item) => { - window._setEscapeTouchBarItem(item) + window._setEscapeTouchBarItem(item != null ? item : {}) } this.on('escape-item-change', escapeItemListener) @@ -107,7 +110,7 @@ class TouchBar extends EventEmitter { this.windowListeners[id] = removeListeners window._setTouchBarItems(this.ordereredItems) - window._setEscapeTouchBarItem(this.escapeItem) + escapeItemListener(this.escapeItem) } _removeFromWindow (window) { diff --git a/spec/api-touch-bar-spec.js b/spec/api-touch-bar-spec.js index aafb8c68217c..3b986ba032ea 100644 --- a/spec/api-touch-bar-spec.js +++ b/spec/api-touch-bar-spec.js @@ -23,7 +23,7 @@ describe('TouchBar module', function () { it('throws an error when an invalid escape item is set', function () { assert.throws(() => { const touchBar = new TouchBar([]) - touchBar.setEscapeItem('esc') + touchBar.escapeItem = 'esc' }, /Escape item must be an instance of TouchBarItem/) }) @@ -66,12 +66,12 @@ describe('TouchBar module', function () { label: 'foo' }) window.setTouchBar(touchBar) - touchBar.setEscapeItem(escapeButton) + touchBar.escapeItem = escapeButton label.label = 'baz' escapeButton.label = 'hello' window.setTouchBar() window.setTouchBar(new TouchBar([new TouchBarLabel({label: 'two'})])) - touchBar.setEscapeItem() + touchBar.escapeItem = null }) }) })