diff --git a/packages/desktop/lib/electron/interfaces/electron-settings.interface.ts b/packages/desktop/lib/electron/interfaces/electron-settings.interface.ts new file mode 100644 index 0000000000..d4ec0ee091 --- /dev/null +++ b/packages/desktop/lib/electron/interfaces/electron-settings.interface.ts @@ -0,0 +1,5 @@ +import { IWindowState } from './window-state.interface' + +export interface IElectronSettings { + windowState: IWindowState +} diff --git a/packages/desktop/lib/electron/interfaces/index.ts b/packages/desktop/lib/electron/interfaces/index.ts new file mode 100644 index 0000000000..379e42f5ef --- /dev/null +++ b/packages/desktop/lib/electron/interfaces/index.ts @@ -0,0 +1,2 @@ +export * from './electron-settings.interface' +export * from './window-state.interface' diff --git a/packages/desktop/lib/electron/interfaces/window-state.interface.ts b/packages/desktop/lib/electron/interfaces/window-state.interface.ts new file mode 100644 index 0000000000..2ad28433a6 --- /dev/null +++ b/packages/desktop/lib/electron/interfaces/window-state.interface.ts @@ -0,0 +1,11 @@ +import { BrowserWindow } from 'electron' + +export interface IWindowState { + x?: number + y?: number + width: number + height: number + isMaximized?: boolean + + track?(window: BrowserWindow): void +} diff --git a/packages/desktop/lib/electron/managers/electron-settings.manager.ts b/packages/desktop/lib/electron/managers/electron-settings.manager.ts new file mode 100644 index 0000000000..6b012ef2e4 --- /dev/null +++ b/packages/desktop/lib/electron/managers/electron-settings.manager.ts @@ -0,0 +1,20 @@ +import { JsonFileManager } from './json-file.manager' +import { IElectronSettings } from '../interfaces' + +const SETTINGS_FILE_NAME = 'settings.json' + +export class ElectronSettingsManager { + public static saveSettings(settings: IElectronSettings): void { + JsonFileManager.saveJsonToFile(SETTINGS_FILE_NAME, settings) + } + + public static loadSettings(): IElectronSettings { + const settings = JsonFileManager.loadJsonFromFile(SETTINGS_FILE_NAME) as IElectronSettings | undefined + return settings ? settings : { windowState: { width: 800, height: 600 } } + } + + public static updateSettings(settings: IElectronSettings): void { + const config = ElectronSettingsManager.loadSettings() + ElectronSettingsManager.saveSettings({ ...config, ...settings }) + } +} diff --git a/packages/desktop/lib/electron/processes/main.process.ts b/packages/desktop/lib/electron/processes/main.process.ts index dcc90c83fe..7df542116e 100644 --- a/packages/desktop/lib/electron/processes/main.process.ts +++ b/packages/desktop/lib/electron/processes/main.process.ts @@ -36,7 +36,8 @@ import { getMachineId } from '../utils/os.utils' import { registerPowerMonitorListeners } from '../listeners' import ThirdPartyAppManager from '../managers/third-party-profiles.manager' import { ITransakWindowData } from '@core/app/interfaces' -import { JsonFileManager } from '../managers/json-file.manager' +import { ElectronSettingsManager } from '../managers/electron-settings.manager' +import { IElectronSettings, IWindowState } from '../interfaces' export let appIsReady = false @@ -179,7 +180,7 @@ function tryOpenExternalUrl(e: Event, url: string): void { } export function createMainWindow(): BrowserWindow { - const mainWindowState = windowStateKeeper('main', 'settings.json') + const mainWindowState = windowStateKeeper('main') // Create the browser window windows.main = new BrowserWindow({ @@ -206,7 +207,7 @@ export function createMainWindow(): BrowserWindow { windows.main.maximize() } - mainWindowState.track(windows.main) + mainWindowState.track?.(windows.main) if (!app.isPackaged) { // Enable dev tools only in developer mode @@ -490,7 +491,7 @@ ipcMain.handle('handle-error', (_e, errorType, error) => { ipcMain.handle('get-machine-id', () => getMachineId()) // Settings -ipcMain.handle('update-app-settings', (_e, settings) => updateSettings(settings)) +ipcMain.handle('update-app-settings', (_e, settings) => ElectronSettingsManager.updateSettings(settings)) // Theme nativeTheme.on('updated', () => { @@ -642,9 +643,9 @@ export function closeErrorWindow(): void { } } -function windowStateKeeper(windowName: string, settingsFilename: string): IAppState { +function windowStateKeeper(windowName: string): IWindowState { let window: BrowserWindow - let windowState = { + let windowState = { x: 0, y: 0, width: 1280, @@ -652,7 +653,7 @@ function windowStateKeeper(windowName: string, settingsFilename: string): IAppSt } function setBounds(): void { - const settings = JsonFileManager.loadJsonFromFile(settingsFilename) + const settings = ElectronSettingsManager.loadSettings() if (settings && settings.windowState && settings.windowState[windowName]) { windowState = settings.windowState[windowName] @@ -663,16 +664,16 @@ function windowStateKeeper(windowName: string, settingsFilename: string): IAppSt function saveState(): void { windowState.isMaximized = window.isMaximized() if (!windowState.isMaximized) { - windowState = window.getBounds() as IAppState + windowState = window.getBounds() as IWindowState } - let settings = JsonFileManager.loadJsonFromFile(settingsFilename) as ISettings + let settings = ElectronSettingsManager.loadSettings() - settings = settings || {} - settings.windowState = settings.windowState || {} + settings = settings || {} + settings.windowState = settings.windowState || {} settings.windowState[windowName] = windowState - JsonFileManager.saveJsonToFile(settingsFilename, settings) + ElectronSettingsManager.saveSettings(settings) } function track(win: BrowserWindow): void { @@ -696,32 +697,6 @@ function windowStateKeeper(windowName: string, settingsFilename: string): IAppSt } } -interface ISettings { - windowState: IAppState -} - -interface IAppState { - x: number - y: number - width: number - height: number - isMaximized: boolean - - track(window: BrowserWindow): void -} - -function updateSettings(data: object): void { - const filename = 'settings.json' - const config = JsonFileManager.loadJsonFromFile(filename) - - /** - * CAUTION: We must be careful saving properties to this file, as - * once we decide to save it there then it will be there forever - * even if the name changes later. - */ - JsonFileManager.saveJsonToFile(filename, { ...config, ...data }) -} - export function updateAppVersionDetails(details: object): void { versionDetails = Object.assign({}, versionDetails, details)