2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2025-01-09 08:30:15 +00:00

App/menu: cleanup, typing, properly hide devtools when asked (fix #842)

This commit is contained in:
Ronan Jouchet 2020-03-18 00:16:47 -04:00
parent 72a9eae6d6
commit f7215814d7

View File

@ -1,4 +1,10 @@
import { Menu, clipboard, globalShortcut, shell } from 'electron'; import {
Menu,
clipboard,
globalShortcut,
shell,
MenuItemConstructorOptions,
} from 'electron';
export function createMenu({ export function createMenu({
nativefierVersion, nativefierVersion,
@ -18,8 +24,7 @@ export function createMenu({
? 'Reset Zoom' ? 'Reset Zoom'
: `Reset Zoom (to ${zoomBuildTimeValue * 100}%, set at build time)`; : `Reset Zoom (to ${zoomBuildTimeValue * 100}%, set at build time)`;
const template: any[] = [ const editMenu: MenuItemConstructorOptions = {
{
label: '&Edit', label: '&Edit',
submenu: [ submenu: [
{ {
@ -61,20 +66,21 @@ export function createMenu({
{ {
label: 'Paste and Match Style', label: 'Paste and Match Style',
accelerator: 'CmdOrCtrl+Shift+V', accelerator: 'CmdOrCtrl+Shift+V',
role: 'pasteandmatchstyle', role: 'pasteAndMatchStyle',
}, },
{ {
label: 'Select All', label: 'Select All',
accelerator: 'CmdOrCtrl+A', accelerator: 'CmdOrCtrl+A',
role: 'selectall', role: 'selectAll',
}, },
{ {
label: 'Clear App Data', label: 'Clear App Data',
click: clearAppData, click: clearAppData,
}, },
], ],
}, };
{
const viewMenu: MenuItemConstructorOptions = {
label: '&View', label: '&View',
submenu: [ submenu: [
{ {
@ -143,6 +149,14 @@ export function createMenu({
})(), })(),
click: zoomReset, click: zoomReset,
}, },
],
};
if (!disableDevTools) {
(viewMenu.submenu as MenuItemConstructorOptions[]).push(
{
type: 'separator',
},
{ {
label: 'Toggle Developer Tools', label: 'Toggle Developer Tools',
accelerator: (() => { accelerator: (() => {
@ -153,13 +167,14 @@ export function createMenu({
})(), })(),
click: (item, focusedWindow) => { click: (item, focusedWindow) => {
if (focusedWindow) { if (focusedWindow) {
focusedWindow.toggleDevTools(); focusedWindow.webContents.toggleDevTools();
} }
}, },
}, },
], );
}, }
{
const windowMenu: MenuItemConstructorOptions = {
label: '&Window', label: '&Window',
role: 'window', role: 'window',
submenu: [ submenu: [
@ -174,8 +189,9 @@ export function createMenu({
role: 'close', role: 'close',
}, },
], ],
}, };
{
const helpMenu: MenuItemConstructorOptions = {
label: '&Help', label: '&Help',
role: 'help', role: 'help',
submenu: [ submenu: [
@ -192,17 +208,12 @@ export function createMenu({
}, },
}, },
], ],
}, };
];
if (disableDevTools) { let menuTemplate: MenuItemConstructorOptions[];
// remove last item (dev tools) from menu > view
const { submenu } = template[1];
submenu.splice(submenu.length - 1, 1);
}
if (process.platform === 'darwin') { if (process.platform === 'darwin') {
template.unshift({ const electronMenu: MenuItemConstructorOptions = {
label: 'E&lectron', label: 'E&lectron',
submenu: [ submenu: [
{ {
@ -221,7 +232,7 @@ export function createMenu({
{ {
label: 'Hide Others', label: 'Hide Others',
accelerator: 'Cmd+Shift+H', accelerator: 'Cmd+Shift+H',
role: 'hideothers', role: 'hideOthers',
}, },
{ {
label: 'Show All', label: 'Show All',
@ -236,8 +247,8 @@ export function createMenu({
click: appQuit, click: appQuit,
}, },
], ],
}); };
template[3].submenu.push( (windowMenu.submenu as MenuItemConstructorOptions[]).push(
{ {
type: 'separator', type: 'separator',
}, },
@ -246,8 +257,11 @@ export function createMenu({
role: 'front', role: 'front',
}, },
); );
menuTemplate = [electronMenu, editMenu, viewMenu, windowMenu, helpMenu];
} else {
menuTemplate = [editMenu, viewMenu, windowMenu, helpMenu];
} }
const menu = Menu.buildFromTemplate(template); const menu = Menu.buildFromTemplate(menuTemplate);
Menu.setApplicationMenu(menu); Menu.setApplicationMenu(menu);
} }