mirror of
https://github.com/bitwarden/jslib
synced 2025-12-20 10:13:43 +00:00
[feature] Implement account switching capable services across components and processes
* Replace calls to StorageService and deprecated services with calls to a StateService
This commit is contained in:
@@ -7,9 +7,8 @@ import * as path from 'path';
|
||||
import * as url from 'url';
|
||||
|
||||
import { LogService } from 'jslib-common/abstractions/log.service';
|
||||
import { StorageService } from 'jslib-common/abstractions/storage.service';
|
||||
import { StateService } from 'jslib-common/abstractions/state.service';
|
||||
|
||||
import { ElectronConstants } from './electronConstants';
|
||||
import {
|
||||
cleanUserAgent,
|
||||
isDev,
|
||||
@@ -17,11 +16,8 @@ import {
|
||||
isSnapStore,
|
||||
} from './utils';
|
||||
|
||||
const mainWindowSizeKey = 'mainWindowSize';
|
||||
const WindowEventHandlingDelay = 100;
|
||||
const Keys = {
|
||||
mainWindowSize: 'mainWindowSize',
|
||||
};
|
||||
|
||||
export class WindowMain {
|
||||
win: BrowserWindow;
|
||||
isQuitting: boolean = false;
|
||||
@@ -30,7 +26,7 @@ export class WindowMain {
|
||||
private windowStates: { [key: string]: any; } = {};
|
||||
private enableAlwaysOnTop: boolean = false;
|
||||
|
||||
constructor(private storageService: StorageService, private logService: LogService,
|
||||
constructor(private stateService: StateService, private logService: LogService,
|
||||
private hideTitleBar = false, private defaultWidth = 950, private defaultHeight = 600,
|
||||
private argvCallback: (argv: string[]) => void = null,
|
||||
private createWindowCallback: (win: BrowserWindow) => void) { }
|
||||
@@ -81,7 +77,7 @@ export class WindowMain {
|
||||
// Quit when all windows are closed.
|
||||
app.on('window-all-closed', () => {
|
||||
// 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 KKKactive until the user quits explicitly with Cmd + Q
|
||||
if (process.platform !== 'darwin' || this.isQuitting || isMacAppStore()) {
|
||||
app.quit();
|
||||
}
|
||||
@@ -107,100 +103,104 @@ export class WindowMain {
|
||||
}
|
||||
|
||||
async createWindow(): Promise<void> {
|
||||
this.windowStates[Keys.mainWindowSize] = await this.getWindowState(Keys.mainWindowSize, this.defaultWidth,
|
||||
this.defaultHeight);
|
||||
this.enableAlwaysOnTop = await this.storageService.get<boolean>(ElectronConstants.enableAlwaysOnTopKey);
|
||||
try {
|
||||
this.windowStates[mainWindowSizeKey] = await this.getWindowState(mainWindowSizeKey, this.defaultWidth,
|
||||
this.defaultHeight);
|
||||
this.enableAlwaysOnTop = await this.stateService.getEnableAlwaysOnTop();
|
||||
|
||||
// Create the browser window.
|
||||
this.win = new BrowserWindow({
|
||||
width: this.windowStates[Keys.mainWindowSize].width,
|
||||
height: this.windowStates[Keys.mainWindowSize].height,
|
||||
minWidth: 680,
|
||||
minHeight: 500,
|
||||
x: this.windowStates[Keys.mainWindowSize].x,
|
||||
y: this.windowStates[Keys.mainWindowSize].y,
|
||||
title: app.name,
|
||||
icon: process.platform === 'linux' ? path.join(__dirname, '/images/icon.png') : undefined,
|
||||
titleBarStyle: this.hideTitleBar && process.platform === 'darwin' ? 'hiddenInset' : undefined,
|
||||
show: false,
|
||||
backgroundColor: '#fff',
|
||||
alwaysOnTop: this.enableAlwaysOnTop,
|
||||
webPreferences: {
|
||||
nodeIntegration: true,
|
||||
backgroundThrottling: false,
|
||||
contextIsolation: false,
|
||||
},
|
||||
});
|
||||
|
||||
if (this.windowStates[Keys.mainWindowSize].isMaximized) {
|
||||
this.win.maximize();
|
||||
}
|
||||
|
||||
// Show it later since it might need to be maximized.
|
||||
this.win.show();
|
||||
|
||||
// and load the index.html of the app.
|
||||
this.win.loadURL(url.format(
|
||||
{
|
||||
protocol: 'file:',
|
||||
pathname: path.join(__dirname, '/index.html'),
|
||||
slashes: true,
|
||||
}),
|
||||
{
|
||||
userAgent: cleanUserAgent(this.win.webContents.userAgent),
|
||||
}
|
||||
);
|
||||
|
||||
// Open the DevTools.
|
||||
if (isDev()) {
|
||||
this.win.webContents.openDevTools();
|
||||
}
|
||||
|
||||
// Emitted when the window is closed.
|
||||
this.win.on('closed', async () => {
|
||||
await this.updateWindowState(Keys.mainWindowSize, this.win);
|
||||
|
||||
// Dereference the window object, usually you would store window
|
||||
// in an array if your app supports multi windows, this is the time
|
||||
// when you should delete the corresponding element.
|
||||
this.win = null;
|
||||
});
|
||||
|
||||
this.win.on('close', async () => {
|
||||
await this.updateWindowState(Keys.mainWindowSize, this.win);
|
||||
});
|
||||
|
||||
this.win.on('maximize', async () => {
|
||||
await this.updateWindowState(Keys.mainWindowSize, this.win);
|
||||
});
|
||||
|
||||
this.win.on('unmaximize', async () => {
|
||||
await this.updateWindowState(Keys.mainWindowSize, this.win);
|
||||
});
|
||||
|
||||
this.win.on('resize', () => {
|
||||
this.windowStateChangeHandler(Keys.mainWindowSize, this.win);
|
||||
});
|
||||
|
||||
this.win.on('move', () => {
|
||||
this.windowStateChangeHandler(Keys.mainWindowSize, this.win);
|
||||
});
|
||||
this.win.on('focus', () => {
|
||||
this.win.webContents.send('messagingService', {
|
||||
command: 'windowIsFocused',
|
||||
windowIsFocused: true,
|
||||
// Create the browser window.
|
||||
this.win = new BrowserWindow({
|
||||
width: this.windowStates[mainWindowSizeKey].width,
|
||||
height: this.windowStates[mainWindowSizeKey].height,
|
||||
minWidth: 680,
|
||||
minHeight: 500,
|
||||
x: this.windowStates[mainWindowSizeKey].x,
|
||||
y: this.windowStates[mainWindowSizeKey].y,
|
||||
title: app.name,
|
||||
icon: process.platform === 'linux' ? path.join(__dirname, '/images/icon.png') : undefined,
|
||||
titleBarStyle: this.hideTitleBar && process.platform === 'darwin' ? 'hiddenInset' : undefined,
|
||||
show: false,
|
||||
backgroundColor: '#fff',
|
||||
alwaysOnTop: this.enableAlwaysOnTop,
|
||||
webPreferences: {
|
||||
nodeIntegration: true,
|
||||
backgroundThrottling: false,
|
||||
contextIsolation: false,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
if (this.createWindowCallback) {
|
||||
this.createWindowCallback(this.win);
|
||||
if (this.windowStates[mainWindowSizeKey].isMaximized) {
|
||||
this.win.maximize();
|
||||
}
|
||||
|
||||
// Show it later since it might need to be maximized.
|
||||
this.win.show();
|
||||
|
||||
// and load the index.html of the app.
|
||||
this.win.loadURL(url.format(
|
||||
{
|
||||
protocol: 'file:',
|
||||
pathname: path.join(__dirname, '/index.html'),
|
||||
slashes: true,
|
||||
}),
|
||||
{
|
||||
userAgent: cleanUserAgent(this.win.webContents.userAgent),
|
||||
}
|
||||
);
|
||||
|
||||
// Open the DevTools.
|
||||
if (isDev()) {
|
||||
this.win.webContents.openDevTools();
|
||||
}
|
||||
|
||||
// Emitted when the window is closed.
|
||||
this.win.on('closed', async () => {
|
||||
await this.updateWindowState(mainWindowSizeKey, this.win);
|
||||
|
||||
// Dereference the window object, usually you would store window
|
||||
// in an array if your app supports multi windows, this is the time
|
||||
// when you should delete the corresponding element.
|
||||
this.win = null;
|
||||
});
|
||||
|
||||
this.win.on('close', async () => {
|
||||
await this.updateWindowState(mainWindowSizeKey, this.win);
|
||||
});
|
||||
|
||||
this.win.on('maximize', async () => {
|
||||
await this.updateWindowState(mainWindowSizeKey, this.win);
|
||||
});
|
||||
|
||||
this.win.on('unmaximize', async () => {
|
||||
await this.updateWindowState(mainWindowSizeKey, this.win);
|
||||
});
|
||||
|
||||
this.win.on('resize', () => {
|
||||
this.windowStateChangeHandler(mainWindowSizeKey, this.win);
|
||||
});
|
||||
|
||||
this.win.on('move', () => {
|
||||
this.windowStateChangeHandler(mainWindowSizeKey, this.win);
|
||||
});
|
||||
this.win.on('focus', () => {
|
||||
this.win.webContents.send('messagingService', {
|
||||
command: 'windowIsFocused',
|
||||
windowIsFocused: true,
|
||||
});
|
||||
});
|
||||
|
||||
if (this.createWindowCallback) {
|
||||
this.createWindowCallback(this.win);
|
||||
}
|
||||
} catch (e) {
|
||||
this.logService.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
async toggleAlwaysOnTop() {
|
||||
this.enableAlwaysOnTop = !this.win.isAlwaysOnTop();
|
||||
this.win.setAlwaysOnTop(this.enableAlwaysOnTop);
|
||||
await this.storageService.save(ElectronConstants.enableAlwaysOnTopKey, this.enableAlwaysOnTop);
|
||||
await this.stateService.setEnableAlwaysOnTop(this.enableAlwaysOnTop);
|
||||
}
|
||||
|
||||
private windowStateChangeHandler(configKey: string, win: BrowserWindow) {
|
||||
@@ -219,7 +219,7 @@ export class WindowMain {
|
||||
const bounds = win.getBounds();
|
||||
|
||||
if (this.windowStates[configKey] == null) {
|
||||
this.windowStates[configKey] = await this.storageService.get<any>(configKey);
|
||||
this.windowStates[configKey] = (await this.stateService.getWindow()).get(configKey);
|
||||
if (this.windowStates[configKey] == null) {
|
||||
this.windowStates[configKey] = {};
|
||||
}
|
||||
@@ -235,14 +235,19 @@ export class WindowMain {
|
||||
this.windowStates[configKey].height = bounds.height;
|
||||
}
|
||||
|
||||
await this.storageService.save(configKey, this.windowStates[configKey]);
|
||||
const cachedWindow = await this.stateService.getWindow() ?? new Map<string, any>();
|
||||
cachedWindow.set(configKey, this.windowStates[configKey]);
|
||||
await this.stateService.setWindow(cachedWindow);
|
||||
} catch (e) {
|
||||
this.logService.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
private async getWindowState(configKey: string, defaultWidth: number, defaultHeight: number) {
|
||||
let state = await this.storageService.get<any>(configKey);
|
||||
const windowState = await this.stateService.getWindow() ?? new Map<string, any>();
|
||||
let state = windowState.has(configKey) ?
|
||||
windowState.get(configKey) :
|
||||
null;
|
||||
|
||||
const isValid = state != null && (this.stateHasBounds(state) || state.isMaximized);
|
||||
let displayBounds: Electron.Rectangle = null;
|
||||
|
||||
Reference in New Issue
Block a user