forked from CWSpear/hyperterm-visor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
visor.js
144 lines (112 loc) · 3.92 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
144
'use strict';
const remove = require('lodash.remove');
const electron = require('electron');
const { globalShortcut } = electron;
const DEBUG = false;
module.exports = class Visor {
constructor(config = {}) {
this.windowStack = [];
this.visorWindow = null;
this.oldBounds = null;
this.config = config;
// if no hotkey, don't do anything!
this.registerGlobalHotkey();
}
setConfig(config = {}) {
this.unregisterGlobalHotkey();
this.config = config;
this.registerGlobalHotkey();
}
registerGlobalHotkey() {
if (!this.config.hotkey) return;
// Register a hotkey shortcut listener.
const wasRegistered = globalShortcut.register(this.config.hotkey, () => this.toggleWindow());
// @TODO error handling on failure?
if (!wasRegistered) {
debug('registration failed');
} else {
debug('registration worked');
}
}
unregisterGlobalHotkey() {
if (!this.config.hotkey) return;
globalShortcut.unregister(this.config.hotkey);
}
toggleWindow() {
debug('toggling window');
if (!this.visorWindow) return;
if (this.visorWindow.isFocused()) {
this.visorWindow.hide();
} else {
this.setBounds();
this.visorWindow.isVisible() ? this.visorWindow.focus() : this.visorWindow.show();
}
}
setBounds() {
debug(`setting position to ${this.config.position}`);
if (!this.config.position) return;
this.oldBounds = this.visorWindow.getBounds();
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;
case 'center':
bounds.width = this.config.width || width / 1.4;
bounds.height = this.config.height || height / 2;
bounds.y = bounds.height / 2;
bounds.x += (display.size.width - bounds.width) / 2;
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);
}
restoreBounds() {
if (!this.config.position) return;
if (this.oldBounds) {
this.visorWindow.setBounds(this.oldBounds);
}
}
registerWindow(win) {
const onClose = () => {
debug('closing', win.id);
remove(this.windowStack, { id: win.id });
if (this.visorWindow.id === win.id) {
this.visorWindow = this.windowStack.length ? this.windowStack[this.windowStack.length - 1] : null;
}
};
debug('registering new window', win.id);
win.on('close', onClose);
this.windowStack.push(win);
this.visorWindow = win;
}
destroy() {
this.unregisterGlobalHotkey();
// @TODO other cleanup?
}
};
function debug(...args) {
if (DEBUG) {
console.log(...args);
}
}