This repository has been archived by the owner on Jul 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathvisor.js
143 lines (117 loc) · 4.01 KB
/
visor.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
'use strict';
const electron = require('electron');
const { BrowserWindow, Menu } = electron;
const DEBUG = process.env.NODE_ENV === 'development' || process.env.DEBUG || false;
const isMac = process.platform === 'darwin';
let log;
if (DEBUG) {
log = require('electron-log');
log.transports.file.level = 'silly';
}
module.exports = class Visor {
constructor(app, visorWindow = null) {
this.app = app;
this.config = app.config.getConfig().visor || {};
this.visorWindow = visorWindow;
this.visorWindow.on('close', () => this.handleOnVisorWindowClose());
this.previousAppFocus = null;
if (this.config.hideDock) {
this.app.dock.hide();
}
if (this.visorWindow) {
this.setBounds();
}
}
toggleWindow() {
debug('toggling window');
console.error('test2');
if (!this.visorWindow) {
// if no visor window, create one and try toggling again after it's created
this.createNewVisorWindow(() => this.setBounds());
return;
}
if (this.visorWindow.isFocused()) {
if (!this.visorWindow.isFullScreen()) {
this.visorWindow.hide();
}
this.returnFocus();
} else {
this.setBounds();
if (this.visorWindow.isVisible()) {
this.visorWindow.focus();
} else {
this.previousAppFocus = BrowserWindow.getFocusedWindow();
this.visorWindow.show(() => debug('test'));
this.visorWindow.focus();
}
}
}
setBounds() {
debug(`setting position to ${this.config.position}`);
if (!this.config.position) return;
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;
// fall through
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;
// fall through
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);
}
createNewVisorWindow(callback) {
debug('creating new window');
this.app.createWindow(win => {
this.visorWindow = win;
// creates a shell in the new window
win.rpc.emit('termgroup add req');
this.visorWindow.on('close', () => this.handleOnVisorWindowClose());
if (callback) {
callback();
}
});
}
returnFocus() {
// this attempts to return focus to the app that previously had focus before Hyper
if (((this.previousAppFocus || {}).sessions || {}).size) {
this.previousAppFocus.focus();
} else if (isMac) {
Menu.sendActionToFirstResponder('hide:');
}
}
handleOnVisorWindowClose() {
debug('closing');
this.visorWindow = null;
}
destroy() {
this.visorWindow = null;
this.previousAppFocus = null;
debug('destroyed');
// @TODO other cleanup?
}
};
function debug(...args) {
if (DEBUG) {
console.error(...args);
log.info(...args);
}
}