max-mapper/menubar

Show context menu on right click only

sahilparikh98 opened this issue ยท 9 comments

Description

Currently if you would like to show an app on click and have a context menu, the menu appears over the app on every click.

Steps to Reproduce the Problem

const { menubar } = require('menubar');
const { app, Menu, Tray } = require('electron');

app.on('ready', () => {
  let tray = new Tray("icon-white.png")
  const contextMenu = Menu.buildFromTemplate([{label: "Quit"}])
  tray.setContextMenu(contextMenu);

  const mb = menubar({
    index: "https://www.notion.so",
    showDockIcon: false,
    showOnAllWorkspaces: false,
    tray: tray,
  });

  mb.on('ready', () => {
    console.log("menubar is ready");
  })
})

Expected Behaviour

Context menu opens on right click only.

Actual Behaviour

Context menu opens on each click.

Specifications

  • Menubar version:
  • Platform: macOS
  • Electron version: 10.1.5

Other information

say4n commented

You can do something like this:

const { app, session, Menu, Tray } = require('electron');
const { menubar } = require('menubar');
const path = require('path');

app.on('ready', () => {
    'use strict';

    const iconPath = path.join(__dirname, 'assets', 'icon.png');
    const tray = new Tray(iconPath);

    const contextMenu = Menu.buildFromTemplate([
        { role: 'about' },
        { type: 'separator' },
        { role: 'quit' }
    ]);

    const mb = menubar({
        tray,
        index: "{{ url goes here }}",
        showDockIcon: false,
        showOnAllWorkspaces: false,
    });

    mb.on('ready', () => {
        tray.on('right-click',() => {
            mb.tray.popUpContextMenu(contextMenu);
        })
    });
});

Code snippet by @say4n works, thank you! But would also be neat to have this built in. Closing issue.

@sahilparikh98 @say4n did you manage to get this working with a multi-monitor setup?

On macOS, using the technique above the context menu only shows on my primary monitor.

If I right-click the menu bar icon on my secondary monitor, the 'right-click' arrow function gets called, but the menu doesn't show

It has been working for me on both my monitors. What props are you sending to the menubar?

@sahilparikh98 thanks for the reply! I'm using the exact same code as posted by @say4n

After a bit more investigation, it seems that the menu does show, but only if the displays are arranged horizontally.

If the displays are arranged vertically, then the context menu only shows on the top display. Can you reproduce that?

say4n commented

@jamiebullock sorry but I do not have a multi-monitor setup, can't help. :)

@jamiebullock I arranged my displays vertically and the context menu still shows on both displays. I'm still not able to reproduce this. FWIW I am now running macOS Big Sur.

@sahilparikh98 @say4n ... since upgrading to Big Sur the problem has disappeared, so ๐Ÿคท๐Ÿปโ€โ™‚๏ธ

Thanks for helping anyhow.

I'm still getting the context menu with popup open at the same time :(