[bug] Properly define stored window state (#638)

This commit is contained in:
Addison Beck 2022-01-27 10:44:09 -05:00 committed by GitHub
parent 0186610ca4
commit 83305313f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 25 additions and 18 deletions

View File

@ -19,6 +19,7 @@ import { GeneratedPasswordHistory } from "../models/domain/generatedPasswordHist
import { Policy } from "../models/domain/policy"; import { Policy } from "../models/domain/policy";
import { StorageOptions } from "../models/domain/storageOptions"; import { StorageOptions } from "../models/domain/storageOptions";
import { SymmetricCryptoKey } from "../models/domain/symmetricCryptoKey"; import { SymmetricCryptoKey } from "../models/domain/symmetricCryptoKey";
import { WindowState } from "../models/domain/windowState";
import { CipherView } from "../models/view/cipherView"; import { CipherView } from "../models/view/cipherView";
import { CollectionView } from "../models/view/collectionView"; import { CollectionView } from "../models/view/collectionView";
@ -299,6 +300,6 @@ export abstract class StateService<T extends Account = Account> {
setVaultTimeoutAction: (value: string, options?: StorageOptions) => Promise<void>; setVaultTimeoutAction: (value: string, options?: StorageOptions) => Promise<void>;
getStateVersion: () => Promise<number>; getStateVersion: () => Promise<number>;
setStateVersion: (value: number) => Promise<void>; setStateVersion: (value: number) => Promise<void>;
getWindow: () => Promise<Map<string, any>>; getWindow: () => Promise<WindowState>;
setWindow: (value: Map<string, any>) => Promise<void>; setWindow: (value: WindowState) => Promise<void>;
} }

View File

@ -1,5 +1,6 @@
import { StateVersion } from "../../enums/stateVersion"; import { StateVersion } from "../../enums/stateVersion";
import { EnvironmentUrls } from "./environmentUrls"; import { EnvironmentUrls } from "./environmentUrls";
import { WindowState } from "./windowState";
export class GlobalState { export class GlobalState {
enableAlwaysOnTop?: boolean; enableAlwaysOnTop?: boolean;
@ -11,7 +12,7 @@ export class GlobalState {
ssoState?: string; ssoState?: string;
rememberedEmail?: string; rememberedEmail?: string;
theme?: string = "light"; theme?: string = "light";
window?: Map<string, any> = new Map<string, any>(); window?: WindowState = new WindowState();
twoFactorToken?: string; twoFactorToken?: string;
disableFavicon?: boolean; disableFavicon?: boolean;
biometricAwaitingAcceptance?: boolean; biometricAwaitingAcceptance?: boolean;

View File

@ -0,0 +1,10 @@
export class WindowState {
width?: number;
height?: number;
isMaximized?: boolean;
// TODO: displayBounds is an Electron.Rectangle.
// We need to establish some kind of client-specific global state, similiar to the way we already extend a base Account.
displayBounds: any;
x?: number;
y?: number;
}

View File

@ -36,6 +36,7 @@ import { BehaviorSubject } from "rxjs";
import { StateMigrationService } from "../abstractions/stateMigration.service"; import { StateMigrationService } from "../abstractions/stateMigration.service";
import { EnvironmentUrls } from "../models/domain/environmentUrls"; import { EnvironmentUrls } from "../models/domain/environmentUrls";
import { WindowState } from "../models/domain/windowState";
const keys = { const keys = {
global: "global", global: "global",
@ -2066,14 +2067,14 @@ export class StateService<TAccount extends Account = Account>
await this.saveGlobals(globals, await this.defaultOnDiskOptions()); await this.saveGlobals(globals, await this.defaultOnDiskOptions());
} }
async getWindow(): Promise<Map<string, any>> { async getWindow(): Promise<WindowState> {
const globals = await this.getGlobals(await this.defaultOnDiskOptions()); const globals = await this.getGlobals(await this.defaultOnDiskOptions());
return globals?.window != null && Object.keys(globals.window).length > 0 return globals?.window != null && Object.keys(globals.window).length > 0
? globals.window ? globals.window
: new Map<string, any>(); : new WindowState();
} }
async setWindow(value: Map<string, any>, options?: StorageOptions): Promise<void> { async setWindow(value: WindowState, options?: StorageOptions): Promise<void> {
const globals = await this.getGlobals( const globals = await this.getGlobals(
this.reconcileOptions(options, await this.defaultOnDiskOptions()) this.reconcileOptions(options, await this.defaultOnDiskOptions())
); );

View File

@ -99,7 +99,6 @@ export class WindowMain {
async createWindow(): Promise<void> { async createWindow(): Promise<void> {
this.windowStates[mainWindowSizeKey] = await this.getWindowState( this.windowStates[mainWindowSizeKey] = await this.getWindowState(
mainWindowSizeKey,
this.defaultWidth, this.defaultWidth,
this.defaultHeight this.defaultHeight
); );
@ -214,7 +213,7 @@ export class WindowMain {
const bounds = win.getBounds(); const bounds = win.getBounds();
if (this.windowStates[configKey] == null) { if (this.windowStates[configKey] == null) {
this.windowStates[configKey] = (await this.stateService.getWindow()).get(configKey); this.windowStates[configKey] = await this.stateService.getWindow();
if (this.windowStates[configKey] == null) { if (this.windowStates[configKey] == null) {
this.windowStates[configKey] = {}; this.windowStates[configKey] = {};
} }
@ -230,25 +229,20 @@ export class WindowMain {
this.windowStates[configKey].height = bounds.height; this.windowStates[configKey].height = bounds.height;
} }
const cachedWindow = (await this.stateService.getWindow()) ?? new Map<string, any>(); await this.stateService.setWindow(this.windowStates[configKey]);
cachedWindow.set(configKey, this.windowStates[configKey]);
await this.stateService.setWindow(cachedWindow);
} catch (e) { } catch (e) {
this.logService.error(e); this.logService.error(e);
} }
} }
private async getWindowState(configKey: string, defaultWidth: number, defaultHeight: number) { private async getWindowState(defaultWidth: number, defaultHeight: number) {
const windowState = (await this.stateService.getWindow()) ?? new Map<string, any>(); const state = await this.stateService.getWindow();
let state = windowState.has(configKey) ? windowState.get(configKey) : null;
const isValid = state != null && (this.stateHasBounds(state) || state.isMaximized); const isValid = state != null && (this.stateHasBounds(state) || state.isMaximized);
let displayBounds: Electron.Rectangle = null; let displayBounds: Electron.Rectangle = null;
if (!isValid) { if (!isValid) {
state = { state.width = defaultWidth;
width: defaultWidth, state.height = defaultHeight;
height: defaultHeight,
};
displayBounds = screen.getPrimaryDisplay().bounds; displayBounds = screen.getPrimaryDisplay().bounds;
} else if (this.stateHasBounds(state) && state.displayBounds) { } else if (this.stateHasBounds(state) && state.displayBounds) {