let nextItemID = 1
+const DEFAULT_ESCAPE_ITEM = new EventEmitter()
+
class TouchBar extends EventEmitter {
// Bind a touch bar to a window
static _setOnWindow (touchBar, window) {
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)
}
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)
}
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)
}
},
set: function (value) {
this[privateName] = value
- this.emit('change')
+ this.emit('change', this)
},
enumerable: true
})
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()