-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
120 lines (106 loc) · 3.55 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const { TouchBar, nativeImage } = require('electron');
let currentUid, options, currentWindow;
exports.onApp = app => {
app.on('browser-window-focus', (event, win) => {
currentWindow = win;
});
};
exports.onWindow = win => {
win.rpc.on('uid set', uid => { currentUid = uid });
currentWindow = win;
populateTouchBar();
};
exports.middleware = () => next => action => {
switch (action.type) {
case 'SESSION_SET_ACTIVE':
case 'SESSION_ADD': {
window.rpc.emit('uid set', action.uid);
break;
}
default: {
break;
}
}
return next(action);
};
exports.decorateConfig = (config) => {
const isSame = JSON.stringify(config.hyperCustomTouchbar) === JSON.stringify(options);
if (!isSame) {
options = config.hyperCustomTouchbar;
populateTouchBar();
}
return config;
};
function populateTouchBar() {
const { TouchBarPopover } = TouchBar;
const tbElements = [];
if (currentWindow && options) {
for (const [index, module] of Object.entries(options)) {
// if there is options array create popover and buttons
if (module.options) {
const buttons = [];
// create buttons for child
for (const [key, btn] of Object.entries(module.options)) {
buttons.push(createTouchBarButton(btn));
}
// create popover for each parent
const popover = new TouchBarPopover({
label: module.label || '',
icon: module.icon ? nativeImage.createFromPath(module.icon) : '',
items: new TouchBar({
items: [
...buttons
]
}),
showCloseButton: true,
});
tbElements.push(popover);
}
// if not array then consider as a button
else {
tbElements.push(createTouchBarButton(module));
}
}
// main touchbar
const touchBar = new TouchBar({
items: [...tbElements]
});
currentWindow.setTouchBar(touchBar);
}
}
function createTouchBarButton(options = {}) {
const { TouchBarButton } = TouchBar;
const label = options.label || '';
const icon = options.icon ? nativeImage.createFromPath(options.icon) : '';
const backgroundColor = options.backgroundColor ? options.backgroundColor : '#363636';
let iconPosition = options.iconPosition ? options.iconPosition : 'left';
// if no icon position and no label then center the icon
if (options.iconPosition === undefined && options.label === undefined)
iconPosition = 'overlay';
const command = options.command || '';
// for vim
const esc = options.esc || false;
// to wait for user input
const promptUser = options.prompt || false;
const button = new TouchBarButton({
label: label,
icon: icon,
backgroundColor: backgroundColor,
iconPosition: iconPosition,
click: () => {
writeToTerminal(command, {
esc: esc,
promptUser: promptUser,
});
}
});
return button;
}
function writeToTerminal(command, options = {}) {
// \x1B ESC char
// \x03 ETX char for ^C
// \x0D CR char for enter
let esc = options.esc ? `\x1B` : `\x03`
let enter = options.promptUser ? `` : `\x0D`
currentWindow.sessions.get(currentUid).write(`${esc} ${command}${enter}`);
}