From 591cd8d07338c69dd177e90dd222cf1112ec35ce Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 29 Mar 2017 13:11:25 -0700 Subject: [PATCH] Update window via listener when escape item changes --- lib/browser/api/touch-bar.js | 25 ++++++++++++++----------- spec/api-touch-bar-spec.js | 7 +++++++ 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/browser/api/touch-bar.js b/lib/browser/api/touch-bar.js index 2c6b72b..02e719a 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 465be62..81bf47e 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 -- 2.7.4