From f27d1ccbb24ec2fd4098f2976503478831006cc8 Mon Sep 17 00:00:00 2001 From: Gabriel de Dietrich Date: Wed, 9 Aug 2017 11:10:40 +0700 Subject: QCocoaMenu: De-pessimize the number of calls to validateMenuItem: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Calling -[NSMenu update] every time we add a new item can result in a quadratic behavior since the function itself will iterate over all the items in the menu. We solve this by using a 0-timer which will trigger the call to update the next time the event loop spins. Menurama manual test updated. Change-Id: Ic155d364515cc93eb81b1c8085c8e44c93799954 Task-number: QTBUG-62396 Reviewed-by: Tor Arne Vestbø --- src/plugins/platforms/cocoa/qcocoamenu.mm | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'src/plugins/platforms/cocoa/qcocoamenu.mm') diff --git a/src/plugins/platforms/cocoa/qcocoamenu.mm b/src/plugins/platforms/cocoa/qcocoamenu.mm index 8e47974d12..e5681c0894 100644 --- a/src/plugins/platforms/cocoa/qcocoamenu.mm +++ b/src/plugins/platforms/cocoa/qcocoamenu.mm @@ -260,6 +260,7 @@ QT_BEGIN_NAMESPACE QCocoaMenu::QCocoaMenu() : m_attachedItem(0), m_tag(0), + m_updateTimer(0), m_enabled(true), m_parentEnabled(true), m_visible(true), @@ -410,6 +411,20 @@ QCocoaMenuItem *QCocoaMenu::itemOrNull(int index) const return m_menuItems.at(index); } +void QCocoaMenu::scheduleUpdate() +{ + if (!m_updateTimer) + m_updateTimer = startTimer(0); +} + +void QCocoaMenu::timerEvent(QTimerEvent *e) +{ + if (e->timerId() == m_updateTimer) { + m_updateTimer = 0; + [m_nativeMenu update]; + } +} + void QCocoaMenu::syncMenuItem(QPlatformMenuItem *menuItem) { QMacAutoReleasePool pool; @@ -436,9 +451,9 @@ void QCocoaMenu::syncMenuItem(QPlatformMenuItem *menuItem) QCocoaMenuItem* beforeItem = itemOrNull(m_menuItems.indexOf(cocoaItem) + 1); insertNative(cocoaItem, beforeItem); } else { - // Force NSMenuValidation to kick in. This is needed e.g. + // Schedule NSMenuValidation to kick in. This is needed e.g. // when an item's enabled state changes after menuWillOpen: - [m_nativeMenu update]; + scheduleUpdate(); } } -- cgit v1.2.3