diff --git a/lib/browser/api/touch-bar.js b/lib/browser/api/touch-bar.js index 2c6b72b89331..02e719ada71e 100644 --- a/lib/browser/api/touch-bar.js +++ b/lib/browser/api/touch-bar.js @@ -28,9 +28,9 @@ class TouchBar extends EventEmitter { } this.windowListeners = {} - this.windows = {} this.items = {} this.ordereredItems = [] + this.escapeItem = {} const registerItem = (item) => { this.items[item.id] = item @@ -51,12 +51,12 @@ class TouchBar extends EventEmitter { } setEscapeItem (item) { - if (!item) item = {} - Object.keys(this.windows).forEach((windowID) => { - const window = this.windows[windowID] - window._setEscapeTouchBarItem(item) - }) - this._escape = item + if (item != null && !(item instanceof TouchBarItem)) { + throw new Error('Escape item must be an instance of TouchBarItem') + } + if (item == null) item = {} + this.escapeItem = item + this.emit('escape-item-change', item) } _addToWindow (window) { @@ -72,6 +72,11 @@ class TouchBar extends EventEmitter { } this.on('change', changeListener) + const escapeItemListener = (item) => { + window._setEscapeTouchBarItem(item) + } + this.on('escape-item-change', escapeItemListener) + const interactionListener = (event, itemID, details) => { const item = this.items[itemID] if (item != null && item.onInteraction != null) { @@ -82,19 +87,17 @@ class TouchBar extends EventEmitter { const removeListeners = () => { this.removeListener('change', changeListener) + this.removeListener('escape-item-change', escapeItemListener) window.removeListener('-touch-bar-interaction', interactionListener) window.removeListener('closed', removeListeners) window._touchBar = null delete this.windowListeners[id] - delete this.windows[id] } window.once('closed', removeListeners) this.windowListeners[id] = removeListeners - this.windows[id] = window window._setTouchBarItems(this.ordereredItems) - - if (this._escape) window._setEscapeTouchBarItem(this._escape) + window._setEscapeTouchBarItem(this.escapeItem) } _removeFromWindow (window) { diff --git a/spec/api-touch-bar-spec.js b/spec/api-touch-bar-spec.js index 465be6287fc9..81bf47eba392 100644 --- a/spec/api-touch-bar-spec.js +++ b/spec/api-touch-bar-spec.js @@ -20,6 +20,13 @@ describe('TouchBar module', function () { }, /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([]) + touchBar.setEscapeItem('esc') + }, /Escape item must be an instance of TouchBarItem/) + }) + describe('BrowserWindow behavior', function () { let window