From 21c1ddffb38cd6eededb057d6af5997727f20067 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 29 Mar 2017 13:26:52 -0700 Subject: [PATCH] Handle change and interaction events on escape items --- lib/browser/api/touch-bar.js | 24 +++++++++++++++++------- spec/api-touch-bar-spec.js | 8 +++++--- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/browser/api/touch-bar.js b/lib/browser/api/touch-bar.js index 02e719a..2b95616 100644 --- a/lib/browser/api/touch-bar.js +++ b/lib/browser/api/touch-bar.js @@ -2,6 +2,8 @@ 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) { @@ -30,13 +32,14 @@ class TouchBar extends EventEmitter { this.windowListeners = {} this.items = {} this.ordereredItems = [] - this.escapeItem = {} + this.escapeItem = DEFAULT_ESCAPE_ITEM + this.changeListener = (item) => { + this.emit('change', item.id, item.type) + } const registerItem = (item) => { this.items[item.id] = item - item.on('change', () => { - this.emit('change', item.id, item.type) - }) + item.on('change', this.changeListener) if (item.child instanceof TouchBar) { item.child.ordereredItems.forEach(registerItem) } @@ -54,8 +57,12 @@ class TouchBar extends EventEmitter { if (item != null && !(item instanceof TouchBarItem)) { throw new Error('Escape item must be an instance of TouchBarItem') } - if (item == null) item = {} + if (item == null) { + item = DEFAULT_ESCAPE_ITEM + } + this.escapeItem.removeListener('change', this.changeListener) this.escapeItem = item + this.escapeItem.on('change', this.changeListener) this.emit('escape-item-change', item) } @@ -78,7 +85,10 @@ class TouchBar extends EventEmitter { this.on('escape-item-change', escapeItemListener) const interactionListener = (event, itemID, details) => { - const item = this.items[itemID] + let item = this.items[itemID] + if (item == null && this.escapeItem.id === itemID) { + item = this.escapeItem + } if (item != null && item.onInteraction != null) { item.onInteraction(details) } @@ -121,7 +131,7 @@ class TouchBarItem extends EventEmitter { }, set: function (value) { this[privateName] = value - this.emit('change') + this.emit('change', this) }, enumerable: true }) diff --git a/spec/api-touch-bar-spec.js b/spec/api-touch-bar-spec.js index 81bf47e..aafb8c6 100644 --- a/spec/api-touch-bar-spec.js +++ b/spec/api-touch-bar-spec.js @@ -62,11 +62,13 @@ describe('TouchBar module', function () { showArrowButtons: true }) ]) - window.setTouchBar(touchBar) - touchBar.setEscapeItem(new TouchBarButton({ + const escapeButton = new TouchBarButton({ label: 'foo' - })) + }) + window.setTouchBar(touchBar) + touchBar.setEscapeItem(escapeButton) label.label = 'baz' + escapeButton.label = 'hello' window.setTouchBar() window.setTouchBar(new TouchBar([new TouchBarLabel({label: 'two'})])) touchBar.setEscapeItem() -- 2.7.4