2018-09-22 12:28:50 +00:00
|
|
|
'use strict'
|
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { EventEmitter } = require('events')
|
2017-02-28 23:37:15 +00:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-04-03 22:12:57 +00:00
|
|
|
constructor (options) {
|
2017-02-28 23:37:15 +00:00
|
|
|
super()
|
|
|
|
|
2017-04-03 22:12:57 +00:00
|
|
|
if (options == null) {
|
|
|
|
throw new Error('Must specify options object as first argument')
|
|
|
|
}
|
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
let { items, escapeItem } = options
|
2017-04-03 22:12:57 +00:00
|
|
|
|
2016-11-27 11:54:12 +00:00
|
|
|
if (!Array.isArray(items)) {
|
2017-04-03 22:12:57 +00:00
|
|
|
items = []
|
2016-11-27 11:54:12 +00:00
|
|
|
}
|
2017-02-28 23:37:15 +00:00
|
|
|
|
2017-04-04 20:14:56 +00:00
|
|
|
this.changeListener = (item) => {
|
|
|
|
this.emit('change', item.id, item.type)
|
|
|
|
}
|
|
|
|
|
2017-03-01 18:55:28 +00:00
|
|
|
this.windowListeners = {}
|
2017-02-28 23:37:15 +00:00
|
|
|
this.items = {}
|
|
|
|
this.ordereredItems = []
|
2017-04-03 22:12:57 +00:00
|
|
|
this.escapeItem = escapeItem
|
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-29 20:26:52 +00:00
|
|
|
item.on('change', this.changeListener)
|
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)) {
|
2017-03-03 18:22:25 +00:00
|
|
|
throw new Error('Each item must be an instance of TouchBarItem')
|
2016-11-27 11:54:12 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2017-04-03 16:34:55 +00:00
|
|
|
set escapeItem (item) {
|
2017-03-29 20:11:25 +00:00
|
|
|
if (item != null && !(item instanceof TouchBarItem)) {
|
|
|
|
throw new Error('Escape item must be an instance of TouchBarItem')
|
|
|
|
}
|
2017-04-03 16:34:55 +00:00
|
|
|
if (this.escapeItem != null) {
|
|
|
|
this.escapeItem.removeListener('change', this.changeListener)
|
|
|
|
}
|
|
|
|
this._escapeItem = item
|
|
|
|
if (this.escapeItem != null) {
|
|
|
|
this.escapeItem.on('change', this.changeListener)
|
2017-03-29 20:26:52 +00:00
|
|
|
}
|
2017-03-29 20:11:25 +00:00
|
|
|
this.emit('escape-item-change', item)
|
2017-03-27 00:22:52 +00:00
|
|
|
}
|
|
|
|
|
2017-04-03 16:34:55 +00:00
|
|
|
get escapeItem () {
|
|
|
|
return this._escapeItem
|
|
|
|
}
|
|
|
|
|
2017-03-01 18:55:28 +00:00
|
|
|
_addToWindow (window) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const { id } = window
|
2017-03-01 18:55:28 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
2017-03-29 20:11:25 +00:00
|
|
|
const escapeItemListener = (item) => {
|
2017-04-03 16:34:55 +00:00
|
|
|
window._setEscapeTouchBarItem(item != null ? item : {})
|
2017-03-29 20:11:25 +00:00
|
|
|
}
|
|
|
|
this.on('escape-item-change', escapeItemListener)
|
|
|
|
|
2017-03-01 18:55:28 +00:00
|
|
|
const interactionListener = (event, itemID, details) => {
|
2017-03-29 20:26:52 +00:00
|
|
|
let item = this.items[itemID]
|
2017-04-04 20:12:29 +00:00
|
|
|
if (item == null && this.escapeItem != null && this.escapeItem.id === itemID) {
|
2017-03-29 20:26:52 +00:00
|
|
|
item = this.escapeItem
|
|
|
|
}
|
2017-02-28 23:37:15 +00:00
|
|
|
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)
|
2017-03-29 20:11:25 +00:00
|
|
|
this.removeListener('escape-item-change', escapeItemListener)
|
2017-03-01 18:55:28 +00:00
|
|
|
window.removeListener('-touch-bar-interaction', interactionListener)
|
|
|
|
window.removeListener('closed', removeListeners)
|
|
|
|
window._touchBar = null
|
|
|
|
delete this.windowListeners[id]
|
2018-04-15 15:24:47 +00:00
|
|
|
const unregisterItems = (items) => {
|
|
|
|
for (const item of items) {
|
|
|
|
item.removeListener('change', this.changeListener)
|
|
|
|
if (item.child instanceof TouchBar) {
|
|
|
|
unregisterItems(item.child.ordereredItems)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unregisterItems(this.ordereredItems)
|
|
|
|
if (this.escapeItem) {
|
|
|
|
this.escapeItem.removeListener('change', this.changeListener)
|
|
|
|
}
|
2017-03-01 18:55:28 +00:00
|
|
|
}
|
|
|
|
window.once('closed', removeListeners)
|
|
|
|
this.windowListeners[id] = removeListeners
|
|
|
|
|
2017-03-01 19:05:34 +00:00
|
|
|
window._setTouchBarItems(this.ordereredItems)
|
2017-04-03 16:34:55 +00:00
|
|
|
escapeItemListener(this.escapeItem)
|
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 {
|
2017-03-02 22:29:59 +00:00
|
|
|
constructor () {
|
2017-03-01 00:08:12 +00:00
|
|
|
super()
|
2018-02-12 17:53:04 +00:00
|
|
|
this._addImmutableProperty('id', `${nextItemID++}`)
|
|
|
|
this._parents = []
|
|
|
|
}
|
|
|
|
|
|
|
|
_addImmutableProperty (name, value) {
|
|
|
|
Object.defineProperty(this, name, {
|
|
|
|
get: function () {
|
|
|
|
return value
|
|
|
|
},
|
|
|
|
set: function () {
|
|
|
|
throw new Error(`Cannot override property ${name}`)
|
|
|
|
},
|
|
|
|
enumerable: true,
|
|
|
|
configurable: false
|
|
|
|
})
|
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
|
2017-03-29 20:26:52 +00:00
|
|
|
this.emit('change', this)
|
2017-03-01 22:54:43 +00:00
|
|
|
},
|
|
|
|
enumerable: true
|
|
|
|
})
|
|
|
|
}
|
2018-02-12 17:53:04 +00:00
|
|
|
|
|
|
|
_addParent (item) {
|
|
|
|
const existing = this._parents.some(test => test.id === item.id)
|
|
|
|
if (!existing) {
|
|
|
|
this._parents.push({
|
|
|
|
id: item.id,
|
|
|
|
type: item.type
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2016-11-27 11:54:12 +00:00
|
|
|
}
|
|
|
|
|
2017-03-02 22:32:38 +00:00
|
|
|
TouchBar.TouchBarButton = class TouchBarButton extends TouchBarItem {
|
2016-11-27 11:54:12 +00:00
|
|
|
constructor (config) {
|
2017-03-02 22:29:59 +00:00
|
|
|
super()
|
|
|
|
if (config == null) config = {}
|
2018-02-12 17:53:04 +00:00
|
|
|
this._addImmutableProperty('type', 'button')
|
2018-09-13 16:10:51 +00:00
|
|
|
const { click, icon, iconPosition, label, backgroundColor } = config
|
2017-03-01 22:54:43 +00:00
|
|
|
this._addLiveProperty('label', label)
|
|
|
|
this._addLiveProperty('backgroundColor', backgroundColor)
|
2017-03-02 20:56:23 +00:00
|
|
|
this._addLiveProperty('icon', icon)
|
2017-04-17 00:26:33 +00:00
|
|
|
this._addLiveProperty('iconPosition', iconPosition)
|
2017-03-01 22:54:43 +00:00
|
|
|
if (typeof click === 'function') {
|
2018-02-12 17:53:04 +00:00
|
|
|
this._addImmutableProperty('onInteraction', () => {
|
2017-03-03 18:22:25 +00:00
|
|
|
config.click()
|
2018-02-12 17:53:04 +00:00
|
|
|
})
|
2017-03-01 22:54:43 +00:00
|
|
|
}
|
2016-11-27 11:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-02 22:32:38 +00:00
|
|
|
TouchBar.TouchBarColorPicker = class TouchBarColorPicker extends TouchBarItem {
|
2016-11-27 11:54:12 +00:00
|
|
|
constructor (config) {
|
2017-03-02 22:29:59 +00:00
|
|
|
super()
|
|
|
|
if (config == null) config = {}
|
2018-02-12 17:53:04 +00:00
|
|
|
this._addImmutableProperty('type', 'colorpicker')
|
2018-09-13 16:10:51 +00:00
|
|
|
const { availableColors, change, selectedColor } = config
|
2017-03-01 22:54:43 +00:00
|
|
|
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') {
|
2018-02-12 17:53:04 +00:00
|
|
|
this._addImmutableProperty('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)
|
2018-02-12 17:53:04 +00:00
|
|
|
})
|
2016-11-27 11:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-02 22:32:38 +00:00
|
|
|
TouchBar.TouchBarGroup = class TouchBarGroup extends TouchBarItem {
|
2016-11-28 07:24:48 +00:00
|
|
|
constructor (config) {
|
2017-03-02 22:29:59 +00:00
|
|
|
super()
|
|
|
|
if (config == null) config = {}
|
2018-02-12 17:53:04 +00:00
|
|
|
this._addImmutableProperty('type', 'group')
|
|
|
|
const defaultChild = (config.items instanceof TouchBar) ? config.items : new TouchBar(config.items)
|
|
|
|
this._addLiveProperty('child', defaultChild)
|
|
|
|
this.child.ordereredItems.forEach((item) => item._addParent(this))
|
2016-11-28 07:24:48 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-27 11:54:12 +00:00
|
|
|
|
2017-03-02 22:32:38 +00:00
|
|
|
TouchBar.TouchBarLabel = class TouchBarLabel extends TouchBarItem {
|
2016-11-28 10:43:39 +00:00
|
|
|
constructor (config) {
|
2017-03-02 22:29:59 +00:00
|
|
|
super()
|
|
|
|
if (config == null) config = {}
|
2018-02-12 17:53:04 +00:00
|
|
|
this._addImmutableProperty('type', 'label')
|
2017-03-01 22:54:43 +00:00
|
|
|
this._addLiveProperty('label', config.label)
|
2017-03-03 23:14:51 +00:00
|
|
|
this._addLiveProperty('textColor', config.textColor)
|
2016-11-28 10:43:39 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-27 11:54:12 +00:00
|
|
|
|
2017-03-02 22:32:38 +00:00
|
|
|
TouchBar.TouchBarPopover = class TouchBarPopover extends TouchBarItem {
|
2016-11-29 07:00:08 +00:00
|
|
|
constructor (config) {
|
2017-03-02 22:29:59 +00:00
|
|
|
super()
|
|
|
|
if (config == null) config = {}
|
2018-02-12 17:53:04 +00:00
|
|
|
this._addImmutableProperty('type', 'popover')
|
2017-03-01 22:54:43 +00:00
|
|
|
this._addLiveProperty('label', config.label)
|
2017-03-02 21:37:34 +00:00
|
|
|
this._addLiveProperty('icon', config.icon)
|
2018-02-12 17:53:04 +00:00
|
|
|
this._addLiveProperty('showCloseButton', config.showCloseButton)
|
|
|
|
const defaultChild = (config.items instanceof TouchBar) ? config.items : new TouchBar(config.items)
|
|
|
|
this._addLiveProperty('child', defaultChild)
|
|
|
|
this.child.ordereredItems.forEach((item) => item._addParent(this))
|
2016-11-29 07:00:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-02 22:32:38 +00:00
|
|
|
TouchBar.TouchBarSlider = class TouchBarSlider extends TouchBarItem {
|
2016-11-27 11:54:12 +00:00
|
|
|
constructor (config) {
|
2017-03-02 22:29:59 +00:00
|
|
|
super()
|
|
|
|
if (config == null) config = {}
|
2018-02-12 17:53:04 +00:00
|
|
|
this._addImmutableProperty('type', 'slider')
|
2018-09-13 16:10:51 +00:00
|
|
|
const { change, label, minValue, maxValue, value } = config
|
2017-03-01 22:54:43 +00:00
|
|
|
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') {
|
2018-02-12 17:53:04 +00:00
|
|
|
this._addImmutableProperty('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)
|
2018-02-12 17:53:04 +00:00
|
|
|
})
|
2016-11-27 11:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-03 17:54:46 +00:00
|
|
|
TouchBar.TouchBarSpacer = class TouchBarSpacer extends TouchBarItem {
|
|
|
|
constructor (config) {
|
|
|
|
super()
|
|
|
|
if (config == null) config = {}
|
2018-02-12 17:53:04 +00:00
|
|
|
this._addImmutableProperty('type', 'spacer')
|
|
|
|
this._addImmutableProperty('size', config.size)
|
2017-03-03 17:54:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-10 06:40:39 +00:00
|
|
|
TouchBar.TouchBarSegmentedControl = class TouchBarSegmentedControl extends TouchBarItem {
|
|
|
|
constructor (config) {
|
|
|
|
super()
|
|
|
|
if (config == null) config = {}
|
2018-09-13 16:10:51 +00:00
|
|
|
const { segmentStyle, segments, selectedIndex, change, mode } = config
|
2018-02-12 17:53:04 +00:00
|
|
|
this._addImmutableProperty('type', 'segmented_control')
|
2017-03-10 06:56:26 +00:00
|
|
|
this._addLiveProperty('segmentStyle', segmentStyle)
|
|
|
|
this._addLiveProperty('segments', segments || [])
|
2017-03-10 06:40:39 +00:00
|
|
|
this._addLiveProperty('selectedIndex', selectedIndex)
|
2017-04-28 04:50:58 +00:00
|
|
|
this._addLiveProperty('mode', mode)
|
2017-03-10 06:40:39 +00:00
|
|
|
|
|
|
|
if (typeof change === 'function') {
|
2018-02-12 17:53:04 +00:00
|
|
|
this._addImmutableProperty('onInteraction', (details) => {
|
2017-03-10 06:56:26 +00:00
|
|
|
this._selectedIndex = details.selectedIndex
|
2017-05-03 10:25:50 +00:00
|
|
|
change(details.selectedIndex, details.isSelected)
|
2018-02-12 17:53:04 +00:00
|
|
|
})
|
2017-03-10 06:40:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-12 23:51:12 +00:00
|
|
|
TouchBar.TouchBarScrubber = class TouchBarScrubber extends TouchBarItem {
|
2017-03-13 00:00:10 +00:00
|
|
|
constructor (config) {
|
2017-03-12 23:51:12 +00:00
|
|
|
super()
|
|
|
|
if (config == null) config = {}
|
2018-09-13 16:10:51 +00:00
|
|
|
const { items, selectedStyle, overlayStyle, showArrowButtons, continuous, mode } = config
|
|
|
|
let { select, highlight } = config
|
2018-02-12 17:53:04 +00:00
|
|
|
this._addImmutableProperty('type', 'scrubber')
|
2017-03-12 23:51:12 +00:00
|
|
|
this._addLiveProperty('items', items)
|
2017-03-14 07:57:57 +00:00
|
|
|
this._addLiveProperty('selectedStyle', selectedStyle || null)
|
2017-03-15 16:47:07 +00:00
|
|
|
this._addLiveProperty('overlayStyle', overlayStyle || null)
|
2017-03-14 07:57:57 +00:00
|
|
|
this._addLiveProperty('showArrowButtons', showArrowButtons || false)
|
|
|
|
this._addLiveProperty('mode', mode || 'free')
|
2017-07-27 07:49:05 +00:00
|
|
|
this._addLiveProperty('continuous', typeof continuous === 'undefined' ? true : continuous)
|
2017-03-12 23:51:12 +00:00
|
|
|
|
2017-03-14 21:02:48 +00:00
|
|
|
if (typeof select === 'function' || typeof highlight === 'function') {
|
|
|
|
if (select == null) select = () => {}
|
|
|
|
if (highlight == null) highlight = () => {}
|
2018-02-12 17:53:04 +00:00
|
|
|
this._addImmutableProperty('onInteraction', (details) => {
|
|
|
|
if (details.type === 'select' && typeof select === 'function') {
|
2017-03-14 21:02:48 +00:00
|
|
|
select(details.selectedIndex)
|
2018-02-12 17:53:04 +00:00
|
|
|
} else if (details.type === 'highlight' && typeof highlight === 'function') {
|
2017-03-14 21:02:48 +00:00
|
|
|
highlight(details.highlightedIndex)
|
2017-03-12 23:51:12 +00:00
|
|
|
}
|
2018-02-12 17:53:04 +00:00
|
|
|
})
|
2017-03-12 23:51:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-29 07:55:07 +00:00
|
|
|
module.exports = TouchBar
|