Created
September 17, 2016 22:14
-
-
Save ccnokes/d666eee93b7c86e661dbd0f930a9a6c2 to your computer and use it in GitHub Desktop.
Sample electron app demonstrating how to save user data to a file.
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
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