Skip to content

Instantly share code, notes, and snippets.

@ccnokes
Created September 17, 2016 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccnokes/d666eee93b7c86e661dbd0f930a9a6c2 to your computer and use it in GitHub Desktop.
Save ccnokes/d666eee93b7c86e661dbd0f930a9a6c2 to your computer and use it in GitHub Desktop.
Sample electron app demonstrating how to save user data to a file.
const { app, BrowserWindow } = require('electron');
const path = require('path');
const Store = require('./store.js');
let mainWindow; //do this so that the window object doesn't get GC'd
// First instantiate the class
const store = new Store({
// We'll call our data file 'user-preferences'
configName: 'user-preferences',
defaults: {
// 800x600 is the default size of our window
windowBounds: { width: 800, height: 600 }
}
});
// When our app is ready, we'll create our BrowserWindow
app.on('ready', function() {
// First we'll get our height and width. This will be the defaults if there wasn't anything saved
let { width, height } = store.get('windowBounds');
// Pass those values in to the BrowserWindow options
mainWindow = new BrowserWindow({ width, height });
// The BrowserWindow class extends the node.js core EventEmitter class, so we use that API
// to listen to events on the BrowserWindow. The resize event is emitted when the window size changes.
mainWindow.on('resize', () => {
// The event doesn't pass us the window size, so we call the `getBounds` method which returns an object with
// the height, width, and x and y coordinates.
let { width, height } = mainWindow.getBounds();
// Now that we have them, save them using the `set` method.
store.set('windowBounds', { width, height });
});
mainWindow.loadURL('file://' + path.join(__dirname, 'index.html'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment