Skip to content

Commit 91a2821

Browse files
committed
Addded child windows to getWindows()
1 parent c7eb766 commit 91a2821

File tree

2 files changed

+62
-27
lines changed

2 files changed

+62
-27
lines changed

packages/monitor/lib/launchpad-monitor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ export class LaunchpadMonitor {
408408

409409
appNames = this._validateAppNames(appNames);
410410

411-
this._logger.info(`Applying window settings to apps: ${appNames}...`);
411+
this._logger.info(`Applying window settings to ${appNames}...`);
412412

413413
let windowsApi = null;
414414
try {

packages/monitor/lib/windows-api.js

Lines changed: 61 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ export const user32 = new ffi.Library('user32', {
4141
AttachThreadInput: ['bool', ['int', 'long', 'bool']],
4242
BringWindowToTop: ['bool', ['long']],
4343
EnumWindows: ['bool', [voidPtr, 'int32']],
44+
EnumChildWindows: ['bool', ['long', voidPtr, 'int32']],
45+
EnumThreadWindows: ['bool', ['int', voidPtr, 'int32']],
4446
FindWindowA: ['long', [lpctStr, lpctStr]],
4547
GetForegroundWindow: ['long', []],
4648
GetTopWindow: ['long', ['long']],
@@ -136,11 +138,37 @@ export class Rect {
136138
}
137139

138140
export class WindowInfo {
139-
/** @type {number} */
141+
/**
142+
* @type {number}
143+
*/
140144
hwnd = 0;
141-
/** @type {number} */
145+
146+
/**
147+
* @type {number}
148+
*/
142149
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+
*/
143170
toString() {
171+
// return `(pid: ${this.pid}, hwnd: ${this.hwnd}, name: ${this.name})`;
144172
return `(pid: ${this.pid}, hwnd: ${this.hwnd})`;
145173
}
146174
}
@@ -262,21 +290,28 @@ export const getWindowPlacement = function (hwnd) {
262290
export const getWindowPid = (hwnd) => {
263291
const buffer = ref.alloc(lpdwordPtr);
264292
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+
*/
268297
return buffer.readUInt32LE();
269298
}
270299

271300
/**
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.
273302
*/
274303
export const enumWindows = (callback) => {
275304
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);
280315
}), 0);
281316
};
282317

@@ -298,7 +333,7 @@ export const getAllWindows = () => {
298333
*/
299334
export const getWindowsByName = (name) => {
300335
name = name.toLowerCase();
301-
return getAllWindows().filter(win => getWindowName(win).toLowerCase().includes(name));
336+
return getAllWindows().filter(win => getWindowName(win.hwnd).toLowerCase().includes(name));
302337
}
303338

304339
/**
@@ -321,22 +356,20 @@ export const getWindowsByPid = (pid) => {
321356
*/
322357
export const sortWindows = (fgPids, minPids, hidePids, fakeKey = defaultFakeKey) => {
323358
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);
329362
});
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);
334366
});
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);
340373
});
341374
}
342375

@@ -376,7 +409,9 @@ export const foregroundWindowsByPid = (pid, fakeKey = defaultFakeKey) => {
376409
getWindowsByPid(pid).forEach(win => {
377410
// See: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow
378411
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);
380415
}
381416
});
382417
}

0 commit comments

Comments
 (0)