QCocoaMenuLoader - ensure that ensureAppMenuInMenu indeed, ensures

The logic seems to be incorrect (or the naming is misleading): it
only adds 'appMenu' if it was found in the previous 'mainMenu',
failing otherwise. Consider the following example:

while (true){
    QApplication app(a,b);
    MainWindow w;
    w.show();
    app.exec();
}

It's quite a contrived but apparently allowed API use (OP claims
they have to switch languages in their app). The main window and the
app are destroyed, so is the menu bar. Then a new main window is
created, with a new menu bar. Now the current [NSApp mainMenu]
(the one set after we deleted the previous) does not have 'appMenu'
anymore (we removed it when initializing the first menu bar).
So as a result we have app menu missing and add new menus/items
to a wrong menus/at wrong index.

Change-Id: I64fce766d6c12ebf7ae12bb94af41c8c1de3d78b
Task-number: QTBUG-69496
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
This commit is contained in:
Gabriel de Dietrich 2018-07-24 13:23:35 -07:00
parent 843629dd1f
commit 4126de8877

View File

@ -191,27 +191,24 @@
Q_ASSERT(mainMenu); Q_ASSERT(mainMenu);
#endif #endif
// Grab the app menu out of the current menu. // Grab the app menu out of the current menu.
int numItems = [mainMenu numberOfItems]; auto unparentAppMenu = ^bool (NSMenu *supermenu) {
NSMenuItem *oldAppMenuItem = 0; auto index = [supermenu indexOfItemWithSubmenu:appMenu];
for (int i = 0; i < numItems; ++i) { if (index != -1) {
NSMenuItem *item = [mainMenu itemAtIndex:i]; [supermenu removeItemAtIndex:index];
if ([item submenu] == appMenu) { return true;
oldAppMenuItem = item;
[oldAppMenuItem retain];
[mainMenu removeItemAtIndex:i];
break;
}
} }
return false;
};
if (!mainMenu || !unparentAppMenu(mainMenu))
if (appMenu.supermenu)
unparentAppMenu(appMenu.supermenu);
if (oldAppMenuItem) {
[oldAppMenuItem setSubmenu:nil];
[oldAppMenuItem release];
NSMenuItem *appMenuItem = [[NSMenuItem alloc] initWithTitle:@"Apple" NSMenuItem *appMenuItem = [[NSMenuItem alloc] initWithTitle:@"Apple"
action:nil keyEquivalent:@""]; action:nil keyEquivalent:@""];
[appMenuItem setSubmenu:appMenu]; [appMenuItem setSubmenu:appMenu];
[menu insertItem:appMenuItem atIndex:0]; [menu insertItem:appMenuItem atIndex:0];
} }
}
- (void)removeActionsFromAppMenu - (void)removeActionsFromAppMenu
{ {