fix: menu should allow focused BaseWindow
where possible (#43437)
fix: menu should allow focused BaseWindow Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
This commit is contained in:
parent
a9a117ed71
commit
204011c3f2
5 changed files with 25 additions and 19 deletions
|
@ -1,4 +1,4 @@
|
|||
import { app, BrowserWindow, session, webContents, WebContents, MenuItemConstructorOptions } from 'electron/main';
|
||||
import { app, BaseWindow, BrowserWindow, session, webContents, WebContents, MenuItemConstructorOptions } from 'electron/main';
|
||||
|
||||
const isMac = process.platform === 'darwin';
|
||||
const isWindows = process.platform === 'win32';
|
||||
|
@ -13,7 +13,7 @@ interface Role {
|
|||
label: string;
|
||||
accelerator?: string;
|
||||
checked?: boolean;
|
||||
windowMethod?: ((window: BrowserWindow) => void);
|
||||
windowMethod?: ((window: BaseWindow) => void);
|
||||
webContentsMethod?: ((webContents: WebContents) => void);
|
||||
appMethod?: () => void;
|
||||
registerAccelerator?: boolean;
|
||||
|
@ -53,8 +53,10 @@ export const roleList: Record<RoleId, Role> = {
|
|||
label: 'Force Reload',
|
||||
accelerator: 'Shift+CmdOrCtrl+R',
|
||||
nonNativeMacOSRole: true,
|
||||
windowMethod: (window: BrowserWindow) => {
|
||||
window.webContents.reloadIgnoringCache();
|
||||
windowMethod: (window: BaseWindow) => {
|
||||
if (window instanceof BrowserWindow) {
|
||||
window.webContents.reloadIgnoringCache();
|
||||
}
|
||||
}
|
||||
},
|
||||
front: {
|
||||
|
@ -110,7 +112,11 @@ export const roleList: Record<RoleId, Role> = {
|
|||
label: 'Reload',
|
||||
accelerator: 'CmdOrCtrl+R',
|
||||
nonNativeMacOSRole: true,
|
||||
windowMethod: w => w.reload()
|
||||
windowMethod: (w: BaseWindow) => {
|
||||
if (w instanceof BrowserWindow) {
|
||||
w.reload();
|
||||
}
|
||||
}
|
||||
},
|
||||
resetzoom: {
|
||||
label: 'Actual Size',
|
||||
|
@ -164,7 +170,7 @@ export const roleList: Record<RoleId, Role> = {
|
|||
togglefullscreen: {
|
||||
label: 'Toggle Full Screen',
|
||||
accelerator: isMac ? 'Control+Command+F' : 'F11',
|
||||
windowMethod: (window: BrowserWindow) => {
|
||||
windowMethod: (window: BaseWindow) => {
|
||||
window.setFullScreen(!window.isFullScreen());
|
||||
}
|
||||
},
|
||||
|
@ -361,7 +367,7 @@ export function getDefaultSubmenu (role: RoleId) {
|
|||
return submenu;
|
||||
}
|
||||
|
||||
export function execute (role: RoleId, focusedWindow: BrowserWindow, focusedWebContents: WebContents) {
|
||||
export function execute (role: RoleId, focusedWindow: BaseWindow, focusedWebContents: WebContents) {
|
||||
if (!canExecuteRole(role)) return false;
|
||||
|
||||
const { appMethod, webContentsMethod, windowMethod } = roleList[role];
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import * as roles from '@electron/internal/browser/api/menu-item-roles';
|
||||
import { Menu, BrowserWindow, WebContents, KeyboardEvent } from 'electron/main';
|
||||
import { Menu, BaseWindow, WebContents, KeyboardEvent } from 'electron/main';
|
||||
|
||||
let nextCommandId = 0;
|
||||
|
||||
|
@ -53,7 +53,7 @@ const MenuItem = function (this: any, options: any) {
|
|||
});
|
||||
|
||||
const click = options.click;
|
||||
this.click = (event: KeyboardEvent, focusedWindow: BrowserWindow, focusedWebContents: WebContents) => {
|
||||
this.click = (event: KeyboardEvent, focusedWindow: BaseWindow, focusedWebContents: WebContents) => {
|
||||
// Manually flip the checked flags when clicked.
|
||||
if (!roles.shouldOverrideCheckStatus(this.role) &&
|
||||
(this.type === 'checkbox' || this.type === 'radio')) {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { BaseWindow, MenuItem, webContents, Menu as MenuType, BrowserWindow, MenuItemConstructorOptions } from 'electron/main';
|
||||
import { BaseWindow, MenuItem, webContents, Menu as MenuType, MenuItemConstructorOptions } from 'electron/main';
|
||||
import { sortMenuItems } from '@electron/internal/browser/api/menu-utils';
|
||||
import { setApplicationMenuWasSet } from '@electron/internal/browser/default-menu';
|
||||
|
||||
|
@ -54,7 +54,7 @@ Menu.prototype._executeCommand = function (event, id) {
|
|||
const command = this.commandsMap[id];
|
||||
if (!command) return;
|
||||
const focusedWindow = BaseWindow.getFocusedWindow();
|
||||
command.click(event, focusedWindow instanceof BrowserWindow ? focusedWindow : undefined, webContents.getFocusedWebContents());
|
||||
command.click(event, focusedWindow, webContents.getFocusedWebContents());
|
||||
};
|
||||
|
||||
Menu.prototype._menuWillShow = function () {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue