Convert menu.js to typescript

Change-type: patch
This commit is contained in:
Alexis Svinartchouk 2020-01-07 13:19:59 +01:00 committed by Lorenzo Alberto Maria Ambrosi
parent f31cb49e2a
commit 233a2e6400
3 changed files with 133 additions and 130 deletions

View File

@ -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')

View File

@ -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

131
lib/gui/menu.ts Normal file
View File

@ -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);
}