Skip to content

Commit 0e69a68

Browse files
committed
Moved window mgmt to util file
1 parent eb90bf5 commit 0e69a68

File tree

2 files changed

+85
-58
lines changed

2 files changed

+85
-58
lines changed

packages/monitor/lib/launchpad-monitor.js

Lines changed: 11 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ import chalk from 'chalk';
66
import autoBind from 'auto-bind';
77
import pDebounce from 'p-debounce';
88
import pm2 from 'pm2';
9-
import semver from 'semver';
109
import { spawn } from 'cross-spawn';
1110
import { SubEmitterSocket } from 'axon'; // used by PM2
12-
import { windowManager } from 'node-window-manager';
1311

1412
import { LogManager, Logger } from '@bluecadet/launchpad-utils';
1513
import AppLogRouter from './app-log-router.js';
1614
import { AppLogOptions, MonitorOptions, WindowOptions } from './monitor-options.js';
15+
import sortWindows, { SortApp } from './utils/sort-windows.js';
1716

1817
export class LaunchpadMonitor {
1918
/** @type {MonitorOptions} */
@@ -388,71 +387,25 @@ export class LaunchpadMonitor {
388387
* @returns {Promise}
389388
*/
390389
async _applyWindowSettings(appNames = null) {
391-
const currVersion = process.version;
392-
const requVersion = this._config.windowsApi.nodeVersion;
393-
394-
if (!semver.satisfies(currVersion, requVersion)) {
395-
this._logger.warn(`Not applying window settings since your node version '${currVersion}' doesn't satisfy the required version '${requVersion}'. Please upgrade node to apply window settings like foreground/minimize/hide.`);
396-
return;
397-
}
398-
399390
appNames = this._validateAppNames(appNames);
400-
401-
this._logger.info(`Applying window settings to ${appNames.length} ${appNames.length === 1 ? 'app' : 'apps'}`);
402-
403-
404-
const fgPids = new Set();
405-
const minPids = new Set();
406-
const hidePids = new Set();
407-
408-
windowManager.requestAccessibility();
409-
const visibleWindows = windowManager.getWindows().filter(win => win.isVisible());
410-
const visiblePids = new Set(visibleWindows.map(win => win.processId));
391+
const apps = [];
411392

412393
for (const appName of appNames) {
413-
const appOptions = this.getAppOptions(appName);
414-
const winOptions = appOptions.windows;
415-
const appProcess = await this.getAppProcess(appName);
416-
417-
if (!appProcess || appProcess.pm2_env.status !== 'online') {
418-
this._logger.warn(`Not applying window settings to ${chalk.blue(appName)} because it's not online.`);
419-
continue;
420-
}
394+
const sortApp = new SortApp();
395+
sortApp.options = this.getAppOptions(appName);
421396

422-
if (!visiblePids.has(appProcess.pid)) {
423-
this._logger.warn(`No window found for ${chalk.blue(appName)} with pid ${chalk.blue(appProcess.pid)}.`);
397+
try {
398+
const process = await this.getAppProcess(appName);
399+
sortApp.pid = process.pid;
400+
} catch (error) {
401+
this._logger.error(`Could not get process for app ${appName}`);
424402
continue;
425403
}
426404

427-
if (winOptions.hide) {
428-
hidePids.add(appProcess.pid);
429-
}
430-
if (winOptions.minimize) {
431-
minPids.add(appProcess.pid);
432-
}
433-
if (winOptions.foreground) {
434-
fgPids.add(appProcess.pid);
435-
}
405+
apps.push(sortApp);
436406
}
437407

438-
visibleWindows.filter(win => hidePids.has(win.processId)).forEach(win => {
439-
this._logger.info(`Hiding ${chalk.blue(win.getTitle())} (pid: ${chalk.blue(win.processId)})`);
440-
win.hide();
441-
});
442-
visibleWindows.filter(win => minPids.has(win.processId)).forEach(win => {
443-
this._logger.info(`Minimizing ${chalk.blue(win.getTitle())} (pid: ${chalk.blue(win.processId)})`);
444-
win.minimize();
445-
});
446-
visibleWindows.filter(win => fgPids.has(win.processId)).forEach(win => {
447-
this._logger.info(`Foregrounding ${chalk.blue(win.getTitle())} (pid: ${chalk.blue(win.processId)})`);
448-
win.bringToTop();
449-
});
450-
451-
this._logger.debug(`✅ Done applying window settings.`);
452-
}
453-
454-
_isWindowsOS() {
455-
return process.platform === 'win32';
408+
return sortWindows(apps, this._logger, this._config.windowsApi.nodeVersion);
456409
}
457410

458411
async _connectPm2Bus() {
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import semver from 'semver';
2+
import chalk from 'chalk';
3+
import { windowManager } from 'node-window-manager';
4+
import { Logger } from '@bluecadet/launchpad-utils';
5+
import { AppOptions } from '../monitor-options.js';
6+
7+
export class SortApp {
8+
/**
9+
* @type {AppOptions}
10+
*/
11+
options = null;
12+
/**
13+
* @type {number}
14+
*/
15+
pid = null;
16+
}
17+
18+
/**
19+
*
20+
* @param {Array.<SortApp>} apps
21+
* @param {Logger} logger
22+
* @param {string} minNodeVersion
23+
* @returns
24+
*/
25+
const sortWindows = async (apps, logger = console, minNodeVersion = undefined) => {
26+
const currNodeVersion = process.version;
27+
if (!semver.satisfies(currNodeVersion, minNodeVersion)) {
28+
return Promise.reject(`Can't sort windows because the current node version '${currNodeVersion}' doesn't satisfy the required version '${minNodeVersion}'. Please upgrade node to apply window settings like foreground/minimize/hide.`);
29+
}
30+
31+
logger.debug(`Applying window settings to ${apps.length} ${apps.length === 1 ? 'app' : 'apps'}`);
32+
33+
const fgPids = new Set();
34+
const minPids = new Set();
35+
const hidePids = new Set();
36+
37+
windowManager.requestAccessibility();
38+
const visibleWindows = windowManager.getWindows().filter(win => win.isVisible());
39+
const visiblePids = new Set(visibleWindows.map(win => win.processId));
40+
41+
for (const app of apps) {
42+
if (!visiblePids.has(app.pid)) {
43+
logger.warn(`No window found for ${chalk.blue(app.options.pm2.name)} with pid ${chalk.blue(app.pid)}.`);
44+
continue;
45+
}
46+
47+
if (app.options.windows.hide) {
48+
hidePids.add(app.pid);
49+
}
50+
if (app.options.windows.minimize) {
51+
minPids.add(app.pid);
52+
}
53+
if (app.options.windows.foreground) {
54+
fgPids.add(app.pid);
55+
}
56+
}
57+
58+
visibleWindows.filter(win => hidePids.has(win.processId)).forEach(win => {
59+
logger.info(`Hiding ${chalk.blue(win.getTitle())} (pid: ${chalk.blue(win.processId)})`);
60+
win.hide();
61+
});
62+
visibleWindows.filter(win => minPids.has(win.processId)).forEach(win => {
63+
logger.info(`Minimizing ${chalk.blue(win.getTitle())} (pid: ${chalk.blue(win.processId)})`);
64+
win.minimize();
65+
});
66+
visibleWindows.filter(win => fgPids.has(win.processId)).forEach(win => {
67+
logger.info(`Foregrounding ${chalk.blue(win.getTitle())} (pid: ${chalk.blue(win.processId)})`);
68+
win.bringToTop();
69+
});
70+
71+
logger.debug(`Done applying window settings.`);
72+
};
73+
74+
export default sortWindows;

0 commit comments

Comments
 (0)