electron/lib/browser/api/touch-bar.js

366 lines
11 KiB
JavaScript
Raw Normal View History

2020-03-20 20:28:31 +00:00
'use strict';
2020-03-20 20:28:31 +00:00
const { EventEmitter } = require('events');
2020-03-20 20:28:31 +00:00
let nextItemID = 1;
class TouchBar extends EventEmitter {
// Bind a touch bar to a window
static _setOnWindow (touchBar, window) {
if (window._touchBar != null) {
2020-03-20 20:28:31 +00:00
window._touchBar._removeFromWindow(window);
}
if (touchBar == null) {
2020-03-20 20:28:31 +00:00
window._setTouchBarItems([]);
return;
}
if (Array.isArray(touchBar)) {
2020-03-20 20:28:31 +00:00
touchBar = new TouchBar(touchBar);
}
2020-03-20 20:28:31 +00:00
touchBar._addToWindow(window);
}
constructor (options) {
2020-03-20 20:28:31 +00:00
super();
if (options == null) {
2020-03-20 20:28:31 +00:00
throw new Error('Must specify options object as first argument');
}
2020-03-20 20:28:31 +00:00
let { items, escapeItem } = options;
if (!Array.isArray(items)) {
2020-03-20 20:28:31 +00:00
items = [];
}
this.changeListener = (item) => {
2020-03-20 20:28:31 +00:00
this.emit('change', item.id, item.type);
};
2020-03-20 20:28:31 +00:00
this.windowListeners = {};
this.items = {};
this.ordereredItems = [];
this.escapeItem = escapeItem;
2017-03-02 17:30:21 +00:00
const registerItem = (item) => {
2020-03-20 20:28:31 +00:00
this.items[item.id] = item;
item.on('change', this.changeListener);
if (item.child instanceof TouchBar) {
2020-03-20 20:28:31 +00:00
item.child.ordereredItems.forEach(registerItem);
}
2020-03-20 20:28:31 +00:00
};
2020-03-20 20:28:31 +00:00
let hasOtherItemsProxy = false;
const idSet = new Set();
items.forEach((item) => {
if (!(item instanceof TouchBarItem)) {
2020-03-20 20:28:31 +00:00
throw new Error('Each item must be an instance of TouchBarItem');
}
if (item.type === 'other_items_proxy') {
if (!hasOtherItemsProxy) {
2020-03-20 20:28:31 +00:00
hasOtherItemsProxy = true;
} else {
2020-03-20 20:28:31 +00:00
throw new Error('Must only have one OtherItemsProxy per TouchBar');
}
}
if (!idSet.has(item.id)) {
2020-03-20 20:28:31 +00:00
idSet.add(item.id);
} else {
2020-03-20 20:28:31 +00:00
throw new Error('Cannot add a single instance of TouchBarItem multiple times in a TouchBar');
}
2020-03-20 20:28:31 +00:00
});
// register in separate loop after all items are validated
for (const item of items) {
2020-03-20 20:28:31 +00:00
this.ordereredItems.push(item);
registerItem(item);
}
2017-03-01 18:55:28 +00:00
}
set escapeItem (item) {
if (item != null && !(item instanceof TouchBarItem)) {
2020-03-20 20:28:31 +00:00
throw new Error('Escape item must be an instance of TouchBarItem');
}
if (this.escapeItem != null) {
2020-03-20 20:28:31 +00:00
this.escapeItem.removeListener('change', this.changeListener);
}
2020-03-20 20:28:31 +00:00
this._escapeItem = item;
if (this.escapeItem != null) {
2020-03-20 20:28:31 +00:00
this.escapeItem.on('change', this.changeListener);
}
2020-03-20 20:28:31 +00:00
this.emit('escape-item-change', item);
}
get escapeItem () {
2020-03-20 20:28:31 +00:00
return this._escapeItem;
}
2017-03-01 18:55:28 +00:00
_addToWindow (window) {
2020-03-20 20:28:31 +00:00
const { id } = window;
2017-03-01 18:55:28 +00:00
// Already added to window
2020-03-20 20:28:31 +00:00
if (Object.prototype.hasOwnProperty.call(this.windowListeners, id)) return;
2020-03-20 20:28:31 +00:00
window._touchBar = this;
2017-03-01 18:55:28 +00:00
const changeListener = (itemID) => {
2020-03-20 20:28:31 +00:00
window._refreshTouchBarItem(itemID);
};
this.on('change', changeListener);
2017-03-01 18:55:28 +00:00
const escapeItemListener = (item) => {
2020-03-20 20:28:31 +00:00
window._setEscapeTouchBarItem(item != null ? item : {});
};
this.on('escape-item-change', escapeItemListener);
2017-03-01 18:55:28 +00:00
const interactionListener = (event, itemID, details) => {
2020-03-20 20:28:31 +00:00
let item = this.items[itemID];
if (item == null && this.escapeItem != null && this.escapeItem.id === itemID) {
2020-03-20 20:28:31 +00:00
item = this.escapeItem;
}
if (item != null && item.onInteraction != null) {
2020-03-20 20:28:31 +00:00
item.onInteraction(details);
}
2020-03-20 20:28:31 +00:00
};
window.on('-touch-bar-interaction', interactionListener);
2017-03-01 18:55:28 +00:00
const removeListeners = () => {
2020-03-20 20:28:31 +00:00
this.removeListener('change', changeListener);
this.removeListener('escape-item-change', escapeItemListener);
window.removeListener('-touch-bar-interaction', interactionListener);
window.removeListener('closed', removeListeners);
window._touchBar = null;
delete this.windowListeners[id];
const unregisterItems = (items) => {
for (const item of items) {
2020-03-20 20:28:31 +00:00
item.removeListener('change', this.changeListener);
if (item.child instanceof TouchBar) {
2020-03-20 20:28:31 +00:00
unregisterItems(item.child.ordereredItems);
}
}
2020-03-20 20:28:31 +00:00
};
unregisterItems(this.ordereredItems);
if (this.escapeItem) {
2020-03-20 20:28:31 +00:00
this.escapeItem.removeListener('change', this.changeListener);
}
2020-03-20 20:28:31 +00:00
};
window.once('closed', removeListeners);
this.windowListeners[id] = removeListeners;
2017-03-01 18:55:28 +00:00
2020-03-20 20:28:31 +00:00
window._setTouchBarItems(this.ordereredItems);
escapeItemListener(this.escapeItem);
2017-03-01 18:55:28 +00:00
}
_removeFromWindow (window) {
2020-03-20 20:28:31 +00:00
const removeListeners = this.windowListeners[window.id];
if (removeListeners != null) removeListeners();
}
}
2017-03-01 00:08:12 +00:00
class TouchBarItem extends EventEmitter {
2017-03-02 22:29:59 +00:00
constructor () {
2020-03-20 20:28:31 +00:00
super();
this._addImmutableProperty('id', `${nextItemID++}`);
this._parents = [];
}
_addImmutableProperty (name, value) {
Object.defineProperty(this, name, {
get: function () {
2020-03-20 20:28:31 +00:00
return value;
},
set: function () {
2020-03-20 20:28:31 +00:00
throw new Error(`Cannot override property ${name}`);
},
enumerable: true,
configurable: false
2020-03-20 20:28:31 +00:00
});
}
2017-03-01 22:54:43 +00:00
_addLiveProperty (name, initialValue) {
2020-03-20 20:28:31 +00:00
const privateName = `_${name}`;
this[privateName] = initialValue;
2017-03-01 22:54:43 +00:00
Object.defineProperty(this, name, {
get: function () {
2020-03-20 20:28:31 +00:00
return this[privateName];
2017-03-01 22:54:43 +00:00
},
set: function (value) {
2020-03-20 20:28:31 +00:00
this[privateName] = value;
this.emit('change', this);
2017-03-01 22:54:43 +00:00
},
enumerable: true
2020-03-20 20:28:31 +00:00
});
2017-03-01 22:54:43 +00:00
}
_addParent (item) {
2020-03-20 20:28:31 +00:00
const existing = this._parents.some(test => test.id === item.id);
if (!existing) {
this._parents.push({
id: item.id,
type: item.type
2020-03-20 20:28:31 +00:00
});
}
}
}
TouchBar.TouchBarButton = class TouchBarButton extends TouchBarItem {
constructor (config) {
2020-03-20 20:28:31 +00:00
super();
if (config == null) config = {};
this._addImmutableProperty('type', 'button');
this._addLiveProperty('label', config.label);
this._addLiveProperty('accessibilityLabel', config.accessibilityLabel);
this._addLiveProperty('backgroundColor', config.backgroundColor);
this._addLiveProperty('icon', config.icon);
this._addLiveProperty('iconPosition', config.iconPosition);
this._addLiveProperty('enabled', typeof config.enabled !== 'boolean' ? true : config.enabled);
if (typeof config.click === 'function') {
this._addImmutableProperty('onInteraction', () => {
2020-03-20 20:28:31 +00:00
config.click();
});
2017-03-01 22:54:43 +00:00
}
}
2020-03-20 20:28:31 +00:00
};
TouchBar.TouchBarColorPicker = class TouchBarColorPicker extends TouchBarItem {
constructor (config) {
2020-03-20 20:28:31 +00:00
super();
if (config == null) config = {};
this._addImmutableProperty('type', 'colorpicker');
this._addLiveProperty('availableColors', config.availableColors);
this._addLiveProperty('selectedColor', config.selectedColor);
if (typeof config.change === 'function') {
this._addImmutableProperty('onInteraction', (details) => {
2020-03-20 20:28:31 +00:00
this._selectedColor = details.color;
config.change(details.color);
});
}
}
2020-03-20 20:28:31 +00:00
};
TouchBar.TouchBarGroup = class TouchBarGroup extends TouchBarItem {
2016-11-28 07:24:48 +00:00
constructor (config) {
2020-03-20 20:28:31 +00:00
super();
if (config == null) config = {};
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
}
2020-03-20 20:28:31 +00:00
};
TouchBar.TouchBarLabel = class TouchBarLabel extends TouchBarItem {
constructor (config) {
2020-03-20 20:28:31 +00:00
super();
if (config == null) config = {};
this._addImmutableProperty('type', 'label');
this._addLiveProperty('label', config.label);
this._addLiveProperty('accessibilityLabel', config.accessibilityLabel);
this._addLiveProperty('textColor', config.textColor);
}
2020-03-20 20:28:31 +00:00
};
TouchBar.TouchBarPopover = class TouchBarPopover extends TouchBarItem {
constructor (config) {
2020-03-20 20:28:31 +00:00
super();
if (config == null) config = {};
this._addImmutableProperty('type', 'popover');
this._addLiveProperty('label', config.label);
this._addLiveProperty('icon', config.icon);
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));
}
2020-03-20 20:28:31 +00:00
};
TouchBar.TouchBarSlider = class TouchBarSlider extends TouchBarItem {
constructor (config) {
2020-03-20 20:28:31 +00:00
super();
if (config == null) config = {};
this._addImmutableProperty('type', 'slider');
this._addLiveProperty('label', config.label);
this._addLiveProperty('minValue', config.minValue);
this._addLiveProperty('maxValue', config.maxValue);
this._addLiveProperty('value', config.value);
if (typeof config.change === 'function') {
this._addImmutableProperty('onInteraction', (details) => {
2020-03-20 20:28:31 +00:00
this._value = details.value;
config.change(details.value);
});
}
}
2020-03-20 20:28:31 +00:00
};
2017-03-03 17:54:46 +00:00
TouchBar.TouchBarSpacer = class TouchBarSpacer extends TouchBarItem {
constructor (config) {
2020-03-20 20:28:31 +00:00
super();
if (config == null) config = {};
this._addImmutableProperty('type', 'spacer');
this._addImmutableProperty('size', config.size);
2017-03-03 17:54:46 +00:00
}
2020-03-20 20:28:31 +00:00
};
2017-03-03 17:54:46 +00:00
2017-03-10 06:40:39 +00:00
TouchBar.TouchBarSegmentedControl = class TouchBarSegmentedControl extends TouchBarItem {
constructor (config) {
2020-03-20 20:28:31 +00:00
super();
if (config == null) config = {};
this._addImmutableProperty('type', 'segmented_control');
this._addLiveProperty('segmentStyle', config.segmentStyle);
this._addLiveProperty('segments', config.segments || []);
this._addLiveProperty('selectedIndex', config.selectedIndex);
this._addLiveProperty('mode', config.mode);
2017-03-10 06:40:39 +00:00
if (typeof config.change === 'function') {
this._addImmutableProperty('onInteraction', (details) => {
2020-03-20 20:28:31 +00:00
this._selectedIndex = details.selectedIndex;
config.change(details.selectedIndex, details.isSelected);
});
2017-03-10 06:40:39 +00:00
}
}
2020-03-20 20:28:31 +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) {
2020-03-20 20:28:31 +00:00
super();
if (config == null) config = {};
let { select, highlight } = config;
this._addImmutableProperty('type', 'scrubber');
this._addLiveProperty('items', config.items);
this._addLiveProperty('selectedStyle', config.selectedStyle || null);
this._addLiveProperty('overlayStyle', config.overlayStyle || null);
this._addLiveProperty('showArrowButtons', config.showArrowButtons || false);
this._addLiveProperty('mode', config.mode || 'free');
const cont = typeof config.continuous === 'undefined' ? true : config.continuous;
this._addLiveProperty('continuous', cont);
2017-03-12 23:51:12 +00:00
2017-03-14 21:02:48 +00:00
if (typeof select === 'function' || typeof highlight === 'function') {
2020-03-20 20:28:31 +00:00
if (select == null) select = () => {};
if (highlight == null) highlight = () => {};
this._addImmutableProperty('onInteraction', (details) => {
if (details.type === 'select' && typeof select === 'function') {
2020-03-20 20:28:31 +00:00
select(details.selectedIndex);
} else if (details.type === 'highlight' && typeof highlight === 'function') {
2020-03-20 20:28:31 +00:00
highlight(details.highlightedIndex);
2017-03-12 23:51:12 +00:00
}
2020-03-20 20:28:31 +00:00
});
2017-03-12 23:51:12 +00:00
}
}
2020-03-20 20:28:31 +00:00
};
2017-03-12 23:51:12 +00:00
TouchBar.TouchBarOtherItemsProxy = class TouchBarOtherItemsProxy extends TouchBarItem {
constructor (config) {
2020-03-20 20:28:31 +00:00
super();
this._addImmutableProperty('type', 'other_items_proxy');
}
2020-03-20 20:28:31 +00:00
};
2020-03-20 20:28:31 +00:00
module.exports = TouchBar;