Files
huly-platform/desktop/src/main/settings.ts
T

90 lines
2.8 KiB
TypeScript

//
// Copyright © 2024 Hardcore Engineering Inc.
//
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. You may
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Store from 'electron-store'
import { PackedConfig } from './config'
export class Settings {
private static readonly SETTINGS_KEY_SERVER = 'server'
private static readonly SETTINGS_KEY_MINIMIZE_TO_TRAY = 'minimize-to-tray'
private static readonly SETTINGS_KEY_AUTO_LAUNCH = 'auto-launch'
private static readonly SETTINGS_KEY_WINDOW_BOUNDS = 'windowBounds'
private readonly store: Store
private readonly isDev: boolean
private readonly packedConfig?: PackedConfig
constructor (store: Store, isDev: boolean, packedConfig?: PackedConfig) {
this.store = store
this.isDev = isDev
this.packedConfig = packedConfig
}
readServerUrl (): string {
const url = this.extractUrl()
// Motivation: fix existing Huly installations (saved on disk URLs).
return Settings.sanitizeUrl(url)
}
isMinimizeToTrayEnabled (): boolean {
return (this.store as any).get(Settings.SETTINGS_KEY_MINIMIZE_TO_TRAY) as boolean ?? false
}
isAutoLaunchEnabled (): boolean {
return (this.store as any).get(Settings.SETTINGS_KEY_AUTO_LAUNCH) as boolean ?? false
}
setMinimizeToTrayEnabled (enabled: boolean): void {
(this.store as any).set(Settings.SETTINGS_KEY_MINIMIZE_TO_TRAY, enabled)
}
setAutoLaunchEnabled (enabled: boolean): void {
(this.store as any).set(Settings.SETTINGS_KEY_AUTO_LAUNCH, enabled)
}
setServerUrl (serverUrl: string): void {
const sanitizedUrl: string = Settings.sanitizeUrl(serverUrl)
;(this.store as any).set(Settings.SETTINGS_KEY_SERVER, sanitizedUrl)
}
getWindowBounds (): any {
return (this.store as any).get(Settings.SETTINGS_KEY_WINDOW_BOUNDS)
}
setWindowBounds (bounds: any): void {
(this.store as any).set(Settings.SETTINGS_KEY_WINDOW_BOUNDS, bounds)
}
private extractUrl (): string {
if (this.isDev) {
return process.env.FRONT_URL ?? 'http://huly.local:8087'
}
return (
(this.store as any).get(Settings.SETTINGS_KEY_SERVER) as string ??
this.packedConfig?.server ??
process.env.FRONT_URL ??
'https://huly.app'
)
}
private static sanitizeUrl (serverUrl: string): string {
let cleanUrl: string = serverUrl
while (cleanUrl.endsWith('/')) {
cleanUrl = cleanUrl.slice(0, -1)
}
return cleanUrl
}
}