Update window via listener when escape item changes

This commit is contained in:
Kevin Sawicki 2017-03-29 13:11:25 -07:00
parent 0501a20fe6
commit 591cd8d073
2 changed files with 21 additions and 11 deletions

View file

@ -28,9 +28,9 @@ class TouchBar extends EventEmitter {
} }
this.windowListeners = {} this.windowListeners = {}
this.windows = {}
this.items = {} this.items = {}
this.ordereredItems = [] this.ordereredItems = []
this.escapeItem = {}
const registerItem = (item) => { const registerItem = (item) => {
this.items[item.id] = item this.items[item.id] = item
@ -51,12 +51,12 @@ class TouchBar extends EventEmitter {
} }
setEscapeItem (item) { setEscapeItem (item) {
if (!item) item = {} if (item != null && !(item instanceof TouchBarItem)) {
Object.keys(this.windows).forEach((windowID) => { throw new Error('Escape item must be an instance of TouchBarItem')
const window = this.windows[windowID] }
window._setEscapeTouchBarItem(item) if (item == null) item = {}
}) this.escapeItem = item
this._escape = item this.emit('escape-item-change', item)
} }
_addToWindow (window) { _addToWindow (window) {
@ -72,6 +72,11 @@ class TouchBar extends EventEmitter {
} }
this.on('change', changeListener) this.on('change', changeListener)
const escapeItemListener = (item) => {
window._setEscapeTouchBarItem(item)
}
this.on('escape-item-change', escapeItemListener)
const interactionListener = (event, itemID, details) => { const interactionListener = (event, itemID, details) => {
const item = this.items[itemID] const item = this.items[itemID]
if (item != null && item.onInteraction != null) { if (item != null && item.onInteraction != null) {
@ -82,19 +87,17 @@ class TouchBar extends EventEmitter {
const removeListeners = () => { const removeListeners = () => {
this.removeListener('change', changeListener) this.removeListener('change', changeListener)
this.removeListener('escape-item-change', escapeItemListener)
window.removeListener('-touch-bar-interaction', interactionListener) window.removeListener('-touch-bar-interaction', interactionListener)
window.removeListener('closed', removeListeners) window.removeListener('closed', removeListeners)
window._touchBar = null window._touchBar = null
delete this.windowListeners[id] delete this.windowListeners[id]
delete this.windows[id]
} }
window.once('closed', removeListeners) window.once('closed', removeListeners)
this.windowListeners[id] = removeListeners this.windowListeners[id] = removeListeners
this.windows[id] = window
window._setTouchBarItems(this.ordereredItems) window._setTouchBarItems(this.ordereredItems)
window._setEscapeTouchBarItem(this.escapeItem)
if (this._escape) window._setEscapeTouchBarItem(this._escape)
} }
_removeFromWindow (window) { _removeFromWindow (window) {

View file

@ -20,6 +20,13 @@ describe('TouchBar module', function () {
}, /Each item must be an instance of TouchBarItem/) }, /Each item must be an instance of TouchBarItem/)
}) })
it('throws an error when an invalid escape item is set', function () {
assert.throws(() => {
const touchBar = new TouchBar([])
touchBar.setEscapeItem('esc')
}, /Escape item must be an instance of TouchBarItem/)
})
describe('BrowserWindow behavior', function () { describe('BrowserWindow behavior', function () {
let window let window