Add window helpers to TouchBar class

This commit is contained in:
Kevin Sawicki 2017-03-01 10:55:28 -08:00
parent 347d472841
commit d5dbe3676e
2 changed files with 53 additions and 31 deletions

View file

@ -10,6 +10,7 @@ class TouchBar extends EventEmitter {
throw new Error('The items object provided has to be an array')
}
this.windowListeners = {}
this.items = {}
this.ordereredItems = []
const registerItem = (item) => {
@ -28,13 +29,45 @@ class TouchBar extends EventEmitter {
}
registerItem(item)
})
}
this.on('interaction', (itemID, details) => {
// Called by BrowserWindow.setTouchBar
_addToWindow (window) {
const {id} = window
// Already added to window
if (this.windowListeners.hasOwnProperty(id)) return
const changeListener = (itemID) => {
window._refreshTouchBarItem(itemID)
}
this.on('change', changeListener)
const interactionListener = (event, itemID, details) => {
const item = this.items[itemID]
if (item != null && item.onInteraction != null) {
item.onInteraction(details)
}
})
}
window.on('-touch-bar-interaction', interactionListener)
const removeListeners = () => {
this.removeListener('change', changeListener)
window.removeListener('-touch-bar-interaction', interactionListener)
window.removeListener('closed', removeListeners)
window._touchBar = null
delete this.windowListeners[id]
}
window.once('closed', removeListeners)
this.windowListeners[id] = removeListeners
window._setTouchBar(this.ordereredItems)
}
// Called by BrowserWindow.setTouchBar
_removeFromWindow (window) {
const removeListeners = this.windowListeners[window.id]
if (removeListeners != null) removeListeners()
}
}