From 1880fba971ed8ae8e813829ff3668132371e88a7 Mon Sep 17 00:00:00 2001 From: Eskil Abrahamsen Blomfeldt Date: Mon, 29 Jul 2019 14:16:00 +0200 Subject: Android: Fix QMenu on 64 bit The platform menu tags in Qt are actually the pointers, so they are 64-bit values when the build is 64 bit. Since menu IDs in Android are 32-bit ints, we cannot cast back and forth like we do. To fix this, we add a separate hash of menu IDs to allow mapping between Java and C++. For easier book-keeping, we add the hashes to the menu bar and menu classes, so that we can easily recycle old menu IDs when they are no longer in use. Note that overriding the tag on the menus by calling setTag() will not work, since Qt Widgets will later override it again by setting it back to the menu's pointer. [ChangeLog][Android] Fixed an issue where menus would not work on 64 bit builds. Task-number: QTBUG-76036 Change-Id: Icaa1d235d4166331669139251656ea0159e85195 Reviewed-by: Frederik Gladhorn --- src/plugins/platforms/android/androidjnimenu.cpp | 12 +++--- .../platforms/android/qandroidplatformmenu.cpp | 43 +++++++++++++++++++--- .../platforms/android/qandroidplatformmenu.h | 5 +++ .../platforms/android/qandroidplatformmenubar.cpp | 34 ++++++++++++++++- .../platforms/android/qandroidplatformmenubar.h | 6 +++ 5 files changed, 88 insertions(+), 12 deletions(-) (limited to 'src/plugins') diff --git a/src/plugins/platforms/android/androidjnimenu.cpp b/src/plugins/platforms/android/androidjnimenu.cpp index 6f548aba52..e9359def0f 100644 --- a/src/plugins/platforms/android/androidjnimenu.cpp +++ b/src/plugins/platforms/android/androidjnimenu.cpp @@ -225,10 +225,11 @@ namespace QtAndroidMenu QString itemText = removeAmpersandEscapes(item->text()); jstring jtext = env->NewString(reinterpret_cast(itemText.data()), itemText.length()); + jint menuId = platformMenu->menuId(item); jobject menuItem = env->CallObjectMethod(menu, addMenuItemMethodID, menuNoneValue, - int(item->tag()), + menuId, order++, jtext); env->DeleteLocalRef(jtext); @@ -262,10 +263,11 @@ namespace QtAndroidMenu QString itemText = removeAmpersandEscapes(item->text()); jstring jtext = env->NewString(reinterpret_cast(itemText.data()), itemText.length()); + jint menuId = visibleMenuBar->menuId(item); jobject menuItem = env->CallObjectMethod(menu, addMenuItemMethodID, menuNoneValue, - int(item->tag()), + menuId, order++, jtext); env->DeleteLocalRef(jtext); @@ -290,7 +292,7 @@ namespace QtAndroidMenu const QAndroidPlatformMenuBar::PlatformMenusType &menus = visibleMenuBar->menus(); if (menus.size() == 1) { // Expanded menu - QAndroidPlatformMenuItem *item = static_cast(menus.front()->menuItemForTag(menuId)); + QAndroidPlatformMenuItem *item = static_cast(menus.front()->menuItemForId(menuId)); if (item) { if (item->menu()) { showContextMenu(item->menu(), QRect(), env); @@ -301,7 +303,7 @@ namespace QtAndroidMenu } } } else { - QAndroidPlatformMenu *menu = static_cast(visibleMenuBar->menuForTag(menuId)); + QAndroidPlatformMenu *menu = static_cast(visibleMenuBar->menuForId(menuId)); if (menu) showContextMenu(menu, QRect(), env); } @@ -341,7 +343,7 @@ namespace QtAndroidMenu static jboolean onContextItemSelected(JNIEnv *env, jobject /*thiz*/, jint menuId, jboolean checked) { QMutexLocker lock(&visibleMenuMutex); - QAndroidPlatformMenuItem * item = static_cast(visibleMenu->menuItemForTag(menuId)); + QAndroidPlatformMenuItem * item = static_cast(visibleMenu->menuItemForId(menuId)); if (item) { if (item->menu()) { showContextMenu(item->menu(), QRect(), env); diff --git a/src/plugins/platforms/android/qandroidplatformmenu.cpp b/src/plugins/platforms/android/qandroidplatformmenu.cpp index d9cecebf2c..7ce603831f 100644 --- a/src/plugins/platforms/android/qandroidplatformmenu.cpp +++ b/src/plugins/platforms/android/qandroidplatformmenu.cpp @@ -62,6 +62,7 @@ void QAndroidPlatformMenu::insertMenuItem(QPlatformMenuItem *menuItem, QPlatform m_menuItems.end(), static_cast(before)), static_cast(menuItem)); + m_menuHash.insert(m_nextMenuId++, menuItem); } void QAndroidPlatformMenu::removeMenuItem(QPlatformMenuItem *menuItem) @@ -72,6 +73,21 @@ void QAndroidPlatformMenu::removeMenuItem(QPlatformMenuItem *menuItem) static_cast(menuItem)); if (it != m_menuItems.end()) m_menuItems.erase(it); + + { + int maxId = -1; + QHash::iterator it = m_menuHash.begin(); + while (it != m_menuHash.end()) { + if (it.value() == menuItem) { + it = m_menuHash.erase(it); + } else { + maxId = qMax(maxId, it.key()); + ++it; + } + } + + m_nextMenuId = maxId + 1; + } } void QAndroidPlatformMenu::syncMenuItem(QPlatformMenuItem *menuItem) @@ -139,6 +155,16 @@ void QAndroidPlatformMenu::showPopup(const QWindow *parentWindow, const QRect &t QtAndroidMenu::showContextMenu(this, targetRect, QJNIEnvironmentPrivate()); } +QPlatformMenuItem *QAndroidPlatformMenu::menuItemForTag(quintptr tag) const +{ + for (QAndroidPlatformMenuItem *menuItem : m_menuItems) { + if (menuItem->tag() == tag) + return menuItem; + } + + return nullptr; +} + QPlatformMenuItem *QAndroidPlatformMenu::menuItemAt(int position) const { if (position < m_menuItems.size()) @@ -146,13 +172,20 @@ QPlatformMenuItem *QAndroidPlatformMenu::menuItemAt(int position) const return 0; } -QPlatformMenuItem *QAndroidPlatformMenu::menuItemForTag(quintptr tag) const +int QAndroidPlatformMenu::menuId(QPlatformMenuItem *menu) const { - for (QPlatformMenuItem *menuItem : m_menuItems) { - if (menuItem->tag() == tag) - return menuItem; + QHash::const_iterator it; + for (it = m_menuHash.constBegin(); it != m_menuHash.constEnd(); ++it) { + if (it.value() == menu) + return it.key(); } - return 0; + + return -1; +} + +QPlatformMenuItem *QAndroidPlatformMenu::menuItemForId(int menuId) const +{ + return m_menuHash.value(menuId); } QAndroidPlatformMenu::PlatformMenuItemsType QAndroidPlatformMenu::menuItems() const diff --git a/src/plugins/platforms/android/qandroidplatformmenu.h b/src/plugins/platforms/android/qandroidplatformmenu.h index 47e650f2d7..b1d6a88787 100644 --- a/src/plugins/platforms/android/qandroidplatformmenu.h +++ b/src/plugins/platforms/android/qandroidplatformmenu.h @@ -73,6 +73,8 @@ public: QPlatformMenuItem *menuItemAt(int position) const override; QPlatformMenuItem *menuItemForTag(quintptr tag) const override; + QPlatformMenuItem *menuItemForId(int menuId) const; + int menuId(QPlatformMenuItem *menuItem) const; PlatformMenuItemsType menuItems() const; QMutex *menuItemsMutex(); @@ -84,6 +86,9 @@ private: bool m_enabled; bool m_isVisible; QMutex m_menuItemsMutex; + + int m_nextMenuId = 0; + QHash m_menuHash; }; QT_END_NAMESPACE diff --git a/src/plugins/platforms/android/qandroidplatformmenubar.cpp b/src/plugins/platforms/android/qandroidplatformmenubar.cpp index 35930f0628..7c6299b4b7 100644 --- a/src/plugins/platforms/android/qandroidplatformmenubar.cpp +++ b/src/plugins/platforms/android/qandroidplatformmenubar.cpp @@ -61,6 +61,7 @@ void QAndroidPlatformMenuBar::insertMenu(QPlatformMenu *menu, QPlatformMenu *bef m_menus.end(), static_cast(before)), static_cast(menu)); + m_menuHash.insert(m_nextMenuId++, menu); } void QAndroidPlatformMenuBar::removeMenu(QPlatformMenu *menu) @@ -69,6 +70,30 @@ void QAndroidPlatformMenuBar::removeMenu(QPlatformMenu *menu) m_menus.erase(std::find(m_menus.begin(), m_menus.end(), static_cast(menu))); + + int maxId = -1; + QHash::iterator it = m_menuHash.begin(); + while (it != m_menuHash.end()) { + if (it.value() == menu) { + it = m_menuHash.erase(it); + } else { + maxId = qMax(maxId, it.key()); + ++it; + } + } + + m_nextMenuId = maxId + 1; +} + +int QAndroidPlatformMenuBar::menuId(QPlatformMenu *menu) const +{ + QHash::const_iterator it; + for (it = m_menuHash.constBegin(); it != m_menuHash.constEnd(); ++it) { + if (it.value() == menu) + return it.key(); + } + + return -1; } void QAndroidPlatformMenuBar::syncMenu(QPlatformMenu *menu) @@ -86,12 +111,17 @@ void QAndroidPlatformMenuBar::handleReparent(QWindow *newParentWindow) QPlatformMenu *QAndroidPlatformMenuBar::menuForTag(quintptr tag) const { - for (QPlatformMenu *menu : m_menus) { + for (QAndroidPlatformMenu *menu : m_menus) { if (menu->tag() == tag) return menu; } - return 0; + return nullptr; +} + +QPlatformMenu *QAndroidPlatformMenuBar::menuForId(int menuId) const +{ + return m_menuHash.value(menuId); } QWindow *QAndroidPlatformMenuBar::parentWindow() const diff --git a/src/plugins/platforms/android/qandroidplatformmenubar.h b/src/plugins/platforms/android/qandroidplatformmenubar.h index f5935b8177..81a26c72f4 100644 --- a/src/plugins/platforms/android/qandroidplatformmenubar.h +++ b/src/plugins/platforms/android/qandroidplatformmenubar.h @@ -43,6 +43,7 @@ #include #include #include +#include QT_BEGIN_NAMESPACE @@ -60,6 +61,8 @@ public: void syncMenu(QPlatformMenu *menu) override; void handleReparent(QWindow *newParentWindow) override; QPlatformMenu *menuForTag(quintptr tag) const override; + QPlatformMenu *menuForId(int menuId) const; + int menuId(QPlatformMenu *menu) const; QWindow *parentWindow() const override; PlatformMenusType menus() const; @@ -69,6 +72,9 @@ private: PlatformMenusType m_menus; QWindow *m_parentWindow; QMutex m_menusListMutex; + + int m_nextMenuId = 0; + QHash m_menuHash; }; QT_END_NAMESPACE -- cgit v1.2.3 From 4d9375020c59282b447aad5189c2adb12931e4a1 Mon Sep 17 00:00:00 2001 From: Jason Haslam Date: Tue, 18 Jun 2019 11:51:38 -0600 Subject: macOS: Fix tab button rendering issue This fixes rendering artifacts for the specific case of the first unselected vertical (west) tab button in a tab bar. The popup button gets drawn at the beginning of the tab bar instead of translated to the actual location of the tab. Fixes: QTBUG-76385 Change-Id: I17112c56eabacf34e470314d4cc6b263ba632ec1 Reviewed-by: Timur Pocheptsov --- src/plugins/styles/mac/qmacstyle_mac.mm | 1 + 1 file changed, 1 insertion(+) (limited to 'src/plugins') diff --git a/src/plugins/styles/mac/qmacstyle_mac.mm b/src/plugins/styles/mac/qmacstyle_mac.mm index 1ff1c22788..07651fc206 100644 --- a/src/plugins/styles/mac/qmacstyle_mac.mm +++ b/src/plugins/styles/mac/qmacstyle_mac.mm @@ -3944,6 +3944,7 @@ void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter CGContextScaleCTM(ctx, -1, 1); CGContextTranslateCTM(ctx, -frameRect.left(), 0); } else if (tabDirection == QMacStylePrivate::West && tp == QStyleOptionTab::Beginning) { + CGContextTranslateCTM(ctx, 0, opt->rect.top()); CGContextScaleCTM(ctx, 1, -1); CGContextTranslateCTM(ctx, 0, -frameRect.right()); } else if (tabDirection == QMacStylePrivate::East && tp == QStyleOptionTab::End) { -- cgit v1.2.3 From aef0fba3c550915318db5b350892f558ec7e77ff Mon Sep 17 00:00:00 2001 From: Timur Pocheptsov Date: Wed, 7 Aug 2019 13:33:41 +0200 Subject: QCocoaMenuLoader: get rid of lastAppSpecificItem MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Look it up when needed instead. Also, simplify our ownership logic - do not retain/autorelease that is already owned by a menu (via its itemArray). Fixes: QTBUG-76523 Change-Id: I60a2ed0d192396baf99eec7b37fa5cc10e5db626 Reviewed-by: Tor Arne Vestbø --- src/plugins/platforms/cocoa/qcocoamenuloader.mm | 48 ++++++++++++++----------- 1 file changed, 27 insertions(+), 21 deletions(-) (limited to 'src/plugins') diff --git a/src/plugins/platforms/cocoa/qcocoamenuloader.mm b/src/plugins/platforms/cocoa/qcocoamenuloader.mm index da0fc5c6a1..d384078e91 100644 --- a/src/plugins/platforms/cocoa/qcocoamenuloader.mm +++ b/src/plugins/platforms/cocoa/qcocoamenuloader.mm @@ -59,7 +59,6 @@ NSMenuItem *aboutItem; NSMenuItem *aboutQtItem; NSMenuItem *hideItem; - NSMenuItem *lastAppSpecificItem; NSMenuItem *servicesItem; NSMenuItem *hideAllOthersItem; NSMenuItem *showAllItem; @@ -118,6 +117,9 @@ [appMenu addItem:[NSMenuItem separatorItem]]; // Preferences + // We'll be adding app specific items after this. The macOS HIG state that, + // "In general, a Preferences menu item should be the first app-specific menu item." + // https://developer.apple.com/macos/human-interface-guidelines/menus/menu-bar-menus/ preferencesItem = [[QCocoaNSMenuItem alloc] init]; preferencesItem.title = @"Preferences…"; preferencesItem.keyEquivalent = @","; @@ -126,11 +128,6 @@ preferencesItem.hidden = YES; [appMenu addItem:preferencesItem]; - // We'll be adding app specific items after this. The macOS HIG state that, - // "In general, a Preferences menu item should be the first app-specific menu item." - // https://developer.apple.com/macos/human-interface-guidelines/menus/menu-bar-menus/ - lastAppSpecificItem = preferencesItem; - [appMenu addItem:[NSMenuItem separatorItem]]; // Services item and menu @@ -194,8 +191,6 @@ [showAllItem release]; [quitItem release]; - [lastAppSpecificItem release]; - [super dealloc]; } @@ -272,25 +267,20 @@ // No reason to create the item if it already exists. for (NSMenuItem *item in appMenu.itemArray) if (qt_objc_cast(item).platformMenuItem == platformItem) - return [[item retain] autorelease]; + return item; // Create an App-Specific menu item, insert it into the menu and return // it as an autorelease item. QCocoaNSMenuItem *item; if (platformItem->isSeparator()) - item = [[QCocoaNSMenuItem separatorItemWithPlatformMenuItem:platformItem] retain]; + item = [QCocoaNSMenuItem separatorItemWithPlatformMenuItem:platformItem]; else - item = [[QCocoaNSMenuItem alloc] initWithPlatformMenuItem:platformItem]; - - const auto location = [appMenu indexOfItem:lastAppSpecificItem]; + item = [[[QCocoaNSMenuItem alloc] initWithPlatformMenuItem:platformItem] autorelease]; - if (!lastAppSpecificItem.separatorItem) - [lastAppSpecificItem release]; - lastAppSpecificItem = item; // Keep track of this for later (i.e., don't release it) + const auto location = [self indexOfLastAppSpecificMenuItem]; + [appMenu insertItem:item atIndex:NSInteger(location) + 1]; - [appMenu insertItem:item atIndex:location + 1]; - - return [[item retain] autorelease]; + return item; } - (void)orderFrontStandardAboutPanel:(id)sender @@ -344,8 +334,24 @@ - (NSArray *)mergeable { // Don't include the quitItem here, since we want it always visible and enabled regardless - // Note that lastAppSpecificItem may be nil, so we can't use @[] here. - return [NSArray arrayWithObjects:preferencesItem, aboutItem, aboutQtItem, lastAppSpecificItem, nil]; + auto items = [NSArray arrayWithObjects:preferencesItem, aboutItem, aboutQtItem, + appMenu.itemArray[[self indexOfLastAppSpecificMenuItem]], nil]; + return items; } +- (NSUInteger)indexOfLastAppSpecificMenuItem +{ + // Either the 'Preferences', which is the first app specific menu item, or something + // else we appended later (thus the reverse order): + const auto location = [appMenu.itemArray indexOfObjectWithOptions:NSEnumerationReverse + passingTest:^BOOL(NSMenuItem *item, NSUInteger, BOOL *) { + if (auto qtItem = qt_objc_cast(item)) + return qtItem != quitItem; + return NO; + }]; + Q_ASSERT(location != NSNotFound); + return location; +} + + @end -- cgit v1.2.3 From 3729695cc9d550e831567772441ad55bd767ab1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Johan=20S=C3=B8rvig?= Date: Fri, 9 Aug 2019 12:25:05 +0200 Subject: =?UTF-8?q?macOS:=20Don=E2=80=99t=20show=20hidden=20windows=20whil?= =?UTF-8?q?e=20z-ordering?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Calling [NSWindow orderBack] will make the window visible again, and will e.g. bring back closed menus on application modality changes. Fixes: QTBUG-77281 Change-Id: I2f89b852ea9f8ab34c709cec96d93fe305984fb9 Reviewed-by: Timur Pocheptsov Reviewed-by: Tor Arne Vestbø --- src/plugins/platforms/cocoa/qcocoawindowmanager.mm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/plugins') diff --git a/src/plugins/platforms/cocoa/qcocoawindowmanager.mm b/src/plugins/platforms/cocoa/qcocoawindowmanager.mm index 879bfaa546..9c45d8c7fc 100644 --- a/src/plugins/platforms/cocoa/qcocoawindowmanager.mm +++ b/src/plugins/platforms/cocoa/qcocoawindowmanager.mm @@ -92,7 +92,8 @@ void QCocoaWindowManager::modalSessionChanged() if (NSApp.modalWindow) { // Lower window to that of the modal windows, but no less nativeWindow.level = NSModalPanelWindowLevel; - [nativeWindow orderBack:nil]; + if ([nativeWindow isVisible]) + [nativeWindow orderBack:nil]; } else { // Restore window's natural window level, whatever that was nativeWindow.level = naturalWindowLevel; -- cgit v1.2.3 From 9bcbba36c73b8d03e8e58898629259489a24e040 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Johan=20S=C3=B8rvig?= Date: Wed, 12 Jun 2019 15:29:31 +0200 Subject: QWizard: Account for missing background image on macOS 10.14+ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We were loading “Background.png” from the KeyboardSetupAssistant app bundle. As of macOS 10.14 that image is no longer there. Adjust auto tests and document the behavior. Change-Id: Icb4dd73b3fa88927e87bb86db2bc9f7b4a8094f7 Reviewed-by: Tor Arne Vestbø --- src/plugins/platforms/cocoa/qcocoanativeinterface.mm | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/plugins') diff --git a/src/plugins/platforms/cocoa/qcocoanativeinterface.mm b/src/plugins/platforms/cocoa/qcocoanativeinterface.mm index 7979e430ac..9bd19dd07c 100644 --- a/src/plugins/platforms/cocoa/qcocoanativeinterface.mm +++ b/src/plugins/platforms/cocoa/qcocoanativeinterface.mm @@ -177,6 +177,9 @@ void *QCocoaNativeInterface::NSPrintInfoForPrintEngine(QPrintEngine *printEngine QPixmap QCocoaNativeInterface::defaultBackgroundPixmapForQWizard() { + // Note: starting with macOS 10.14, the KeyboardSetupAssistant app bundle no + // longer contains the "Background.png" image. This function then returns a + // null pixmap. const int ExpectedImageWidth = 242; const int ExpectedImageHeight = 414; QCFType urls = LSCopyApplicationURLsForBundleIdentifier( -- cgit v1.2.3