Skip to content

Commit

Permalink
SDA-3990: Hide all windows and restore with previously focused window
Browse files Browse the repository at this point in the history
  • Loading branch information
NguyenTranHoangSym committed Dec 21, 2022
1 parent 33d11d7 commit 02bfe7e
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 23 deletions.
7 changes: 1 addition & 6 deletions src/app/main-api-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ ipcMain.on(
return;
}
const mainWebContents = windowHandler.getMainWebContents();
const currentWindow = BrowserWindow.getFocusedWindow();
logApiCallParams(arg);
switch (arg.cmd) {
case apiCmds.isOnline:
Expand Down Expand Up @@ -204,11 +203,7 @@ ipcMain.on(
}
break;
case apiCmds.openScreenSnippet:
screenSnippet.capture(
event.sender,
(currentWindow as ICustomBrowserWindow)?.winName,
arg.hideOnCapture,
);
screenSnippet.capture(event.sender, arg.hideOnCapture);
break;
case apiCmds.closeScreenSnippet:
screenSnippet.cancelCapture();
Expand Down
62 changes: 50 additions & 12 deletions src/app/screen-snippet-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ import {
AnalyticsElements,
ScreenSnippetActionTypes,
} from './analytics-handler';
import { winStore } from './stores';
import { updateAlwaysOnTop } from './window-actions';
import { windowHandler } from './window-handler';
import { ICustomBrowserWindow, windowHandler } from './window-handler';
import { getWindowByName, windowExists } from './window-utils';

const readFile = util.promisify(fs.readFile);
Expand Down Expand Up @@ -87,19 +88,37 @@ class ScreenSnippet {
*
* @param webContents {WeContents}
*/
public async capture(
webContents: WebContents,
currentWindow?: string,
hideOnCapture?: boolean,
) {
public async capture(webContents: WebContents, hideOnCapture?: boolean) {
const currentWindowObj = BrowserWindow.getFocusedWindow();
const windowObj = winStore.getWindowStore();
const currentWindow = (currentWindowObj as ICustomBrowserWindow)?.winName;
const mainWindow = windowHandler.getMainWindow();

if (windowObj.windowIds.length < 1) {
const allWindows = BrowserWindow.getAllWindows();
const arr: string[] = ['main'];

allWindows.forEach((window) => {
if (
(window as ICustomBrowserWindow).winName !== currentWindow ||
(window as ICustomBrowserWindow).winName !== 'main'
) {
arr.push((window as ICustomBrowserWindow).winName);
}
});

arr.push(currentWindow);
winStore.setWindowStore({
windowIds: arr,
});
}

if (hideOnCapture) {
const curWindow = getWindowByName(currentWindow || '');
const mainWindow = windowHandler.getMainWindow();
mainWindow?.minimize();
if (currentWindow !== 'main') {
curWindow?.minimize();
}
const currentWindows = BrowserWindow.getAllWindows();

currentWindows.forEach((currentWindow) => {
currentWindow?.minimize();
});
}

if (mainWindow && windowExists(mainWindow) && isWindowsOS) {
Expand Down Expand Up @@ -180,6 +199,25 @@ class ScreenSnippet {

if (dimensions.width === 0 && dimensions.height === 0) {
logger.info('screen-snippet-handler: no screen capture picture');

if (hideOnCapture) {
const currentFocusedWindow = winStore.getWindowStore();

currentFocusedWindow.windowIds.forEach((currentWindow) => {
getWindowByName(currentWindow || '')?.focus();
});

// A temporary solution to restore focused minimized window
// to set it always on top
getWindowByName(
currentFocusedWindow.windowIds[
currentFocusedWindow.windowIds.length - 1
] || '',
)?.setAlwaysOnTop(true);

winStore.destroyWindowStore();
}

return;
}

Expand Down
5 changes: 5 additions & 0 deletions src/app/stores/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { WindowStore } from './window-store';

const winStore = new WindowStore();

export { winStore };
26 changes: 26 additions & 0 deletions src/app/stores/window-store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
interface IWindowObject {
windowIds: string[];
}

export class WindowStore {
private windowVariable: IWindowObject = {
windowIds: [],
};

// Send signal
public setWindowStore = (signalData: IWindowObject) => {
this.windowVariable = { ...signalData };
};

// Destroy signal
public destroyWindowStore = () => {
this.windowVariable = {
windowIds: [],
} as IWindowObject;
};

// Retrieve signal
public getWindowStore = (): IWindowObject => {
return { ...this.windowVariable } as IWindowObject;
};
}
15 changes: 10 additions & 5 deletions src/app/window-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import LocalMenuShortcuts from './local-menu-shortcuts';
import { mainEvents } from './main-event-handler';
import { exportLogs } from './reports-handler';
import { SpellChecker } from './spell-check-handler';
import { winStore } from './stores';
import { checkIfBuildExpired } from './ttl-handler';
import { versionHandler } from './version-handler';
import {
Expand Down Expand Up @@ -1372,11 +1373,15 @@ export class WindowHandler {
this.removeWindow(opts.winKey);
this.screenPickerWindow = null;
if (this.hideOnCapture) {
const curWindow = getWindowByName(currentWindow || '');
const mainWindow = windowHandler.getMainWindow();
mainWindow?.focus();
if (currentWindow !== 'main') {
curWindow?.focus();
if (hideOnCapture) {
const currentFocusedWindow = winStore.getWindowStore();

currentFocusedWindow.windowIds.forEach((currentWindow) => {
getWindowByName(currentWindow)?.setAlwaysOnTop(false);
getWindowByName(currentWindow)?.focus();
});

winStore.destroyWindowStore();
}
}
});
Expand Down

0 comments on commit 02bfe7e

Please sign in to comment.