-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: remember last window size and maximized state
- Loading branch information
1 parent
f5e7468
commit b5ea5ac
Showing
7 changed files
with
105 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import DTOVersioner from "Shared/dto-versioner"; | ||
import Ajv, { JTDDataType } from 'ajv/dist/jtd'; | ||
import fs from 'fs'; | ||
import { APP_CACHE_DIR } from "Shared/const"; | ||
import { getLogger } from "Shared/logging"; | ||
import { homedir } from 'os'; | ||
|
||
const LOG = getLogger(__filename); | ||
const ajv = new Ajv(); | ||
|
||
const TempConfigSchema = { | ||
properties: { | ||
version: { type: 'float32' }, | ||
windowHeight: { type: 'int32' }, | ||
windowWidth: { type: 'int32' }, | ||
maximized: { type: 'boolean' }, | ||
}, | ||
} as const; | ||
export type TempConfig = JTDDataType<typeof TempConfigSchema>; | ||
|
||
export const TempConfigVersioner = new DTOVersioner<TempConfig>({ | ||
name: 'TempConfig', | ||
validator: ((() => { | ||
const validate = ajv.compile(TempConfigSchema); | ||
return obj => { | ||
const res = validate(obj); | ||
return { success: res, error: JSON.stringify(validate.errors) }; | ||
} | ||
}))(), | ||
getVersion: obj => obj.version, | ||
updateFunctions: { | ||
|
||
} | ||
}); | ||
|
||
const TEMP_CONFIG_FILE = | ||
`${APP_CACHE_DIR}/tmp-config.json`.replace(/^~/, homedir); | ||
|
||
export const TEMP_CONFIG_CURRENT_VERSION = TempConfigVersioner.maxVersion(); | ||
|
||
const defaultTempConfig = (): TempConfig => ({ | ||
version: TEMP_CONFIG_CURRENT_VERSION, | ||
windowHeight: 600, | ||
windowWidth: 800, | ||
maximized: false, | ||
}); | ||
|
||
export function getTempConfig(): TempConfig { | ||
try { | ||
if (!fs.existsSync(TEMP_CONFIG_FILE)) { | ||
LOG.info('Was not created yet, using default'); | ||
return defaultTempConfig(); | ||
} | ||
const file = fs.readFileSync(TEMP_CONFIG_FILE, { encoding: 'utf-8' }); | ||
const ret = TempConfigVersioner.updateToCurrent(JSON.parse(file)); | ||
LOG.info('Found and parsed, using existing', ret); | ||
return ret; | ||
} catch (e) { | ||
LOG.warn('Could not read, using default', e); | ||
return defaultTempConfig(); | ||
} | ||
} | ||
|
||
export function writeTempConfig(cfg: TempConfig): void { | ||
TempConfigVersioner.validate(cfg); | ||
fs.writeFileSync( | ||
TEMP_CONFIG_FILE, JSON.stringify(cfg, null, 2), | ||
{ encoding: 'utf-8' } | ||
); | ||
LOG.info('Wrote', cfg); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters