Handle change and interaction events on escape items
This commit is contained in:
parent
591cd8d073
commit
21c1ddffb3
2 changed files with 22 additions and 10 deletions
|
@ -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
|
||||
})
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue