Cocoa: Allow to hide menu items in menubar

Task-number: QTBUG-32899

Change-Id: I423ac2d636306303d39e973f19032c9004957095
Reviewed-by: Jens Bache-Wiig <jens.bache-wiig@digia.com>
This commit is contained in:
Gabriel de Dietrich 2013-08-09 14:09:06 +02:00 committed by The Qt Project
parent 1dcdc506f3
commit 8c3b31182c
3 changed files with 15 additions and 8 deletions

View File

@ -84,6 +84,8 @@ public:
inline NSMenuItem *nsMenuItem() const
{ return m_nativeItem; }
inline bool isVisible() const { return m_visible; }
virtual QPlatformMenuItem *menuItemAt(int position) const;
virtual QPlatformMenuItem *menuItemForTag(quintptr tag) const;
@ -100,6 +102,7 @@ private:
NSMenuItem *m_nativeItem;
NSObject *m_delegate;
bool m_enabled;
bool m_visible;
quintptr m_tag;
QCocoaMenuBar *m_menuBar;
};

View File

@ -216,6 +216,7 @@ QT_BEGIN_NAMESPACE
QCocoaMenu::QCocoaMenu() :
m_enabled(true),
m_visible(true),
m_tag(0),
m_menuBar(0)
{
@ -421,6 +422,7 @@ void QCocoaMenu::setEnabled(bool enabled)
void QCocoaMenu::setVisible(bool visible)
{
[m_nativeItem setSubmenu:(visible ? m_nativeMenu : nil)];
m_visible = visible;
}
void QCocoaMenu::showPopup(const QWindow *parentWindow, QPoint pos, const QPlatformMenuItem *item)

View File

@ -149,15 +149,17 @@ void QCocoaMenuBar::syncMenu(QPlatformMenu *menu)
Q_FOREACH (QCocoaMenuItem *item, cocoaMenu->items())
cocoaMenu->syncMenuItem(item);
// If the NSMenu has no visble items, or only separators, we should hide it
// on the menubar. This can happen after syncing the menu items since they
// can be moved to other menus.
BOOL shouldHide = YES;
for (NSMenuItem *item in [cocoaMenu->nsMenu() itemArray])
if (![item isSeparatorItem] && ![item isHidden]) {
shouldHide = NO;
break;
}
if (cocoaMenu->isVisible()) {
// If the NSMenu has no visble items, or only separators, we should hide it
// on the menubar. This can happen after syncing the menu items since they
// can be moved to other menus.
for (NSMenuItem *item in [cocoaMenu->nsMenu() itemArray])
if (![item isSeparatorItem] && ![item isHidden]) {
shouldHide = NO;
break;
}
}
[cocoaMenu->nsMenuItem() setHidden:shouldHide];
}