Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Impossible to update the menu #245

Open
Makio64 opened this issue Oct 29, 2023 · 2 comments
Open

Impossible to update the menu #245

Makio64 opened this issue Oct 29, 2023 · 2 comments

Comments

@Makio64
Copy link

Makio64 commented Oct 29, 2023

Problem Description

On recent electron version, changing the menu using the example or other methods didnt work and the menu in the title bar simply disappear.

Steps to Reproduce

	// in the main
	const menu = new Menu()
	menu.append(
		new MenuItem({
			label: 'File',
			submenu: [
				{
					label: 'Import gltf/glb',
					click: () => {
						console.log('Open File')
					},
				},
				{
					type: 'separator',
				},
				{
					label: 'Close',
					click: () => {
						win.close()
					},
				},
			],
		}),
	)
	Menu.setApplicationMenu(menu)

Expected Behavior

The title bar is updated with the new menu

Current Behavior

the menu disappear

Additional Information

  • windows 11
  • node v20.5.0
  • electron 27.0.2 / also didn't work on ^26.0.0

Note:
Labels - bug

@AlexTorresDev
Copy link
Owner

You should use titlebar.refreshMenu(), an asynchronous method that refreshes the menu with what is in Menu.getApplicationMenu(), after updating the menu with the sample code you showed above.

@hello112334
Copy link

hello112334 commented Aug 3, 2024

I update all menu like this:

// main.ts
import { app, BrowserWindow, Menu } from "electron";
import registerListeners from "./helpers/ipc/listeners-register";
import path from "path";

const inDevelopment = process.env.NODE_ENV === "development";

if (require("electron-squirrel-startup")) {
    app.quit();
}

import { setupTitlebar, attachTitlebarToWindow } from "custom-electron-titlebar/main";
import { MenuItem, MenuItemConstructorOptions } from "electron";

// setup the titlebar main process
setupTitlebar();

function createWindow() {
    const preload = path.join(__dirname, "preload.js");
    const mainWindow = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences: {
            sandbox: false,
            devTools: inDevelopment,
            contextIsolation: true,
            nodeIntegration: true,
            nodeIntegrationInSubFrames: false,
            preload: preload,
        },
        titleBarStyle: "hidden",
        frame: false, // Add this line
    });
    registerListeners(mainWindow);

    if (MAIN_WINDOW_VITE_DEV_SERVER_URL) {
        mainWindow.loadURL(MAIN_WINDOW_VITE_DEV_SERVER_URL);
    } else {
        mainWindow.loadFile(
            path.join(__dirname, `../renderer/${MAIN_WINDOW_VITE_NAME}/index.html`)
        );
    }

    if (inDevelopment) {
        mainWindow.webContents.openDevTools();
    }

    // Create menu template
    // Define the menu template
    const isMac = process.platform === "darwin";
    const template: (MenuItemConstructorOptions | MenuItem)[] = [
        ...(isMac
            ? [
                  {
                      label: app.name,
                      submenu: [
                          { role: "about" as const },
                          { type: "separator" as const },
                          { role: "services" as const },
                          { type: "separator" as const },
                          { role: "hide" as const },
                          { role: "hideOthers" as const },
                          { role: "unhide" as const },
                          { type: "separator" as const },
                          { role: "quit" as const },
                      ],
                  },
              ]
            : []),
        {
            label: "File",
            submenu: [isMac ? { role: "close" as const } : { role: "quit" as const }],
        },
        {
            label: "Edit",
            submenu: [
                { role: "undo" as const },
                { role: "redo" as const },
                { type: "separator" as const },
                { role: "cut" as const },
                { role: "copy" as const },
                { role: "paste" as const },
                ...(isMac
                    ? [
                          { role: "pasteAndMatchStyle" as const },
                          { role: "delete" as const },
                          { role: "selectAll" as const },
                          { type: "separator" as const },
                          {
                              label: "Speech",
                              submenu: [
                                  { role: "startSpeaking" as const },
                                  { role: "stopSpeaking" as const },
                              ],
                          },
                      ]
                    : [
                          { role: "delete" as const },
                          { type: "separator" as const },
                          { role: "selectAll" as const },
                      ]),
            ],
        },
        {
            label: "View",
            submenu: [
                { role: "reload" as const },
                { role: "forceReload" as const },
                { role: "toggleDevTools" as const },
                { type: "separator" as const },
                { role: "resetZoom" as const },
                { role: "zoomIn" as const },
                { role: "zoomOut" as const },
                { type: "separator" as const },
                { role: "togglefullscreen" as const },
            ],
        },
        {
            label: "Window",
            submenu: [
                { role: "minimize" as const },
                { role: "zoom" as const },
                ...(isMac
                    ? [
                          { type: "separator" as const },
                          { role: "front" as const },
                          { type: "separator" as const },
                          { role: "window" as const },
                      ]
                    : [{ role: "close" as const }]),
            ],
        },
        {
            role: "help",
            submenu: [
                {
                    label: "Learn More",
                    click: async () => {
                        const { shell } = require("electron");
                        await shell.openExternal("https://electronjs.org");
                    },
                },
            ],
        },
    ];

    const menu = Menu.buildFromTemplate(template);
    Menu.setApplicationMenu(menu);

    // attach fullScreen(f11 and not 'maximized') && focus listeners
    attachTitlebarToWindow(mainWindow);
}

// app.whenReady().then(createWindow);
app.whenReady().then(createWindow);

// osX only
app.on("window-all-closed", () => {
    if (process.platform !== "darwin") {
        app.quit();
    }
});

app.on("activate", () => {
    if (BrowserWindow.getAllWindows().length === 0) {
        createWindow();
    }
});
// osX only ends

refer

https://www.electronjs.org/docs/latest/api/menu#examples

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants