summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/cocoa/qcocoamenuitem.mm
diff options
context:
space:
mode:
authorGabriel de Dietrich <gabriel.dedietrich@qt.io>2016-07-15 11:51:36 -0700
committerTimur Pocheptsov <timur.pocheptsov@theqtcompany.com>2016-08-17 18:21:53 +0000
commit9b491616f8353be243c5e5e98c864d09a25b99ea (patch)
tree56da56c45fa421060c2c687d7e0c67554a1a78aa /src/plugins/platforms/cocoa/qcocoamenuitem.mm
parent810363945f0038ad40a48e1c5a86e3451a028822 (diff)
Cocoa QPA Menus: Propagate enabled state downwards
NSMenu has autoenableItems set to true by default, and we keep it this way in Qt. This means that NSMenuItem's enabled property is basically ignored and therefore QCocoaMenuItem::syncModalState() is wrong. What is also wrong, is syncModalState()'s name in both QCocoaMenuItem and QCocoaMenu. Indeed, this function's role should be to ensure that the enabled state is properly propagated down the menu hierarchy, whether the reason is being in the context of a modal dialog or the parent menu having been disabled by the app. Notice that the latter case is specially needed when a menubar menu is explicitly disabled. Therefore, we introduce a separate flag for the parent enabled state in order to avoid polluting the app-set enabled state flag. This is done in both QCocoaMenu and QCocoaMenuItem. In the case of QCocoaMenuItem, these two flags define whether an NSMenuItem is enabled state conjointly, and set from -[QCocoaMenuDelegate validateMenuItem:]. The rest of the logic remains as before. Similar logic is used in QCocoaMenu::isEnabled(). In addition, the presence of the second flag allows us to show disabled submenus in the same fashion native Cocoa applications do. This means, the submenu item itself remains enabled, allowing to show the submenu popup where all its menu items will appear disabled. Bonus change: merged all the bool flags into a bitfield and made the compiler happy about the ivar reordering in QCocoaMenu and QCocoaMenuItem's constructor. Task-number: QTBUG-54698 Task-number: QTBUG-55121 Change-Id: Ie156cb3aa57a519103908ad4605f7b43c57e5aef Reviewed-by: Timur Pocheptsov <timur.pocheptsov@theqtcompany.com>
Diffstat (limited to 'src/plugins/platforms/cocoa/qcocoamenuitem.mm')
-rw-r--r--src/plugins/platforms/cocoa/qcocoamenuitem.mm42
1 files changed, 28 insertions, 14 deletions
diff --git a/src/plugins/platforms/cocoa/qcocoamenuitem.mm b/src/plugins/platforms/cocoa/qcocoamenuitem.mm
index 49f3da48c2..2d4073956a 100644
--- a/src/plugins/platforms/cocoa/qcocoamenuitem.mm
+++ b/src/plugins/platforms/cocoa/qcocoamenuitem.mm
@@ -88,16 +88,17 @@ NSUInteger keySequenceModifierMask(const QKeySequence &accel)
QCocoaMenuItem::QCocoaMenuItem() :
m_native(NULL),
m_itemView(nil),
- m_textSynced(false),
m_menu(NULL),
+ m_role(NoRole),
+ m_tag(0),
+ m_iconSize(16),
+ m_textSynced(false),
m_isVisible(true),
m_enabled(true),
+ m_parentEnabled(true),
m_isSeparator(false),
- m_role(NoRole),
m_checked(false),
- m_merged(false),
- m_tag(0),
- m_iconSize(16)
+ m_merged(false)
{
}
@@ -133,15 +134,23 @@ void QCocoaMenuItem::setMenu(QPlatformMenu *menu)
if (menu == m_menu)
return;
- if (m_menu) {
- if (m_menu->menuParent() == this)
- m_menu->setMenuParent(0);
+ if (m_menu && m_menu->menuParent() == this) {
+ m_menu->setMenuParent(0);
+ // Free the menu from its parent's influence
+ m_menu->propagateEnabledState(true);
+ if (m_native && m_menu->attachedItem() == m_native)
+ m_menu->setAttachedItem(nil);
}
QMacAutoReleasePool pool;
m_menu = static_cast<QCocoaMenu *>(menu);
if (m_menu) {
+ if (m_native) {
+ // Skip automatic menu item validation
+ m_native.action = nil;
+ }
m_menu->setMenuParent(this);
+ m_menu->propagateEnabledState(isEnabled());
} else {
// we previously had a menu, but no longer
// clear out our item so the nexy sync() call builds a new one
@@ -184,7 +193,11 @@ void QCocoaMenuItem::setChecked(bool isChecked)
void QCocoaMenuItem::setEnabled(bool enabled)
{
- m_enabled = enabled;
+ if (m_enabled != enabled) {
+ m_enabled = enabled;
+ if (m_menu)
+ m_menu->propagateEnabledState(isEnabled());
+ }
}
void QCocoaMenuItem::setNativeContents(WId item)
@@ -392,12 +405,13 @@ void QCocoaMenuItem::syncMerged()
[m_native setHidden: !m_isVisible];
}
-void QCocoaMenuItem::syncModalState(bool modal)
+void QCocoaMenuItem::setParentEnabled(bool enabled)
{
- if (modal)
- [m_native setEnabled:NO];
- else
- [m_native setEnabled:YES];
+ if (m_parentEnabled != enabled) {
+ m_parentEnabled = enabled;
+ if (m_menu)
+ m_menu->propagateEnabledState(isEnabled());
+ }
}
QPlatformMenuItem::MenuRole QCocoaMenuItem::effectiveRole() const