hyper/index.js

235 lines
5.3 KiB
JavaScript
Raw Normal View History

2016-07-01 12:01:33 -08:00
const { app, BrowserWindow, shell, Menu } = require('electron');
2016-06-30 22:01:04 -08:00
const createRPC = require('./rpc');
2016-07-04 18:46:24 -08:00
const createMenu = require('./menu');
2016-06-30 22:01:04 -08:00
const Session = require('./session');
const genUid = require('uid2');
const { resolve } = require('path');
const isDev = require('electron-is-dev');
2016-07-06 06:58:39 -08:00
const AutoUpdater = require('./auto-updater');
2016-07-13 12:44:24 -08:00
const toHex = require('convert-css-color-name-to-hex');
// set up config
const config = require('./config');
config.init();
const plugins = require('./plugins');
2016-06-30 22:01:04 -08:00
// expose to plugins
app.config = config;
app.plugins = plugins;
if (isDev) {
console.log('running in dev mode');
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(),
// in prod version, we copy over index.html and dist from 'app'
// into one dist folder to avoid unwanted files in package
isDev ? 'app' : 'build',
'index.html'
);
console.log('electron will open', url);
2016-06-30 22:01:04 -08:00
app.on('window-all-closed', () => {
// by subscribing to this event and nooping
// we prevent electron's default behavior
// of quitting the app when the last
// terminal is closed
});
2016-07-01 16:02:08 -08:00
let winCount = 0;
2016-06-30 22:01:04 -08:00
app.on('ready', () => {
function createWindow (fn) {
const cfg = plugins.getDecoratedConfig();
const win = new BrowserWindow({
2016-06-30 22:01:04 -08:00
width: 540,
height: 380,
2016-07-14 06:55:24 -08:00
minHeight: 190,
minWidth: 370,
titleBarStyle: 'hidden-inset',
2016-06-30 22:01:04 -08:00
title: 'HyperTerm',
backgroundColor: toHex(cfg.backgroundColor || '#000'),
2016-06-30 22:01:04 -08:00
transparent: true,
// we only want to show when the prompt
// is ready for user input
2016-07-01 14:44:37 -08:00
show: process.env.HYPERTERM_DEBUG || isDev
2016-06-30 22:01:04 -08:00
});
2016-07-01 16:02:08 -08:00
winCount++;
win.loadURL(url);
2016-06-30 22:01:04 -08:00
const rpc = createRPC(win);
const sessions = new Map();
// config changes
const cfgUnsubscribe = config.subscribe(() => {
win.webContents.send('config change');
});
2016-06-30 22:01:04 -08:00
rpc.on('init', () => {
win.show();
// auto updates
if (!isDev) {
2016-07-08 04:48:53 -08:00
AutoUpdater(win);
} else {
console.log('ignoring auto updates during dev');
}
2016-06-30 22:01:04 -08:00
});
rpc.on('new', ({ rows = 40, cols = 100 }) => {
initSession({ rows, cols }, (uid, session) => {
sessions.set(uid, session);
2016-07-14 15:40:15 -08:00
rpc.emit('session add', {
uid,
shell: session.shell
});
2016-06-30 22:01:04 -08:00
session.on('data', (data) => {
2016-07-13 12:44:24 -08:00
rpc.emit('session data', { uid, data });
2016-06-30 22:01:04 -08:00
});
session.on('title', (title) => {
2016-07-13 12:44:24 -08:00
rpc.emit('session title', { uid, title });
2016-06-30 22:01:04 -08:00
});
session.on('exit', () => {
2016-07-13 12:44:24 -08:00
rpc.emit('session exit', { uid });
2016-07-03 12:35:45 -08:00
sessions.delete(uid);
2016-06-30 22:01:04 -08:00
});
});
});
2016-07-13 12:44:24 -08:00
// TODO: this goes away when we are able to poll
// for the title ourseleves, instead of relying
// on Session and focus/blur to subscribe
2016-06-30 22:01:04 -08:00
rpc.on('focus', ({ uid }) => {
2016-07-13 12:44:24 -08:00
const session = sessions.get(uid);
if (session) {
session.focus();
} else {
console.log('session not found by', uid);
}
2016-06-30 22:01:04 -08:00
});
rpc.on('blur', ({ uid }) => {
2016-07-13 12:44:24 -08:00
const session = sessions.get(uid);
if (session) {
session.blur();
} else {
console.log('session not found by', uid);
}
2016-06-30 22:01:04 -08:00
});
rpc.on('exit', ({ uid }) => {
sessions.get(uid).exit();
});
rpc.on('unmaximize', () => {
2016-07-04 17:54:55 -08:00
win.unmaximize();
});
rpc.on('maximize', () => {
2016-07-04 17:54:55 -08:00
win.maximize();
});
2016-06-30 22:01:04 -08:00
rpc.on('resize', ({ cols, rows }) => {
sessions.forEach((session) => {
session.resize({ cols, rows });
});
});
rpc.on('data', ({ uid, data }) => {
sessions.get(uid).write(data);
});
2016-07-01 12:01:33 -08:00
rpc.on('open external', ({ url }) => {
shell.openExternal(url);
});
2016-06-30 22:01:04 -08:00
const deleteSessions = () => {
sessions.forEach((session, key) => {
session.removeAllListeners();
session.destroy();
sessions.delete(key);
});
};
// we reset the rpc channel only upon
// subsequent refreshes (ie: F5)
let i = 0;
win.webContents.on('did-navigate', () => {
if (i++) {
deleteSessions();
}
});
2016-07-07 16:16:44 -08:00
// expose internals to extension authors
win.rpc = rpc;
win.sessions = sessions;
const load = () => {
plugins.onWindow(win);
};
// load plugins
load();
2016-07-13 18:07:58 -08:00
const pluginsUnsubscribe = plugins.subscribe((err) => {
2016-07-13 12:44:24 -08:00
if (!err) {
load();
win.webContents.send('plugins change');
2016-07-13 12:44:24 -08:00
}
2016-07-07 16:16:44 -08:00
});
2016-06-30 22:01:04 -08:00
// the window can be closed by the browser process itself
win.on('close', () => {
2016-07-07 16:16:44 -08:00
winCount--;
2016-06-30 22:01:04 -08:00
rpc.destroy();
deleteSessions();
cfgUnsubscribe();
2016-07-07 16:16:44 -08:00
pluginsUnsubscribe();
2016-06-30 22:01:04 -08:00
});
}
// when opening create a new window
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 (!winCount) {
createWindow();
}
});
2016-07-07 16:16:44 -08:00
const setupMenu = () => {
const tpl = plugins.decorateMenu(createMenu({
createWindow,
2016-07-08 06:40:48 -08:00
updatePlugins: () => {
plugins.updatePlugins({ force: true });
}
2016-07-07 16:16:44 -08:00
}));
Menu.setApplicationMenu(Menu.buildFromTemplate(tpl));
};
const load = () => {
plugins.onApp(app);
setupMenu();
};
load();
plugins.subscribe(load);
2016-06-30 22:01:04 -08:00
});
function initSession (opts, fn) {
genUid(20, (err, uid) => {
if (err) throw err;
fn(uid, new Session(opts));
});
}