2017-02-28 23:37:15 +00:00
|
|
|
const {EventEmitter} = require('events')
|
|
|
|
|
2017-03-01 20:50:36 +00:00
|
|
|
let nextItemID = 1
|
2017-02-28 23:37:15 +00:00
|
|
|
|
|
|
|
class TouchBar extends EventEmitter {
|
2017-03-01 19:12:22 +00:00
|
|
|
// Bind a touch bar to a window
|
|
|
|
static _setOnWindow (touchBar, window) {
|
|
|
|
if (window._touchBar != null) {
|
|
|
|
window._touchBar._removeFromWindow(window)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (touchBar == null) {
|
|
|
|
window._setTouchBarItems([])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(touchBar)) {
|
|
|
|
touchBar = new TouchBar(touchBar)
|
|
|
|
}
|
|
|
|
touchBar._addToWindow(window)
|
|
|
|
}
|
|
|
|
|
2016-11-27 11:54:12 +00:00
|
|
|
constructor (items) {
|
2017-02-28 23:37:15 +00:00
|
|
|
super()
|
|
|
|
|
2016-11-27 11:54:12 +00:00
|
|
|
if (!Array.isArray(items)) {
|
|
|
|
throw new Error('The items object provided has to be an array')
|
|
|
|
}
|
2017-02-28 23:37:15 +00:00
|
|
|
|
2017-03-01 18:55:28 +00:00
|
|
|
this.windowListeners = {}
|
2017-02-28 23:37:15 +00:00
|
|
|
this.items = {}
|
|
|
|
this.ordereredItems = []
|
2017-03-02 17:30:21 +00:00
|
|
|
|
2017-02-28 23:37:15 +00:00
|
|
|
const registerItem = (item) => {
|
|
|
|
this.items[item.id] = item
|
2017-03-01 00:08:12 +00:00
|
|
|
item.on('change', () => {
|
|
|
|
this.emit('change', item.id, item.type)
|
|
|
|
})
|
2017-02-28 23:37:15 +00:00
|
|
|
if (item.child instanceof TouchBar) {
|
|
|
|
item.child.ordereredItems.forEach(registerItem)
|
|
|
|
}
|
|
|
|
}
|
2016-11-27 11:54:12 +00:00
|
|
|
items.forEach((item) => {
|
2017-02-28 23:37:15 +00:00
|
|
|
if (!(item instanceof TouchBarItem)) {
|
2016-11-27 11:54:12 +00:00
|
|
|
throw new Error('Each item must be an instance of a TouchBarItem')
|
|
|
|
}
|
2017-03-02 17:30:21 +00:00
|
|
|
this.ordereredItems.push(item)
|
2017-02-28 23:37:15 +00:00
|
|
|
registerItem(item)
|
2016-11-27 11:54:12 +00:00
|
|
|
})
|
2017-03-01 18:55:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_addToWindow (window) {
|
|
|
|
const {id} = window
|
|
|
|
|
|
|
|
// Already added to window
|
|
|
|
if (this.windowListeners.hasOwnProperty(id)) return
|
2016-11-27 11:54:12 +00:00
|
|
|
|
2017-03-01 19:05:34 +00:00
|
|
|
window._touchBar = this
|
|
|
|
|
2017-03-01 18:55:28 +00:00
|
|
|
const changeListener = (itemID) => {
|
|
|
|
window._refreshTouchBarItem(itemID)
|
|
|
|
}
|
|
|
|
this.on('change', changeListener)
|
|
|
|
|
|
|
|
const interactionListener = (event, itemID, details) => {
|
2017-02-28 23:37:15 +00:00
|
|
|
const item = this.items[itemID]
|
|
|
|
if (item != null && item.onInteraction != null) {
|
|
|
|
item.onInteraction(details)
|
|
|
|
}
|
2017-03-01 18:55:28 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
|
2017-03-01 19:05:34 +00:00
|
|
|
window._setTouchBarItems(this.ordereredItems)
|
2017-03-01 18:55:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_removeFromWindow (window) {
|
|
|
|
const removeListeners = this.windowListeners[window.id]
|
|
|
|
if (removeListeners != null) removeListeners()
|
2016-11-28 10:43:39 +00:00
|
|
|
}
|
2016-11-27 11:54:12 +00:00
|
|
|
}
|
|
|
|
|
2017-03-01 00:08:12 +00:00
|
|
|
class TouchBarItem extends EventEmitter {
|
2016-11-27 11:54:12 +00:00
|
|
|
constructor (config) {
|
2017-03-01 00:08:12 +00:00
|
|
|
super()
|
2017-03-01 20:50:36 +00:00
|
|
|
this.id = `${nextItemID++}`
|
2016-11-27 11:54:12 +00:00
|
|
|
}
|
2017-03-01 22:54:43 +00:00
|
|
|
|
|
|
|
_addLiveProperty (name, initialValue) {
|
|
|
|
const privateName = `_${name}`
|
|
|
|
this[privateName] = initialValue
|
|
|
|
Object.defineProperty(this, name, {
|
|
|
|
get: function () {
|
|
|
|
return this[privateName]
|
|
|
|
},
|
|
|
|
set: function (value) {
|
|
|
|
this[privateName] = value
|
|
|
|
this.emit('change')
|
|
|
|
},
|
|
|
|
enumerable: true
|
|
|
|
})
|
|
|
|
}
|
2016-11-27 11:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TouchBar.Button = class TouchBarButton extends TouchBarItem {
|
|
|
|
constructor (config) {
|
|
|
|
super(config)
|
2017-02-28 23:37:15 +00:00
|
|
|
this.type = 'button'
|
2017-03-01 22:54:43 +00:00
|
|
|
const {click, label, backgroundColor} = config
|
|
|
|
this._addLiveProperty('label', label)
|
|
|
|
this._addLiveProperty('backgroundColor', backgroundColor)
|
|
|
|
if (typeof click === 'function') {
|
|
|
|
this.onInteraction = config.click
|
|
|
|
}
|
2016-11-27 11:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TouchBar.ColorPicker = class TouchBarColorPicker extends TouchBarItem {
|
|
|
|
constructor (config) {
|
|
|
|
super(config)
|
2017-02-28 23:37:15 +00:00
|
|
|
this.type = 'colorpicker'
|
2017-03-01 22:54:43 +00:00
|
|
|
const {availableColors, change, selectedColor} = config
|
|
|
|
this._addLiveProperty('availableColors', availableColors)
|
|
|
|
this._addLiveProperty('selectedColor', selectedColor)
|
2017-02-28 23:37:15 +00:00
|
|
|
|
2016-11-27 11:54:12 +00:00
|
|
|
if (typeof change === 'function') {
|
2017-02-28 23:37:15 +00:00
|
|
|
this.onInteraction = (details) => {
|
2017-03-01 22:54:43 +00:00
|
|
|
this._selectedColor = details.color
|
2017-02-28 23:37:15 +00:00
|
|
|
change(details.color)
|
|
|
|
}
|
2016-11-27 11:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-29 07:36:57 +00:00
|
|
|
TouchBar.Group = class TouchBarGroup extends TouchBarItem {
|
2016-11-28 07:24:48 +00:00
|
|
|
constructor (config) {
|
|
|
|
super(config)
|
2017-02-28 23:37:15 +00:00
|
|
|
this.type = 'group'
|
|
|
|
this.child = config.items
|
|
|
|
if (!(this.child instanceof TouchBar)) {
|
|
|
|
this.child = new TouchBar(this.items)
|
|
|
|
}
|
2016-11-28 07:24:48 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-27 11:54:12 +00:00
|
|
|
|
2016-11-29 07:36:57 +00:00
|
|
|
TouchBar.Label = class TouchBarLabel extends TouchBarItem {
|
2016-11-28 10:43:39 +00:00
|
|
|
constructor (config) {
|
|
|
|
super(config)
|
2017-02-28 23:37:15 +00:00
|
|
|
this.type = 'label'
|
2017-03-01 22:54:43 +00:00
|
|
|
this._addLiveProperty('label', config.label)
|
2016-11-28 10:43:39 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-27 11:54:12 +00:00
|
|
|
|
2017-03-01 22:10:52 +00:00
|
|
|
TouchBar.Spacer = class TouchBarSpacer extends TouchBarItem {
|
|
|
|
constructor (config) {
|
|
|
|
super(config)
|
|
|
|
this.type = 'spacer'
|
2017-03-01 22:54:43 +00:00
|
|
|
this._addLiveProperty('size', config.size)
|
2017-03-01 22:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-01 20:50:36 +00:00
|
|
|
TouchBar.Popover = class TouchBarPopover extends TouchBarItem {
|
2016-11-29 07:00:08 +00:00
|
|
|
constructor (config) {
|
|
|
|
super(config)
|
2017-02-28 23:37:15 +00:00
|
|
|
this.type = 'popover'
|
2017-03-01 22:54:43 +00:00
|
|
|
this._addLiveProperty('label', config.label)
|
2017-02-28 23:37:15 +00:00
|
|
|
this.showCloseButton = config.showCloseButton
|
|
|
|
this.child = config.items
|
|
|
|
if (!(this.child instanceof TouchBar)) {
|
|
|
|
this.child = new TouchBar(this.items)
|
|
|
|
}
|
2016-11-29 07:00:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-27 11:54:12 +00:00
|
|
|
TouchBar.Slider = class TouchBarSlider extends TouchBarItem {
|
|
|
|
constructor (config) {
|
|
|
|
super(config)
|
2017-02-28 23:37:15 +00:00
|
|
|
this.type = 'slider'
|
2017-03-01 22:54:43 +00:00
|
|
|
const {change, label, minValue, maxValue, value} = config
|
|
|
|
this._addLiveProperty('label', label)
|
|
|
|
this._addLiveProperty('minValue', minValue)
|
|
|
|
this._addLiveProperty('maxValue', maxValue)
|
|
|
|
this._addLiveProperty('value', value)
|
2017-02-28 23:37:15 +00:00
|
|
|
|
2016-11-27 11:54:12 +00:00
|
|
|
if (typeof change === 'function') {
|
2017-02-28 23:37:15 +00:00
|
|
|
this.onInteraction = (details) => {
|
2017-03-01 22:54:43 +00:00
|
|
|
this._value = details.value
|
2017-02-28 23:37:15 +00:00
|
|
|
change(details.value)
|
|
|
|
}
|
2016-11-27 11:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-29 07:55:07 +00:00
|
|
|
module.exports = TouchBar
|