Refactor storage in app.js

This commit is contained in:
Gregory Schier
2016-07-29 10:18:04 -07:00
parent 7af8e00c19
commit f3f215ec6e
3 changed files with 183 additions and 160 deletions

View File

@@ -6,9 +6,12 @@ if (require('electron-squirrel-startup')) {
// Don't npm install this (it breaks). Rely on the global one.
const electron = require('electron');
const path = require('path');
const appVersion = require('./app.json').version;
const storage = require('electron-json-storage');
const {app, dialog, ipcMain, autoUpdater, Menu, BrowserWindow, webContents} = electron;
const {LocalStorage} = require('node-localstorage');
const localStorage = new LocalStorage(path.join(app.getPath('userData'), 'localStorage'));
const IS_DEV = process.env.NODE_ENV === 'development';
const IS_MAC = process.platform === 'darwin';
@@ -22,7 +25,6 @@ const UPDATE_URLS = {
};
let mainWindow = null;
let zoomFactor = 1;
// Enable this for CSS grid layout :)
app.commandLine.appendSwitch('enable-experimental-web-platform-features');
@@ -77,6 +79,38 @@ ipcMain.on('check-for-updates', () => {
}
});
function saveBounds () {
localStorage.setItem('bounds', JSON.stringify(mainWindow.getBounds()));
}
function getBounds () {
let bounds = {};
try {
bounds = JSON.parse(localStorage.getItem('bounds') || '{}');
} catch (e) {
// This should never happen, but if it does...!
console.error('Failed to parse window bounds', e);
}
return bounds;
}
function saveZoomFactor (zoomFactor) {
localStorage.setItem('zoomFactor', JSON.stringify(zoomFactor));
}
function getZoomFactor () {
let zoomFactor = 1;
try {
zoomFactor = JSON.parse(localStorage.getItem('zoomFactor') || '1');
} catch (e) {
// This should never happen, but if it does...!
console.error('Failed to parse zoomFactor', e);
}
return zoomFactor;
}
// Quit when all windows are closed (except on Mac).
app.on('window-all-closed', () => {
if (!IS_MAC) {
@@ -85,162 +119,151 @@ app.on('window-all-closed', () => {
});
app.on('ready', () => {
storage.get('bounds', (err, data) => {
if (err) {
console.log('Failed to fetch window bounds', err);
const zoomFactor = getZoomFactor();
const bounds = getBounds();
const {x, y, width, height} = bounds;
mainWindow = new BrowserWindow({
x: x,
y: y,
width: width || 1200,
height: height || 600,
minHeight: 500,
minWidth: 500,
acceptFirstMouse: true,
webPreferences: {
zoomFactor: zoomFactor
}
let bounds = {};
try {
bounds = JSON.parse(data || '{}');
} catch (e) {
// This should never happen, but if it does...!
console.error('Failed to parse window bounds', err, data);
}
const {x, y, width, height} = bounds;
mainWindow = new BrowserWindow({
x: x,
y: y,
width: width || 1200,
height: height || 600,
minHeight: 500,
minWidth: 500,
acceptFirstMouse: true,
webPreferences: {
zoomFactor: zoomFactor
}
});
const saveBounds = () => {
storage.set('bounds', JSON.stringify(mainWindow.getBounds()), err => {
if (err) {
console.log('Failed to save window bounds', err);
}
});
};
mainWindow.on('resize', e => saveBounds());
mainWindow.on('move', e => saveBounds());
// and load the app.html of the app.
mainWindow.loadURL(`file://${__dirname}/app.html`);
// Uncomment this to test things
// mainWindow.toggleDevTools();
// Emitted when the window is closed.
mainWindow.on('closed', () => {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
var template = [
{
label: "Application",
role: "window",
submenu: [
{label: "About Application", selector: "orderFrontStandardAboutPanel:"},
{type: "separator"},
{
label: "Quit",
accelerator: "Command+Q",
click: function () {
app.quit();
}
}
]
},
{
label: "Edit",
submenu: [
{label: "Undo", accelerator: "CmdOrCtrl+Z", selector: "undo:"},
{label: "Redo", accelerator: "Shift+CmdOrCtrl+Z", selector: "redo:"},
{type: "separator"},
{label: "Cut", accelerator: "CmdOrCtrl+X", selector: "cut:"},
{label: "Copy", accelerator: "CmdOrCtrl+C", selector: "copy:"},
{label: "Paste", accelerator: "CmdOrCtrl+V", selector: "paste:"},
{label: "Select All", accelerator: "CmdOrCtrl+A", selector: "selectAll:"}
]
},
{
label: "View",
role: "window",
submenu: [
{
label: "Actual Size",
accelerator: "CmdOrCtrl+0",
click: () => {
const window = electron.BrowserWindow.getFocusedWindow();
zoomFactor = 1;
window.webContents.setZoomFactor(zoomFactor);
}
},
{
label: "Zoom In",
accelerator: IS_MAC ? "CmdOrCtrl+Plus" : "CmdOrCtrl+=",
click: () => {
const window = electron.BrowserWindow.getFocusedWindow();
zoomFactor = Math.min(1.8, zoomFactor + 0.1);
window.webContents.setZoomFactor(zoomFactor);
}
},
{
label: "Zoom Out",
accelerator: "CmdOrCtrl+-",
click: () => {
const window = electron.BrowserWindow.getFocusedWindow();
zoomFactor = Math.max(0.5, zoomFactor - 0.1);
window.webContents.setZoomFactor(zoomFactor);
}
}
]
},
{
label: "Help",
role: "help",
id: "help",
submenu: [
{
label: "Report an Issue...",
click: () => {
electron.shell.openExternal('mailto:support@insomnia.rest');
}
},
{
label: "Insomnia Help",
accelerator: "CmdOrCtrl+?",
click: () => {
electron.shell.openExternal('http://insomnia.rest');
}
}
]
}
];
if (IS_DEV) {
template.push({
label: 'Developer',
position: 'before=help',
submenu: [{
label: 'Reload',
accelerator: 'Command+R',
click: function () {
mainWindow.reload();
}
}, {
label: 'Toggle DevTools',
accelerator: 'Alt+Command+I',
click: function () {
mainWindow.toggleDevTools();
}
}]
});
}
Menu.setApplicationMenu(Menu.buildFromTemplate(template));
});
mainWindow.on('resize', e => saveBounds());
mainWindow.on('move', e => saveBounds());
// and load the app.html of the app.
mainWindow.loadURL(`file://${__dirname}/app.html`);
// Uncomment this to test things
// mainWindow.toggleDevTools();
// Emitted when the window is closed.
mainWindow.on('closed', () => {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
var template = [
{
label: "Application",
role: "window",
submenu: [
{label: "About Application", selector: "orderFrontStandardAboutPanel:"},
{type: "separator"},
{
label: "Quit",
accelerator: "Command+Q",
click: function () {
app.quit();
}
}
]
},
{
label: "Edit",
submenu: [
{label: "Undo", accelerator: "CmdOrCtrl+Z", selector: "undo:"},
{label: "Redo", accelerator: "Shift+CmdOrCtrl+Z", selector: "redo:"},
{type: "separator"},
{label: "Cut", accelerator: "CmdOrCtrl+X", selector: "cut:"},
{label: "Copy", accelerator: "CmdOrCtrl+C", selector: "copy:"},
{label: "Paste", accelerator: "CmdOrCtrl+V", selector: "paste:"},
{label: "Select All", accelerator: "CmdOrCtrl+A", selector: "selectAll:"}
]
},
{
label: "View",
role: "window",
submenu: [
{
label: "Actual Size",
accelerator: "CmdOrCtrl+0",
click: () => {
const window = electron.BrowserWindow.getFocusedWindow();
const zoomFactor = 1;
window.webContents.setZoomFactor(zoomFactor);
saveZoomFactor(zoomFactor);
}
},
{
label: "Zoom In",
accelerator: IS_MAC ? "CmdOrCtrl+Plus" : "CmdOrCtrl+=",
click: () => {
let zoomFactor = getZoomFactor();
zoomFactor = Math.min(1.8, zoomFactor + 0.1);
const window = electron.BrowserWindow.getFocusedWindow();
window.webContents.setZoomFactor(zoomFactor);
saveZoomFactor(zoomFactor);
}
},
{
label: "Zoom Out",
accelerator: "CmdOrCtrl+-",
click: () => {
let zoomFactor = getZoomFactor();
zoomFactor = Math.max(0.5, zoomFactor - 0.1);
const window = electron.BrowserWindow.getFocusedWindow();
window.webContents.setZoomFactor(zoomFactor);
saveZoomFactor(zoomFactor);
}
}
]
},
{
label: "Help",
role: "help",
id: "help",
submenu: [
{
label: "Report an Issue...",
click: () => {
electron.shell.openExternal('mailto:support@insomnia.rest');
}
},
{
label: "Insomnia Help",
accelerator: "CmdOrCtrl+?",
click: () => {
electron.shell.openExternal('http://insomnia.rest');
}
}
]
}
];
if (IS_DEV) {
template.push({
label: 'Developer',
position: 'before=help',
submenu: [{
label: 'Reload',
accelerator: 'Command+R',
click: function () {
mainWindow.reload();
}
}, {
label: 'Toggle DevTools',
accelerator: 'Alt+Command+I',
click: function () {
mainWindow.toggleDevTools();
}
}]
});
}
Menu.setApplicationMenu(Menu.buildFromTemplate(template));
});

View File

@@ -11,8 +11,8 @@
"dependencies": {
"analytics-node": "^2.1.0",
"electron-squirrel-startup": "^1.0.0",
"electron-json-storage": "^2.0.0",
"nedb": "^1.8.0",
"node-localstorage": "^1.3.0",
"request": "^2.71.0"
}
}

View File

@@ -43,13 +43,13 @@
"analytics-node": "^2.1.0",
"classnames": "^2.2.3",
"codemirror": "^5.12.0",
"electron-json-storage": "^2.0.0",
"electron-prebuilt": "^1.2.6",
"electron-squirrel-startup": "^1.0.0",
"json-lint": "^0.1.0",
"jsonschema": "^1.1.0",
"mousetrap": "^1.6.0",
"nedb": "^1.8.0",
"node-localstorage": "^1.3.0",
"nunjucks": "^1.3.4",
"react": "^15.2.0",
"react-dnd": "^2.1.4",