Skip to content

Commit

Permalink
Resizable Height
Browse files Browse the repository at this point in the history
  • Loading branch information
andreivreja committed May 16, 2021
1 parent 0797ae8 commit 3f32d30
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
25 changes: 19 additions & 6 deletions main.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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: {
Expand Down Expand Up @@ -70,6 +80,9 @@ menubarApp.on('ready', () => {
menubarApp.tray.setImage(iconIdleMacOS)
}
})
})


menubarApp.window.on('resize', () => {
const { height } = menubarApp.window.getBounds();
store.set('windowSize', { height });
});
})
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
"index.html",
"LICENSE",
"main.js",
"first-run.js"
"first-run.js",
"store.js"
],
"directories": {
"buildResources": "buildResources",
Expand Down
31 changes: 31 additions & 0 deletions store.js
Original file line number Diff line number Diff line change
@@ -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 };
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"src/**/*.ts",
"src/**/*.tsx",
"first-run.js",
"store.js",
"main.js"
]
}

0 comments on commit 3f32d30

Please sign in to comment.