insomnia/app/app.js

531 lines
12 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);
}
// Some useful helpers
const IS_DEV = process.env.INSOMNIA_ENV === 'development';
const IS_MAC = process.platform === 'darwin';
const IS_WINDOWS = process.platform === 'win32';
const IS_LINUX = !IS_MAC && !IS_WINDOWS;
2016-08-17 22:44:03 +00:00
var raven = require('raven');
var ravenClient = new raven.Client(
'https://fb3242f902b54cdd934b8ffa204426c0:23430fbe203a' +
2016-08-19 23:12:33 +00:00
'4189a68efb63c38fc50b@app.getsentry.com/88289', {
environment: process.env.INSOMNIA_ENV || 'production',
release: require('./app.json').version,
logger: 'main'
2016-08-19 23:12:33 +00:00
});
2016-08-17 22:44:03 +00:00
2016-09-13 02:09:35 +00:00
if (!IS_DEV) {
ravenClient.patchGlobal();
}
2016-08-17 22:44:03 +00:00
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-08-20 20:41:44 +00:00
const request = require('request');
2016-07-29 17:18:04 +00:00
const path = require('path');
const {version: appVersion, productName: appName} = require('./app.json');
2016-08-20 20:41:44 +00:00
const {LocalStorage} = require('node-localstorage');
const {
app,
dialog,
2016-08-20 20:41:44 +00:00
shell,
ipcMain,
autoUpdater,
Menu,
BrowserWindow,
webContents
} = electron;
2016-07-29 17:18:04 +00:00
2016-07-18 19:44:46 +00:00
const UPDATE_URLS = {
2016-08-19 23:12:33 +00:00
darwin: `https://updates.insomnia.rest/builds/check/mac?v=${appVersion}`,
2016-07-18 19:44:46 +00:00
win32: 'https://s3.amazonaws.com/builds-insomnia-rest/win',
2016-08-20 20:41:44 +00:00
linux: `https://updates.insomnia.rest/builds/check/linux?v=${appVersion}`
2016-07-18 19:44:46 +00:00
};
2016-07-14 01:12:42 +00:00
2016-08-20 20:41:44 +00:00
const DOWNLOAD_URL = 'http://download.insomnia.rest';
2016-07-07 20:10:55 +00:00
let mainWindow = null;
2016-08-03 16:35:43 +00:00
let localStorage = null;
2016-08-23 20:54:11 +00:00
let hasPromptedForUpdates = false;
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-09-13 17:29:09 +00:00
process.on('uncaughtException', e => {
if (IS_DEV) {
console.error(e);
} else {
ravenClient.captureError(e, {});
}
});
2016-08-17 22:44:03 +00:00
autoUpdater.on('error', e => {
// Failed to launch auto updater
2016-09-13 02:09:35 +00:00
if (IS_DEV) {
console.error(e);
} else {
ravenClient.captureError(e, {
level: 'warning'
});
2016-09-13 02:09:35 +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 () {
2016-09-13 02:09:35 +00:00
if (IS_DEV) {
console.log('Skipping update check in Development');
return;
}
2016-08-23 20:54:11 +00:00
if (hasPromptedForUpdates) {
// We've already prompted for updates. Don't bug the user anymore
return;
}
2016-08-20 20:41:44 +00:00
if (IS_LINUX) {
try {
request.get(UPDATE_URLS.linux, null, (err, response) => {
if (err) {
ravenClient.captureError(err);
return;
}
if (response.statusCode === 200) {
showDownloadModal(response.body);
} else {
// No update available (should be STATUS=204)
}
});
} catch (e) {
ravenClient.captureException(e);
}
} else {
try {
autoUpdater.setFeedURL(UPDATE_URLS[process.platform]);
autoUpdater.checkForUpdates();
} catch (e) {
// This will fail in development
}
2016-08-17 22:44:03 +00:00
}
}
2016-09-13 02:09:35 +00:00
function showUnresponsiveModal () {
dialog.showMessageBox({
type: 'info',
buttons: [
'Cancel',
'Reload',
],
defaultId: 1,
cancelId: 0,
title: 'Unresponsive',
message: 'Insomnia has become unresponsive. Do you want to reload?'
}, id => {
if (id === 1) {
mainWindow.destroy();
createWindow();
}
});
}
2016-08-17 22:44:03 +00:00
function showUpdateModal () {
2016-08-23 20:54:11 +00:00
hasPromptedForUpdates = true;
dialog.showMessageBox({
type: 'info',
buttons: [
'Install and Restart',
'Later',
],
defaultId: 0,
cancelId: 1,
title: 'Insomnia Update Available',
message: 'Exciting news!\n\nA new version of Insomnia 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 --');
}
});
}
2016-08-20 20:41:44 +00:00
function showDownloadModal (version) {
2016-08-23 20:54:11 +00:00
hasPromptedForUpdates = true;
2016-08-20 20:41:44 +00:00
dialog.showMessageBox({
type: 'info',
buttons: [
'Download',
'Later',
],
defaultId: 0,
cancelId: 1,
title: 'Insomnia Update Available',
message: `Exciting news!\n\nVersion ${version} of Insomnia is now available.\n\n\n~Gregory`
2016-08-20 20:41:44 +00:00
}, id => {
if (id === 0) {
console.log('-- Installing Update --');
shell.openExternal(DOWNLOAD_URL);
} else {
console.log('-- Cancel Update --');
}
});
}
ipcMain.on('check-for-updates', () => {
console.log('-- Checking for Updates --');
2016-08-17 22:44:03 +00:00
checkForUpdates();
});
function storeValue (key, obj) {
try {
localStorage.setItem(key, JSON.stringify(obj));
} catch (e) {
console.error('Failed to save to LocalStorage', obj, e);
ravenClient.captureError(e, {
level: 'warning'
});
}
}
function getValue (key, defaultObj) {
try {
return JSON.parse(localStorage.getItem(key) || JSON.stringify(defaultObj))
} catch (e) {
console.error('Failed to get from LocalStorage', e);
ravenClient.captureError(e, {
level: 'warning'
});
}
}
2016-08-17 22:44:03 +00:00
function saveBounds () {
if (!mainWindow) {
return;
}
const fullscreen = mainWindow.isFullScreen();
// Only save the size if we're not in fullscreen
if (!fullscreen) {
storeValue('bounds', mainWindow.getBounds());
storeValue('fullscreen', false);
} else {
storeValue('fullscreen', true);
}
2016-07-29 17:18:04 +00:00
}
2016-08-17 22:44:03 +00:00
function getBounds () {
2016-07-29 17:18:04 +00:00
let bounds = {};
let fullscreen = false;
2016-07-29 17:18:04 +00:00
try {
bounds = getValue('bounds', {});
fullscreen = getValue('fullscreen', false);
2016-07-29 17:18:04 +00:00
} catch (e) {
// This should never happen, but if it does...!
console.error('Failed to parse window bounds', e);
}
return {bounds, fullscreen};
2016-07-29 17:18:04 +00:00
}
2016-08-17 22:44:03 +00:00
function saveZoomFactor (zoomFactor) {
storeValue('zoomFactor', zoomFactor);
2016-07-29 17:18:04 +00:00
}
2016-08-17 22:44:03 +00:00
function getZoomFactor () {
2016-07-29 17:18:04 +00:00
let zoomFactor = 1;
try {
zoomFactor = getValue('zoomFactor', 1);
2016-07-29 17:18:04 +00:00
} 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();
}
});
// Mac-only, when the user clicks the doc icon
app.on('activate', (e, hasVisibleWindows) => {
// Create a new window when clicking the doc icon if there isn't one open
if (!hasVisibleWindows) {
createWindow()
}
});
// When the app is first launched
app.on('ready', () => {
2016-08-17 22:44:03 +00:00
checkForUpdates();
createWindow();
});
2016-08-17 22:44:03 +00:00
function initLocalStorage () {
if (!localStorage) {
localStorage = new LocalStorage(path.join(app.getPath('userData'), 'localStorage'));
}
}
function createWindow () {
initLocalStorage();
2016-08-03 16:35:43 +00:00
2016-07-29 17:18:04 +00:00
const zoomFactor = getZoomFactor();
const {bounds, fullscreen} = 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,
fullscreen: fullscreen,
fullscreenable: true,
2016-09-13 02:09:35 +00:00
title: appName,
2016-07-29 17:18:04 +00:00
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-09-13 02:09:35 +00:00
mainWindow.on('unresponsive', e => showUnresponsiveModal());
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-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
2016-08-25 19:44:51 +00:00
require('electron-context-menu')({});
2016-08-22 20:08:22 +00:00
var template = [
{
label: "Application",
submenu: [
{
label: `About ${appName}`,
2016-08-22 20:05:42 +00:00
role: 'about',
visible: IS_MAC
},
{
type: "separator",
visible: IS_MAC
},
{
label: "Preferences",
accelerator: "CmdOrCtrl+,",
2016-08-22 20:05:42 +00:00
click: function (menuItem, window, e) {
if (!window || !window.webContents) {
return
2016-08-23 20:54:11 +00:00
}
window.webContents.send('toggle-preferences');
}
},
{
2016-08-23 20:54:11 +00:00
type: "separator",
visible: IS_MAC
},
{
2016-08-23 20:54:11 +00:00
role: "hide",
visible: IS_MAC
},
{
role: "hideothers",
visible: IS_MAC
},
{type: "separator"},
{
label: "Quit",
accelerator: "Command+Q",
click: function () {
app.quit();
}
}
]
2016-08-22 20:08:22 +00:00
},
{
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",
submenu: [
{
role: 'togglefullscreen'
},
{
label: "Actual Size",
accelerator: "CmdOrCtrl+0",
click: () => {
const window = BrowserWindow.getFocusedWindow();
if (!window || !window.webContents) {
return;
2016-09-13 02:09:35 +00:00
}
const zoomFactor = 1;
window.webContents.setZoomFactor(zoomFactor);
saveZoomFactor(zoomFactor);
2016-08-22 20:08:22 +00:00
}
},
{
label: "Zoom In",
accelerator: IS_MAC ? "CmdOrCtrl+Plus" : "CmdOrCtrl+=",
click: () => {
const window = BrowserWindow.getFocusedWindow();
if (!window || !window.webContents) {
return;
2016-09-13 02:09:35 +00:00
}
const zoomFactor = Math.min(1.8, getZoomFactor() + 0.1);
window.webContents.setZoomFactor(zoomFactor);
saveZoomFactor(zoomFactor);
2016-08-22 20:08:22 +00:00
}
},
{
label: "Zoom Out",
accelerator: "CmdOrCtrl+-",
click: () => {
const window = BrowserWindow.getFocusedWindow();
if (!window || !window.webContents) {
return;
2016-09-13 02:09:35 +00:00
}
const zoomFactor = Math.max(0.5, getZoomFactor() - 0.1);
window.webContents.setZoomFactor(zoomFactor);
saveZoomFactor(zoomFactor);
2016-08-22 20:08:22 +00:00
}
},
{
label: "Toggle Sidebar",
accelerator: "CmdOrCtrl+\\",
click: () => {
const window = BrowserWindow.getFocusedWindow();
if (!window || !window.webContents) {
return;
}
window.webContents.send('toggle-sidebar');
}
}
2016-08-22 20:08:22 +00:00
]
}, {
label: "Window",
role: "window",
submenu: [
{
role: 'minimize'
},
{
role: 'close'
}
2016-08-22 20:08:22 +00:00
]
}, {
label: "Help",
role: "help",
id: "help",
submenu: [
{
label: "Contact Support",
2016-08-22 20:08:22 +00:00
click: () => {
electron.shell.openExternal('mailto:support@insomnia.rest');
}
},
{
2016-09-13 17:39:55 +00:00
label: "Insomnia Help",
2016-08-22 20:08:22 +00:00
accelerator: "CmdOrCtrl+?",
click: () => {
electron.shell.openExternal('http://docs.insomnia.rest');
2016-08-22 20:08:22 +00:00
}
}
2016-08-22 20:08:22 +00:00
]
}
];
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));
}