From 233a2e640063c23b12f5dd4a43011e3926924198 Mon Sep 17 00:00:00 2001 From: Alexis Svinartchouk Date: Tue, 7 Jan 2020 13:19:59 +0100 Subject: [PATCH] Convert menu.js to typescript Change-type: patch --- lib/gui/etcher.js | 3 +- lib/gui/menu.js | 129 --------------------------------------------- lib/gui/menu.ts | 131 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+), 130 deletions(-) delete mode 100644 lib/gui/menu.js create mode 100644 lib/gui/menu.ts diff --git a/lib/gui/etcher.js b/lib/gui/etcher.js index 8f054ce9..2e0b7e8e 100644 --- a/lib/gui/etcher.js +++ b/lib/gui/etcher.js @@ -23,7 +23,8 @@ const { autoUpdater } = require('electron-updater') const Bluebird = require('bluebird') const semver = require('semver') const EXIT_CODES = require('../shared/exit-codes') -const buildWindowMenu = require('./menu') +// eslint-disable-next-line node/no-missing-require +const { buildWindowMenu } = require('./menu') const settings = require('./app/models/settings') const analytics = require('./app/modules/analytics') const { getConfig } = require('../shared/utils') diff --git a/lib/gui/menu.js b/lib/gui/menu.js deleted file mode 100644 index 44a4f9ad..00000000 --- a/lib/gui/menu.js +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Copyright 2017 balena.io - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict' - -const electron = require('electron') -const packageJson = require('../../package.json') - -/* eslint-disable no-magic-numbers */ - -/** - * @summary Builds a native application menu for a given window - * @param {electron.BrowserWindow} window - BrowserWindow instance - * @example - * buildWindowMenu(mainWindow) - */ -const buildWindowMenu = (window) => { - /** - * @summary Toggle the main window's devtools - * @example - * toggleDevTools() - */ - const toggleDevTools = () => { - if (!window) { - return - } - - // NOTE: We can't use `webContents.toggleDevTools()` here, - // as we need to force detached mode - if (window.webContents.isDevToolsOpened()) { - window.webContents.closeDevTools() - } else { - window.webContents.openDevTools({ - mode: 'detach' - }) - } - } - - const menuTemplate = [ - { - role: 'editMenu' - }, - { - label: 'View', - submenu: [ - { - label: 'Toggle Developer Tools', - accelerator: process.platform === 'darwin' - ? 'Command+Alt+I' : 'Control+Shift+I', - click: toggleDevTools - } - ] - }, - { - role: 'windowMenu' - }, - { - role: 'help', - submenu: [ - { - label: 'Etcher Pro', - click () { - electron.shell.openExternal('https://etcher.io/pro?utm_source=etcher_menu&ref=etcher_menu') - } - }, - { - label: 'Etcher Website', - click () { - electron.shell.openExternal('https://etcher.io?ref=etcher_menu') - } - }, - { - label: 'Report an issue', - click () { - electron.shell.openExternal('https://github.com/balena-io/etcher/issues') - } - } - ] - } - ] - - if (process.platform === 'darwin') { - menuTemplate.unshift({ - label: packageJson.displayName, - submenu: [ { - role: 'about', - label: 'About Etcher' - }, { - type: 'separator' - }, { - role: 'hide' - }, { - role: 'hideothers' - }, { - role: 'unhide' - }, { - type: 'separator' - }, { - role: 'quit' - } ] - }) - } else { - menuTemplate.unshift({ - label: packageJson.displayName, - submenu: [ { - role: 'quit' - } ] - }) - } - - const menu = electron.Menu.buildFromTemplate(menuTemplate) - - electron.Menu.setApplicationMenu(menu) -} - -module.exports = buildWindowMenu diff --git a/lib/gui/menu.ts b/lib/gui/menu.ts new file mode 100644 index 00000000..317814d8 --- /dev/null +++ b/lib/gui/menu.ts @@ -0,0 +1,131 @@ +/* + * Copyright 2017 balena.io + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import * as electron from 'electron'; +import { displayName } from '../../package.json'; + +/** + * @summary Builds a native application menu for a given window + */ +export function buildWindowMenu(window: electron.BrowserWindow) { + /** + * @summary Toggle the main window's devtools + */ + function toggleDevTools() { + if (!window) { + return; + } + // NOTE: We can't use `webContents.toggleDevTools()` here, + // as we need to force detached mode + if (window.webContents.isDevToolsOpened()) { + window.webContents.closeDevTools(); + } else { + window.webContents.openDevTools({ + mode: 'detach', + }); + } + } + + const menuTemplate: electron.MenuItemConstructorOptions[] = [ + { + role: 'editMenu', + }, + { + label: 'View', + submenu: [ + { + label: 'Toggle Developer Tools', + accelerator: + process.platform === 'darwin' ? 'Command+Alt+I' : 'Control+Shift+I', + click: toggleDevTools, + }, + ], + }, + { + role: 'windowMenu', + }, + { + role: 'help', + submenu: [ + { + label: 'Etcher Pro', + click() { + electron.shell.openExternal( + 'https://etcher.io/pro?utm_source=etcher_menu&ref=etcher_menu', + ); + }, + }, + { + label: 'Etcher Website', + click() { + electron.shell.openExternal('https://etcher.io?ref=etcher_menu'); + }, + }, + { + label: 'Report an issue', + click() { + electron.shell.openExternal( + 'https://github.com/balena-io/etcher/issues', + ); + }, + }, + ], + }, + ]; + + if (process.platform === 'darwin') { + menuTemplate.unshift({ + label: displayName, + submenu: [ + { + role: 'about' as const, + label: 'About Etcher', + }, + { + type: 'separator' as const, + }, + { + role: 'hide' as const, + }, + { + role: 'hideOthers' as const, + }, + { + role: 'unhide' as const, + }, + { + type: 'separator' as const, + }, + { + role: 'quit' as const, + }, + ], + }); + } else { + menuTemplate.unshift({ + label: displayName, + submenu: [ + { + role: 'quit', + }, + ], + }); + } + + const menu = electron.Menu.buildFromTemplate(menuTemplate); + + electron.Menu.setApplicationMenu(menu); +}