hyper/app/index.js

312 lines
7.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-07-25 10:01:01 -08:00
const uuid = require('uuid');
2016-06-30 22:01:04 -08:00
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');
const notify = require('./notify');
2016-07-27 18:02:19 -08:00
app.commandLine.appendSwitch('js-flags', '--harmony');
// set up config
const config = require('./config');
config.init();
const plugins = require('./plugins');
const Session = require('./session');
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 retrive 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');
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);
2016-06-30 22:01:04 -08:00
app.on('ready', () => {
function createWindow (fn) {
let cfg = plugins.getDecoratedConfig();
const [width, height] = cfg.windowSize || [540, 380];
const browserDefaults = {
width,
height,
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,
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
};
const browserOptions = plugins.getDecoratedBrowserOptions(browserDefaults);
const win = new BrowserWindow(browserOptions);
windowSet.add(win);
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(() => {
const cfg_ = plugins.getDecoratedConfig();
win.webContents.send('config change');
if (cfg_.shell !== cfg.shell) {
notify(
'Shell configuration changed!',
'Open a new tab or window to start using the new shell'
);
}
cfg = cfg_;
});
2016-06-30 22:01:04 -08:00
rpc.on('init', () => {
win.show();
// If no callback is passed to createWindow,
// a new session will be created by default.
if (!fn) fn = (win) => win.rpc.emit('session add req');
// app.windowCallback is the createWindow callback
// that can be setted before the 'ready' app event
// and createWindow deifinition. It's exeuted in place of
// the callback passed as parameter, and deleted right after.
(app.windowCallback || fn)(win);
delete (app.windowCallback);
// 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);
} else {
console.log('ignoring auto updates during dev');
}
2016-06-30 22:01:04 -08:00
});
rpc.on('new', ({ rows = 40, cols = 100, cwd = process.env.HOME }) => {
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,
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-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-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 }) => {
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
});
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);
});
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();
win.webContents.send('plugins change');
2016-07-13 12:44:24 -08:00
}
2016-07-07 16:16:44 -08:00
});
// Keep track of focus time of every window, to figure out
// which one of the existing window is the last focused.
// Works nicely even if a window is closed and removed.
win.on('focus', () => {
win.focusTime = process.uptime();
});
2016-06-30 22:01:04 -08:00
// the window can be closed by the browser process itself
win.on('close', () => {
windowSet.delete(win);
2016-06-30 22:01:04 -08:00
rpc.destroy();
deleteSessions();
cfgUnsubscribe();
2016-07-07 16:16:44 -08:00
pluginsUnsubscribe();
});
win.on('closed', () => {
if (process.platform !== 'darwin') {
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();
}
});
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-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
}
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 && app.hasOwnProperty('createWindow')) {
app.createWindow(callback);
} else {
// if createWindow not exists yet ('ready' event was not fired),
// sets his callback to an app.windowCallback property.
app.windowCallback = callback;
}
});