insomnia/app/main.development.js

534 lines
13 KiB
JavaScript
Raw Normal View History

2016-07-18 19:44:46 +00:00
if (require('electron-squirrel-startup')) {
process.exit(0);
}
2016-11-10 02:53:06 +00:00
import raven from 'raven';
import request from 'request';
import path from 'path';
import electron from 'electron';
import * as packageJSON from './package.json';
2016-11-10 21:03:12 +00:00
import LocalStorage from './common/LocalStorage';
import installExtension, {REACT_DEVELOPER_TOOLS} from 'electron-devtools-installer';
2016-11-10 02:53:06 +00:00
// 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-11-10 02:53:06 +00:00
var ravenClient = new raven.Client('https://786e43ae199c4757a9ea4a48a9abd17d@sentry.io/109702', {
environment: process.env.INSOMNIA_ENV || 'production',
release: require('./package.json').version,
logger: 'electron.main'
});
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-11-10 02:53:06 +00:00
const {app, dialog, shell, ipcMain, autoUpdater, Menu, BrowserWindow, webContents} = electron;
const {version: appVersion, productName: appName} = packageJSON;
2016-10-27 05:37:40 +00:00
2016-07-18 19:44:46 +00:00
const UPDATE_URLS = {
2016-09-21 19:03:40 +00:00
// Add `r` param to help cache bust
2016-09-22 23:18:46 +00:00
darwin: `https://updates.insomnia.rest/builds/check/mac?v=${appVersion}`,
linux: `https://updates.insomnia.rest/builds/check/linux?v=${appVersion}`,
win32: `https://downloads.insomnia.rest/win`
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';
const localStoragePath = path.join(app.getPath('userData'), 'localStorage');
const localStorage = new LocalStorage(localStoragePath);
2016-07-07 20:10:55 +00:00
let mainWindow = 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 => {
2016-09-13 02:09:35 +00:00
if (IS_DEV) {
console.error(e);
2016-10-27 03:41:30 +00:00
} else {
// Don't report autoUpdater error. They are way too noisy
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} --`);
showUpdateNotification();
2016-07-14 01:12:42 +00:00
});
2016-08-17 22:44:03 +00:00
function checkForUpdates () {
if (hasPromptedForUpdates) {
// We've already prompted for updates. Don't bug the user anymore
2016-09-13 02:09:35 +00:00
return;
}
if (IS_DEV) {
Sync Proof of Concept (#33) * Maybe working POC * Change to use remote url * Other URL too * Some logic * Got the push part working * Made some updates * Fix * Update * Add status code check * Stuff * Implemented new sync api * A bit more robust * Debounce changes * Change timeout * Some fixes * Remove .less * Better error handling * Fix base url * Support for created vs updated docs * Try silent * Silence removal too * Small fix after merge * Fix test * Stuff * Implement key generation algorithm * Tidy * stuff * A bunch of stuff for the new API * Integrated the session stuff * Stuff * Just started on encryption * Lots of updates to encryption * Finished createResourceGroup function * Full encryption/decryption working (I think) * Encrypt localstorage with sessionID * Some more * Some extra checks * Now uses separate DB. Still needs to be simplified a LOT * Fix deletion bug * Fixed unicode bug with encryption * Simplified and working * A bunch of polish * Some stuff * Removed some workspace meta properties * Migrated a few more meta properties * Small changes * Fix body scrolling and url cursor jumping * Removed duplication of webpack port * Remove workspaces reduces * Some small fixes * Added sync modal and opt-in setting * Good start to sync flow * Refactored modal footer css * Update sync status * Sync logger * A bit better logging * Fixed a bunch of sync-related bugs * Fixed signup form button * Gravatar component * Split sync modal into tabs * Tidying * Some more error handling * start sending 'user agent * Login/signup error handling * Use real UUIDs * Fixed tests * Remove unused function * Some extra checks * Moved cloud sync setting to about page * Some small changes * Some things
2016-10-21 17:20:36 +00:00
console.log('-- Skipping update check in Development --');
2016-08-23 20:54:11 +00:00
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',
2016-10-11 21:06:25 +00:00
buttons: ['Cancel', 'Reload'],
2016-09-13 02:09:35 +00:00
defaultId: 1,
cancelId: 0,
title: 'Unresponsive',
message: 'Insomnia has become unresponsive. Do you want to reload?'
}, id => {
if (id === 1) {
mainWindow.destroy();
createWindow();
}
});
}
function showUpdateNotification () {
if (hasPromptedForUpdates) {
return;
}
2016-08-23 20:54:11 +00:00
const window = BrowserWindow.getFocusedWindow();
if (!window || !window.webContents) {
return;
}
2016-10-11 21:06:25 +00:00
window.webContents.send('update-available');
hasPromptedForUpdates = true;
}
2016-11-23 19:49:10 +00:00
function trackEvent (...args) {
const window = BrowserWindow.getFocusedWindow();
if (!window || !window.webContents) {
return;
}
window.webContents.send('analytics-track-event', args);
}
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'],
2016-08-20 20:41:44 +00:00
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();
});
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) {
localStorage.setItem('bounds', mainWindow.getBounds());
localStorage.setItem('fullscreen', false);
} else {
localStorage.setItem('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 = localStorage.getItem('bounds', {});
fullscreen = localStorage.getItem('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) {
localStorage.setItem('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 = localStorage.getItem('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-11-18 01:25:45 +00:00
app.quit();
2016-11-18 01:23:41 +00:00
}
});
// 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) {
2016-10-27 01:29:25 +00:00
try {
createWindow()
} catch (e) {
// This might happen if 'ready' hasn't fired yet. So we're just going
// to silence these errors.
console.log('-- App not ready to "activate" yet --');
}
}
});
// When the app is first launched
app.on('ready', () => {
createWindow();
2016-10-11 21:06:25 +00:00
checkForUpdates();
});
2016-08-17 22:44:03 +00:00
function createWindow () {
2016-07-29 17:18:04 +00:00
const zoomFactor = getZoomFactor();
const {bounds, fullscreen} = getBounds();
const {x, y, width, height} = bounds;
// Make sure we don't place the window outside of the visible space
let maxX = 0;
let maxY = 0;
for (const d of electron.screen.getAllDisplays()) {
// Set the maximum placement location to 50 pixels short of the end
maxX = Math.max(maxX, d.bounds.x + d.bounds.width - 50);
maxY = Math.max(maxY, d.bounds.y + d.bounds.height - 50);
}
const finalX = Math.min(maxX, x);
const finalY = Math.min(maxX, y);
2016-03-22 18:20:05 +00:00
2016-07-29 17:18:04 +00:00
mainWindow = new BrowserWindow({
// Make sure we don't initialize the window outside the bounds
x: finalX,
y: finalY,
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-11-23 19:49:10 +00:00
let _resizeTimeout = null;
mainWindow.on('resize', e => {
saveBounds();
clearTimeout(_resizeTimeout);
_resizeTimeout = setTimeout(() => {
trackEvent('Window', 'Resize');
}, 1000);
});
mainWindow.on('move', e => {
saveBounds();
trackEvent('Window', 'Move');
});
mainWindow.on('unresponsive', e => {
showUnresponsiveModal();
trackEvent('Window', 'Unresponsive');
});
2016-04-20 16:15:19 +00:00
2016-07-29 17:18:04 +00:00
// and load the app.html of the app.
2016-09-17 05:46:44 +00:00
mainWindow.loadURL(`file://${__dirname}/main.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-11-23 19:49:10 +00:00
trackEvent('Window', 'Close');
2016-07-29 17:18:04 +00:00
});
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-11-23 19:49:10 +00:00
trackEvent('App Menu', 'Preferences');
}
},
2016-11-23 19:59:26 +00:00
{
label: "Changelog",
click: function (menuItem, window, e) {
if (!window || !window.webContents) {
return
}
window.webContents.send('toggle-changelog');
trackEvent('App Menu', 'Changelog');
}
},
{
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-11-23 19:49:10 +00:00
trackEvent('App Menu', 'Zoom Reset');
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
}
2016-10-26 22:13:16 +00:00
const zoomFactor = Math.min(1.8, getZoomFactor() + 0.05);
window.webContents.setZoomFactor(zoomFactor);
saveZoomFactor(zoomFactor);
2016-11-23 19:49:10 +00:00
trackEvent('App Menu', 'Zoom In');
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
}
2016-10-26 22:13:16 +00:00
const zoomFactor = Math.max(0.5, getZoomFactor() - 0.05);
window.webContents.setZoomFactor(zoomFactor);
saveZoomFactor(zoomFactor);
2016-11-23 19:49:10 +00:00
trackEvent('App Menu', 'Zoom Out');
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-11-23 19:49:10 +00:00
trackEvent('App Menu', '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: () => {
2016-11-23 19:49:10 +00:00
trackEvent('App Menu', 'Contact');
shell.openExternal('mailto:support@insomnia.rest');
2016-08-22 20:08:22 +00:00
}
},
{
2016-09-13 17:39:55 +00:00
label: "Insomnia Help",
2016-08-22 20:08:22 +00:00
accelerator: "CmdOrCtrl+?",
2016-11-23 19:49:10 +00:00
click: (e) => {
trackEvent('App Menu', 'Help');
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) {
installExtension(REACT_DEVELOPER_TOOLS)
.then((name) => console.log(`Added Extension: ${name}`))
.catch((err) => console.log('An error occurred: ', err));
2016-07-29 17:18:04 +00:00
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));
}