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-07-25 10:01:01 -08:00
|
|
|
const uuid = require('uuid');
|
2016-06-30 22:01:04 -08:00
|
|
|
const { resolve } = require('path');
|
2016-07-01 12:26:51 -08:00
|
|
|
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');
|
2016-07-26 15:37:42 -08:00
|
|
|
const notify = require('./notify');
|
2016-07-07 12:49:34 -08:00
|
|
|
|
|
|
|
|
// set up config
|
2016-07-07 07:17:02 -08:00
|
|
|
const config = require('./config');
|
2016-07-07 12:49:34 -08:00
|
|
|
config.init();
|
|
|
|
|
const plugins = require('./plugins');
|
2016-07-24 10:03:24 -08:00
|
|
|
const Session = require('./session');
|
2016-06-30 22:01:04 -08:00
|
|
|
|
2016-07-18 16:11:30 -08:00
|
|
|
const windowSet = new Set([]);
|
|
|
|
|
|
2016-07-16 10:58:21 -08:00
|
|
|
// expose to plugins
|
|
|
|
|
app.config = config;
|
|
|
|
|
app.plugins = plugins;
|
2016-07-18 16:11:30 -08:00
|
|
|
app.getWindows = () => new Set([...windowSet]); // return a clone
|
2016-07-16 10:58:21 -08:00
|
|
|
|
2016-07-01 12:26:51 -08:00
|
|
|
if (isDev) {
|
|
|
|
|
console.log('running in dev mode');
|
2016-06-30 22:01:04 -08:00
|
|
|
} else {
|
2016-07-01 12:26:51 -08:00
|
|
|
console.log('running in prod mode');
|
2016-06-30 22:01:04 -08:00
|
|
|
}
|
|
|
|
|
|
2016-07-01 12:26:51 -08:00
|
|
|
const url = 'file://' + resolve(
|
2016-07-01 14:44:24 -08:00
|
|
|
isDev ? __dirname : app.getAppPath(),
|
2016-07-01 12:26:51 -08:00
|
|
|
'index.html'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
console.log('electron will open', url);
|
|
|
|
|
|
2016-06-30 22:01:04 -08:00
|
|
|
app.on('window-all-closed', () => {
|
2016-07-21 19:26:10 -08:00
|
|
|
if (process.platform !== 'darwin') {
|
2016-07-21 11:34:28 -08:00
|
|
|
app.quit();
|
2016-07-21 19:26:10 -08:00
|
|
|
}
|
2016-06-30 22:01:04 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.on('ready', () => {
|
|
|
|
|
function createWindow (fn) {
|
2016-07-26 15:37:42 -08:00
|
|
|
let cfg = plugins.getDecoratedConfig();
|
|
|
|
|
|
2016-07-22 10:47:23 -08:00
|
|
|
const [width, height] = cfg.windowSize || [540, 380];
|
2016-07-18 08:44:33 -08:00
|
|
|
|
2016-07-21 16:32:39 -08:00
|
|
|
const browserDefaults = {
|
2016-07-22 10:47:23 -08:00
|
|
|
width,
|
|
|
|
|
height,
|
2016-07-14 06:55:24 -08:00
|
|
|
minHeight: 190,
|
|
|
|
|
minWidth: 370,
|
2016-07-08 05:03:40 -08:00
|
|
|
titleBarStyle: 'hidden-inset',
|
2016-06-30 22:01:04 -08:00
|
|
|
title: 'HyperTerm',
|
2016-07-16 10:58:41 -08:00
|
|
|
backgroundColor: toHex(cfg.backgroundColor || '#000'),
|
2016-06-30 22:01:04 -08:00
|
|
|
transparent: true,
|
2016-07-16 17:30:12 -08:00
|
|
|
icon: resolve(__dirname, 'static/icon.png'),
|
2016-06-30 22:01:04 -08:00
|
|
|
// 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-07-21 16:32:39 -08:00
|
|
|
};
|
|
|
|
|
const browserOptions = plugins.getDecoratedBrowserOptions(browserDefaults);
|
|
|
|
|
|
|
|
|
|
const win = new BrowserWindow(browserOptions);
|
2016-07-18 08:44:33 -08:00
|
|
|
|
2016-07-18 16:11:30 -08:00
|
|
|
windowSet.add(win);
|
2016-07-01 12:26:51 -08:00
|
|
|
win.loadURL(url);
|
2016-06-30 22:01:04 -08:00
|
|
|
|
|
|
|
|
const rpc = createRPC(win);
|
|
|
|
|
const sessions = new Map();
|
2016-07-07 15:22:45 -08:00
|
|
|
|
|
|
|
|
// config changes
|
2016-07-07 07:17:02 -08:00
|
|
|
const cfgUnsubscribe = config.subscribe(() => {
|
2016-07-26 15:37:42 -08:00
|
|
|
const cfg_ = plugins.getDecoratedConfig();
|
|
|
|
|
|
2016-07-07 07:17:02 -08:00
|
|
|
win.webContents.send('config change');
|
2016-07-26 15:37:42 -08:00
|
|
|
|
|
|
|
|
if (cfg_.shell !== cfg.shell) {
|
|
|
|
|
notify(
|
|
|
|
|
'Shell configuration changed!',
|
|
|
|
|
'Open a new tab or window to start using the new shell'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cfg = cfg_;
|
2016-07-07 07:17:02 -08:00
|
|
|
});
|
|
|
|
|
|
2016-06-30 22:01:04 -08:00
|
|
|
rpc.on('init', () => {
|
|
|
|
|
win.show();
|
2016-07-18 08:44:33 -08:00
|
|
|
if (fn) fn(win);
|
2016-07-07 15:22:45 -08:00
|
|
|
|
|
|
|
|
// auto updates
|
2016-07-21 10:54:14 -08:00
|
|
|
if (!isDev && process.platform !== 'linux') {
|
2016-07-08 04:48:53 -08:00
|
|
|
AutoUpdater(win);
|
2016-07-07 15:22:45 -08:00
|
|
|
} else {
|
|
|
|
|
console.log('ignoring auto updates during dev');
|
|
|
|
|
}
|
2016-06-30 22:01:04 -08:00
|
|
|
});
|
|
|
|
|
|
2016-07-16 14:41:13 -08:00
|
|
|
rpc.on('new', ({ rows = 40, cols = 100, cwd = process.env.HOME }) => {
|
2016-07-17 13:05:08 -08:00
|
|
|
const shell = cfg.shell;
|
|
|
|
|
|
|
|
|
|
initSession({ rows, cols, cwd, shell }, (uid, session) => {
|
2016-06-30 22:01:04 -08:00
|
|
|
sessions.set(uid, session);
|
2016-07-14 15:40:15 -08:00
|
|
|
rpc.emit('session add', {
|
|
|
|
|
uid,
|
2016-07-16 14:41:13 -08:00
|
|
|
shell: session.shell,
|
|
|
|
|
pid: session.pty.pid
|
2016-07-14 15:40:15 -08:00
|
|
|
});
|
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);
|
2016-07-18 08:44:33 -08:00
|
|
|
|
2016-07-13 12:44:24 -08:00
|
|
|
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);
|
2016-07-18 08:44:33 -08:00
|
|
|
|
2016-07-13 12:44:24 -08:00
|
|
|
if (session) {
|
|
|
|
|
session.blur();
|
|
|
|
|
} else {
|
|
|
|
|
console.log('session not found by', uid);
|
|
|
|
|
}
|
2016-06-30 22:01:04 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
rpc.on('exit', ({ uid }) => {
|
2016-07-24 10:02:21 -08:00
|
|
|
const session = sessions.get(uid);
|
|
|
|
|
|
|
|
|
|
if (session) {
|
|
|
|
|
session.exit();
|
|
|
|
|
} else {
|
|
|
|
|
console.log('session not found by', uid);
|
|
|
|
|
}
|
2016-06-30 22:01:04 -08:00
|
|
|
});
|
|
|
|
|
|
2016-07-04 16:48:38 -08:00
|
|
|
rpc.on('unmaximize', () => {
|
2016-07-04 17:54:55 -08:00
|
|
|
win.unmaximize();
|
2016-07-04 16:48:38 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
rpc.on('maximize', () => {
|
2016-07-04 17:54:55 -08:00
|
|
|
win.maximize();
|
2016-07-04 16:48:38 -08:00
|
|
|
});
|
|
|
|
|
|
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-07-19 10:30:57 -08:00
|
|
|
rpc.win.on('move', () => {
|
|
|
|
|
rpc.emit('move');
|
|
|
|
|
});
|
|
|
|
|
|
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();
|
2016-07-16 10:58:56 -08:00
|
|
|
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-18 16:11:30 -08:00
|
|
|
windowSet.delete(win);
|
2016-06-30 22:01:04 -08:00
|
|
|
rpc.destroy();
|
|
|
|
|
deleteSessions();
|
2016-07-07 07:17:02 -08:00
|
|
|
cfgUnsubscribe();
|
2016-07-07 16:16:44 -08:00
|
|
|
pluginsUnsubscribe();
|
2016-07-26 19:11:54 -08:00
|
|
|
if (windowSet.size === 0) {
|
|
|
|
|
win.close();
|
|
|
|
|
}
|
2016-06-30 22:01:04 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// when opening create a new window
|
|
|
|
|
createWindow();
|
|
|
|
|
|
2016-07-18 16:11:30 -08:00
|
|
|
// 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', () => {
|
2016-07-18 16:11:30 -08:00
|
|
|
if (!windowSet.size) {
|
2016-07-01 16:02:08 -08:00
|
|
|
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
|
|
|
}));
|
2016-07-18 08:44:33 -08:00
|
|
|
|
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) {
|
2016-07-25 10:01:01 -08:00
|
|
|
fn(uuid.v4(), new Session(opts));
|
2016-06-30 22:01:04 -08:00
|
|
|
}
|