feat: add support for share menu on macOS (#25629)

This commit is contained in:
Cheng Zhao 2020-10-20 10:33:06 +09:00 committed by GitHub
parent 89c04b3c6c
commit 6b6ffbdd10
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 316 additions and 6 deletions

View file

@ -6,7 +6,7 @@ const isLinux = process.platform === 'linux';
type RoleId = 'about' | 'close' | 'copy' | 'cut' | 'delete' | 'forcereload' | 'front' | 'help' | 'hide' | 'hideothers' | 'minimize' |
'paste' | 'pasteandmatchstyle' | 'quit' | 'redo' | 'reload' | 'resetzoom' | 'selectall' | 'services' | 'recentdocuments' | 'clearrecentdocuments' | 'startspeaking' | 'stopspeaking' |
'toggledevtools' | 'togglefullscreen' | 'undo' | 'unhide' | 'window' | 'zoom' | 'zoomin' | 'zoomout' | 'appmenu' | 'filemenu' | 'editmenu' | 'viewmenu' | 'windowmenu'
'toggledevtools' | 'togglefullscreen' | 'undo' | 'unhide' | 'window' | 'zoom' | 'zoomin' | 'zoomout' | 'appmenu' | 'filemenu' | 'editmenu' | 'viewmenu' | 'windowmenu' | 'sharemenu'
interface Role {
label: string;
accelerator?: string;
@ -261,6 +261,11 @@ export const roleList: Record<RoleId, Role> = {
{ role: 'close' }
] as MenuItemConstructorOptions[])
]
},
// Share submenu
sharemenu: {
label: 'Share',
submenu: []
}
};

View file

@ -41,6 +41,12 @@ Menu.prototype._shouldRegisterAcceleratorForCommandId = function (id) {
return this.commandsMap[id] ? this.commandsMap[id].registerAccelerator : false;
};
if (process.platform === 'darwin') {
Menu.prototype._getSharingItemForCommandId = function (id) {
return this.commandsMap[id] ? this.commandsMap[id].sharingItem : null;
};
}
Menu.prototype._executeCommand = function (event, id) {
const command = this.commandsMap[id];
if (!command) return;

View file

@ -26,6 +26,7 @@ export const browserModuleList: ElectronInternal.ModuleEntry[] = [
{ name: 'protocol', loader: () => require('./protocol') },
{ name: 'screen', loader: () => require('./screen') },
{ name: 'session', loader: () => require('./session') },
{ name: 'ShareMenu', loader: () => require('./share-menu') },
{ name: 'systemPreferences', loader: () => require('./system-preferences') },
{ name: 'TouchBar', loader: () => require('./touch-bar') },
{ name: 'Tray', loader: () => require('./tray') },

View file

@ -29,6 +29,7 @@ export const browserModuleNames = [
'protocol',
'screen',
'session',
'ShareMenu',
'systemPreferences',
'TouchBar',
'Tray',

View file

@ -0,0 +1,19 @@
import { BrowserWindow, Menu, SharingItem, PopupOptions } from 'electron/main';
class ShareMenu {
private menu: Menu;
constructor (sharingItem: SharingItem) {
this.menu = new (Menu as any)({ sharingItem });
}
popup (options?: PopupOptions) {
this.menu.popup(options);
}
closePopup (browserWindow?: BrowserWindow) {
this.menu.closePopup(browserWindow);
}
}
export default ShareMenu;