2017-08-20 03:03:13 -08:00
|
|
|
// Packages
|
2019-12-18 07:28:28 -09:00
|
|
|
import {app, dialog, Menu, BrowserWindow} from 'electron';
|
2017-06-19 13:02:53 -08:00
|
|
|
|
2017-08-20 03:03:13 -08:00
|
|
|
// Utilities
|
2019-11-28 05:17:01 -09:00
|
|
|
import {getConfig} from '../config';
|
|
|
|
|
import {icon} from '../config/paths';
|
|
|
|
|
import viewMenu from './menus/view';
|
|
|
|
|
import shellMenu from './menus/shell';
|
|
|
|
|
import editMenu from './menus/edit';
|
|
|
|
|
import pluginsMenu from './menus/plugins';
|
|
|
|
|
import windowMenu from './menus/window';
|
|
|
|
|
import helpMenu from './menus/help';
|
|
|
|
|
import darwinMenu from './menus/darwin';
|
|
|
|
|
import {getDecoratedKeymaps} from '../plugins';
|
|
|
|
|
import {execCommand} from '../commands';
|
|
|
|
|
import {getRendererTypes} from '../utils/renderer-utils';
|
2017-05-25 22:59:02 -08:00
|
|
|
|
2020-03-06 08:08:27 -09:00
|
|
|
const appName = app.name;
|
2017-06-19 13:02:53 -08:00
|
|
|
const appVersion = app.getVersion();
|
|
|
|
|
|
2019-12-18 07:28:28 -09:00
|
|
|
let menu_: Menu;
|
2017-11-02 18:51:18 -08:00
|
|
|
|
2019-12-18 07:28:28 -09:00
|
|
|
export const createMenu = (
|
|
|
|
|
createWindow: (fn?: (win: BrowserWindow) => void, options?: Record<string, any>) => BrowserWindow,
|
|
|
|
|
getLoadedPluginVersions: () => {name: string; version: string}[]
|
|
|
|
|
) => {
|
2017-08-20 03:03:13 -08:00
|
|
|
const config = getConfig();
|
2017-11-02 18:51:18 -08:00
|
|
|
// We take only first shortcut in array for each command
|
|
|
|
|
const allCommandKeys = getDecoratedKeymaps();
|
2019-12-18 07:28:28 -09:00
|
|
|
const commandKeys = Object.keys(allCommandKeys).reduce((result: Record<string, string>, command) => {
|
2017-11-02 18:51:18 -08:00
|
|
|
result[command] = allCommandKeys[command][0];
|
|
|
|
|
return result;
|
|
|
|
|
}, {});
|
2017-08-20 03:03:13 -08:00
|
|
|
|
2017-08-30 09:14:28 -08:00
|
|
|
let updateChannel = 'stable';
|
|
|
|
|
|
|
|
|
|
if (config && config.updateChannel && config.updateChannel === 'canary') {
|
|
|
|
|
updateChannel = 'canary';
|
|
|
|
|
}
|
2017-08-20 03:03:13 -08:00
|
|
|
|
2017-06-19 13:02:53 -08:00
|
|
|
const showAbout = () => {
|
|
|
|
|
const loadedPlugins = getLoadedPluginVersions();
|
2017-09-10 05:35:10 -08:00
|
|
|
const pluginList =
|
2020-03-25 02:15:08 -08:00
|
|
|
loadedPlugins.length === 0 ? 'none' : loadedPlugins.map((plugin) => `\n ${plugin.name} (${plugin.version})`);
|
2017-08-20 03:03:13 -08:00
|
|
|
|
2019-12-18 07:28:28 -09:00
|
|
|
const rendererCounts = Object.values(getRendererTypes()).reduce((acc: Record<string, number>, type) => {
|
2019-01-24 14:46:39 -09:00
|
|
|
acc[type] = acc[type] ? acc[type] + 1 : 1;
|
|
|
|
|
return acc;
|
|
|
|
|
}, {});
|
|
|
|
|
const renderers = Object.entries(rendererCounts)
|
|
|
|
|
.map(([type, count]) => type + (count > 1 ? ` (${count})` : ''))
|
|
|
|
|
.join(', ');
|
|
|
|
|
|
2017-06-19 13:02:53 -08:00
|
|
|
dialog.showMessageBox({
|
|
|
|
|
title: `About ${appName}`,
|
2017-08-20 03:03:13 -08:00
|
|
|
message: `${appName} ${appVersion} (${updateChannel})`,
|
2020-01-09 10:28:25 -09:00
|
|
|
detail: `Renderers: ${renderers}\nPlugins: ${pluginList}\n\nCreated by Guillermo Rauch\nCopyright © 2020 ZEIT, Inc.`,
|
2017-06-19 13:02:53 -08:00
|
|
|
buttons: [],
|
2019-12-18 07:28:28 -09:00
|
|
|
icon: icon as any
|
2017-06-19 13:02:53 -08:00
|
|
|
});
|
|
|
|
|
};
|
2017-06-02 16:03:47 -08:00
|
|
|
const menu = [
|
2017-11-03 13:06:48 -08:00
|
|
|
...(process.platform === 'darwin' ? [darwinMenu(commandKeys, execCommand, showAbout)] : []),
|
2017-11-02 18:51:18 -08:00
|
|
|
shellMenu(commandKeys, execCommand),
|
|
|
|
|
editMenu(commandKeys, execCommand),
|
|
|
|
|
viewMenu(commandKeys, execCommand),
|
|
|
|
|
pluginsMenu(commandKeys, execCommand),
|
|
|
|
|
windowMenu(commandKeys, execCommand),
|
|
|
|
|
helpMenu(commandKeys, showAbout)
|
2017-06-02 16:03:47 -08:00
|
|
|
];
|
2017-05-25 22:59:02 -08:00
|
|
|
|
|
|
|
|
return menu;
|
|
|
|
|
};
|
2017-11-02 18:51:18 -08:00
|
|
|
|
2019-12-18 07:28:28 -09:00
|
|
|
export const buildMenu = (template: Electron.MenuItemConstructorOptions[]): Electron.Menu => {
|
2017-11-02 18:51:18 -08:00
|
|
|
menu_ = Menu.buildFromTemplate(template);
|
|
|
|
|
return menu_;
|
|
|
|
|
};
|