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-08-04 05:43:57 -08:00
|
|
|
const { parse: parseUrl } = require('url');
|
|
|
|
|
const fileUriToPath = require('file-uri-to-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-09-18 22:47:33 -08:00
|
|
|
const toElectronBackgroundColor = require('./utils/to-electron-background-color');
|
2016-07-26 15:37:42 -08:00
|
|
|
const notify = require('./notify');
|
2016-07-07 12:49:34 -08:00
|
|
|
|
2016-07-27 18:02:19 -08:00
|
|
|
app.commandLine.appendSwitch('js-flags', '--harmony');
|
|
|
|
|
|
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-08-01 14:52:21 -08:00
|
|
|
// 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;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
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('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-08-01 16:00:49 -08:00
|
|
|
const { screen } = require('electron');
|
|
|
|
|
|
|
|
|
|
let startX = 50;
|
|
|
|
|
let startY = 50;
|
|
|
|
|
|
|
|
|
|
// Open the new window roughly the height of the header away from the
|
|
|
|
|
// previous window. This also ensures in multi monitor setups that the
|
|
|
|
|
// new terminal is on the correct screen.
|
2016-08-06 02:58:57 -08:00
|
|
|
const focusedWindow = BrowserWindow.getFocusedWindow() || app.getLastFocusedWindow();
|
|
|
|
|
if (focusedWindow) {
|
|
|
|
|
const points = focusedWindow.getPosition();
|
2016-08-01 16:00:49 -08:00
|
|
|
const currentScreen = screen.getDisplayNearestPoint({ x: points[0], y: points[1] });
|
|
|
|
|
|
|
|
|
|
const biggestX = ((points[0] + 100 + width) - currentScreen.bounds.x);
|
|
|
|
|
const biggestY = ((points[1] + 100 + height) - currentScreen.bounds.y);
|
|
|
|
|
|
|
|
|
|
if (biggestX > currentScreen.size.width) {
|
|
|
|
|
startX = 50;
|
|
|
|
|
} else {
|
|
|
|
|
startX = points[0] + 34;
|
|
|
|
|
}
|
|
|
|
|
if (biggestY > currentScreen.size.height) {
|
|
|
|
|
startY = 50;
|
|
|
|
|
} else {
|
|
|
|
|
startY = points[1] + 34;
|
|
|
|
|
}
|
|
|
|
|
}
|
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-09-18 22:47:33 -08:00
|
|
|
backgroundColor: toElectronBackgroundColor(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-08-01 16:00:49 -08:00
|
|
|
show: process.env.HYPERTERM_DEBUG || isDev,
|
|
|
|
|
x: startX,
|
|
|
|
|
y: startY
|
2016-07-21 16:32:39 -08:00
|
|
|
};
|
|
|
|
|
const browserOptions = plugins.getDecoratedBrowserOptions(browserDefaults);
|
|
|
|
|
|
|
|
|
|
const win = new BrowserWindow(browserOptions);
|
2016-07-18 16:11:30 -08:00
|
|
|
windowSet.add(win);
|
2016-08-01 14:52:21 -08:00
|
|
|
|
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-09-18 22:47:33 -08:00
|
|
|
// notify renderer
|
2016-07-07 07:17:02 -08:00
|
|
|
win.webContents.send('config change');
|
2016-07-26 15:37:42 -08:00
|
|
|
|
2016-09-18 22:47:33 -08:00
|
|
|
// notify user that shell changes require new sessions
|
2016-08-13 11:30:17 -08:00
|
|
|
if (cfg_.shell !== cfg.shell || cfg_.shellArgs !== cfg.shellArgs) {
|
2016-07-26 15:37:42 -08:00
|
|
|
notify(
|
|
|
|
|
'Shell configuration changed!',
|
|
|
|
|
'Open a new tab or window to start using the new shell'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-18 22:47:33 -08:00
|
|
|
// update background color if necessary
|
|
|
|
|
win.setBackgroundColor(toElectronBackgroundColor(cfg_.backgroundColor || '#000'));
|
|
|
|
|
|
2016-07-26 15:37:42 -08:00
|
|
|
cfg = cfg_;
|
2016-07-07 07:17:02 -08:00
|
|
|
});
|
|
|
|
|
|
2016-06-30 22:01:04 -08:00
|
|
|
rpc.on('init', () => {
|
|
|
|
|
win.show();
|
2016-08-01 14:52:21 -08:00
|
|
|
|
|
|
|
|
// 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);
|
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;
|
2016-08-31 18:33:01 -08:00
|
|
|
const shellArgs = cfg.shellArgs && Array.from(cfg.shellArgs);
|
2016-07-17 13:05:08 -08:00
|
|
|
|
2016-08-13 11:30:17 -08:00
|
|
|
initSession({ rows, cols, cwd, shell, shellArgs }, (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-08-03 16:52:31 -08:00
|
|
|
win.setTitle(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-08-03 16:52:31 -08:00
|
|
|
if (typeof session !== 'undefined' && typeof session.lastTitle !== 'undefined') {
|
|
|
|
|
win.setTitle(session.lastTitle);
|
|
|
|
|
}
|
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-08-04 05:43:57 -08:00
|
|
|
// If file is dropped onto the terminal window, navigate event is prevented
|
|
|
|
|
// and his path is added to active session.
|
|
|
|
|
win.webContents.on('will-navigate', (event, url) => {
|
|
|
|
|
var protocol = typeof url === 'string' && parseUrl(url).protocol;
|
|
|
|
|
if (protocol === 'file:') {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
let path = fileUriToPath(url).replace(/ /g, '\\ ');
|
|
|
|
|
rpc.emit('session data send', { data: path });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
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-08-01 14:52:21 -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.
|
2016-08-06 02:58:57 -08:00
|
|
|
const updateFocusTime = () => {
|
2016-08-01 14:52:21 -08:00
|
|
|
win.focusTime = process.uptime();
|
2016-08-06 02:58:57 -08:00
|
|
|
};
|
|
|
|
|
win.on('focus', () => {
|
|
|
|
|
updateFocusTime();
|
2016-08-01 14:52:21 -08:00
|
|
|
});
|
2016-08-06 02:58:57 -08:00
|
|
|
// Ensure focusTime is set on window open. The focus event doesn't
|
|
|
|
|
// fire from the dock (see bug #583)
|
|
|
|
|
updateFocusTime();
|
2016-08-01 14:52:21 -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-29 20:12:21 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
win.on('closed', () => {
|
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
|
app.quit();
|
2016-07-26 19:11:54 -08:00
|
|
|
}
|
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-08-03 16:52:31 -08:00
|
|
|
// If we're on Mac make a Dock Menu
|
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
|
const { app, Menu } = require('electron');
|
|
|
|
|
const dockMenu = Menu.buildFromTemplate([
|
|
|
|
|
{label: 'New Window', click () { createWindow(); }}
|
|
|
|
|
]);
|
|
|
|
|
app.dock.setMenu(dockMenu);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
2016-08-01 14:52:21 -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;
|
|
|
|
|
}
|
|
|
|
|
});
|