Typescript - Add login to close screen snippet

This commit is contained in:
Kiran Niranjan 2018-12-06 10:33:23 +05:30
parent b70777bd81
commit 7771ea5b41
4 changed files with 60 additions and 6 deletions

View File

@ -5,7 +5,14 @@ import { LocaleType } from '../common/i18n';
import { logger } from '../common/logger'; import { logger } from '../common/logger';
import { activityDetection } from './activity-detection'; import { activityDetection } from './activity-detection';
import { screenSnippet } from './screen-snippet'; import { screenSnippet } from './screen-snippet';
import { isValidWindow, setDataUrl, showBadgeCount, showPopupMenu, updateLocale } from './window-utils'; import {
isValidWindow,
sanitize,
setDataUrl,
showBadgeCount,
showPopupMenu,
updateLocale,
} from './window-utils';
/** /**
* Handle API related ipc messages from renderers. Only messages from windows * Handle API related ipc messages from renderers. Only messages from windows
@ -64,12 +71,12 @@ ipcMain.on(apiName.symphonyApi, (event: Electron.Event, arg: IApiArgs) => {
configureNotification.openConfigurationWindow(arg.windowName); configureNotification.openConfigurationWindow(arg.windowName);
} }
break; break;
case ApiCmds.sanitize: */case apiCmds.sanitize:
if (typeof arg.windowName === 'string') { if (typeof arg.windowName === 'string') {
sanitize(arg.windowName); sanitize(arg.windowName);
} }
break; break;
case ApiCmds.bringToFront: /*case ApiCmds.bringToFront:
// validates the user bring to front config and activates the wrapper // validates the user bring to front config and activates the wrapper
if (typeof arg.reason === 'string' && arg.reason === 'notification') { if (typeof arg.reason === 'string' && arg.reason === 'notification') {
bringToFront(arg.windowName, arg.reason); bringToFront(arg.windowName, arg.reason);

View File

@ -55,7 +55,7 @@ class ScreenSnippet {
// only allow one screen capture at a time. // only allow one screen capture at a time.
if (this.child) this.child.kill(); if (this.child) this.child.kill();
try { try {
this.child = await this.execCmd(this.captureUtil, this.captureUtilArgs); await this.execCmd(this.captureUtil, this.captureUtilArgs);
const { message, data, type } = await this.convertFileToData(); const { message, data, type } = await this.convertFileToData();
webContents.send('screen-snippet-data', { message, data, type }); webContents.send('screen-snippet-data', { message, data, type });
} catch (error) { } catch (error) {
@ -63,6 +63,15 @@ class ScreenSnippet {
} }
} }
/**
* Kills the child process when the application is reloaded
*/
public killChildProcess(): void {
if (this.child && typeof this.child.kill === 'function') {
this.child.kill();
}
}
/** /**
* Executes the given command via a child process * Executes the given command via a child process
* *
@ -76,7 +85,7 @@ class ScreenSnippet {
*/ */
private execCmd(captureUtil: string, captureUtilArgs: ReadonlyArray<string>): Promise<ChildProcess> { private execCmd(captureUtil: string, captureUtilArgs: ReadonlyArray<string>): Promise<ChildProcess> {
return new Promise<ChildProcess>((resolve, reject) => { return new Promise<ChildProcess>((resolve, reject) => {
return execFile(captureUtil, captureUtilArgs, (error: ExecException | null) => { return this.child = execFile(captureUtil, captureUtilArgs, (error: ExecException | null) => {
if (this.isAlwaysOnTop) updateAlwaysOnTop(true, false); if (this.isAlwaysOnTop) updateAlwaysOnTop(true, false);
if (error && error.killed) { if (error && error.killed) {
// processs was killed, just resolve with no data. // processs was killed, just resolve with no data.

View File

@ -5,6 +5,7 @@ import * as url from 'url';
import { isMac, isWindowsOS } from '../common/env'; import { isMac, isWindowsOS } from '../common/env';
import { i18n, LocaleType } from '../common/i18n'; import { i18n, LocaleType } from '../common/i18n';
import { logger } from '../common/logger'; import { logger } from '../common/logger';
import { screenSnippet } from './screen-snippet';
import { ICustomBrowserWindow, windowHandler } from './window-handler'; import { ICustomBrowserWindow, windowHandler } from './window-handler';
const checkValidWindow = true; const checkValidWindow = true;
@ -217,4 +218,27 @@ export const showPopupMenu = (opts: Electron.PopupOptions): void => {
const appMenu = windowHandler.appMenu; const appMenu = windowHandler.appMenu;
if (appMenu) appMenu.popupMenu({ ...popupOpts, ...opts }); if (appMenu) appMenu.popupMenu({ ...popupOpts, ...opts });
} }
};
/**
* Method that is invoked when the application is reloaded/navigated
* window.addEventListener('beforeunload')
*
* @param windowName {string}
*/
export const sanitize = (windowName: string): void => {
// To make sure the reload event is from the main window
const mainWindow = windowHandler.getMainWindow();
if (mainWindow && windowName === mainWindow.winName) {
// reset the badge count whenever an user refreshes the electron client
showBadgeCount(0);
// Terminates the screen snippet process on reload
if (!isMac) {
screenSnippet.killChildProcess();
}
// TODO: write method to clean up child window
// Closes all the child windows
// windowMgr.cleanUpChildWindows();
}
}; };

View File

@ -150,4 +150,18 @@ ipcRenderer.on('activity', (_event: Event, arg: IActivityDetection) => {
if (typeof arg === 'object' && typeof local.activityDetection === 'function') { if (typeof arg === 'object' && typeof local.activityDetection === 'function') {
local.activityDetection(arg); local.activityDetection(arg);
} }
}); });
// Invoked whenever the app is reloaded/navigated
const sanitize = (): void => {
local.ipcRenderer.send(apiName, {
cmd: apiCmds.sanitize,
windowName: window.name || 'main',
});
};
/**
* Window Events
*/
window.addEventListener('beforeunload', sanitize, false);