2023-07-25 09:30:19 -08:00
|
|
|
// eslint-disable-next-line import/order
|
2022-11-16 04:48:20 -09:00
|
|
|
import {cfgPath} from './config/paths';
|
|
|
|
|
|
2017-01-21 09:54:32 -09:00
|
|
|
// Print diagnostic information for a few arguments instead of running Hyper.
|
|
|
|
|
if (['--help', '-v', '--version'].includes(process.argv[1])) {
|
2019-12-18 07:28:28 -09:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
2017-01-21 09:54:32 -09:00
|
|
|
const {version} = require('./package');
|
|
|
|
|
console.log(`Hyper version ${version}`);
|
|
|
|
|
console.log('Hyper does not accept any command line arguments. Please modify the config file instead.');
|
2022-11-16 04:48:20 -09:00
|
|
|
console.log(`Hyper configuration file located at: ${cfgPath}`);
|
2017-01-21 09:54:32 -09:00
|
|
|
process.exit();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-07 03:32:49 -08:00
|
|
|
// Enable remote module
|
2023-07-25 09:30:19 -08:00
|
|
|
// eslint-disable-next-line import/order
|
2021-11-22 18:50:10 -09:00
|
|
|
import {initialize as remoteInitialize} from '@electron/remote/main';
|
|
|
|
|
remoteInitialize();
|
2021-09-07 03:32:49 -08:00
|
|
|
|
2023-07-25 09:30:19 -08:00
|
|
|
// set up config
|
|
|
|
|
// eslint-disable-next-line import/order
|
|
|
|
|
import * as config from './config';
|
|
|
|
|
config.setup();
|
|
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
// Native
|
2019-11-28 05:17:01 -09:00
|
|
|
import {resolve} from 'path';
|
2016-09-21 06:27:11 -08:00
|
|
|
|
|
|
|
|
// Packages
|
2022-02-06 22:11:56 -09:00
|
|
|
import {app, BrowserWindow, Menu, screen} from 'electron';
|
2016-07-07 12:49:34 -08:00
|
|
|
|
2023-07-25 09:30:19 -08:00
|
|
|
import isDev from 'electron-is-dev';
|
|
|
|
|
import {gitDescribe} from 'git-describe';
|
|
|
|
|
import parseUrl from 'parse-url';
|
2016-09-21 06:27:11 -08:00
|
|
|
|
2019-11-28 05:17:01 -09:00
|
|
|
import * as AppMenu from './menus/menu';
|
2023-07-25 09:30:19 -08:00
|
|
|
import * as plugins from './plugins';
|
2019-12-18 07:28:28 -09:00
|
|
|
import {newWindow} from './ui/window';
|
2023-07-25 09:30:19 -08:00
|
|
|
import {installCLI} from './utils/cli-install';
|
2019-11-28 05:17:01 -09:00
|
|
|
import * as windowUtils from './utils/window-utils';
|
2016-06-30 22:01:04 -08:00
|
|
|
|
2019-12-18 07:28:28 -09:00
|
|
|
const windowSet = new Set<BrowserWindow>([]);
|
2016-07-18 16:11:30 -08:00
|
|
|
|
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-10-10 02:26:47 -08:00
|
|
|
// function to retrieve the last focused window in windowSet;
|
2016-08-01 14:52:21 -08:00
|
|
|
// added to app object in order to expose it to plugins.
|
|
|
|
|
app.getLastFocusedWindow = () => {
|
2016-09-21 06:27:11 -08:00
|
|
|
if (!windowSet.size) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2016-08-01 14:52:21 -08:00
|
|
|
return Array.from(windowSet).reduce((lastWindow, win) => {
|
|
|
|
|
return win.focusTime > lastWindow.focusTime ? win : lastWindow;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2018-04-23 12:12:53 -08:00
|
|
|
console.log('Disabling Chromium GPU blacklist');
|
|
|
|
|
app.commandLine.appendSwitch('ignore-gpu-blacklist');
|
|
|
|
|
|
2016-07-01 12:26:51 -08:00
|
|
|
if (isDev) {
|
|
|
|
|
console.log('running in dev mode');
|
2016-09-20 14:51:15 -08:00
|
|
|
|
2018-10-13 06:35:51 -08:00
|
|
|
// Override default appVersion which is set from package.json
|
2023-06-26 01:23:37 -08:00
|
|
|
gitDescribe({customArguments: ['--tags']}, (error: any, gitInfo: {raw: string}) => {
|
2016-09-21 06:27:11 -08:00
|
|
|
if (!error) {
|
|
|
|
|
app.setVersion(gitInfo.raw);
|
|
|
|
|
}
|
2016-09-20 14:51:15 -08:00
|
|
|
});
|
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
|
|
|
}
|
|
|
|
|
|
2019-11-28 05:17:01 -09:00
|
|
|
const url = `file://${resolve(isDev ? __dirname : app.getAppPath(), 'index.html')}`;
|
2016-07-01 12:26:51 -08:00
|
|
|
console.log('electron will open', url);
|
|
|
|
|
|
2021-04-20 00:38:36 -08:00
|
|
|
async function installDevExtensions(isDev_: boolean) {
|
2019-12-18 07:28:28 -09:00
|
|
|
if (!isDev_) {
|
2021-04-20 00:38:36 -08:00
|
|
|
return [];
|
2019-12-18 07:28:28 -09:00
|
|
|
}
|
2023-05-29 22:21:20 -08:00
|
|
|
const {default: installer, REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS} = await import('electron-devtools-installer');
|
2019-12-18 07:28:28 -09:00
|
|
|
|
2023-05-29 22:21:20 -08:00
|
|
|
const extensions = [REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS];
|
2019-12-18 07:28:28 -09:00
|
|
|
const forceDownload = Boolean(process.env.UPGRADE_EXTENSIONS);
|
|
|
|
|
|
2021-04-20 00:38:36 -08:00
|
|
|
return Promise.all(
|
2023-05-29 22:21:20 -08:00
|
|
|
extensions.map((extension) => installer(extension, {forceDownload, loadExtensionOptions: {allowFileAccess: true}}))
|
2021-04-20 00:38:36 -08:00
|
|
|
);
|
2019-12-18 07:28:28 -09:00
|
|
|
}
|
|
|
|
|
|
2021-03-28 09:04:10 -08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
2018-12-23 08:37:32 -09:00
|
|
|
app.on('ready', () =>
|
2017-09-10 05:35:10 -08:00
|
|
|
installDevExtensions(isDev)
|
|
|
|
|
.then(() => {
|
2021-03-28 09:48:36 -08:00
|
|
|
function createWindow(
|
|
|
|
|
fn?: (win: BrowserWindow) => void,
|
2023-06-30 11:07:04 -08:00
|
|
|
options: {size?: [number, number]; position?: [number, number]} = {},
|
|
|
|
|
profileName: string = config.getDefaultProfile()
|
2021-03-28 09:48:36 -08:00
|
|
|
) {
|
2023-06-30 11:07:04 -08:00
|
|
|
const cfg = plugins.getDecoratedConfig(profileName);
|
2017-09-10 05:35:10 -08:00
|
|
|
|
|
|
|
|
const winSet = config.getWin();
|
|
|
|
|
let [startX, startY] = winSet.position;
|
|
|
|
|
|
|
|
|
|
const [width, height] = options.size ? options.size : cfg.windowSize || winSet.size;
|
|
|
|
|
|
|
|
|
|
const winPos = options.position;
|
|
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
const focusedWindow = BrowserWindow.getFocusedWindow() || app.getLastFocusedWindow();
|
|
|
|
|
// In case of options defaults position and size, we should ignore the focusedWindow.
|
|
|
|
|
if (winPos !== undefined) {
|
|
|
|
|
[startX, startY] = winPos;
|
|
|
|
|
} else if (focusedWindow) {
|
|
|
|
|
const points = focusedWindow.getPosition();
|
|
|
|
|
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-08-01 16:00:49 -08:00
|
|
|
|
2018-03-02 12:11:33 -09:00
|
|
|
if (!windowUtils.positionIsValid([startX, startY])) {
|
|
|
|
|
[startX, startY] = config.windowDefaults.windowPosition;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-30 11:07:04 -08:00
|
|
|
const hwin = newWindow({width, height, x: startX, y: startY}, cfg, fn, profileName);
|
2017-09-10 05:35:10 -08:00
|
|
|
windowSet.add(hwin);
|
2025-04-24 20:37:56 -08:00
|
|
|
void hwin.loadURL(url);
|
2025-04-22 18:30:27 -08:00
|
|
|
|
|
|
|
|
hwin.once('ready-to-show', () => {
|
|
|
|
|
hwin.show();
|
|
|
|
|
});
|
2016-07-18 08:44:33 -08:00
|
|
|
|
2017-09-10 05:35:10 -08:00
|
|
|
// the window can be closed by the browser process itself
|
|
|
|
|
hwin.on('close', () => {
|
|
|
|
|
hwin.clean();
|
|
|
|
|
windowSet.delete(hwin);
|
|
|
|
|
});
|
2016-08-01 14:52:21 -08:00
|
|
|
|
2017-09-10 05:35:10 -08:00
|
|
|
return hwin;
|
2016-07-26 19:11:54 -08:00
|
|
|
}
|
2017-08-14 17:29:50 -08:00
|
|
|
|
2017-09-10 05:35:10 -08:00
|
|
|
// when opening create a new window
|
2016-07-01 16:02:08 -08:00
|
|
|
createWindow();
|
|
|
|
|
|
2017-09-10 05:35:10 -08:00
|
|
|
// expose to plugins
|
|
|
|
|
app.createWindow = createWindow;
|
|
|
|
|
|
|
|
|
|
// 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-09-21 06:27:11 -08:00
|
|
|
createWindow();
|
|
|
|
|
}
|
2017-09-10 05:35:10 -08:00
|
|
|
});
|
2016-07-07 16:16:44 -08:00
|
|
|
|
2021-01-07 02:44:50 -09:00
|
|
|
app.on('window-all-closed', () => {
|
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
|
app.quit();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2017-09-10 05:35:10 -08:00
|
|
|
const makeMenu = () => {
|
2017-11-02 18:51:18 -08:00
|
|
|
const menu = plugins.decorateMenu(AppMenu.createMenu(createWindow, plugins.getLoadedPluginVersions));
|
2017-09-10 05:35:10 -08:00
|
|
|
|
|
|
|
|
// If we're on Mac make a Dock Menu
|
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
|
const dockMenu = Menu.buildFromTemplate([
|
|
|
|
|
{
|
|
|
|
|
label: 'New Window',
|
|
|
|
|
click() {
|
|
|
|
|
createWindow();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]);
|
2026-01-03 23:43:36 -09:00
|
|
|
app.dock?.setMenu(dockMenu);
|
2017-09-10 05:35:10 -08:00
|
|
|
}
|
2016-07-07 16:16:44 -08:00
|
|
|
|
2017-11-02 18:51:18 -08:00
|
|
|
Menu.setApplicationMenu(AppMenu.buildMenu(menu));
|
2017-09-10 05:35:10 -08:00
|
|
|
};
|
|
|
|
|
|
2017-11-14 14:55:21 -09:00
|
|
|
plugins.onApp(app);
|
|
|
|
|
makeMenu();
|
|
|
|
|
plugins.subscribe(plugins.onApp.bind(undefined, app));
|
|
|
|
|
config.subscribe(makeMenu);
|
2018-04-22 12:13:23 -08:00
|
|
|
if (!isDev) {
|
|
|
|
|
// check if should be set/removed as default ssh protocol client
|
|
|
|
|
if (config.getConfig().defaultSSHApp && !app.isDefaultProtocolClient('ssh')) {
|
|
|
|
|
console.log('Setting Hyper as default client for ssh:// protocol');
|
|
|
|
|
app.setAsDefaultProtocolClient('ssh');
|
|
|
|
|
} else if (!config.getConfig().defaultSSHApp && app.isDefaultProtocolClient('ssh')) {
|
2018-08-22 09:42:52 -08:00
|
|
|
console.log('Removing Hyper from default client for ssh:// protocol');
|
2018-04-22 12:13:23 -08:00
|
|
|
app.removeAsDefaultProtocolClient('ssh');
|
|
|
|
|
}
|
2021-03-28 08:42:20 -08:00
|
|
|
void installCLI(false);
|
2018-04-22 12:13:23 -08:00
|
|
|
}
|
2017-09-10 05:35:10 -08:00
|
|
|
})
|
2020-03-25 02:15:08 -08:00
|
|
|
.catch((err) => {
|
2017-09-10 05:35:10 -08:00
|
|
|
console.error('Error while loading devtools extensions', err);
|
2018-12-23 08:37:32 -09:00
|
|
|
})
|
|
|
|
|
);
|
2016-06-30 22:01:04 -08:00
|
|
|
|
2021-05-12 13:04:16 -08:00
|
|
|
/**
|
|
|
|
|
* Get last focused BrowserWindow or create new if none and callback
|
|
|
|
|
* @param callback Function to call with the BrowserWindow
|
|
|
|
|
*/
|
|
|
|
|
function GetWindow(callback: (win: BrowserWindow) => void) {
|
2016-08-01 14:52:21 -08:00
|
|
|
const lastWindow = app.getLastFocusedWindow();
|
|
|
|
|
if (lastWindow) {
|
|
|
|
|
callback(lastWindow);
|
2016-09-21 06:27:11 -08:00
|
|
|
} else if (!lastWindow && {}.hasOwnProperty.call(app, 'createWindow')) {
|
2016-08-01 14:52:21 -08:00
|
|
|
app.createWindow(callback);
|
|
|
|
|
} else {
|
2016-10-10 02:26:47 -08:00
|
|
|
// If createWindow doesn't exist yet ('ready' event was not fired),
|
2016-08-01 14:52:21 -08:00
|
|
|
// sets his callback to an app.windowCallback property.
|
|
|
|
|
app.windowCallback = callback;
|
|
|
|
|
}
|
2021-05-12 13:04:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.on('open-file', (_event, path) => {
|
|
|
|
|
GetWindow((win: BrowserWindow) => {
|
|
|
|
|
win.rpc.emit('open file', {path});
|
|
|
|
|
});
|
2016-08-01 14:52:21 -08:00
|
|
|
});
|
2016-09-21 14:11:42 -08:00
|
|
|
|
2021-05-12 13:04:16 -08:00
|
|
|
app.on('open-url', (_event, sshUrl) => {
|
|
|
|
|
GetWindow((win: BrowserWindow) => {
|
2023-05-05 20:52:45 -08:00
|
|
|
win.rpc.emit('open ssh', parseUrl(sshUrl));
|
2021-05-12 13:04:16 -08:00
|
|
|
});
|
2018-02-12 11:20:45 -09:00
|
|
|
});
|