How to show main menu on Mac OS when a detached view is focused?

Kildary Klein :

I have an Eclipse RCP Application, using Eclipse 3.8, and on Mac OS the main menu bar is shown only when the main window is focused. When I change the focus to a detached window, the menu bar disappears.

So, I understand that Mac shows the menu depending on the focused window, if the window does not provide a menu bar, nothing is shown.

On Eclipse the behavior is the same.

Main window focused: https://i.stack.imgur.com/4aRMa.png

Detached window focused: https://i.stack.imgur.com/easMD.png

Is there any way to set the main menu regardless of the focused view?

Mario Marinato :

We found a way to do this, but you have to branch a local copy of SWT.

First, we obtained the active workbench window menu bar:

Menu menuBar = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().getMenuBar();

Setting it directly on the shell, using .setMenuBar( menuBar ) won't work, because it checks the menu's parent and throws a IllegalArgumentException. What had to create a new method on Decorations, which is a copy of the original setMenuBar method, minus the parent check:

public void setAdoptedMenuBar( Menu menu ) {
    checkWidget();
    if (menuBar == menu) return;
    if (menu != null) {
        if (menu.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT);
        if ((menu.style & SWT.BAR) == 0) error (SWT.ERROR_MENU_NOT_BAR);
    }
    menuBar = menu;
}

We also had to add a new method on Shell, to make sure it uses the new method on Decorations. It's a copy of Shell.setMenuBar, with a change on the second line.

public void setAdoptedMenuBar( Menu menu ) {
    checkWidget();
    super.setAdoptedMenuBar( menu );
    if (display.getActiveShell () == this) {
        display.setMenuBar (menuBar);
    }
}

Having those changes in place, we can then set the active workbench window menu bar on the shell, using the new method.

We also added a dispose listener to our shell, to make sure we unhook the menu from the shell, to prevent its disposal:

 shell.addDisposeListener( event -> shell.setMenuBar( null ) );

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=356631&siteId=1