hyper/index.js

268 lines
6.1 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');
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', () => {
win.unmaximize();
});
rpc.on('maximize', () => {
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
Menu.setApplicationMenu(Menu.buildFromTemplate([
{
label: 'Application',
submenu: [
{
role: 'quit'
}
]
},
{
label: 'Shell',
submenu: [
{
label: 'New Window',
accelerator: 'CmdOrCtrl+N',
click (item, focusedWindow) {
createWindow();
}
},
{
label: 'New Tab',
accelerator: 'CmdOrCtrl+T',
click (item, focusedWindow) {
if (focusedWindow) {
focusedWindow.rpc.emit('new tab');
} else {
createWindow();
}
}
},
{
label: 'Close',
accelerator: 'CmdOrCtrl+W',
click (item, focusedWindow) {
if (focusedWindow) {
focusedWindow.rpc.emit('close tab');
}
}
}
]
},
{
label: 'Edit',
submenu: [
{ label: 'Copy', accelerator: 'CmdOrCtrl+C', selector: 'copy:' },
{ label: 'Paste', accelerator: 'CmdOrCtrl+V', selector: 'paste:' },
2016-07-03 12:35:45 -08:00
{ label: 'Select All', accelerator: 'CmdOrCtrl+A', selector: 'selectAll:' },
{ type: 'separator' },
{
label: 'Clear',
accelerator: 'CmdOrCtrl+K',
click (item, focusedWindow) {
if (focusedWindow) {
focusedWindow.rpc.emit('clear');
}
}
}
2016-06-30 22:01:04 -08:00
]
},
{
label: 'View',
submenu: [
{
label: 'Reload',
accelerator: 'CmdOrCtrl+R',
click (item, focusedWindow) {
if (focusedWindow) focusedWindow.reload();
}
},
{
label: 'Toggle Developer Tools',
accelerator: process.platform === 'darwin' ? 'Alt+Command+I' : 'Ctrl+Shift+I',
click (item, focusedWindow) {
if (focusedWindow) {
focusedWindow.webContents.toggleDevTools();
}
}
},
2016-07-03 12:35:45 -08:00
{
type: 'separator'
},
{
role: 'togglefullscreen'
2016-06-30 22:01:04 -08:00
}
]
},
{
label: 'Window',
submenu: [
{
label: 'Select Previous Tab',
accelerator: 'CmdOrCtrl+Left',
click (item, focusedWindow) {
if (focusedWindow) {
focusedWindow.rpc.emit('move left');
}
}
},
{
label: 'Select Next Tab',
accelerator: 'CmdOrCtrl+Right',
click (item, focusedWindow) {
if (focusedWindow) {
focusedWindow.rpc.emit('move right');
}
}
}
]
}
]));
});
function initSession (opts, fn) {
genUid(20, (err, uid) => {
if (err) throw err;
fn(uid, new Session(opts));
});
}