This repository has been archived by the owner on May 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodule.js
220 lines (200 loc) · 6.54 KB
/
module.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
const electron = require('electron');
const ffi = require('ffi-napi');
// create foreign function
const user32 = new ffi.Library('user32', {
'GetForegroundWindow': ['long', []],
'ShowWindow': ['bool', ['long', 'int']],
'GetWindowRect': ['bool', ['long', 'pointer']],
'SetWindowPos': ['bool', ['long', 'long', 'int', 'int', 'int', 'int', 'uint']],
'GetWindowTextA': ['int32', ['int32','string','int32']]
});
// create rectangle from pointer
const pointerToRect = function (rectPointer) {
return {
left: rectPointer.readInt16LE(0),
top: rectPointer.readInt16LE(4),
right: rectPointer.readInt16LE(8),
bottom: rectPointer.readInt16LE(12)
};
}
// obtain window dimension
const getWindowDimensions = function (handle) {
const rectPointer = Buffer.alloc(16);
const getWindowRect = user32.GetWindowRect(handle, rectPointer);
return !getWindowRect
? null
: pointerToRect(rectPointer);
}
// get the active window title
const getWindowTitle = function (handle) {
const title = Buffer.alloc(256);
const window = user32.GetWindowTextA(handle, title, 256);
return title.toString().substr(0, window);
}
module.exports = function (position) {
// electron screens
const screens = electron.screen;
// current mouse pointer position
const currentMousePosition = screens.getCursorScreenPoint();
const { scaleFactor, depthPerComponent, workArea } = screens.getAllDisplays().length > 1
// get monitor where mouse is active
? screens.getDisplayNearestPoint(currentMousePosition)
// get the primary monitor
: screens.getPrimaryDisplay();
// convert workArea to dip screen rectangle
const updatedWorkArea = screens.dipToScreenRect(null, workArea);
// multiply value by current display scale factor
const multiplyByCurrentDisplayScaleFactor = function (value) {
return value * scaleFactor;
};
// window shadow margin { left: 7, top: 0, right: 7, bottom: 7 } + 1px border on 100% scale
const computedComponentDepth = multiplyByCurrentDisplayScaleFactor(depthPerComponent - scaleFactor);
// workArea
const workAreaX = updatedWorkArea.x;
const workAreaY = updatedWorkArea.y;
const workAreaWidth = updatedWorkArea.width;
const workAreaHeight = updatedWorkArea.height;
// computed bounds
const boundsXDefault = workAreaX - computedComponentDepth;
const boundsXMiddle = workAreaX - computedComponentDepth + workAreaWidth / 2;
const boundsYDefault = workAreaY;
const boundsYMiddle = workAreaY + workAreaHeight / 2;
const boundsWidthFull = workAreaWidth + (computedComponentDepth * 2);
const boundsWidthHalf = workAreaWidth / 2 + (computedComponentDepth * 2);
const boundsHeightFull = workAreaHeight + computedComponentDepth;
const boundsHeightHalf = workAreaHeight / 2 + computedComponentDepth;
// get active window
const activeWindow = user32.GetForegroundWindow();
// get active window title and prevent specific windows modals
const activeWindowTitle = getWindowTitle(activeWindow);
const forbiddenTitles = [
'feeds',
'search',
'cortana',
'task view',
'snap assist',
'action center',
'volume control',
'task switching',
'people bar flyout',
'battery information',
'network connections',
'windows ink workspace',
'date and time information'
];
if (activeWindowTitle === '' || forbiddenTitles.includes(activeWindowTitle.toLowerCase())) {
return false;
}
// get and set window dimension
const activeWindowDimensions = getWindowDimensions(activeWindow);
// create bounds object
const bounds = {}
switch (position) {
// upper left
case 'ul':
bounds.x = boundsXDefault;
bounds.y = boundsYDefault;
bounds.w = boundsWidthHalf;
bounds.h = boundsHeightHalf;
break;
// upper half
case 'uh':
bounds.x = boundsXDefault;
bounds.y = boundsYDefault;
bounds.w = boundsWidthFull;
bounds.h = boundsHeightHalf;
break;
// upper right
case 'ur':
bounds.x = boundsXMiddle;
bounds.y = boundsYDefault;
bounds.w = boundsWidthHalf;
bounds.h = boundsHeightHalf;
break;
// half left
case 'hl':
bounds.x = boundsXDefault;
bounds.y = boundsYDefault;
bounds.w = boundsWidthHalf;
bounds.h = boundsHeightFull;
break;
// center
case 'c':
const currentWidth = activeWindowDimensions.right - activeWindowDimensions.left;
const currentHeight = activeWindowDimensions.bottom - activeWindowDimensions.top;
const halfScreenWidth = ((workAreaWidth / 2) + workAreaX);
const halfScreenHeight = ((workAreaHeight / 2) + workAreaY);
bounds.x = halfScreenWidth - (currentWidth / 2);
bounds.y = halfScreenHeight - (currentHeight / 2);
bounds.w = currentWidth;
bounds.h = currentHeight;
break;
// half right
case 'hr':
bounds.x = boundsXMiddle;
bounds.y = boundsYDefault;
bounds.w = boundsWidthHalf;
bounds.h = boundsHeightFull;
break;
// lower left
case 'll':
bounds.x = boundsXDefault;
bounds.y = boundsYMiddle;
bounds.w = boundsWidthHalf;
bounds.h = boundsHeightHalf;
break;
// lower half
case 'lh':
bounds.x = boundsXDefault;
bounds.y = boundsYMiddle;
bounds.w = boundsWidthFull;
bounds.h = boundsHeightHalf;
break;
// lower right
case 'lr':
bounds.x = boundsXMiddle;
bounds.y = boundsYMiddle;
bounds.w = boundsWidthHalf;
bounds.h = boundsHeightHalf;
break;
// fallback
default:
console.log(
'+ Supported Position:\n' +
'- Upper Left: \t windowControl("ul") \n' +
'- Upper Half: \t windowControl("uh") \n' +
'- Upper Right: \t windowControl("ur") \n' +
'- Half Left: \t windowControl("hl") \n' +
'- Center: \t\t windowControl("c") \n' +
'- Half Right: \t windowControl("hr") \n' +
'- Lower Left: \t windowControl("ll") \n' +
'- Lower Half: \t windowControl("lh") \n' +
'- Lower Right: \t windowControl("lr")'
);
break;
}
// force active window to restore mode
user32.ShowWindow(activeWindow, 9);
// set window position based on bounds values
user32.SetWindowPos(
activeWindow,
0,
bounds.x,
bounds.y,
bounds.w,
bounds.h,
0x4000 | 0x0020 | 0x0020 | 0x0040
);
// moving from different screen scales requires re-run of SetWindowPos
setTimeout(function () {
user32.SetWindowPos(
activeWindow,
0,
bounds.x,
bounds.y,
bounds.w,
bounds.h,
0x4000 | 0x0020 | 0x0020 | 0x0040
);
}, 0);
}