-
Notifications
You must be signed in to change notification settings - Fork 19
/
overlay.js
394 lines (336 loc) · 9.44 KB
/
overlay.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
'use strict';
//dependencies
const electron = require('electron');
const {BrowserWindow,globalShortcut,Tray,Menu,nativeImage} = electron;
const path = require('path');
//check if platform is Mac
const isMac = process.platform === 'darwin';
class Overlay {
constructor() {
this._app = null;
this._win = null;
this._creatingWindow = false;
this._decoratingWindow = false;
this._animating = false;
this._config = {};
this._tray = null;
this._trayAnimation = null;
this._lastFocus = null;
this._forceStartup = false;
//store tray images
this._trayImage = nativeImage.createFromPath(path.join(__dirname, 'images', 'trayTemplate.png'));
this._tray2Image = nativeImage.createFromPath(path.join(__dirname, 'images', 'tray2Template.png'));
this._trayPressedImage = nativeImage.createFromPath(path.join(__dirname, 'images', 'tray-hover.png'));
}
//app started
registerApp(app) {
let startup = false;
//subscribe for config changes only on first time
if(!this._app) {
app.config.subscribe(() => {
if(this._win)
this._refreshConfig(true);
});
startup = true;
}
//load user configs
this._app = app;
//creating the overlay window
this._create(() => {
//open on startup
if((startup && this._config.startup) || this._forceStartup)
this.show();
});
}
//checks if new window could be created
registerWindow(win) {
if(!this._creatingWindow && this._config.unique)
win.close();
else if(this._decoratingWindow)
this._decoratingWindow = false;
}
//creating a new overlay window
_create(fn) {
if(this._win) return;
this._creatingWindow = true;
this._decoratingWindow = true;
this._app.createWindow(win => {
this._win = win;
//apply configurations
this._refreshConfig();
this._setConfigs(win);
//hide window when loses focus
win.on('blur', () => {
if(this._config.hideOnBlur)
this.hide();
});
//store the new size selected
win.on('resize', () => {
if(this._config.resizable && !this._creatingWindow && !this._animating) {
switch(this._config.position) {
case 'top':
case 'bottom':
this._config.size = win.getSize()[1];
break;
case 'right':
case 'left':
this._config.size = win.getSize()[0];
break;
}
}
});
//permanent window
win.on('closed', () => {
this._win = null;
this._clearTrayAnimation();
});
//forces hide initially
win.hide();
//activate terminal
win.rpc.emit('termgroup add req');
//callback
this._creatingWindow = false;
if(fn) fn();
});
}
//apply user overlay window configs
_refreshConfig(reapply) {
//get user configs
let userConfig = this._app.config.getConfig().overlay;
//comparing old and new configs
if(userConfig) {
//hide dock icon or not, check before apply
if(userConfig.unique && userConfig.hideDock)
this._app.dock.hide();
else if(this._config.unique && this._config.hideDock)
this._app.dock.show();
//removing the initial windows of hyperterm
if((userConfig.unique && !this._config.unique) || (userConfig.startAlone && !reapply)) {
this._app.getWindows().forEach(win => {
if(win != this._win)
win.close();
});
}
}
//default configuration
this._config = {
alwaysOnTop: true,
animate: true,
hasShadow: false,
hideDock: false,
hideOnBlur: false,
hotkeys: ['Option+Space'],
position: 'top',
primaryDisplay: false,
resizable: true,
size: 0.4,
startAlone: false,
startup: false,
tray: true,
unique: false
};
//replacing user preferences
if(userConfig) Object.assign(this._config,userConfig);
//registering the hotkeys
globalShortcut.unregisterAll();
for(let hotkey of this._config.hotkeys)
globalShortcut.register(hotkey, () => this.interact());
//tray icon
let trayCreated = false;
if(this._config.tray && !this._tray) {
//prevent destroy / create bug
this._tray = new Tray(this._trayImage);
this._tray.setToolTip('Open HyperTerm Overlay');
this._tray.setPressedImage(this._trayPressedImage);
this._tray.on('click', () => this.interact());
trayCreated = true;
} else if(!this._config.tray && this._tray) {
this._clearTrayAnimation();
this._tray.destroy();
this._tray = null;
}
if(reapply && this._win) {
this._setConfigs(this._win);
this._endBounds(this._win);
//animate tray
if(this._win.isVisible() && trayCreated)
this._animateTray();
}
}
//change windows settings for the overlay window
_setConfigs(win) {
win.setHasShadow(this._config.hasShadow);
win.setResizable(this._config.resizable);
win.setAlwaysOnTop(this._config.alwaysOnTop);
}
//get current display
_getDisplay() {
const screen = electron.screen;
let display = (!this._config.primaryDisplay) ? screen.getDisplayNearestPoint(screen.getCursorScreenPoint()) : screen.getPrimaryDisplay();
return display;
}
_startBounds() {
const {x, y, width, height} = this._getDisplay().workArea;
switch(this._config.position) {
case 'left':
this._win.setBounds({ x: x, y: y, width: 1, height: height }, this._config.animate);
break;
case 'right':
this._win.setBounds({ x: x + width - 1, y: y, width: 1, height: height }, this._config.animate);
break;
case 'bottom':
this._win.setBounds({ x: x, y: y + height, width: width, height: 0 }, this._config.animate);
break;
default:
case 'top':
this._win.setBounds({ x: x, y: y, width: width, height: 0 }, this._config.animate);
break;
}
}
//set window bounds according to config
_endBounds() {
const {x, y, width, height} = this._getDisplay().workArea;
let size;
//end position
switch(this._config.position) {
case 'left':
size = (this._config.size > 1) ? this._config.size : Math.round(width * this._config.size);
this._win.setBounds({ x: x, y: y, width: size, height: height }, this._config.animate);
break;
case 'bottom':
size = (this._config.size > 1) ? this._config.size : Math.round(height * this._config.size);
this._win.setBounds({ x: x, y: y + height - size, width: width, height: size }, this._config.animate);
break;
case 'right':
size = (this._config.size > 1) ? this._config.size : Math.round(width * this._config.size);
this._win.setBounds({ x: width - size, y: y, width: size, height: height }, this._config.animate);
break;
default:
case 'top':
size = (this._config.size > 1) ? this._config.size : Math.round(height * this._config.size);
this._win.setBounds({ x: x, y: y, width: width, height: size }, this._config.animate);
break;
}
}
//tray animation when overlay window is open
_animateTray() {
if(!this._config.tray || !this._tray) return;
//tool tip
this._tray.setToolTip('Close HyperTerm Overlay');
if(isMac) {
if(this._trayAnimation) clearInterval(this._trayAnimation);
let type = 0;
this._trayAnimation = setInterval(() => {
if(this._tray)
this._tray.setImage((++type % 2) ? this._trayImage : this._tray2Image);
},400);
}
}
//finish tray animation
_clearTrayAnimation() {
if(this._trayAnimation) clearInterval(this._trayAnimation);
if(this._tray) {
this._tray.setToolTip('Open HyperTerm Overlay');
if(isMac) this._tray.setImage(this._trayImage);
}
}
//setting initial configuration for the new window
decorateBrowserOptions(config) {
if(this._decoratingWindow) {
return Object.assign({}, config, {
titleBarStyle: '',
frame: false,
minWidth: 0,
minHeight: 0,
maximizable: false,
minimizable: false,
movable: false,
show: false
});
} else
return config;
}
//open or close overlay window
interact() {
if(!this._win) {
//re-create overlay window and show
this._create(() => this.show());
return;
}
if(!this._win.isVisible())
this.show();
else
this.hide();
}
//show the overlay window
show() {
if(!this._win || this._animating || this._win.isVisible()) return;
//store internal window focus
this._lastFocus = BrowserWindow.getFocusedWindow();
//set window initial bounds (for animation)
if(this._config.animate) {
this._animating = true;
setTimeout(() => {
this._animating = false;
},250);
this._startBounds();
}
//show and focus window
this._win.show();
this._win.focus();
//set end bounds
this._endBounds();
this._animateTray();
}
//hides the overlay window
hide() {
if(!this._win || this._animating || !this._win.isVisible()) return;
//search for the better previous windows focus
let findFocus = () => {
if(this._win.isFocused()) {
//chose internal or external focus
if(this._lastFocus && this._lastFocus.sessions && this._lastFocus.sessions.size)
this._lastFocus.focus();
else if(isMac)
Menu.sendActionToFirstResponder('hide:');
}
};
//control the animation
if(this._config.animate) {
this._animating = true;
//animation end bounds
this._startBounds();
setTimeout(() => {
this._animating = false;
findFocus();
this._win.hide();
}, 250);
}
//close without animation
else {
findFocus();
this._win.hide();
}
this._clearTrayAnimation();
}
//unload everything applied
destroy() {
if(this._tray) {
this._tray.destroy();
this._tray = null;
}
if(this._win) {
//open again if is a plugin reload
this._forceStartup = (this._win.isVisible());
this._win.close();
this._win = null;
}
globalShortcut.unregisterAll();
this._creatingWindow = false;
this._decoratingWindow = false;
this._animating = false;
this._config = {};
this._lastFocus = null;
}
}
module.exports = new Overlay();