diff --git a/main.js b/main.js index 65189dc..bc9c8c1 100644 --- a/main.js +++ b/main.js @@ -1,8 +1,16 @@ const { ipcMain, app, session } = require('electron') const { menubar } = require('menubar') const { autoUpdater } = require('electron-updater') -const { onFirstRunMaybe } = require('./first-run') -const path = require('path') +const path = require('path'); +const { onFirstRunMaybe } = require('./first-run'); +const { Store } = require('./store'); + +const store = new Store({ + configName: 'preferences', + defaults: { + windowSize: { height: 400 }, + }, +}); const iconIdleMacOS = path.join( __dirname, @@ -18,12 +26,14 @@ const iconIdleWindows = path.join( 'tray-windows.ico' ) +const { height } = store.get('windowSize'); const browserWindowOpts = { width: 460, - height: 400, + height, minWidth: 460, minHeight: 400, - resizable: false, + maxWidth: 460, + resizable: true, transparent: true, icon: process.platform === 'win32' ? iconIdleWindows : iconIdleMacOS, webPreferences: { @@ -70,6 +80,9 @@ menubarApp.on('ready', () => { menubarApp.tray.setImage(iconIdleMacOS) } }) -}) - + menubarApp.window.on('resize', () => { + const { height } = menubarApp.window.getBounds(); + store.set('windowSize', { height }); + }); +}) diff --git a/package.json b/package.json index 4bfbdf4..7117d5f 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,8 @@ "index.html", "LICENSE", "main.js", - "first-run.js" + "first-run.js", + "store.js" ], "directories": { "buildResources": "buildResources", diff --git a/store.js b/store.js new file mode 100644 index 0000000..844673a --- /dev/null +++ b/store.js @@ -0,0 +1,31 @@ +const { app } = require('electron'); +const path = require('path'); +const fs = require('fs'); + +class Store { + constructor(opts) { + const userDataPath = app.getPath('userData'); + this.path = path.join(userDataPath, `${opts.configName}.json`); + + this.data = parseDataFile(this.path, opts.defaults); + } + + get(key) { + return this.data[key]; + } + + set(key, val) { + this.data[key] = val; + fs.writeFileSync(this.path, JSON.stringify(this.data)); + } +} + +function parseDataFile(filePath, defaults) { + try { + return JSON.parse(fs.readFileSync(filePath)); + } catch (error) { + return defaults; + } +} + +module.exports = { Store }; diff --git a/tsconfig.json b/tsconfig.json index 945fb21..7c1c7fe 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -19,6 +19,7 @@ "src/**/*.ts", "src/**/*.tsx", "first-run.js", + "store.js", "main.js" ] }