SDA-3990: Hide all windows and restore with previously focused window (#1659)

This commit is contained in:
NguyenTranHoangSym 2023-01-09 15:49:41 +07:00 committed by GitHub
parent 397aa91f8a
commit be174848ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 168 additions and 57 deletions

View File

@ -107,7 +107,7 @@ exports[`about app should render correctly 1`] = `
<p
className="AboutApp-copyright-text"
>
Copyright © 2022 Symphony
Copyright © 2023 Symphony
</p>
</div>
</div>

View File

@ -408,7 +408,7 @@ describe('main api handler', () => {
};
const expectedValue = { send: expect.any(Function) };
ipcMain.send(apiName.symphonyApi, value);
expect(spy).toBeCalledWith(expectedValue, 'main', undefined);
expect(spy).toBeCalledWith(expectedValue, undefined);
});
it('should call `openScreenSnippet` with hideOnCapture correctly', () => {
@ -424,7 +424,7 @@ describe('main api handler', () => {
};
const expectedValue = { send: expect.any(Function) };
ipcMain.send(apiName.symphonyApi, value);
expect(spy).toBeCalledWith(expectedValue, 'main', true);
expect(spy).toBeCalledWith(expectedValue, true);
});
it('should call `closeWindow` correctly', () => {

View File

@ -91,7 +91,6 @@ ipcMain.on(
return;
}
const mainWebContents = windowHandler.getMainWebContents();
const currentWindow = BrowserWindow.getFocusedWindow();
logApiCallParams(arg);
switch (arg.cmd) {
case apiCmds.isOnline:
@ -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();

View File

@ -29,9 +29,11 @@ import {
AnalyticsElements,
ScreenSnippetActionTypes,
} from './analytics-handler';
import { winStore } from './stores';
import { IWindowState } from './stores/window-store';
import { updateAlwaysOnTop } from './window-actions';
import { windowHandler } from './window-handler';
import { getWindowByName, windowExists } from './window-utils';
import { ICustomBrowserWindow, windowHandler } from './window-handler';
import { windowExists } from './window-utils';
const readFile = util.promisify(fs.readFile);
@ -87,20 +89,15 @@ 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 currentWindowName = (currentWindowObj as ICustomBrowserWindow)
?.winName;
const mainWindow = windowHandler.getMainWindow();
if (hideOnCapture) {
const curWindow = getWindowByName(currentWindow || '');
const mainWindow = windowHandler.getMainWindow();
mainWindow?.minimize();
if (currentWindow !== 'main') {
curWindow?.minimize();
}
}
this.storeWindowsState(mainWindow, currentWindowObj);
winStore.hideWindowsOnCapturing(hideOnCapture);
if (mainWindow && windowExists(mainWindow) && isWindowsOS) {
this.shouldUpdateAlwaysOnTop = mainWindow.isAlwaysOnTop();
@ -180,6 +177,7 @@ class ScreenSnippet {
if (dimensions.width === 0 && dimensions.height === 0) {
logger.info('screen-snippet-handler: no screen capture picture');
winStore.restoreWindowsOnCapturing(hideOnCapture);
return;
}
@ -187,20 +185,17 @@ class ScreenSnippet {
windowHandler.createSnippingToolWindow(
this.outputFilePath,
dimensions,
currentWindow,
currentWindowName,
hideOnCapture,
);
this.uploadSnippet(webContents, currentWindow, hideOnCapture);
this.uploadSnippet(webContents, hideOnCapture);
this.closeSnippet();
this.copyToClipboard();
this.saveAs();
return;
}
const {
message,
data,
type,
}: IScreenSnippet = await this.convertFileToData();
const { message, data, type }: IScreenSnippet =
await this.convertFileToData();
logger.info(
`screen-snippet-handler: Snippet captured! Sending data straight to SFE without opening annotate tool`,
);
@ -328,9 +323,7 @@ class ScreenSnippet {
* Gets the dimensions of an image
* @param filePath path to file to get image dimensions of
*/
private getImageDimensions(
filePath: string,
): {
private getImageDimensions(filePath: string): {
height: number;
width: number;
} {
@ -344,11 +337,7 @@ class ScreenSnippet {
* Uploads a screen snippet
* @param webContents A browser window's web contents object
*/
private uploadSnippet(
webContents: WebContents,
currentWindow?: string,
hideOnCapture?: boolean,
) {
private uploadSnippet(webContents: WebContents, hideOnCapture?: boolean) {
ipcMain.once(
'upload-snippet',
async (
@ -367,14 +356,7 @@ class ScreenSnippet {
'screen-snippet-handler: Snippet uploaded correctly, sending payload to SFE',
);
webContents.send('screen-snippet-data', payload);
if (hideOnCapture) {
const curWindow = getWindowByName(currentWindow || '');
const mainWindow = windowHandler.getMainWindow();
mainWindow?.focus();
if (currentWindow !== 'main') {
curWindow?.focus();
}
}
winStore.focusWindowsSnippingFinished(hideOnCapture);
await this.verifyAndUpdateAlwaysOnTop();
} catch (error) {
await this.verifyAndUpdateAlwaysOnTop();
@ -503,6 +485,58 @@ class ScreenSnippet {
},
);
}
/**
* Store current windows state before hiding it
*/
private storeWindowsState = (
mainWindow: ICustomBrowserWindow | null,
currentWindowObj: BrowserWindow | null,
) => {
const windowObj = winStore.getWindowStore();
const currentWindowName = (currentWindowObj as ICustomBrowserWindow)
?.winName;
if (windowObj.windows.length < 1) {
const allWindows = BrowserWindow.getAllWindows();
let windowsArr: IWindowState[] = [];
const mainArr: IWindowState[] = [
{
id: 'main',
focused: mainWindow?.isFocused(),
minimized: mainWindow?.isMinimized(),
},
];
allWindows.forEach((window) => {
if (
(window as ICustomBrowserWindow).winName !== currentWindowName &&
(window as ICustomBrowserWindow).winName !== 'main'
) {
windowsArr.push({
id: (window as ICustomBrowserWindow).winName,
focused: window.isFocused(),
minimized: window?.isMinimized(),
});
}
});
if (currentWindowName !== 'main') {
windowsArr.push({
id: currentWindowName,
focused: currentWindowObj?.isFocused(),
minimized: currentWindowObj?.isMinimized(),
});
windowsArr = mainArr.concat(windowsArr);
} else {
windowsArr = windowsArr.concat(mainArr);
}
winStore.setWindowStore({
windows: windowsArr,
});
}
};
}
const screenSnippet = new ScreenSnippet();

5
src/app/stores/index.ts Normal file
View File

@ -0,0 +1,5 @@
import { WindowStore } from './window-store';
const winStore = new WindowStore();
export { winStore };

View File

@ -0,0 +1,81 @@
import { BrowserWindow } from 'electron';
import { getWindowByName } from '../window-utils';
export interface IWindowObject {
windows: IWindowState[];
}
export interface IWindowState {
id: string;
minimized?: boolean;
focused?: boolean;
}
export class WindowStore {
private windowVariable: IWindowObject = {
windows: [],
};
// Send signal
public setWindowStore = (signalData: IWindowObject) => {
this.windowVariable = { ...signalData };
};
// Destroy signal
public destroyWindowStore = () => {
this.windowVariable = {
windows: [],
} as IWindowObject;
};
// Retrieve signal
public getWindowStore = (): IWindowObject => {
return { ...this.windowVariable } as IWindowObject;
};
public hideWindowsOnCapturing = (hideOnCapture?: boolean) => {
if (hideOnCapture) {
const currentWindows = BrowserWindow.getAllWindows();
currentWindows.forEach((currentWindow) => {
currentWindow?.hide();
});
}
};
public focusWindowsSnippingFinished = (hideOnCapture?: boolean) => {
if (hideOnCapture) {
const currentWindows = this.getWindowStore();
const currentWindow = currentWindows.windows.find(
(currentWindow) => currentWindow.focused,
);
if (currentWindow) {
if (!currentWindow.minimized) {
getWindowByName(currentWindow.id || '')?.show();
}
if (currentWindow.focused) {
getWindowByName(currentWindow.id || '')?.focus();
}
}
}
};
public restoreWindowsOnCapturing = (hideOnCapture?: boolean) => {
if (hideOnCapture) {
const currentWindows = this.getWindowStore();
currentWindows.windows.forEach((currentWindow) => {
if (!currentWindow.minimized) {
getWindowByName(currentWindow.id || '')?.show();
}
if (currentWindow.focused) {
getWindowByName(currentWindow.id || '')?.focus();
}
});
this.destroyWindowStore();
}
};
}

View File

@ -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 {
@ -147,7 +148,8 @@ export class WindowHandler {
private defaultPodUrl: string = 'https://[POD].symphony.com';
private contextIsolation: boolean = true;
private backgroundThrottling: boolean = false;
private windowOpts: ICustomBrowserWindowConstructorOpts = {} as ICustomBrowserWindowConstructorOpts;
private windowOpts: ICustomBrowserWindowConstructorOpts =
{} as ICustomBrowserWindowConstructorOpts;
private globalConfig: IGlobalConfig = {} as IGlobalConfig;
private config: IConfig = {} as IConfig;
// Window reference
@ -1288,7 +1290,7 @@ export class WindowHandler {
height: toolHeight,
parent: getWindowByName(this.currentWindow),
modal: true,
alwaysOnTop: hideOnCapture,
alwaysOnTop: this.hideOnCapture,
resizable: false,
fullscreenable: false,
},
@ -1359,6 +1361,7 @@ export class WindowHandler {
'snipping-tool-data',
snippingToolInfo,
);
winStore.restoreWindowsOnCapturing(this.hideOnCapture);
}
});
this.snippingToolWindow.once('close', () => {
@ -1371,14 +1374,7 @@ export class WindowHandler {
this.deleteFile(snipImage);
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();
}
}
winStore.focusWindowsSnippingFinished(this.hideOnCapture);
});
}