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
|
let nextItemID = 1
|
||||||
|
|
||||||
|
const DEFAULT_ESCAPE_ITEM = new EventEmitter()
|
||||||
|
|
||||||
class TouchBar extends EventEmitter {
|
class TouchBar extends EventEmitter {
|
||||||
// Bind a touch bar to a window
|
// Bind a touch bar to a window
|
||||||
static _setOnWindow (touchBar, window) {
|
static _setOnWindow (touchBar, window) {
|
||||||
|
@ -30,13 +32,14 @@ class TouchBar extends EventEmitter {
|
||||||
this.windowListeners = {}
|
this.windowListeners = {}
|
||||||
this.items = {}
|
this.items = {}
|
||||||
this.ordereredItems = []
|
this.ordereredItems = []
|
||||||
this.escapeItem = {}
|
this.escapeItem = DEFAULT_ESCAPE_ITEM
|
||||||
|
this.changeListener = (item) => {
|
||||||
|
this.emit('change', item.id, item.type)
|
||||||
|
}
|
||||||
|
|
||||||
const registerItem = (item) => {
|
const registerItem = (item) => {
|
||||||
this.items[item.id] = item
|
this.items[item.id] = item
|
||||||
item.on('change', () => {
|
item.on('change', this.changeListener)
|
||||||
this.emit('change', item.id, item.type)
|
|
||||||
})
|
|
||||||
if (item.child instanceof TouchBar) {
|
if (item.child instanceof TouchBar) {
|
||||||
item.child.ordereredItems.forEach(registerItem)
|
item.child.ordereredItems.forEach(registerItem)
|
||||||
}
|
}
|
||||||
|
@ -54,8 +57,12 @@ class TouchBar extends EventEmitter {
|
||||||
if (item != null && !(item instanceof TouchBarItem)) {
|
if (item != null && !(item instanceof TouchBarItem)) {
|
||||||
throw new Error('Escape item must be an instance of 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 = item
|
||||||
|
this.escapeItem.on('change', this.changeListener)
|
||||||
this.emit('escape-item-change', item)
|
this.emit('escape-item-change', item)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,7 +85,10 @@ class TouchBar extends EventEmitter {
|
||||||
this.on('escape-item-change', escapeItemListener)
|
this.on('escape-item-change', escapeItemListener)
|
||||||
|
|
||||||
const interactionListener = (event, itemID, details) => {
|
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) {
|
if (item != null && item.onInteraction != null) {
|
||||||
item.onInteraction(details)
|
item.onInteraction(details)
|
||||||
}
|
}
|
||||||
|
@ -121,7 +131,7 @@ class TouchBarItem extends EventEmitter {
|
||||||
},
|
},
|
||||||
set: function (value) {
|
set: function (value) {
|
||||||
this[privateName] = value
|
this[privateName] = value
|
||||||
this.emit('change')
|
this.emit('change', this)
|
||||||
},
|
},
|
||||||
enumerable: true
|
enumerable: true
|
||||||
})
|
})
|
||||||
|
|
|
@ -62,11 +62,13 @@ describe('TouchBar module', function () {
|
||||||
showArrowButtons: true
|
showArrowButtons: true
|
||||||
})
|
})
|
||||||
])
|
])
|
||||||
window.setTouchBar(touchBar)
|
const escapeButton = new TouchBarButton({
|
||||||
touchBar.setEscapeItem(new TouchBarButton({
|
|
||||||
label: 'foo'
|
label: 'foo'
|
||||||
}))
|
})
|
||||||
|
window.setTouchBar(touchBar)
|
||||||
|
touchBar.setEscapeItem(escapeButton)
|
||||||
label.label = 'baz'
|
label.label = 'baz'
|
||||||
|
escapeButton.label = 'hello'
|
||||||
window.setTouchBar()
|
window.setTouchBar()
|
||||||
window.setTouchBar(new TouchBar([new TouchBarLabel({label: 'two'})]))
|
window.setTouchBar(new TouchBar([new TouchBarLabel({label: 'two'})]))
|
||||||
touchBar.setEscapeItem()
|
touchBar.setEscapeItem()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue