QCocoaMenuBar: set the app's 'Window' menu

To enable a list of windows the app has open in the Dock's menu.
Not to surprise existing applications with a 'Window' menu where
they did not have it before, make the item hidden.

Fixes: QTBUG-59433
Change-Id: I1ac3d3de69f4313f39c4631dc4b68bf6e096532a
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
bb10
Timur Pocheptsov 2021-11-10 15:19:51 +01:00 committed by Tor Arne Vestbø
parent e01c25e859
commit 3fcdb6cb6e
3 changed files with 31 additions and 0 deletions

View File

@ -75,6 +75,7 @@
#include "qcocoaapplicationdelegate.h"
#include "qcocoaintegration.h"
#include "qcocoamenubar.h"
#include "qcocoamenu.h"
#include "qcocoamenuloader.h"
#include "qcocoamenuitem.h"
@ -230,6 +231,8 @@ QT_USE_NAMESPACE
// (See the activateIgnoringOtherApps docs.)
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
}
QCocoaMenuBar::insertWindowMenu();
}
- (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames

View File

@ -67,6 +67,7 @@ public:
NSMenu *nsMenu() const override { return m_nativeMenu; }
static void updateMenuBarImmediately();
static void insertWindowMenu();
QList<QCocoaMenuItem*> merged() const;
NSMenuItem *itemForRole(QPlatformMenuItem::MenuRole role);

View File

@ -333,9 +333,36 @@ void QCocoaMenuBar::updateMenuBarImmediately()
[mergedItems release];
[NSApp setMainMenu:mb->nsMenu()];
insertWindowMenu();
[loader qtTranslateApplicationMenu];
}
void QCocoaMenuBar::insertWindowMenu()
{
// For such an item/menu we get for 'free' an additional feature -
// a list of windows the application has created in the Dock's menu.
NSApplication *app = NSApplication.sharedApplication;
if (app.windowsMenu)
return;
NSMenu *mainMenu = app.mainMenu;
NSMenuItem *winMenuItem = [[[NSMenuItem alloc] initWithTitle:@"QtWindowMenu"
action:nil keyEquivalent:@""] autorelease];
// We don't want to show this menu, nobody asked us to do so:
winMenuItem.hidden = YES;
winMenuItem.submenu = [[[NSMenu alloc] initWithTitle:@"QtWindowMenu"] autorelease];
[mainMenu insertItem:winMenuItem atIndex:mainMenu.itemArray.count];
app.windowsMenu = winMenuItem.submenu;
// Windows, created and 'ordered front' before, will not be in this menu:
for (NSWindow *win in app.windows) {
if (win.title && ![win.title isEqualToString:@""])
[app addWindowsItem:win title:win.title filename:NO];
}
}
QList<QCocoaMenuItem*> QCocoaMenuBar::merged() const
{
QList<QCocoaMenuItem*> r;