Skip to content

Commit

Permalink
Upgrade electron-util and simplify a lot of code
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 9, 2018
1 parent 6074f21 commit 74cc93f
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 121 deletions.
2 changes: 1 addition & 1 deletion app/marketmaker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Marketmaker {

async _killProcess() {
try {
if (process.platform === 'win32') {
if (is.windows) {
await execFile('taskkill', ['/f', '/im', 'marketmaker.exe']);
} else {
await execFile('killall', ['-9', 'marketmaker']);
Expand Down
144 changes: 42 additions & 102 deletions app/menu.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
const path = require('path');
const electron = require('electron');
const {runJS, is} = require('electron-util');
const {runJS, is, appMenu, openUrlMenuItem, aboutMenuItem} = require('electron-util');
const i18next = require('i18next');
const config = require('./config');
const {openGitHubIssue} = require('./util');
Expand All @@ -10,13 +10,12 @@ const {isDevelopment, isNightlyBuild} = require('./util-common');
const {translate} = require('./locale');

const {app, BrowserWindow, shell, clipboard, ipcMain: ipc, Menu} = electron;
const appName = app.getName();
const t = translate('menu');

const sendAction = (action, data) => {
const [win] = BrowserWindow.getAllWindows();

if (process.platform === 'darwin') {
if (is.macos) {
win.restore();
}

Expand All @@ -43,24 +42,18 @@ const createHelpMenu = () => {
{
type: 'separator',
},
{
openUrlMenuItem({
label: t('help.website'),
click() {
shell.openExternal(websiteUrl);
},
},
{
url: websiteUrl,
}),
openUrlMenuItem({
label: t('help.sourceCode'),
click() {
shell.openExternal(repoUrl);
},
},
{
url: repoUrl,
}),
openUrlMenuItem({
label: t('help.reportSecurityIssue'),
click() {
shell.openExternal('mailto:hyperdex@protonmail.com');
},
},
url: 'mailto:hyperdex@protonmail.com',
}),
{
label: t('help.reportIssue'),
click() {
Expand All @@ -69,20 +62,18 @@ const createHelpMenu = () => {
},
];

if (process.platform !== 'darwin') {
helpSubmenu.push({
type: 'separator',
}, {
role: 'about',
click() {
electron.dialog.showMessageBox({
title: t('help.about', {appName}),
message: `${appName} ${app.getVersion()}`,
detail: 'Copyright © Luke Childs',
icon: path.join(__dirname, 'static/icon.png'),
});
if (!is.macos) {
helpSubmenu.push(
{
type: 'separator',
},
});
aboutMenuItem({
icon: path.join(__dirname, 'static/icon.png'),
// FIXME: Doing it like this for now so I don't have to update all the translations
title: t('help.about', {appName: ''}).trim(),
copyright: 'Copyright © Luke Childs',
})
);
}

return helpSubmenu;
Expand Down Expand Up @@ -221,12 +212,12 @@ const createAppMenu = options => {
};

const portfolioSubmenu = [];
for (const [i, view] of appViews.entries()) {
for (const [index, view] of appViews.entries()) {
portfolioSubmenu.push({
label: t(`portfolio.${view.toLowerCase()}`),
type: 'radio',
checked: activeView === view,
accelerator: `CommandOrControl+${i + 1}`,
accelerator: `CommandOrControl+${index + 1}`,
click() {
setActiveView(view);
},
Expand Down Expand Up @@ -262,87 +253,36 @@ const createAppMenu = options => {
}
);

const macosTpl = [
{
label: appName,
submenu: [
{
role: 'about',
},
{
type: 'separator',
},
{
label: t('app.preferences'),
accelerator: 'Cmd+,',
click() {
setActiveView('Settings');
},
},
{
type: 'separator',
},
{
role: 'services',
submenu: [],
},
{
type: 'separator',
},
{
role: 'hide',
},
{
role: 'hideothers',
},
{
role: 'unhide',
},
{
type: 'separator',
},
{
role: 'quit',
const macosTemplate = [
appMenu([
{
label: t('app.preferences'),
accelerator: 'Command+,',
click() {
setActiveView('Settings');
},
],
},
},
]),
{
role: 'editMenu',
},
isLoggedIn ? {
isLoggedIn && {
label: t('portfolio.title'),
// TODO: Can't use `visible` because of Electron bug:
// https://github.com/electron/electron/issues/8703
// visible: isLoggedIn,
submenu: portfolioSubmenu,
} : null,
},
{
role: 'window',
submenu: [
{
role: 'minimize',
},
{
role: 'close',
},
{
type: 'separator',
},
{
role: 'front',
},
{
role: 'togglefullscreen',
},
],
role: 'windowMenu',
},
{
role: 'help',
submenu: createHelpMenu(),
},
];

const otherTpl = [
const otherTemplate = [
{
label: t('other.file'),
submenu: [
Expand All @@ -354,26 +294,26 @@ const createAppMenu = options => {
{
role: 'editMenu',
},
isLoggedIn ? {
isLoggedIn && {
label: t('portfolio.title'),
// TODO: Can't use `visible` because of Electron bug:
// https://github.com/electron/electron/issues/8703
// visible: isLoggedIn,
submenu: portfolioSubmenu,
} : null,
},
{
role: 'help',
submenu: createHelpMenu(),
},
];

const tpl = process.platform === 'darwin' ? macosTpl : otherTpl;
const template = is.macos ? macosTemplate : otherTemplate;

if (isDevelopment) {
tpl.push(createDebugMenu());
template.push(createDebugMenu());
}

Menu.setApplicationMenu(Menu.buildFromTemplate(tpl.filter(x => x !== null)));
Menu.setApplicationMenu(Menu.buildFromTemplate(template.filter(Boolean)));
};

ipc.on('app-container-state-updated', (event, state) => {
Expand Down
2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"electron-timber": "^0.5.1",
"electron-unhandled": "^1.1.0",
"electron-updater": "^2.23.3",
"electron-util": "^0.10.1",
"electron-util": "^0.10.2",
"get-port": "^4.0.0",
"i18next": "^11.9.1",
"i18next-sync-fs-backend": "^1.1.0",
Expand Down
11 changes: 3 additions & 8 deletions app/util.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/* eslint-disable import/prefer-default-export */
'use strict';
const os = require('os');
const {app, shell} = require('electron');
const {debugInfo, openNewGitHubIssue} = require('electron-util');
const {repoUrl} = require('./constants');

exports.openGitHubIssue = message => {
Expand All @@ -13,10 +11,7 @@ ${message}
---
${app.getName()} ${app.getVersion()}
Electron ${process.versions.electron}
${process.platform} ${os.release()}
Locale: ${app.getLocale()}`;
${debugInfo()}`;

shell.openExternal(`${repoUrl}/issues/new?body=${encodeURIComponent(body)}`);
openNewGitHubIssue({repoUrl, body});
};
14 changes: 10 additions & 4 deletions app/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,13 @@ electron-updater@^2.23.3:
semver "^5.5.0"
source-map-support "^0.5.6"

electron-util@^0.10.1:
version "0.10.1"
resolved "https://registry.yarnpkg.com/electron-util/-/electron-util-0.10.1.tgz#3318134cafdef7c15bb69ac5dc8e80d0f23f9049"
integrity sha512-fuuI357P0F7zMiGo6NsO2j6SQ9mI2HtOTiAN0ZxvSNpLoe1fjQEDzyMBMXsqtHfOzyfsokue6534MdzJihbksw==
electron-util@^0.10.2:
version "0.10.2"
resolved "https://registry.yarnpkg.com/electron-util/-/electron-util-0.10.2.tgz#a4f61ebe6305a526d348d034475023089d65c43a"
integrity sha512-m9CLQeG+PnxYJw77R+60ztRaoVs0pzqhQrOQ4kR+LVUBxHtOQXiOL3/TwAPf8LTTyxxn+OdfrjmaNkUl1mpIHg==
dependencies:
electron-is-dev "^1.0.0"
new-github-issue-url "^0.1.2"

electron-util@^0.8.2:
version "0.8.2"
Expand Down Expand Up @@ -695,6 +696,11 @@ ms@2.0.0:
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
integrity sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=

new-github-issue-url@^0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/new-github-issue-url/-/new-github-issue-url-0.1.2.tgz#43f852cbf4f5eaa128e0850ea527d9c103fd3b1c"
integrity sha512-FmF8JH2dGczrnY4cVKB7BjpaKFpySvZvPNZOhuMevY4+a1jSlI/me95rdulg9C5/74P/i02G+3WqpXN1Fhcoow==

node-dir@^0.1.17:
version "0.1.17"
resolved "https://registry.yarnpkg.com/node-dir/-/node-dir-0.1.17.tgz#5f5665d93351335caabef8f1c554516cf5f1e4e5"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"date-fns": "^1.29.0",
"delay": "^4.1.0",
"electron-unhandled": "^1.1.0",
"electron-util": "^0.10.1",
"electron-util": "^0.10.2",
"emittery": "^0.4.1",
"filter-console": "^0.1.0",
"hoist-non-react-statics": "^3.0.1",
Expand Down
14 changes: 10 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3515,12 +3515,13 @@ electron-unhandled@^1.1.0:
ensure-error "^1.0.0"
lodash.debounce "^4.0.8"

electron-util@^0.10.1:
version "0.10.1"
resolved "https://registry.yarnpkg.com/electron-util/-/electron-util-0.10.1.tgz#3318134cafdef7c15bb69ac5dc8e80d0f23f9049"
integrity sha512-fuuI357P0F7zMiGo6NsO2j6SQ9mI2HtOTiAN0ZxvSNpLoe1fjQEDzyMBMXsqtHfOzyfsokue6534MdzJihbksw==
electron-util@^0.10.2:
version "0.10.2"
resolved "https://registry.yarnpkg.com/electron-util/-/electron-util-0.10.2.tgz#a4f61ebe6305a526d348d034475023089d65c43a"
integrity sha512-m9CLQeG+PnxYJw77R+60ztRaoVs0pzqhQrOQ4kR+LVUBxHtOQXiOL3/TwAPf8LTTyxxn+OdfrjmaNkUl1mpIHg==
dependencies:
electron-is-dev "^1.0.0"
new-github-issue-url "^0.1.2"

electron@^3.0.5:
version "3.0.5"
Expand Down Expand Up @@ -7250,6 +7251,11 @@ neo-async@^2.5.0:
resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.5.1.tgz#acb909e327b1e87ec9ef15f41b8a269512ad41ee"
integrity sha512-3KL3fvuRkZ7s4IFOMfztb7zJp3QaVWnBeGoJlgB38XnCRPj/0tLzzLG5IB8NYOHbJ8g8UGrgZv44GLDk6CxTxA==

new-github-issue-url@^0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/new-github-issue-url/-/new-github-issue-url-0.1.2.tgz#43f852cbf4f5eaa128e0850ea527d9c103fd3b1c"
integrity sha512-FmF8JH2dGczrnY4cVKB7BjpaKFpySvZvPNZOhuMevY4+a1jSlI/me95rdulg9C5/74P/i02G+3WqpXN1Fhcoow==

nice-try@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.4.tgz#d93962f6c52f2c1558c0fbda6d512819f1efe1c4"
Expand Down

0 comments on commit 74cc93f

Please sign in to comment.