context-menu: Remove trailing and leading separators when not required.

Fixes: #979.
This commit is contained in:
Abhigyan Khaund
2020-06-27 02:38:03 +05:30
committed by GitHub
parent bda0dd29df
commit addfe2e414

View File

@@ -22,7 +22,8 @@ export const contextMenu = (webContents: Electron.WebContents, event: Event, pro
webContents.session.addWordToSpellCheckerDictionary(props.misspelledWord); webContents.session.addWordToSpellCheckerDictionary(props.misspelledWord);
} }
}, { }, {
type: 'separator' type: 'separator',
visible: props.isEditable && isText && props.misspelledWord.length !== 0
}, { }, {
label: `${t.__('Look Up')} "${props.selectionText}"`, label: `${t.__('Look Up')} "${props.selectionText}"`,
visible: process.platform === 'darwin' && isText, visible: process.platform === 'darwin' && isText,
@@ -30,7 +31,8 @@ export const contextMenu = (webContents: Electron.WebContents, event: Event, pro
webContents.showDefinitionForSelection(); webContents.showDefinitionForSelection();
} }
}, { }, {
type: 'separator' type: 'separator',
visible: process.platform === 'darwin' && isText
}, { }, {
label: t.__('Cut'), label: t.__('Cut'),
visible: isText, visible: isText,
@@ -80,7 +82,8 @@ export const contextMenu = (webContents: Electron.WebContents, event: Event, pro
}); });
} }
}, { }, {
type: 'separator' type: 'separator',
visible: isLink || props.mediaType === 'image'
}, { }, {
label: t.__('Services'), label: t.__('Services'),
visible: process.platform === 'darwin', visible: process.platform === 'darwin',
@@ -98,7 +101,13 @@ export const contextMenu = (webContents: Electron.WebContents, event: Event, pro
}); });
} }
} }
// Hide the invisible separators on Linux and Windows
// Electron has a bug which ignores visible: false parameter for separator menuitems. So we remove them here.
// https://github.com/electron/electron/issues/5869
// https://github.com/electron/electron/issues/6906
const menu = Menu.buildFromTemplate(menuTemplate); // eslint-disable-next-line @typescript-eslint/no-unnecessary-boolean-literal-compare
const filteredMenuTemplate = menuTemplate.filter(menuItem => menuItem.visible !== false);
const menu = Menu.buildFromTemplate(filteredMenuTemplate);
menu.popup(); menu.popup();
}; };