Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move code from main to electron settings manager #2384

Merged
merged 3 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { IWindowState } from './window-state.interface'

export interface IElectronSettings {
windowState: IWindowState
}
2 changes: 2 additions & 0 deletions packages/desktop/lib/electron/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './electron-settings.interface'
export * from './window-state.interface'
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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 })
}
}
51 changes: 13 additions & 38 deletions packages/desktop/lib/electron/processes/main.process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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({
Expand All @@ -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
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -642,17 +643,17 @@ export function closeErrorWindow(): void {
}
}

function windowStateKeeper(windowName: string, settingsFilename: string): IAppState {
function windowStateKeeper(windowName: string): IWindowState {
let window: BrowserWindow
let windowState = <IAppState>{
let windowState = <IWindowState>{
x: 0,
y: 0,
width: 1280,
height: process.platform === 'win32' ? 720 + 28 : 720,
}

function setBounds(): void {
const settings = <ISettings>JsonFileManager.loadJsonFromFile(settingsFilename)
const settings = ElectronSettingsManager.loadSettings()

if (settings && settings.windowState && settings.windowState[windowName]) {
windowState = settings.windowState[windowName]
Expand All @@ -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 || <ISettings>{}
settings.windowState = settings.windowState || <IAppState>{}
settings = settings || <IElectronSettings>{}
settings.windowState = settings.windowState || <IWindowState>{}
settings.windowState[windowName] = windowState

JsonFileManager.saveJsonToFile(settingsFilename, settings)
ElectronSettingsManager.saveSettings(settings)
}

function track(win: BrowserWindow): void {
Expand All @@ -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)

Expand Down
Loading