insomnia/app/app.js

327 lines
7.5 KiB
JavaScript
Raw Normal View History

2016-04-20 06:09:46 +00:00
'use strict';
2016-03-22 18:20:05 +00:00
2016-07-18 19:44:46 +00:00
if (require('electron-squirrel-startup')) {
process.exit(0);
}
2016-08-17 22:44:03 +00:00
var raven = require('raven');
var ravenClient = new raven.Client(
'https://fb3242f902b54cdd934b8ffa204426c0:23430fbe203a' +
'4189a68efb63c38fc50b@app.getsentry.com/88289'
);
if (!IS_DEV) {
ravenClient.patchGlobal();
}
2016-03-22 18:20:05 +00:00
// Don't npm install this (it breaks). Rely on the global one.
2016-04-20 06:09:46 +00:00
const electron = require('electron');
2016-07-29 17:18:04 +00:00
const path = require('path');
const appVersion = require('./app.json').version;
const {
app,
dialog,
ipcMain,
autoUpdater,
Menu,
BrowserWindow,
webContents
} = electron;
const {
LocalStorage
} = require('node-localstorage');
2016-07-29 17:18:04 +00:00
2016-08-17 22:44:03 +00:00
const IS_DEV = process.env.INSOMNIA_ENV === 'development';
2016-03-23 18:34:39 +00:00
const IS_MAC = process.platform === 'darwin';
2016-08-17 18:05:52 +00:00
// const IS_WIN = process.platform === 'win32';
// const IS_LIN = process.platform === 'linux';
2016-07-18 19:44:46 +00:00
const UPDATE_URLS = {
darwin: `http://updates.insomnia.rest/builds/check/mac?v=${appVersion}`,
win32: 'https://s3.amazonaws.com/builds-insomnia-rest/win',
linux: null
};
2016-07-14 01:12:42 +00:00
2016-07-07 20:10:55 +00:00
let mainWindow = null;
2016-08-03 16:35:43 +00:00
let localStorage = null;
2016-03-22 18:20:05 +00:00
2016-05-01 19:56:30 +00:00
// Enable this for CSS grid layout :)
2016-07-16 07:22:08 +00:00
app.commandLine.appendSwitch('enable-experimental-web-platform-features');
2016-05-01 19:56:30 +00:00
2016-08-17 22:44:03 +00:00
autoUpdater.on('error', e => {
// Failed to launch auto updater
if (!IS_DEV) {
ravenClient.captureError(e);
2016-07-25 22:51:34 +00:00
}
2016-08-17 22:44:03 +00:00
});
2016-07-14 01:12:42 +00:00
autoUpdater.on('update-not-available', () => {
console.log('-- Update Not Available --')
});
autoUpdater.on('update-available', () => {
console.log('-- Update Available --');
});
autoUpdater.on('update-downloaded', (e, releaseNotes, releaseName, releaseDate, updateUrl) => {
console.log(`-- Update Downloaded ${releaseName} --`);
showUpdateModal();
2016-07-14 01:12:42 +00:00
});
2016-08-17 22:44:03 +00:00
function checkForUpdates () {
try {
autoUpdater.setFeedURL(UPDATE_URLS[process.platform]);
autoUpdater.checkForUpdates();
} catch (e) {
// This will fail in development
}
}
function showUpdateModal () {
dialog.showMessageBox({
type: 'info',
buttons: [
'Install and Restart',
'Later',
],
defaultId: 0,
cancelId: 1,
title: 'New Update Available!',
message: 'Exciting news!\n\nA fresh new update has been downloaded and is ready to install\n\n\n~Gregory'
}, id => {
if (id === 0) {
console.log('-- Installing Update --');
autoUpdater.quitAndInstall();
} else {
console.log('-- Cancel Update --');
}
});
}
ipcMain.on('check-for-updates', () => {
console.log('-- Checking for Updates --');
2016-08-17 22:44:03 +00:00
checkForUpdates();
});
2016-08-17 22:44:03 +00:00
function saveBounds () {
2016-07-29 17:18:04 +00:00
localStorage.setItem('bounds', JSON.stringify(mainWindow.getBounds()));
}
2016-08-17 22:44:03 +00:00
function getBounds () {
2016-07-29 17:18:04 +00:00
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;
}
2016-08-17 22:44:03 +00:00
function saveZoomFactor (zoomFactor) {
2016-07-29 17:18:04 +00:00
localStorage.setItem('zoomFactor', JSON.stringify(zoomFactor));
}
2016-08-17 22:44:03 +00:00
function getZoomFactor () {
2016-07-29 17:18:04 +00:00
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;
}
2016-07-09 04:49:09 +00:00
// Quit when all windows are closed (except on Mac).
app.on('window-all-closed', () => {
2016-03-23 18:34:39 +00:00
if (!IS_MAC) {
2016-03-22 18:20:05 +00:00
app.quit();
}
});
app.on('ready', () => {
2016-08-17 22:44:03 +00:00
// First, check for updates
checkForUpdates();
2016-08-03 16:35:43 +00:00
localStorage = new LocalStorage(path.join(app.getPath('userData'), 'localStorage'));
2016-07-29 17:18:04 +00:00
const zoomFactor = getZoomFactor();
const bounds = getBounds();
const {
x,
y,
width,
height
} = bounds;
2016-03-22 18:20:05 +00:00
2016-07-29 17:18:04 +00:00
mainWindow = new BrowserWindow({
x: x,
y: y,
width: width || 1200,
height: height || 600,
minHeight: 500,
minWidth: 500,
acceptFirstMouse: true,
webPreferences: {
zoomFactor: zoomFactor
2016-07-27 20:07:50 +00:00
}
2016-07-29 17:18:04 +00:00
});
2016-07-29 17:18:04 +00:00
mainWindow.on('resize', e => saveBounds());
mainWindow.on('move', e => saveBounds());
2016-04-20 16:15:19 +00:00
2016-07-29 17:18:04 +00:00
// and load the app.html of the app.
mainWindow.loadURL(`file://${__dirname}/app.html`);
2016-07-27 20:07:50 +00:00
2016-08-17 22:44:03 +00:00
if (IS_DEV) {
BrowserWindow.addDevToolsExtension(
'/Users/gschier/Library/Application Support/Google/Chrome/Default/' +
'Extensions/fmkadmapgofadopljbjfkapdkoienihi/0.15.0_0'
);
}
2016-07-29 17:18:04 +00:00
// Uncomment this to test things
// mainWindow.toggleDevTools();
2016-07-27 20:07:50 +00:00
2016-07-29 17:18:04 +00:00
// 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;
});
2016-07-27 20:07:50 +00:00
var template = [];
if (IS_MAC) {
template.push({
label: "Application",
role: "window",
submenu: [{
label: "About Application",
selector: "orderFrontStandardAboutPanel:"
}, {
type: "separator"
}, {
label: "Quit",
accelerator: "Command+Q",
2016-08-17 22:44:03 +00:00
click: function () {
app.quit();
}
}]
})
}
template = template.concat([{
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: [{
2016-08-17 22:44:03 +00:00
role: 'minimize'
}, {
role: 'close'
}, {
label: "Actual Size",
accelerator: "CmdOrCtrl+0",
click: () => {
const window = 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 = 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 = 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');
}
}]
}]);
2016-04-20 17:38:27 +00:00
2016-07-29 17:18:04 +00:00
if (IS_DEV) {
template.push({
label: 'Developer',
position: 'before=help',
submenu: [{
label: 'Reload',
accelerator: 'Command+R',
2016-08-17 22:44:03 +00:00
click: function () {
2016-07-29 17:18:04 +00:00
mainWindow.reload();
}
}, {
label: 'Toggle DevTools',
accelerator: 'Alt+Command+I',
2016-08-17 22:44:03 +00:00
click: function () {
2016-07-29 17:18:04 +00:00
mainWindow.toggleDevTools();
}
}]
});
}
Menu.setApplicationMenu(Menu.buildFromTemplate(template));
2016-04-09 19:10:34 +00:00
});