Handle change and interaction events on escape items
authorKevin Sawicki <kevinsawicki@gmail.com>
Wed, 29 Mar 2017 20:26:52 +0000 (13:26 -0700)
committerKevin Sawicki <kevinsawicki@gmail.com>
Tue, 4 Apr 2017 19:50:41 +0000 (12:50 -0700)
lib/browser/api/touch-bar.js
spec/api-touch-bar-spec.js

index 02e719a..2b95616 100644 (file)
@@ -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
     })
index 81bf47e..aafb8c6 100644 (file)
@@ -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()