Skip to content

Commit

Permalink
simplify the code
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimiry committed May 20, 2018
1 parent e585f20 commit 924b7d3
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 82 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
"devtron": "1.4.0",
"electron": "2.0.1",
"electron-builder": "20.13.4",
"electron-builder-squirrel-windows": "20.14.0",
"electron-builder-squirrel-windows": "20.14.4",
"electron-rebuild": "1.7.3",
"exports-loader": "0.7.0",
"extended-define-webpack-plugin": "0.1.3",
Expand Down
13 changes: 5 additions & 8 deletions src/electron/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import logger from "electron-log";
import {app} from "electron";

import {IpcMainActions} from "_shared/electron-actions";
import {Environment} from "_shared/model/electron";
import {isAllowedUrl} from "_shared/util";
import {initEndpoints} from "./ipc-main-api";
import {Context} from "./model";
Expand All @@ -18,8 +17,8 @@ electronUnhandled({logger: logger.error});
// tslint:disable-next-line:no-floating-promises
initContext().then(initApp);

export function initApp(ctx: Context) {
if (app.makeSingleInstance(() => activateBrowserWindow(ctx.uiContext))) {
export async function initApp(ctx: Context) {
if (app.makeSingleInstance(() => activateBrowserWindow(ctx))) {
// calling app.exit() instead of app.quit() in order to prevent "Error: Cannot find module ..." error happening
// https://github.com/electron/electron/issues/8862
app.exit();
Expand All @@ -37,11 +36,9 @@ export function initApp(ctx: Context) {
tray: await initTray(ctx, endpoints),
};

((skipEnvs: Environment[]) => {
if (checkForUpdatesAndNotify && skipEnvs.indexOf(ctx.env) === -1) {
initAutoUpdate();
}
})(["development", "e2e"]);
if (checkForUpdatesAndNotify && !["development", "e2e"].includes(ctx.env)) {
initAutoUpdate();
}

app.on("activate", async () => {
// on macOS it's common to re-create a window in the app when the dock icon is clicked and there are no other windows open
Expand Down
8 changes: 4 additions & 4 deletions src/electron/main/ipc-main-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,12 @@ test.serial(`API: ${IpcMainActions.Logout.channel}`, async (t: TestContext) => {
});

test.serial(`API: ${IpcMainActions.Quit.channel}`, async (t: TestContext) => {
const appQuitSpy: sinon.SinonSpy = t.context.mocks.electron.app.quit;
const appQuitSpy: sinon.SinonSpy = t.context.mocks.electron.app.exit;
const endpoints = t.context.endpoints;
const action = endpoints[IpcMainActions.Quit.channel];

await action.process(undefined);
t.is(appQuitSpy.callCount, 1, "electron.app.quit called once");
t.is(appQuitSpy.callCount, 1, "electron.app.exit called once");
});

test.serial(`API: ${IpcMainActions.ToggleBrowserWindow.channel}`, async (t: TestContext) => {
Expand All @@ -356,7 +356,7 @@ test.serial(`API: ${IpcMainActions.ToggleBrowserWindow.channel}`, async (t: Test
];
payloads.forEach((payload) => {
action.process(payload);
t.true(toggleBrowserWindowSpy.calledWithExactly(t.context.ctx.uiContext, payload.forcedState));
t.true(toggleBrowserWindowSpy.calledWithExactly(t.context.ctx, payload.forcedState));
});
});

Expand Down Expand Up @@ -467,7 +467,7 @@ test.beforeEach(async (t: TestContext) => {
},
"electron": {
app: {
quit: sinon.spy(),
exit: sinon.spy(),
},
remote: {
BrowserWindow: sinon.spy(),
Expand Down
1 change: 0 additions & 1 deletion src/electron/main/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export interface Context {
};
configStore: StoreModel.Store<Config>;
settingsStore: StoreModel.Store<Settings>;
forceClose?: boolean;
uiContext?: UIContext;
}

Expand Down
2 changes: 1 addition & 1 deletion src/electron/main/tray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {toggleBrowserWindow} from "./util";

export async function initTray(ctx: Context, endpoints: EndpointsMap): Promise<Tray> {
const tray = new Tray(ctx.locations.trayIcon);
const toggleWindow = () => toggleBrowserWindow(ctx.uiContext);
const toggleWindow = () => toggleBrowserWindow(ctx);
const contextMenu = Menu.buildFromTemplate([
{
label: "Toggle Window",
Expand Down
14 changes: 7 additions & 7 deletions src/electron/main/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {Config, configEncryptionPresetValidator, Settings, settingsAccountLoginU
import {ElectronTransport, Environment} from "_shared/model/electron";
import {ElectronIpcMainActionType} from "_shared/electron-actions/model";
import {ElectronTransportEvent} from "../model";
import {Context, ContextInitOptions, UIContext} from "./model";
import {Context, ContextInitOptions} from "./model";
import {INITIAL_STORES} from "./constants";

export async function initContext(opts: ContextInitOptions = {}): Promise<Context> {
Expand Down Expand Up @@ -81,21 +81,21 @@ export async function buildSettingsAdapter({configStore}: Context, password: str
return new EncryptionAdapter(password, (await configStore.readExisting()).encryptionPreset);
}

export function toggleBrowserWindow(uiContext?: UIContext, forcedState?: boolean) {
if (!uiContext || !uiContext.browserWindow) {
export function toggleBrowserWindow(ctx: Context, forcedState?: boolean) {
const browserWindow = ctx.uiContext && ctx.uiContext.browserWindow;

if (!browserWindow) {
return;
}

const {browserWindow} = uiContext;

if (typeof forcedState !== "undefined" ? forcedState : !browserWindow.isVisible()) {
activateBrowserWindow(uiContext);
activateBrowserWindow(ctx);
} else {
browserWindow.hide();
}
}

export function activateBrowserWindow(uiContext?: UIContext) {
export function activateBrowserWindow({uiContext}: Context) {
if (!uiContext || !uiContext.browserWindow) {
return;
}
Expand Down
32 changes: 15 additions & 17 deletions src/electron/main/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,37 @@ import {Context} from "./model";
import {activateBrowserWindow} from "./util";

export async function initBrowserWindow(ctx: Context): Promise<BrowserWindow> {
const preload = ctx.locations.preload.browser[ctx.env];
const browserWindowConstructorOptions = {
webPreferences: {
nodeIntegration: ctx.env === "development",
webviewTag: true,
webSecurity: true,
// sandbox: true,
disableBlinkFeatures: "Auxclick",
preload,
preload: ctx.locations.preload.browser[ctx.env],
},
icon: ctx.locations.icon,
...(await ctx.configStore.readExisting()).window.bounds,
show: false,
};
const browserWindow = new BrowserWindow(browserWindowConstructorOptions);
const appBeforeQuitEventHandler = () => ctx.forceClose = true;
const appBeforeQuitEventHandler = () => forceClose = true;
let forceClose = false;

app.removeListener("before-quit", appBeforeQuitEventHandler);
app.on("before-quit", appBeforeQuitEventHandler);

browserWindow.on("ready-to-show", async () => {
const settingsNotConfigured = !(await ctx.settingsStore.readable());
const settingsConfigured = await ctx.settingsStore.readable();
const {startMinimized} = await ctx.configStore.readExisting();

if (settingsNotConfigured || !startMinimized) {
activateBrowserWindow(ctx.uiContext);
if (!settingsConfigured || !startMinimized) {
activateBrowserWindow(ctx);
}
});
browserWindow.on("closed", () => {
browserWindow.destroy();
delete ctx.forceClose;
app.removeListener("before-quit", appBeforeQuitEventHandler);
forceClose = false;

// On macOS it is common for applications and their menu bar to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== "darwin") {
Expand All @@ -45,21 +45,19 @@ export async function initBrowserWindow(ctx: Context): Promise<BrowserWindow> {
browserWindow.on("close", async (event) => {
const sender: BrowserWindow = (event as any).sender;

if (ctx.forceClose) {
if (forceClose) {
return event.returnValue = true;
}

event.returnValue = false;
event.preventDefault();

await (async () => {
if ((await ctx.configStore.readExisting()).closeToTray) {
sender.hide();
} else {
ctx.forceClose = true;
browserWindow.close();
}
})();
if ((await ctx.configStore.readExisting()).closeToTray) {
sender.hide();
} else {
forceClose = true;
browserWindow.close();
}

return event.returnValue;
});
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"lib": [
"es2015",
"es2017.object",
"es2016.array.include",
"dom"
],
"baseUrl": ".",
Expand Down
74 changes: 31 additions & 43 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
"7zip-bin-mac" "~1.0.1"
"7zip-bin-win" "~2.2.0"

"7zip-bin@~4.0.2":
version "4.0.2"
resolved "https://registry.yarnpkg.com/7zip-bin/-/7zip-bin-4.0.2.tgz#6abbdc22f33cab742053777a26db2e25ca527179"

"@angular-devkit/[email protected]":
version "0.6.3"
resolved "https://registry.yarnpkg.com/@angular-devkit/build-optimizer/-/build-optimizer-0.6.3.tgz#f455c44f34f56e3714449ca38f8a6c0ca8fdcfc7"
Expand Down Expand Up @@ -218,16 +222,16 @@
"@types/node" "*"

"@types/node@*":
version "10.1.1"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.1.1.tgz#ca39d8607fa1fcb146b0530420b93f1dd4802f6c"
version "10.1.2"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.1.2.tgz#1b928a0baa408fc8ae3ac012cc81375addc147c6"

"@types/[email protected]":
version "8.10.11"
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.10.11.tgz#971ea8cb91adbe0b74e3fbd867dec192d5893a5f"

"@types/node@^8.0.24":
version "8.10.16"
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.10.16.tgz#96fadb371748845a0c8ea970a565330efb0a67d5"
version "8.10.17"
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.10.17.tgz#d48cf10f0dc6dcf59f827f5a3fc7a4a6004318d3"

"@types/[email protected]":
version "1.1.6"
Expand Down Expand Up @@ -558,26 +562,14 @@ [email protected]:
version "1.8.6"
resolved "https://registry.yarnpkg.com/app-builder-bin-linux/-/app-builder-bin-linux-1.8.6.tgz#81176bbcb2929958a90f2184afb54df90b7210a3"

[email protected]:
version "1.8.9"
resolved "https://registry.yarnpkg.com/app-builder-bin-linux/-/app-builder-bin-linux-1.8.9.tgz#478c3269c89a92e35716200bd10497833431fe94"

[email protected]:
version "1.8.6"
resolved "https://registry.yarnpkg.com/app-builder-bin-mac/-/app-builder-bin-mac-1.8.6.tgz#20d7233c5cadf00472e7b0ccaf85627b53f90787"

[email protected]:
version "1.8.9"
resolved "https://registry.yarnpkg.com/app-builder-bin-mac/-/app-builder-bin-mac-1.8.9.tgz#e4047505b0b0556258cdb2af200806401b34767a"

[email protected]:
version "1.8.6"
resolved "https://registry.yarnpkg.com/app-builder-bin-win/-/app-builder-bin-win-1.8.6.tgz#d09f78fb1dd5a5f8ea231294828fd5c9ad0358a5"

[email protected]:
version "1.8.9"
resolved "https://registry.yarnpkg.com/app-builder-bin-win/-/app-builder-bin-win-1.8.9.tgz#3ffcf0b24a00a4653dbc6dc9865b166194fa7067"

[email protected]:
version "1.8.6"
resolved "https://registry.yarnpkg.com/app-builder-bin/-/app-builder-bin-1.8.6.tgz#85604ece9c1b63ed0437abe92ddaf41c88c3f2e4"
Expand All @@ -586,13 +578,9 @@ [email protected]:
app-builder-bin-mac "1.8.6"
app-builder-bin-win "1.8.6"

[email protected]:
version "1.8.9"
resolved "https://registry.yarnpkg.com/app-builder-bin/-/app-builder-bin-1.8.9.tgz#4b79027d85533926630d5ea749c081d671307616"
optionalDependencies:
app-builder-bin-linux "1.8.9"
app-builder-bin-mac "1.8.9"
app-builder-bin-win "1.8.9"
[email protected]:
version "1.9.0"
resolved "https://registry.yarnpkg.com/app-builder-bin/-/app-builder-bin-1.9.0.tgz#700ff08c2558bb27d271c655e8353fe703fa7647"

app-root-path@^2.0.1:
version "2.0.1"
Expand Down Expand Up @@ -1970,12 +1958,12 @@ [email protected]:
stat-mode "^0.2.2"
temp-file "^3.1.2"

builder-util@^5.8.1, builder-util@^5.9.0:
version "5.9.1"
resolved "https://registry.yarnpkg.com/builder-util/-/builder-util-5.9.1.tgz#4d5c4259e91ddee1824daf4673ade5e9adca335c"
builder-util@^5.10.0, builder-util@^5.8.1:
version "5.10.0"
resolved "https://registry.yarnpkg.com/builder-util/-/builder-util-5.10.0.tgz#025291dba29a865b6e2b28959d0f757f4883c3bd"
dependencies:
"7zip-bin" "~3.1.0"
app-builder-bin "1.8.9"
"7zip-bin" "~4.0.2"
app-builder-bin "1.9.0"
bluebird-lst "^1.0.5"
builder-util-runtime "^4.2.1"
chalk "^2.4.1"
Expand Down Expand Up @@ -3380,13 +3368,13 @@ electron-builder-lib@~20.13.2:
semver "^5.5.0"
temp-file "^3.1.2"

[email protected].0:
version "20.14.0"
resolved "https://registry.yarnpkg.com/electron-builder-squirrel-windows/-/electron-builder-squirrel-windows-20.14.0.tgz#c072b6539506e7e084bd369e10e46bf95ee96193"
[email protected].4:
version "20.14.4"
resolved "https://registry.yarnpkg.com/electron-builder-squirrel-windows/-/electron-builder-squirrel-windows-20.14.4.tgz#0dea0ee5b145836c50161c5510af1b49a106eb24"
dependencies:
archiver "^2.1.1"
bluebird-lst "^1.0.5"
builder-util "^5.9.0"
builder-util "^5.10.0"
fs-extra-p "^4.6.0"
sanitize-filename "^1.6.1"
optionalDependencies:
Expand Down Expand Up @@ -4273,8 +4261,8 @@ fn-name@^2.0.0:
resolved "https://registry.yarnpkg.com/fn-name/-/fn-name-2.0.1.tgz#5214d7537a4d06a4a301c0cc262feb84188002e7"

follow-redirects@^1.0.0:
version "1.4.1"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.4.1.tgz#d8120f4518190f55aac65bb6fc7b85fcd666d6aa"
version "1.5.0"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.0.tgz#234f49cf770b7f35b40e790f636ceba0c3a0ab77"
dependencies:
debug "^3.1.0"

Expand Down Expand Up @@ -5106,11 +5094,11 @@ html-minifier@^3.2.3, html-minifier@^3.5.8:

"html-webpack-plugin@https://github.com/jantimon/html-webpack-plugin#webpack-4":
version "4.0.0"
resolved "https://github.com/jantimon/html-webpack-plugin#b6df42c7eaacc72bf519e4c084cd747c38df0881"
resolved "https://github.com/jantimon/html-webpack-plugin#e8af3c587e7a5006a24a18d11354d93748bf426e"
dependencies:
html-minifier "^3.2.3"
loader-utils "^1.1.0"
lodash "^4.17.3"
lodash "^4.17.10"
pretty-error "^2.0.2"
tapable "^1.0.0"
util.promisify "1.0.0"
Expand Down Expand Up @@ -6498,7 +6486,7 @@ lodash@^3.10.1:
version "3.10.1"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-3.10.1.tgz#5bf45e8e49ba4189e17d482789dfd15bd140b7b6"

lodash@^4.0.0, lodash@^4.13.1, lodash@^4.14.0, lodash@^4.15.0, lodash@^4.17.10, lodash@^4.17.2, lodash@^4.17.3, lodash@^4.17.4, lodash@^4.17.5, lodash@^4.3.0, lodash@^4.8.0, lodash@~4.17.4:
lodash@^4.0.0, lodash@^4.13.1, lodash@^4.14.0, lodash@^4.15.0, lodash@^4.17.10, lodash@^4.17.2, lodash@^4.17.4, lodash@^4.17.5, lodash@^4.3.0, lodash@^4.8.0, lodash@~4.17.4:
version "4.17.10"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7"

Expand Down Expand Up @@ -6872,8 +6860,8 @@ minimist@~0.0.1:
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.10.tgz#de3f98543dbf96082be48ad1a0c7cda836301dcf"

minipass@^2.2.1, minipass@^2.2.4:
version "2.3.0"
resolved "https://registry.yarnpkg.com/minipass/-/minipass-2.3.0.tgz#2e11b1c46df7fe7f1afbe9a490280add21ffe384"
version "2.3.1"
resolved "https://registry.yarnpkg.com/minipass/-/minipass-2.3.1.tgz#4e872b959131a672837ab3cb554962bc84b1537d"
dependencies:
safe-buffer "^5.1.1"
yallist "^3.0.0"
Expand Down Expand Up @@ -10314,8 +10302,8 @@ [email protected]:
tsutils "^2.12.1"

tsutils@^2.12.1, tsutils@^2.19.1:
version "2.27.0"
resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-2.27.0.tgz#9efb252b188eaa0ca3ade41dc410d6ce7eaab816"
version "2.27.1"
resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-2.27.1.tgz#ab0276ac23664f36ce8fd4414daec4aebf4373ee"
dependencies:
tslib "^1.8.1"

Expand Down Expand Up @@ -10470,8 +10458,8 @@ unset-value@^1.0.0:
isobject "^3.0.0"

untildify@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/untildify/-/untildify-3.0.2.tgz#7f1f302055b3fea0f3e81dc78eb36766cb65e3f1"
version "3.0.3"
resolved "https://registry.yarnpkg.com/untildify/-/untildify-3.0.3.tgz#1e7b42b140bcfd922b22e70ca1265bfe3634c7c9"

unzip-response@^2.0.1:
version "2.0.1"
Expand Down

0 comments on commit 924b7d3

Please sign in to comment.