forked from seo-rii/electron-acrylic-window
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
144 lines (126 loc) · 5.01 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const {setVibrancy: wSetVibrancy, disableVibrancy: wDisableVibrancy} = require("bindings")("vibrancy-wrapper");
const os = require("os");
const eBrowserWindow = require("electron").BrowserWindow;
const {nativeTheme, screen} = require("electron");
const supportedType = ['light', 'dark', 'appearance-based'];
function isWindows10() {
if (process.platform !== 'win32') return false;
return os.release().split('.')[0] === '10';
}
function isRS4OrGreater() {
if (!isWindows10()) return false;
return !(os.release().split('.')[1] === '0' && parseInt(os.release().split('.')[2]) < 17134);
}
function getHwnd(win) {
if (!win) throw new TypeError('WINDOW_NOT_GIVEN');
try {
const hbuf = win.getNativeWindowHandle();
if (os.endianness() === "LE") {
return hbuf.readInt32LE();
} else {
return hbuf.readInt32BE();
}
} catch (e) {
throw new TypeError('NOT_VALID_WINDOW');
}
}
class vBrowserWindow extends eBrowserWindow {
constructor(props) {
props.backgroundColor = '#00000000';
props.show = false;
const win = new eBrowserWindow(props);
vBrowserWindow._bindAndReplace(win, vBrowserWindow.setVibrancy);
// Replace window moving behavior to fix mouse polling rate bug
const pollingRate = 144;
win.on('will-move', (e) => {
e.preventDefault()
// Track if the user is moving the window
if (win._moveTimeout) clearTimeout(win._moveTimeout);
win._moveTimeout = setTimeout(
() => {
win._isMoving = false
clearInterval(win._moveInterval)
win._moveInterval = null
}, 1000 / 60)
// Start new behavior if not already
if (!win._isMoving) {
win._isMoving = true
if (win._moveInterval) return false;
// Get start positions
win._moveLastUpdate = 0
win._moveStartBounds = win.getBounds()
win._moveStartCursor = screen.getCursorScreenPoint()
// Poll at 600hz while moving window
win._moveInterval = setInterval(() => {
const now = Date.now()
if (now >= win._moveLastUpdate + (1000 / pollingRate)) {
win._moveLastUpdate = now
const cursor = screen.getCursorScreenPoint()
// Set new position
win.setBounds({
x: win._moveStartBounds.x + (cursor.x - win._moveStartCursor.x),
y: win._moveStartBounds.y + (cursor.y - win._moveStartCursor.y),
width: win._moveStartBounds.width,
height: win._moveStartBounds.height
})
}
}, 1000 / 600)
}
})
// Replace window resizing behavior to fix mouse polling rate bug
win.on('will-resize', (e, newBounds) => {
const now = Date.now()
if (!win._resizeLastUpdate) win._resizeLastUpdate = 0;
if (now >= win._resizeLastUpdate + (1000 / pollingRate)) {
win._resizeLastUpdate = now
} else {
e.preventDefault()
}
})
if (isWindows10() && props.hasOwnProperty('vibrancy')) win.once('ready-to-show', () => {
setTimeout(() => {
win.show();
win.setVibrancy(props.vibrancy);
}, 100);
});
return win;
}
static setVibrancy(op = null) {
if (op) this.setVibrancy(null);
if (!isWindows10()) super.setVibrancy(op);
else {
if (op && supportedType.indexOf(op) === -1) op = 'appearance-based';
if (op === 'appearance-based') {
if (nativeTheme.shouldUseDarkColors) op = 'dark';
else op = 'light';
}
if (op) wSetVibrancy(getHwnd(this), op === 'light' ? 0 : 1, isRS4OrGreater() ? 1 : 0);
else wDisableVibrancy(getHwnd(this));
}
}
static _bindAndReplace(object, method) {
const boundFunction = method.bind(object);
Object.defineProperty(object, method.name, {
get: () => boundFunction
});
}
}
function setVibrancy(win, op = 'appearance-based') {
if (op) setVibrancy(win, null);
if (!isWindows10()) win.setVibrancy(op);
else {
if (op && supportedType.indexOf(op) === -1) op = 'appearance-based';
if (op === 'appearance-based') {
if (nativeTheme.shouldUseDarkColors) op = 'dark';
else op = 'light';
}
if (op) wSetVibrancy(getHwnd(this), op === 'light' ? 0 : 1, isRS4OrGreater() ? 1 : 0);
else wDisableVibrancy(getHwnd(this));
}
}
function disableVibrancy(win) {
setVibrancy(win, null);
}
exports.setVibrancy = setVibrancy;
exports.disableVibrancy = disableVibrancy;
exports.BrowserWindow = vBrowserWindow;