insomnia/app/app.js

187 lines
4.9 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-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-14 01:12:42 +00:00
const appVersion = require('./package.json').version;
const autoUpdater = electron.autoUpdater;
2016-07-07 20:10:55 +00:00
const Menu = electron.Menu;
2016-03-22 18:20:05 +00:00
const app = electron.app; // Module to control application life.
const BrowserWindow = electron.BrowserWindow; // Module to create native browser window.
2016-03-23 05:26:27 +00:00
const IS_DEV = process.env.NODE_ENV === 'development';
2016-03-23 18:34:39 +00:00
const IS_MAC = process.platform === 'darwin';
2016-07-18 19:44:46 +00:00
const IS_WIN = process.platform === 'win32';
const IS_LIN = process.platform === 'linux';
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;
let zoomFactor = 1;
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-07-14 01:12:42 +00:00
if (!IS_DEV) {
2016-07-18 19:44:46 +00:00
autoUpdater.setFeedURL(UPDATE_URLS[process.platform]);
2016-07-14 01:12:42 +00:00
autoUpdater.checkForUpdates();
}
autoUpdater.on('update-not-available', () => {
console.log('-- Update Not Available --')
});
autoUpdater.on('update-available', () => {
console.log('-- Update Available --');
});
autoUpdater.on('update-downloaded', (e) => {
console.log('-- Update Downloaded --');
autoUpdater.quitAndInstall();
});
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-03-22 18:20:05 +00:00
mainWindow = new BrowserWindow({
2016-04-19 06:05:55 +00:00
width: 1200,
height: 600,
2016-04-23 06:53:22 +00:00
minHeight: 500,
2016-04-09 19:10:34 +00:00
minWidth: 500,
2016-07-07 20:10:55 +00:00
acceptFirstMouse: true,
webPreferences: {
zoomFactor: zoomFactor
}
2016-03-22 18:20:05 +00:00
});
2016-04-19 06:05:55 +00:00
// and load the app.html of the app.
2016-04-20 06:09:46 +00:00
mainWindow.loadURL(`file://${__dirname}/app.html`);
2016-03-22 18:20:05 +00:00
// Emitted when the window is closed.
mainWindow.on('closed', () => {
2016-03-22 18:20:05 +00:00
// 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-04-20 16:15:19 +00:00
2016-07-16 06:07:27 +00:00
var template = [
{
label: "Application",
role: "window",
submenu: [
{label: "About Application", selector: "orderFrontStandardAboutPanel:"},
{type: "separator"},
{
label: "Quit",
accelerator: "Command+Q",
click: function () {
app.quit();
}
2016-07-07 20:10:55 +00:00
}
2016-07-16 06:07:27 +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",
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: "CmdOrCtrl+Plus",
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);
}
2016-07-07 20:10:55 +00:00
}
2016-07-16 06:07:27 +00:00
]
},
{
label: "Help",
role: "help",
2016-07-16 07:22:08 +00:00
id: "help",
2016-07-16 06:07:27 +00:00
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-07-07 20:10:55 +00:00
}
2016-07-16 06:07:27 +00:00
]
}
];
2016-04-20 17:38:27 +00:00
if (IS_DEV) {
template.push({
2016-07-16 06:07:27 +00:00
label: 'Developer',
2016-07-16 07:22:08 +00:00
position: 'before=help',
2016-04-20 17:38:27 +00:00
submenu: [{
label: 'Reload',
accelerator: 'Command+R',
click: function () {
mainWindow.reload();
}
}, {
label: 'Toggle DevTools',
accelerator: 'Alt+Command+I',
click: function () {
mainWindow.toggleDevTools();
}
}]
});
}
2016-07-18 19:44:46 +00:00
if (!IS_WIN) {
Menu.setApplicationMenu(Menu.buildFromTemplate(template));
}
2016-04-09 19:10:34 +00:00
});