Skip to content

Commit 3f764a0

Browse files
Added funcionality to guarantee that the windows are open and moved in the correct order, making sure the windows will be moved to the right place when using custom input arguments
1 parent 9eff5df commit 3f764a0

File tree

1 file changed

+114
-8
lines changed

1 file changed

+114
-8
lines changed

src/index.ts

Lines changed: 114 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -210,17 +210,12 @@ function restoreSession(
210210
);
211211
return;
212212
}
213-
return getActiveWindowListFlow();
213+
return;
214214
})
215-
.then(currentWindowList => {
216-
return _startSessionPrograms(savedWindowList, currentWindowList);
215+
.then( () => {
216+
return _startAndWaitPrograms(savedWindowList);
217217
})
218218
.then(() => {
219-
// gets current window list by itself and returns the updated variant
220-
return _waitForAllAppsToStart(savedWindowList);
221-
})
222-
.then((updatedCurrentWindowList: WinObj[]) => {
223-
_updateWindowIds(savedWindowList, updatedCurrentWindowList);
224219
return _restoreWindowPositions(savedWindowList);
225220
})
226221
.then(() => {
@@ -467,6 +462,117 @@ async function _startSessionPrograms(
467462
await Promise.all(promises);
468463
}
469464

465+
466+
// This function is necessary to make possible sending custom arguments to applications to start,
467+
// making sure it will keep track of which windows correspond to which instance of the applications
468+
async function _startAndWaitPrograms(
469+
windowList: WinObj[],
470+
){
471+
472+
// Clear the windowIds from the window objects
473+
windowList.forEach(win=>{
474+
win.windowId = null;
475+
win.windowIdDec = null;
476+
})
477+
478+
// Get the windowIds of all Ids that were previously opened in order to
479+
// avoid these windows from being used isntead of the newly created windows
480+
// (necessary for windows with custom input arguments)
481+
var activeWindows = await getActiveWindowListFlow()
482+
var blackWinIdList = activeWindows.map( win => win.windowId)
483+
484+
// Match all the previously opened windows with the windows that do not have
485+
// custom arguments (in this case, no blacklist needed)
486+
await _matchWindows(windowList, false, [])
487+
488+
var windowsNotStarted = windowList.filter(win => win.windowId === null);
489+
var windowStarted = windowList.filter(win => win.windowId !== null);
490+
var windowsToStart = _getWindowsToStart(windowsNotStarted, windowStarted);
491+
492+
let totalTimeWaited = 0;
493+
// Runs the loop until all windows have been opened or time limit is over
494+
while (windowList.find(win => win.windowId === null)){
495+
totalTimeWaited += CFG.POLL_ALL_APPS_STARTED_INTERVAL;
496+
if (totalTimeWaited > CFG.POLL_ALL_MAX_TIMEOUT * 10) {
497+
console.error("POLL_ALL_MAX_TIMEOUT reached");
498+
windowList.filter(win => win.windowId === null).forEach(e =>{
499+
console.log('Unable to start: ',e.wmClassName)
500+
})
501+
break
502+
}
503+
504+
let promises = windowsToStart.map(
505+
win => {
506+
startProgram(win.executableFile, win.desktopFilePath, win.executableArgs)
507+
}
508+
);
509+
await Promise.all(promises)
510+
511+
// Try to match newly started windows, the black list is needed to avoid matching
512+
// windows that have custom arguments with windows oppened before running lwsm
513+
await _matchWindows( windowList, true, blackWinIdList)
514+
// Update the lists with the windows not to be started yet, the windows to be started next
515+
// and the ones that already have the command to start sent
516+
windowsNotStarted = windowsNotStarted.filter(winNotStarted => !(windowsToStart.find(winToStart => winToStart === winNotStarted)));
517+
windowStarted = windowStarted.concat(windowsToStart);
518+
windowsToStart = _getWindowsToStart(windowsNotStarted, windowStarted);
519+
}
520+
521+
windowList.forEach(win => {win.windowIdDec = parseInt(win.windowId, 16)});
522+
}
523+
524+
525+
// Get, from windowList, all the windows that can be started without the risk of
526+
// mixing same application windows with different arguments. The others will have to wait for the
527+
// previous windows to start, before the command can be issued
528+
function _getWindowsToStart(
529+
windowList: WinObj[],
530+
windowsStarted: WinObj[]
531+
){
532+
var windowsToStart = [];
533+
for (var win of windowList){
534+
let shouldAdd = true;
535+
// If another instance of the same application with different input arguments
536+
// is in the list of windows to start, then the current one should not be added
537+
windowsToStart.forEach( winToRun =>{
538+
if ((win.executableFile === winToRun.executableFile) &&
539+
(win.executableArgs !== winToRun.executableArgs)){
540+
shouldAdd = false;
541+
}
542+
}
543+
)
544+
// If another instance of the same application is in the list of windows that
545+
// have been sent command to start, but still don't have a windowId, the current one should not be added
546+
if (windowsStarted.find(winStarted => (winStarted.windowId === null && winStarted.wmClassName === win.wmClassName)) ){
547+
shouldAdd = false;
548+
}
549+
if (!win.windowId && shouldAdd) {windowsToStart.push(win)};
550+
}
551+
return windowsToStart;
552+
}
553+
554+
555+
// Match the windows that have been opened already to the windows on windowList
556+
async function _matchWindows(
557+
windowList: WinObj[],
558+
includeExecsWithArgs: boolean,
559+
blackWinIdList: String[]
560+
){
561+
var activeWindows = await getActiveWindowListFlow()
562+
// Remove the active windows that already have a window assigned on windowList
563+
// and the ones that are on the black list
564+
activeWindows = activeWindows.filter(actWin => !windowList.find(win => win.windowId === actWin.windowId));
565+
activeWindows = activeWindows.filter(actWin => !blackWinIdList.find(windowId => windowId === actWin.windowId));
566+
567+
for (var win of windowList.filter(win => win.windowId === null)){
568+
let activeWindowMatch = activeWindows.find(actWin => actWin.wmClassName == win.wmClassName);
569+
if (activeWindowMatch && (!win.executableArgs || win.executableArgs === '' || includeExecsWithArgs)){
570+
win.windowId = activeWindowMatch.windowId;
571+
activeWindows = activeWindows.filter(e => e !== activeWindowMatch);
572+
}
573+
}
574+
}
575+
470576
function _getNumberOfInstancesToRun(
471577
windowToMatch: WinObj,
472578
windowList: WinObj[]

0 commit comments

Comments
 (0)