[PM-4441] Refactor utils helper functions (#6672)

This commit is contained in:
Oscar Hinton 2023-10-23 23:52:42 +02:00 committed by GitHub
parent 7ff4a157f9
commit 6355a1964b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 17 deletions

View File

@ -9,7 +9,15 @@ import { LogService } from "@bitwarden/common/platform/abstractions/log.service"
import { StateService } from "@bitwarden/common/platform/abstractions/state.service"; import { StateService } from "@bitwarden/common/platform/abstractions/state.service";
import { AbstractStorageService } from "@bitwarden/common/platform/abstractions/storage.service"; import { AbstractStorageService } from "@bitwarden/common/platform/abstractions/storage.service";
import { cleanUserAgent, isDev, isMacAppStore, isSnapStore } from "../utils"; import {
cleanUserAgent,
isDev,
isLinux,
isMac,
isMacAppStore,
isSnapStore,
isWindows,
} from "../utils";
const mainWindowSizeKey = "mainWindowSize"; const mainWindowSizeKey = "mainWindowSize";
const WindowEventHandlingDelay = 100; const WindowEventHandlingDelay = 100;
@ -41,9 +49,12 @@ export class WindowMain {
// User might have changed theme, ensure the window is updated. // User might have changed theme, ensure the window is updated.
this.win.setBackgroundColor(await this.getBackgroundColor()); this.win.setBackgroundColor(await this.getBackgroundColor());
const crashEvent = once(this.win.webContents, "render-process-gone"); // By default some linux distro collect core dumps on crashes which gets written to disk.
this.win.webContents.forcefullyCrashRenderer(); if (!isLinux()) {
await crashEvent; const crashEvent = once(this.win.webContents, "render-process-gone");
this.win.webContents.forcefullyCrashRenderer();
await crashEvent;
}
this.win.webContents.reloadIgnoringCache(); this.win.webContents.reloadIgnoringCache();
this.session.clearCache(); this.session.clearCache();
@ -66,7 +77,7 @@ export class WindowMain {
} }
this.win.focus(); this.win.focus();
} }
if (process.platform === "win32" || process.platform === "linux") { if (isWindows() || isLinux()) {
if (this.argvCallback != null) { if (this.argvCallback != null) {
this.argvCallback(argv); this.argvCallback(argv);
} }
@ -96,7 +107,7 @@ export class WindowMain {
app.on("window-all-closed", () => { app.on("window-all-closed", () => {
// On OS X it is common for applications and their menu bar // On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q // to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== "darwin" || this.isQuitting || isMacAppStore()) { if (!isMac() || this.isQuitting || isMacAppStore()) {
app.quit(); app.quit();
} }
}); });
@ -137,8 +148,8 @@ export class WindowMain {
x: this.windowStates[mainWindowSizeKey].x, x: this.windowStates[mainWindowSizeKey].x,
y: this.windowStates[mainWindowSizeKey].y, y: this.windowStates[mainWindowSizeKey].y,
title: app.name, title: app.name,
icon: process.platform === "linux" ? path.join(__dirname, "/images/icon.png") : undefined, icon: isLinux() ? path.join(__dirname, "/images/icon.png") : undefined,
titleBarStyle: process.platform === "darwin" ? "hiddenInset" : undefined, titleBarStyle: isMac() ? "hiddenInset" : undefined,
show: false, show: false,
backgroundColor: await this.getBackgroundColor(), backgroundColor: await this.getBackgroundColor(),
alwaysOnTop: this.enableAlwaysOnTop, alwaysOnTop: this.enableAlwaysOnTop,

View File

@ -25,8 +25,16 @@ export function isDev() {
return process.defaultApp || /node_modules[\\/]electron[\\/]/.test(process.execPath); return process.defaultApp || /node_modules[\\/]electron[\\/]/.test(process.execPath);
} }
export function isLinux() {
return process.platform === "linux";
}
export function isAppImage() { export function isAppImage() {
return process.platform === "linux" && "APPIMAGE" in process.env; return isLinux() && "APPIMAGE" in process.env;
}
export function isSnapStore() {
return isLinux() && process.env.SNAP_USER_DATA != null;
} }
export function isMac() { export function isMac() {
@ -37,25 +45,25 @@ export function isMacAppStore() {
return isMac() && process.mas === true; return isMac() && process.mas === true;
} }
export function isWindows() {
return process.platform === "win32";
}
export function isWindowsStore() { export function isWindowsStore() {
const isWindows = process.platform === "win32"; const windows = isWindows();
let windowsStore = process.windowsStore; let windowsStore = process.windowsStore;
if ( if (
isWindows && windows &&
!windowsStore && !windowsStore &&
process.resourcesPath.indexOf("8bitSolutionsLLC.bitwardendesktop_") > -1 process.resourcesPath.indexOf("8bitSolutionsLLC.bitwardendesktop_") > -1
) { ) {
windowsStore = true; windowsStore = true;
} }
return isWindows && windowsStore === true; return windows && windowsStore === true;
}
export function isSnapStore() {
return process.platform === "linux" && process.env.SNAP_USER_DATA != null;
} }
export function isWindowsPortable() { export function isWindowsPortable() {
return process.platform === "win32" && process.env.PORTABLE_EXECUTABLE_DIR != null; return isWindows() && process.env.PORTABLE_EXECUTABLE_DIR != null;
} }
/** /**