-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisor.js
More file actions
134 lines (117 loc) · 3.47 KB
/
visor.js
File metadata and controls
134 lines (117 loc) · 3.47 KB
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
const electron = require('electron');
const { Menu, BrowserWindow } = electron;
const { showInCurrentWorkspace, isMac } = require('./utils');
class Visor {
constructor(app, opt) {
this.app = app;
this.config = opt;
// Hide the icon.
if (this.config.hideDock) this.app.dock.hide();
}
onTrigger() {
if (!this.visorWindow) {
this.previouslyFocusedApp = BrowserWindow.getFocusedWindow();
this.createVisorWindow(() => {
this.layoutWindow();
this.visorWindow.focus();
});
return;
}
const focused = this.visorWindow.isFocused();
const visible = this.visorWindow.isVisible();
const fullScreen = this.visorWindow.isFullScreen();
if (focused) {
if (!fullScreen) {
this.toggleVisible(false);
}
this.returnFocus();
return;
}
if (visible) {
this.layoutWindow();
this.toggleVisible(true);
return;
}
this.previouslyFocusedApp = BrowserWindow.getFocusedWindow();
this.layoutWindow();
this.toggleVisible(true);
}
createVisorWindow(cb) {
// FIXME: not save this window's layout as default, otherwise it would create a full-size terminal the next time terminal startup.
this.app.createWindow(window => {
this.visorWindow = window;
this.visorWindow.on('close', () => {
this.handleVisorWindowClose();
});
this.visorWindow.on('blur', () => {
if (this.config.hideOnBlur) {
this.visorWindow.hide();
this.returnFocus();
}
});
window.rpc.emit('termgroup add req'); // Init terminal in the new window.
if (cb) cb();
});
}
playSound() {}
/**
* Layout the visor on the correct position.
*/
layoutWindow() {
const screen = electron.screen;
const point = screen.getCursorScreenPoint();
const display = screen.getDisplayNearestPoint(point);
const bounds = display.workArea;
const { height, width } = bounds;
switch (this.config.position) {
case 'bottom':
bounds.y += this.config.height || height / 2;
bounds.width = this.config.width || width;
case 'top':
bounds.height = this.config.height || height / 2;
bounds.width = this.config.width || width;
break;
case 'right':
bounds.x += this.config.width || width / 2;
bounds.height = this.config.height || height;
case 'left':
bounds.width = this.config.width || width / 2;
bounds.height = this.config.height || height;
break;
}
bounds.y = Math.round(bounds.y);
bounds.width = Math.round(bounds.width);
bounds.x = Math.round(bounds.x);
bounds.height = Math.round(bounds.height);
this.visorWindow.setBounds(bounds);
}
handleVisorWindowClose() {
this.visorWindow = null;
}
toggleVisible(visible) {
if (visible) {
this.visorWindow.show();
this.visorWindow.focus();
showInCurrentWorkspace(this.visorWindow);
} else {
this.visorWindow.hide();
}
}
returnFocus() {
if (((this.previouslyFocusedApp || {}).sessions || {}).size) {
this.previouslyFocusedApp.focus();
}
// FIXME: return focus without hiding other windows of Hyper.
// else if (isMac) {
// // If it's on Mac, missing this would lead to focusing on Hyper.
// Menu.sendActionToFirstResponder('hide:');
// }
}
dispose() {
if (this.visorWindow) {
this.visorWindow.dispose();
this.visorWindow = null;
}
}
}
module.exports = Visor;