hyper/index.js

161 lines
3.6 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-06-30 22:01:04 -08:00
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) {
let win = new BrowserWindow({
width: 540,
height: 380,
titleBarStyle: 'hidden',
title: 'HyperTerm',
backgroundColor: '#000',
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();
rpc.on('init', () => {
win.show();
});
rpc.on('new', ({ rows = 40, cols = 100 }) => {
initSession({ rows, cols }, (uid, session) => {
sessions.set(uid, session);
rpc.emit('new session', { uid });
session.on('data', (data) => {
rpc.emit('data', { uid, data });
});
session.on('title', (title) => {
rpc.emit('title', { uid, title });
});
session.on('exit', () => {
rpc.emit('exit', { uid });
2016-07-03 12:35:45 -08:00
sessions.delete(uid);
2016-06-30 22:01:04 -08:00
});
});
});
rpc.on('focus', ({ uid }) => {
sessions.get(uid).focus();
});
rpc.on('blur', ({ uid }) => {
sessions.get(uid).blur();
});
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();
}
});
// the window can be closed by the browser process itself
win.on('close', () => {
rpc.destroy();
deleteSessions();
2016-07-01 16:02:08 -08:00
winCount--;
2016-06-30 22:01:04 -08:00
});
win.rpc = rpc;
}
// 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-06-30 22:01:04 -08:00
// set menu
2016-07-04 18:46:24 -08:00
const tpl = createMenu({ createWindow });
Menu.setApplicationMenu(Menu.buildFromTemplate(tpl));
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));
});
}