diff --git a/lib/browser/api/browser-window.js b/lib/browser/api/browser-window.js index 27f425c7ae..aa3872870e 100644 --- a/lib/browser/api/browser-window.js +++ b/lib/browser/api/browser-window.js @@ -131,15 +131,6 @@ BrowserWindow.prototype._init = function () { return this.webContents.devToolsWebContents } }) - - // Proxy TouchBar events - this.on('-touch-bar-interaction', (event, id, details) => { - if (this._touchBar != null) { - this._touchBar.emit('interaction', id, details) - } - }) - - this._touchBarListener = this._refreshTouchBarItem.bind(this) } BrowserWindow.getFocusedWindow = () => { @@ -204,27 +195,25 @@ Object.assign(BrowserWindow.prototype, { }, capturePage (...args) { return this.webContents.capturePage(...args) + }, + + // TouchBar API + setTouchBar (touchBar) { + if (touchBar == null) { + if (this._touchBar != null) { + this._touchBar._removeFromWindow(this) + } + this._destroyTouchBar() + return + } + + if (Array.isArray(touchBar)) { + touchBar = new TouchBar(touchBar) + } + + this._touchBar = touchBar + this._touchBar._addToWindow(this) } }) -// TouchBar API -BrowserWindow.prototype.setTouchBar = function (touchBar) { - if (touchBar == null) { - if (this._touchBar != null) { - this._touchBar.removeListener('change', this._touchBarListener) - this._touchBar = null - } - this._destroyTouchBar() - return - } - - if (Array.isArray(touchBar)) { - touchBar = new TouchBar(touchBar) - } - - this._touchBar = touchBar - this._touchBar.on('change', this._touchBarListener) - this._setTouchBar(touchBar.ordereredItems) -} - module.exports = BrowserWindow diff --git a/lib/browser/api/touch-bar.js b/lib/browser/api/touch-bar.js index 237ef716fa..20285246c3 100644 --- a/lib/browser/api/touch-bar.js +++ b/lib/browser/api/touch-bar.js @@ -10,6 +10,7 @@ class TouchBar extends EventEmitter { throw new Error('The items object provided has to be an array') } + this.windowListeners = {} this.items = {} this.ordereredItems = [] const registerItem = (item) => { @@ -28,13 +29,45 @@ class TouchBar extends EventEmitter { } registerItem(item) }) + } - this.on('interaction', (itemID, details) => { + // Called by BrowserWindow.setTouchBar + _addToWindow (window) { + const {id} = window + + // Already added to window + if (this.windowListeners.hasOwnProperty(id)) return + + const changeListener = (itemID) => { + window._refreshTouchBarItem(itemID) + } + this.on('change', changeListener) + + const interactionListener = (event, itemID, details) => { const item = this.items[itemID] if (item != null && item.onInteraction != null) { item.onInteraction(details) } - }) + } + window.on('-touch-bar-interaction', interactionListener) + + const removeListeners = () => { + this.removeListener('change', changeListener) + window.removeListener('-touch-bar-interaction', interactionListener) + window.removeListener('closed', removeListeners) + window._touchBar = null + delete this.windowListeners[id] + } + window.once('closed', removeListeners) + this.windowListeners[id] = removeListeners + + window._setTouchBar(this.ordereredItems) + } + + // Called by BrowserWindow.setTouchBar + _removeFromWindow (window) { + const removeListeners = this.windowListeners[window.id] + if (removeListeners != null) removeListeners() } }