Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spec/__snapshots__/aboutApp.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
4 changes: 2 additions & 2 deletions spec/mainApiHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand Down
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
110 changes: 72 additions & 38 deletions src/app/screen-snippet-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -180,27 +177,25 @@ class ScreenSnippet {

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

windowHandler.closeSnippingToolWindow();
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`,
);
Expand Down Expand Up @@ -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;
} {
Expand All @@ -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 (
Expand All @@ -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();
Expand Down Expand Up @@ -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();
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 };
81 changes: 81 additions & 0 deletions src/app/stores/window-store.ts
Original file line number Diff line number Diff line change
@@ -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();
}
};
}
16 changes: 6 additions & 10 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 @@ -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
Expand Down Expand Up @@ -1288,7 +1290,7 @@ export class WindowHandler {
height: toolHeight,
parent: getWindowByName(this.currentWindow),
modal: true,
alwaysOnTop: hideOnCapture,
alwaysOnTop: this.hideOnCapture,
resizable: false,
fullscreenable: false,
},
Expand Down Expand Up @@ -1359,6 +1361,7 @@ export class WindowHandler {
'snipping-tool-data',
snippingToolInfo,
);
winStore.restoreWindowsOnCapturing(this.hideOnCapture);
}
});
this.snippingToolWindow.once('close', () => {
Expand All @@ -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);
});
}

Expand Down