hyper/app/index.js

230 lines
6.6 KiB
JavaScript
Raw Normal View History

// Print diagnostic information for a few arguments instead of running Hyper.
if (['--help', '-v', '--version'].includes(process.argv[1])) {
const {version} = require('./package');
const configLocation = process.platform === 'win32' ? process.env.userprofile + '\\.hyper.js' : '~/.hyper.js';
console.log(`Hyper version ${version}`);
console.log('Hyper does not accept any command line arguments. Please modify the config file instead.');
console.log(`Hyper configuration file located at: ${configLocation}`);
// eslint-disable-next-line unicorn/no-process-exit
process.exit();
}
// handle startup squirrel events
if (process.platform === 'win32') {
// eslint-disable-next-line import/order
const systemContextMenu = require('./system-context-menu');
switch (process.argv[1]) {
case '--squirrel-install':
case '--squirrel-updated':
systemContextMenu.add(() => {
// eslint-disable-next-line curly, unicorn/no-process-exit
if (require('electron-squirrel-startup')) process.exit();
});
break;
case '--squirrel-uninstall':
systemContextMenu.remove(() => {
// eslint-disable-next-line curly, unicorn/no-process-exit
if (require('electron-squirrel-startup')) process.exit();
});
break;
default:
// eslint-disable-next-line curly, unicorn/no-process-exit
if (require('electron-squirrel-startup')) process.exit();
}
}
// Native
const {resolve} = require('path');
// Packages
const {app, BrowserWindow, Menu} = require('electron');
const {gitDescribe} = require('git-describe');
const isDev = require('electron-is-dev');
const AppMenu = require('./menus/menu');
const config = require('./config');
app.commandLine.appendSwitch('js-flags', '--harmony-async-await');
2016-07-27 18:02:19 -08:00
// set up config
config.setup();
const plugins = require('./plugins');
const Window = require('./ui/window');
2016-06-30 22:01:04 -08:00
const windowSet = new Set([]);
// expose to plugins
app.config = config;
app.plugins = plugins;
app.getWindows = () => new Set([...windowSet]); // return a clone
// function to retrieve the last focused window in windowSet;
// added to app object in order to expose it to plugins.
app.getLastFocusedWindow = () => {
if (!windowSet.size) {
return null;
}
return Array.from(windowSet).reduce((lastWindow, win) => {
return win.focusTime > lastWindow.focusTime ? win : lastWindow;
});
};
if (isDev) {
console.log('running in dev mode');
// Overide default appVersion which is set from package.json
gitDescribe({customArguments: ['--tags']}, (error, gitInfo) => {
if (!error) {
app.setVersion(gitInfo.raw);
}
});
2016-06-30 22:01:04 -08:00
} else {
console.log('running in prod mode');
2016-06-30 22:01:04 -08:00
}
const url = 'file://' + resolve(
2016-07-01 14:44:24 -08:00
isDev ? __dirname : app.getAppPath(),
'index.html'
);
console.log('electron will open', url);
app.on('ready', () => installDevExtensions(isDev).then(() => {
function createWindow(fn, options = {}) {
const cfg = plugins.getDecoratedConfig();
const winSet = config.getWin();
let [startX, startY] = winSet.position;
const [width, height] = options.size ? options.size : (cfg.windowSize || winSet.size);
const {screen} = require('electron');
const winPos = options.position;
// Open the new window roughly the height of the header away from the
// previous window. This also ensures in multi monitor setups that the
// new terminal is on the correct screen.
const focusedWindow = BrowserWindow.getFocusedWindow() || app.getLastFocusedWindow();
// In case of options defaults position and size, we should ignore the focusedWindow.
if (winPos !== undefined) {
[startX, startY] = winPos;
} else if (focusedWindow) {
const points = focusedWindow.getPosition();
const currentScreen = screen.getDisplayNearestPoint({x: points[0], y: points[1]});
const biggestX = ((points[0] + 100 + width) - currentScreen.bounds.x);
const biggestY = ((points[1] + 100 + height) - currentScreen.bounds.y);
if (biggestX > currentScreen.size.width) {
startX = 50;
} else {
startX = points[0] + 34;
}
if (biggestY > currentScreen.size.height) {
startY = 50;
} else {
startY = points[1] + 34;
}
}
const hwin = new Window({width, height, x: startX, y: startY}, cfg, fn);
windowSet.add(hwin);
hwin.loadURL(url);
2016-06-30 22:01:04 -08:00
// the window can be closed by the browser process itself
hwin.on('close', () => {
hwin.clean();
windowSet.delete(hwin);
});
hwin.on('closed', () => {
Add Windows support and first-class Linux support (#946) * `child_pty` => `pty.js` * Create a frameless window on Windows and Linux * Add a brand new UI for Linux and Windows :nail_care: * [Windows] Fix plugin installation * [Windows] Fix the `build` script * [Windows] Add a bigger `icon.ico` * [Mac] Add `WebKitAppRegion: drag` when running on macOS * Fix code style :thinking: * Add `appveyor.yml` * Fix code style (again) * [Windows] Fix AppVeyor's `install` script * [Windows] Try a new AppVeyor config * [Windows] Set the binary path so Spectron can run the tests * [Windows] Try to build on x64 * Try again to build on x64 * Try one more time :weary: * Throw an error to indicate that `pty.js` was built incorrectly * [Win/Linux] Add `display: hidden` to <Tabs /> if tabs.length === 1 * [Win/Linux] Reorganize SVGs – via @CodeTheory * [Win/Linux] Fix the hamburger menu height * Make the SVGs look better with `shape-rendering: crispEdges;` * [Win/Linux] Add config options for the window controls and the :hamburger: menu * Add `electron-squirrel-startup` dependency * [Win] Handle Squirrel commands * [Win/Linux] Fix default color for the :hamburger: and window controls – via @CodeTheory * [Win/Linux] Add some padding - via @CodeTheory * [Win/Linux] Add hover states – via @CodeTheory * [Win] Fix empty window/tab titles * [Win] Fix opening Preferences (#978) * [Win] Fix opening Preferences * Update ui.js * Update ui.js * Enhance messages and default editor * [Win] Add dependency instructions to the README.md [skip ci] * Fix code style * [Win/Linux] Check the number of open windows before quitting the app
2016-11-11 08:18:04 -09:00
if (process.platform !== 'darwin' && windowSet.size === 0) {
app.quit();
}
2016-06-30 22:01:04 -08:00
});
}
// when opening create a new window
createWindow();
// expose to plugins
app.createWindow = createWindow;
2016-07-01 16:02:08 -08:00
// mac only. when the dock icon is clicked
// and we don't have any active windows open,
// we open one
app.on('activate', () => {
if (!windowSet.size) {
2016-07-01 16:02:08 -08:00
createWindow();
}
});
const makeMenu = () => {
const menu = plugins.decorateMenu(
AppMenu(createWindow, () => {
plugins.updatePlugins({force: true});
},
plugins.getLoadedPluginVersions
));
// If we're on Mac make a Dock Menu
if (process.platform === 'darwin') {
const dockMenu = Menu.buildFromTemplate([{
label: 'New Window',
click() {
createWindow();
}
}]);
app.dock.setMenu(dockMenu);
}
Menu.setApplicationMenu(
Menu.buildFromTemplate(menu)
);
2016-07-07 16:16:44 -08:00
};
const load = () => {
plugins.onApp(app);
plugins.extendKeymaps();
makeMenu();
2016-07-07 16:16:44 -08:00
};
load();
plugins.subscribe(load);
}).catch(err => {
console.error('Error while loading devtools extensions', err);
}));
2016-06-30 22:01:04 -08:00
app.on('open-file', (event, path) => {
const lastWindow = app.getLastFocusedWindow();
const callback = win => win.rpc.emit('open file', {path});
if (lastWindow) {
callback(lastWindow);
} else if (!lastWindow && {}.hasOwnProperty.call(app, 'createWindow')) {
app.createWindow(callback);
} else {
// If createWindow doesn't exist yet ('ready' event was not fired),
// sets his callback to an app.windowCallback property.
app.windowCallback = callback;
}
});
function installDevExtensions(isDev) {
if (!isDev) {
return Promise.resolve();
}
// eslint-disable-next-line import/no-extraneous-dependencies
const installer = require('electron-devtools-installer');
const extensions = [
'REACT_DEVELOPER_TOOLS',
'REDUX_DEVTOOLS'
];
const forceDownload = Boolean(process.env.UPGRADE_EXTENSIONS);
return Promise.all(extensions.map(name => installer.default(installer[name], forceDownload)));
}