2016-11-27 11:54:12 +00:00
|
|
|
class TouchBar {
|
|
|
|
constructor (items) {
|
2016-11-29 07:55:07 +00:00
|
|
|
this.items = items
|
2016-11-27 11:54:12 +00:00
|
|
|
if (!Array.isArray(items)) {
|
|
|
|
throw new Error('The items object provided has to be an array')
|
|
|
|
}
|
|
|
|
items.forEach((item) => {
|
|
|
|
if (!item.id) {
|
|
|
|
throw new Error('Each item must be an instance of a TouchBarItem')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
toJSON () {
|
2016-11-29 07:55:07 +00:00
|
|
|
return this.items.map((item) => item.toJSON ? item.toJSON() : item)
|
2016-11-27 11:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-29 07:55:07 +00:00
|
|
|
let itemIdIncrementor = 1
|
|
|
|
const itemEventHandlers = {}
|
2016-11-27 11:54:12 +00:00
|
|
|
|
2016-11-28 10:43:39 +00:00
|
|
|
TouchBar._event = (itemType, eventArgs) => {
|
|
|
|
let args = eventArgs.slice(1)
|
|
|
|
if (itemType === 'slider') {
|
|
|
|
args = args.map(val => parseInt(val, 10))
|
|
|
|
}
|
2016-11-29 07:55:07 +00:00
|
|
|
const idParts = eventArgs[0].split('.')
|
|
|
|
const itemId = idParts[idParts.length - 1]
|
|
|
|
if (itemEventHandlers[itemId]) itemEventHandlers[itemId](...args)
|
2016-11-27 11:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class TouchBarItem {
|
|
|
|
constructor (config) {
|
2016-11-29 07:55:07 +00:00
|
|
|
this.id = itemIdIncrementor++
|
2016-11-27 11:54:12 +00:00
|
|
|
const mConfig = Object.assign({}, config || {})
|
|
|
|
Object.defineProperty(this, 'config', {
|
|
|
|
configurable: false,
|
|
|
|
enumerable: false,
|
|
|
|
get: () => mConfig
|
|
|
|
})
|
2016-11-29 07:55:07 +00:00
|
|
|
this.config.id = `${this.config.id || this.id}`
|
2016-11-27 11:54:12 +00:00
|
|
|
if (typeof this.config !== 'object' || this.config === null) {
|
|
|
|
throw new Error('Provided config must be a non-null object')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-16 06:24:51 +00:00
|
|
|
updateConfig(newConfig) {
|
|
|
|
if (!this._owner) {
|
|
|
|
throw new Error('Cannot call methods on TouchBarItems without assigning to a BrowserWindow');
|
|
|
|
}
|
|
|
|
const dupConfig = Object.assign({}, newConfig);
|
|
|
|
delete dupConfig.id;
|
|
|
|
Object.assign(this.config, dupConfig);
|
|
|
|
this._owner._updateTouchBarItem(this.toJSON());
|
|
|
|
}
|
|
|
|
|
2016-11-27 11:54:12 +00:00
|
|
|
toJSON () {
|
|
|
|
return this.config
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TouchBar.Button = class TouchBarButton extends TouchBarItem {
|
|
|
|
constructor (config) {
|
|
|
|
super(config)
|
2016-11-28 07:24:48 +00:00
|
|
|
this.config.type = 'button'
|
2016-11-27 11:54:12 +00:00
|
|
|
const click = config.click
|
|
|
|
if (typeof click === 'function') {
|
2016-11-29 07:55:07 +00:00
|
|
|
itemEventHandlers[`${this.id}`] = click
|
2016-11-27 11:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TouchBar.ColorPicker = class TouchBarColorPicker extends TouchBarItem {
|
|
|
|
constructor (config) {
|
|
|
|
super(config)
|
2016-11-28 07:24:48 +00:00
|
|
|
this.config.type = 'colorpicker'
|
|
|
|
const change = this.config.change
|
2016-11-27 11:54:12 +00:00
|
|
|
if (typeof change === 'function') {
|
2016-11-29 07:55:07 +00:00
|
|
|
itemEventHandlers[`${this.id}`] = change
|
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)
|
2016-11-29 07:36:57 +00:00
|
|
|
this.config.type = 'group'
|
|
|
|
}
|
|
|
|
|
|
|
|
toJSON () {
|
2016-11-29 07:55:07 +00:00
|
|
|
const config = this.config
|
2016-11-29 07:36:57 +00:00
|
|
|
return Object.assign({}, config, {
|
|
|
|
items: config.items && config.items.toJSON ? config.items.toJSON() : []
|
|
|
|
})
|
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)
|
2016-11-29 07:36:57 +00:00
|
|
|
this.config.type = 'label'
|
2016-11-28 10:43:39 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-27 11:54:12 +00:00
|
|
|
|
2016-11-29 07:00:08 +00:00
|
|
|
TouchBar.PopOver = class TouchBarPopOver extends TouchBarItem {
|
|
|
|
constructor (config) {
|
|
|
|
super(config)
|
|
|
|
this.config.type = 'popover'
|
|
|
|
}
|
|
|
|
|
|
|
|
toJSON () {
|
2016-11-29 07:55:07 +00:00
|
|
|
const config = this.config
|
2016-11-29 07:00:08 +00:00
|
|
|
return Object.assign({}, config, {
|
|
|
|
touchBar: config.touchBar && config.touchBar.toJSON ? config.touchBar.toJSON() : []
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-27 11:54:12 +00:00
|
|
|
TouchBar.Slider = class TouchBarSlider extends TouchBarItem {
|
|
|
|
constructor (config) {
|
|
|
|
super(config)
|
2016-11-29 07:55:07 +00:00
|
|
|
this.config.type = 'slider'
|
2016-11-28 07:24:48 +00:00
|
|
|
const change = this.config.change
|
2016-11-27 11:54:12 +00:00
|
|
|
if (typeof change === 'function') {
|
2016-11-29 07:55:07 +00:00
|
|
|
itemEventHandlers[this.id] = change
|
2016-11-27 11:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-29 07:55:07 +00:00
|
|
|
module.exports = TouchBar
|