@@ -41,6 +41,8 @@ export const user32 = new ffi.Library('user32', {
41
41
AttachThreadInput : [ 'bool' , [ 'int' , 'long' , 'bool' ] ] ,
42
42
BringWindowToTop : [ 'bool' , [ 'long' ] ] ,
43
43
EnumWindows : [ 'bool' , [ voidPtr , 'int32' ] ] ,
44
+ EnumChildWindows : [ 'bool' , [ 'long' , voidPtr , 'int32' ] ] ,
45
+ EnumThreadWindows : [ 'bool' , [ 'int' , voidPtr , 'int32' ] ] ,
44
46
FindWindowA : [ 'long' , [ lpctStr , lpctStr ] ] ,
45
47
GetForegroundWindow : [ 'long' , [ ] ] ,
46
48
GetTopWindow : [ 'long' , [ 'long' ] ] ,
@@ -136,11 +138,37 @@ export class Rect {
136
138
}
137
139
138
140
export class WindowInfo {
139
- /** @type {number } */
141
+ /**
142
+ * @type {number }
143
+ */
140
144
hwnd = 0 ;
141
- /** @type {number } */
145
+
146
+ /**
147
+ * @type {number }
148
+ */
142
149
pid = 0 ;
150
+
151
+ // /**
152
+ // * @type {string }
153
+ // */
154
+ // name = '';
155
+
156
+ /**
157
+ * @param {number } hwnd
158
+ * @param {number? } pid
159
+ * @param {string? } name
160
+ */
161
+ constructor ( hwnd , pid = null , name = null ) {
162
+ this . hwnd = hwnd ;
163
+ this . pid = pid || getWindowPid ( hwnd ) ;
164
+ // this.name = name || getWindowName(hwnd);
165
+ }
166
+
167
+ /**
168
+ * @returns {string }
169
+ */
143
170
toString ( ) {
171
+ // return `(pid: ${this.pid}, hwnd: ${this.hwnd}, name: ${this.name})`;
144
172
return `(pid: ${ this . pid } , hwnd: ${ this . hwnd } )` ;
145
173
}
146
174
}
@@ -262,21 +290,28 @@ export const getWindowPlacement = function (hwnd) {
262
290
export const getWindowPid = ( hwnd ) => {
263
291
const buffer = ref . alloc ( lpdwordPtr ) ;
264
292
user32 . GetWindowThreadProcessId ( hwnd , buffer ) ;
265
- // The process ID is returned as a LPDWORD, so
266
- // we have to read it in the correct format
267
- // @see https://stackoverflow.com/questions/58477755/what-is-the-equivalent-of-dword-in-nodejs
293
+ /**
294
+ * The process ID is returned as a LPDWORD, so we have to read it in the correct format
295
+ * @see https://stackoverflow.com/questions/58477755/what-is-the-equivalent-of-dword-in-nodejs
296
+ */
268
297
return buffer . readUInt32LE ( ) ;
269
298
}
270
299
271
300
/**
272
- * @param @type {function(WindowInfo) : boolean } callback Return true to continue iterating, false to stop.
301
+ * @param {function(WindowInfo) : boolean } callback Return ` true` to continue iterating, ` false` to stop.
273
302
*/
274
303
export const enumWindows = ( callback ) => {
275
304
user32 . EnumWindows ( ffi . Callback ( 'bool' , [ 'long' , 'int32' ] , ( hwnd , lParam ) => {
276
- const window = new WindowInfo ( ) ;
277
- window . hwnd = hwnd ;
278
- window . pid = getWindowPid ( hwnd ) ;
279
- return callback ( window ) ;
305
+ const win = new WindowInfo ( hwnd ) ;
306
+
307
+ if ( ! callback ( win ) ) {
308
+ return false ;
309
+ }
310
+
311
+ user32 . EnumChildWindows ( win . hwnd , ffi . Callback ( 'bool' , [ 'long' , 'int32' ] , ( childHwnd , lParam ) => {
312
+ const childWin = new WindowInfo ( childHwnd ) ;
313
+ return callback ( childWin ) ;
314
+ } ) , lParam ) ;
280
315
} ) , 0 ) ;
281
316
} ;
282
317
@@ -298,7 +333,7 @@ export const getAllWindows = () => {
298
333
*/
299
334
export const getWindowsByName = ( name ) => {
300
335
name = name . toLowerCase ( ) ;
301
- return getAllWindows ( ) . filter ( win => getWindowName ( win ) . toLowerCase ( ) . includes ( name ) ) ;
336
+ return getAllWindows ( ) . filter ( win => getWindowName ( win . hwnd ) . toLowerCase ( ) . includes ( name ) ) ;
302
337
}
303
338
304
339
/**
@@ -321,22 +356,20 @@ export const getWindowsByPid = (pid) => {
321
356
*/
322
357
export const sortWindows = ( fgPids , minPids , hidePids , fakeKey = defaultFakeKey ) => {
323
358
sendKeyTap ( fakeKey ) ;
324
- const allWins = getAllWindows ( ) ;
325
- allWins . filter ( win => hidePids . includes ( win . pid ) ) . forEach ( win => {
326
- if ( isWindowVisible ( win . hwnd ) ) {
327
- user32 . ShowWindow ( win . hwnd , ShowWindowModes . SW_HIDE ) ;
328
- }
359
+
360
+ const allVisibleWins = getAllWindows ( ) . filter ( win => {
361
+ return isWindowVisible ( win . hwnd ) ;
329
362
} ) ;
330
- allWins . filter ( win => minPids . includes ( win . pid ) ) . forEach ( win => {
331
- if ( isWindowVisible ( win . hwnd ) ) {
332
- user32 . ShowWindow ( win . hwnd , ShowWindowModes . SW_SHOWMINNOACTIVE ) ;
333
- }
363
+
364
+ allVisibleWins . filter ( win => hidePids . includes ( win . pid ) ) . forEach ( win => {
365
+ user32 . ShowWindow ( win . hwnd , ShowWindowModes . SW_HIDE ) ;
334
366
} ) ;
335
- allWins . filter ( win => fgPids . includes ( win . pid ) ) . forEach ( win => {
336
- if ( isWindowVisible ( win . hwnd ) ) {
337
- user32 . ShowWindow ( win . hwnd , ShowWindowModes . SW_SHOW ) ;
338
- user32 . SetForegroundWindow ( win . hwnd ) ;
339
- }
367
+ allVisibleWins . filter ( win => minPids . includes ( win . pid ) ) . forEach ( win => {
368
+ user32 . ShowWindow ( win . hwnd , ShowWindowModes . SW_SHOWMINNOACTIVE ) ;
369
+ } ) ;
370
+ allVisibleWins . filter ( win => fgPids . includes ( win . pid ) ) . forEach ( win => {
371
+ user32 . ShowWindow ( win . hwnd , ShowWindowModes . SW_SHOW ) ;
372
+ user32 . SetForegroundWindow ( win . hwnd ) ;
340
373
} ) ;
341
374
}
342
375
@@ -376,7 +409,9 @@ export const foregroundWindowsByPid = (pid, fakeKey = defaultFakeKey) => {
376
409
getWindowsByPid ( pid ) . forEach ( win => {
377
410
// See: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow
378
411
if ( isWindowVisible ( win . hwnd ) ) {
379
- user32 . ShowWindow ( win . hwnd , ShowWindowModes . SW_SHOWMINNOACTIVE ) ;
412
+ // user32.ShowWindow(win.hwnd, ShowWindowModes.SW_SHOWNA);
413
+ // user32.ShowWindow(win.hwnd, ShowWindowModes.SW_SHOWMINNOACTIVE);
414
+ user32 . ShowWindow ( win . hwnd , ShowWindowModes . SW_SHOWMINIMIZED ) ;
380
415
}
381
416
} ) ;
382
417
}
0 commit comments