Add window helpers to TouchBar class
This commit is contained in:
parent
347d472841
commit
d5dbe3676e
2 changed files with 53 additions and 31 deletions
|
@ -131,15 +131,6 @@ BrowserWindow.prototype._init = function () {
|
||||||
return this.webContents.devToolsWebContents
|
return this.webContents.devToolsWebContents
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// Proxy TouchBar events
|
|
||||||
this.on('-touch-bar-interaction', (event, id, details) => {
|
|
||||||
if (this._touchBar != null) {
|
|
||||||
this._touchBar.emit('interaction', id, details)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
this._touchBarListener = this._refreshTouchBarItem.bind(this)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BrowserWindow.getFocusedWindow = () => {
|
BrowserWindow.getFocusedWindow = () => {
|
||||||
|
@ -204,27 +195,25 @@ Object.assign(BrowserWindow.prototype, {
|
||||||
},
|
},
|
||||||
capturePage (...args) {
|
capturePage (...args) {
|
||||||
return this.webContents.capturePage(...args)
|
return this.webContents.capturePage(...args)
|
||||||
|
},
|
||||||
|
|
||||||
|
// TouchBar API
|
||||||
|
setTouchBar (touchBar) {
|
||||||
|
if (touchBar == null) {
|
||||||
|
if (this._touchBar != null) {
|
||||||
|
this._touchBar._removeFromWindow(this)
|
||||||
|
}
|
||||||
|
this._destroyTouchBar()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Array.isArray(touchBar)) {
|
||||||
|
touchBar = new TouchBar(touchBar)
|
||||||
|
}
|
||||||
|
|
||||||
|
this._touchBar = touchBar
|
||||||
|
this._touchBar._addToWindow(this)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// TouchBar API
|
|
||||||
BrowserWindow.prototype.setTouchBar = function (touchBar) {
|
|
||||||
if (touchBar == null) {
|
|
||||||
if (this._touchBar != null) {
|
|
||||||
this._touchBar.removeListener('change', this._touchBarListener)
|
|
||||||
this._touchBar = null
|
|
||||||
}
|
|
||||||
this._destroyTouchBar()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Array.isArray(touchBar)) {
|
|
||||||
touchBar = new TouchBar(touchBar)
|
|
||||||
}
|
|
||||||
|
|
||||||
this._touchBar = touchBar
|
|
||||||
this._touchBar.on('change', this._touchBarListener)
|
|
||||||
this._setTouchBar(touchBar.ordereredItems)
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = BrowserWindow
|
module.exports = BrowserWindow
|
||||||
|
|
|
@ -10,6 +10,7 @@ class TouchBar extends EventEmitter {
|
||||||
throw new Error('The items object provided has to be an array')
|
throw new Error('The items object provided has to be an array')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.windowListeners = {}
|
||||||
this.items = {}
|
this.items = {}
|
||||||
this.ordereredItems = []
|
this.ordereredItems = []
|
||||||
const registerItem = (item) => {
|
const registerItem = (item) => {
|
||||||
|
@ -28,13 +29,45 @@ class TouchBar extends EventEmitter {
|
||||||
}
|
}
|
||||||
registerItem(item)
|
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]
|
const item = this.items[itemID]
|
||||||
if (item != null && item.onInteraction != null) {
|
if (item != null && item.onInteraction != null) {
|
||||||
item.onInteraction(details)
|
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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue