Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refined the code a little bit. #7407

Open
wants to merge 1 commit into
base: canary
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
79 changes: 38 additions & 41 deletions app/menus/menu.ts
@@ -1,14 +1,10 @@
// Packages
import {app, dialog, Menu} from 'electron';
import type {BrowserWindow} from 'electron';

// Utilities
import {execCommand} from '../commands';
import {getConfig} from '../config';
import {icon} from '../config/paths';
import {getDecoratedKeymaps} from '../plugins';
import {getRendererTypes} from '../utils/renderer-utils';

import { app, dialog, Menu } from 'electron';
import type { BrowserWindow } from 'electron';
import { execCommand } from '../commands';
import { getConfig } from '../config';
import { icon } from '../config/paths';
import { getDecoratedKeymaps } from '../plugins';
import { getRendererTypes } from '../utils/renderer-utils';
import darwinMenu from './menus/darwin';
import editMenu from './menus/edit';
import helpMenu from './menus/help';
Expand All @@ -19,48 +15,49 @@ import windowMenu from './menus/window';

const appName = app.name;
const appVersion = app.getVersion();

let menu_: Menu;

export const createMenu = (
createWindow: (fn?: (win: BrowserWindow) => void, options?: Record<string, any>) => BrowserWindow,
getLoadedPluginVersions: () => {name: string; version: string}[]
) => {
const showAbout = () => {
const loadedPlugins = getLoadedPluginVersions();
const pluginList = loadedPlugins.length === 0
? 'none'
: loadedPlugins.map((plugin) => `\n ${plugin.name} (${plugin.version})`).join('');

const rendererCounts = Object.values(getRendererTypes()).reduce((acc, type) => {
acc[type] = acc[type] ? acc[type] + 1 : 1;
return acc;
}, {});
const renderers = Object.entries(rendererCounts)
.map(([type, count]) => `${type}${count > 1 ? ` (${count})` : ''}`)
.join(', ');

void dialog.showMessageBox({
title: `About ${appName}`,
message: `${appName} ${appVersion} (${updateChannel})`,
detail: `
Renderers: ${renderers}
Plugins: ${pluginList}

Created by Guillermo Rauch
Copyright © 2022 Vercel, Inc.`,
buttons: [],
icon: icon as any
});
};

export const createMenu = (createWindow, getLoadedPluginVersions) => {
const config = getConfig();
// We take only first shortcut in array for each command
const allCommandKeys = getDecoratedKeymaps();
const commandKeys = Object.keys(allCommandKeys).reduce((result: Record<string, string>, command) => {
const commandKeys = Object.keys(allCommandKeys).reduce((result, command) => {
result[command] = allCommandKeys[command][0];
return result;
}, {});

let updateChannel = 'stable';

if (config?.updateChannel && config.updateChannel === 'canary') {
updateChannel = 'canary';
}

const showAbout = () => {
const loadedPlugins = getLoadedPluginVersions();
const pluginList =
loadedPlugins.length === 0 ? 'none' : loadedPlugins.map((plugin) => `\n ${plugin.name} (${plugin.version})`);

const rendererCounts = Object.values(getRendererTypes()).reduce((acc: Record<string, number>, type) => {
acc[type] = acc[type] ? acc[type] + 1 : 1;
return acc;
}, {});
const renderers = Object.entries(rendererCounts)
.map(([type, count]) => type + (count > 1 ? ` (${count})` : ''))
.join(', ');

void dialog.showMessageBox({
title: `About ${appName}`,
message: `${appName} ${appVersion} (${updateChannel})`,
detail: `Renderers: ${renderers}\nPlugins: ${pluginList}\n\nCreated by Guillermo Rauch\nCopyright © 2022 Vercel, Inc.`,
buttons: [],
icon: icon as any
});
};
const menu = [
...(process.platform === 'darwin' ? [darwinMenu(commandKeys, execCommand, showAbout)] : []),
shellMenu(
Expand All @@ -78,7 +75,7 @@ export const createMenu = (
return menu;
};

export const buildMenu = (template: Electron.MenuItemConstructorOptions[]): Electron.Menu => {
export const buildMenu = (template) => {
menu_ = Menu.buildFromTemplate(template);
return menu_;
};