Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquicknativemenuitem.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6
7#include <QtCore/qloggingcategory.h>
8//#include <QtGui/qicon.h>
9//#if QT_CONFIG(shortcut)
10//#include <QtGui/qkeysequence.h>
11//#endif
12#include <QtGui/qpa/qplatformmenu.h>
13#include <QtGui/qpa/qplatformtheme.h>
14#include <QtGui/private/qguiapplication_p.h>
15#include <QtQml/qqml.h>
16//#include <QtQuickTemplates2/private/qquickshortcutcontext_p_p.h>
17#include <QtQuickTemplates2/private/qquickabstractbutton_p_p.h>
18#include <QtQuickTemplates2/private/qquickaction_p.h>
19#include <QtQuickTemplates2/private/qquickmenu_p_p.h>
20#include <QtQuickTemplates2/private/qquickmenuseparator_p.h>
21#include <QtQuickTemplates2/private/qquicknativeiconloader_p.h>
22#include <QtQuickTemplates2/private/qquicknativemenuitem_p.h>
23#include <QtQuickTemplates2/private/qquickshortcutcontext_p_p.h>
24
26
27Q_STATIC_LOGGING_CATEGORY(lcNativeMenuItem, "qt.quick.controls.nativemenuitem")
28
29/*!
30 \class QQuickNativeMenuItem
31 \brief A native menu item.
32 \since 6.7
33 \internal
34
35 Creates a native menu item from an Action/MenuItem/Menu,
36 and syncs the properties and signals. It can also represent a
37 MenuSeparator.
38
39 \sa Menu, Action
40*/
41
42QQuickNativeMenuItem *QQuickNativeMenuItem::createFromNonNativeItem(
43 QQuickMenu *parentMenu, QQuickItem *nonNativeItem)
44{
45 auto *menuItem = qobject_cast<QQuickMenuItem *>(nonNativeItem);
46 Type type = Type::Unknown;
47 if (menuItem) {
48 if (menuItem->action()) {
49 type = Type::Action;
50 } else if (menuItem->subMenu()) {
51 type = Type::SubMenu;
52 } else {
53 // It's a plain MenuItem, rather than a MenuItem created by us for an Action or Menu.
54 type = Type::MenuItem;
55 }
56 } else if (qobject_cast<QQuickMenuSeparator *>(nonNativeItem)) {
57 type = Type::Separator;
58 }
59
60 std::unique_ptr<QQuickNativeMenuItem> nativeMenuItemPtr(new QQuickNativeMenuItem(
61 parentMenu, nonNativeItem, type));
62 if (type == Type::Unknown) {
63 // It's not a Menu/Action/MenuSeparator that we're dealing with, but we still need
64 // to create the QQuickNativeMenu item for it so that our container has the same
65 // indices as the menu's contentModel.
66 return nativeMenuItemPtr.release();
67 }
68
69 qCDebug(lcNativeMenuItem) << "attemping to create native menu item for"
70 << nativeMenuItemPtr->debugText();
71 auto *parentMenuPrivate = QQuickMenuPrivate::get(parentMenu);
72 nativeMenuItemPtr->m_handle.reset(parentMenuPrivate->handle->createMenuItem());
73 if (!nativeMenuItemPtr->m_handle)
74 nativeMenuItemPtr->m_handle.reset(QGuiApplicationPrivate::platformTheme()->createPlatformMenuItem());
75 if (!nativeMenuItemPtr->m_handle)
76 return nullptr;
77
78 auto *nativeMenuItem = nativeMenuItemPtr.release();
79 switch (type) {
80 case Type::Action:
81 // Ensure that the action is triggered when the user clicks on a native menu item.
82 connect(nativeMenuItem->m_handle.get(), &QPlatformMenuItem::activated,
83 nativeMenuItem->action(), [nativeMenuItem, parentMenu](){
84 nativeMenuItem->action()->trigger(parentMenu);
85 });
86 // Handle programmatic changes in the Action.
87 connect(nativeMenuItem->action(), &QQuickAction::textChanged, nativeMenuItem, &QQuickNativeMenuItem::sync);
88 connect(nativeMenuItem->action(), &QQuickAction::iconChanged, nativeMenuItem, &QQuickNativeMenuItem::sync);
89 connect(nativeMenuItem->action(), &QQuickAction::enabledChanged, nativeMenuItem, &QQuickNativeMenuItem::sync);
90 connect(nativeMenuItem->action(), &QQuickAction::checkedChanged, nativeMenuItem, &QQuickNativeMenuItem::sync);
91 connect(nativeMenuItem->action(), &QQuickAction::checkableChanged, nativeMenuItem, &QQuickNativeMenuItem::sync);
92 break;
93 case Type::SubMenu:
94 nativeMenuItem->m_handle->setMenu(QQuickMenuPrivate::get(
95 nativeMenuItem->subMenu())->handle.get());
96
97 // Handle programmatic changes in the Menu.
98 connect(nativeMenuItem->subMenu(), &QQuickMenu::enabledChanged, nativeMenuItem, &QQuickNativeMenuItem::sync);
99 connect(nativeMenuItem->subMenu(), &QQuickMenu::titleChanged, nativeMenuItem, &QQuickNativeMenuItem::sync);
100 break;
101 case Type::MenuItem:
102 // Ensure that the MenuItem is clicked when the user clicks on a native menu item.
103 connect(nativeMenuItem->m_handle.get(), &QPlatformMenuItem::activated,
104 menuItem, [menuItem](){
105 if (menuItem->isCheckable()) {
106 // This changes the checked state, which we need when syncing but also to ensure that
107 // the user can still use MenuItem's API even though they can't actually interact with it.
108 menuItem->toggle();
109 }
110 // The same applies here: allow users to respond to the MenuItem's clicked signal.
111 QQuickAbstractButtonPrivate::get(menuItem)->click();
112 });
113 // Handle programmatic changes in the MenuItem.
114 connect(menuItem, &QQuickMenuItem::textChanged, nativeMenuItem, &QQuickNativeMenuItem::sync);
115 connect(menuItem, &QQuickMenuItem::iconChanged, nativeMenuItem, &QQuickNativeMenuItem::sync);
116 connect(menuItem, &QQuickMenuItem::enabledChanged, nativeMenuItem, &QQuickNativeMenuItem::sync);
117 connect(menuItem, &QQuickMenuItem::checkedChanged, nativeMenuItem, &QQuickNativeMenuItem::sync);
118 connect(menuItem, &QQuickMenuItem::checkableChanged, nativeMenuItem, &QQuickNativeMenuItem::sync);
119 break;
120 case Type::Separator:
121 case Type::Unknown:
122 break;
123 }
124
125 return nativeMenuItem;
126}
127
128QQuickNativeMenuItem::QQuickNativeMenuItem(QQuickMenu *parentMenu, QQuickItem *nonNativeItem,
129 QQuickNativeMenuItem::Type type)
130 : QObject(parentMenu)
131 , m_parentMenu(parentMenu)
132 , m_nonNativeItem(nonNativeItem)
133 , m_type(type)
134{
135}
136
137QQuickNativeMenuItem::~QQuickNativeMenuItem()
138{
139 qCDebug(lcNativeMenuItem) << "destroying" << this << debugText();
140}
141
142QQuickAction *QQuickNativeMenuItem::action() const
143{
144 return m_type == Type::Action ? qobject_cast<QQuickMenuItem *>(m_nonNativeItem)->action() : nullptr;
145}
146
147QQuickMenu *QQuickNativeMenuItem::subMenu() const
148{
149 return m_type == Type::SubMenu ? qobject_cast<QQuickMenuItem *>(m_nonNativeItem)->subMenu() : nullptr;
150}
151
152QQuickMenuSeparator *QQuickNativeMenuItem::separator() const
153{
154 return m_type == Type::Separator ? qobject_cast<QQuickMenuSeparator *>(m_nonNativeItem) : nullptr;
155}
156
157QPlatformMenuItem *QQuickNativeMenuItem::handle() const
158{
159 return m_handle.get();
160}
161
162void QQuickNativeMenuItem::sync()
163{
164 if (m_type == Type::Unknown)
165 return;
166
167 if (m_syncing)
168 return;
169
170 QScopedValueRollback recursionGuard(m_syncing, true);
171
172 const auto *action = this->action();
173 const auto *separator = this->separator();
174 auto *subMenu = this->subMenu();
175 auto *menuItem = qobject_cast<QQuickMenuItem *>(m_nonNativeItem);
176
177 // Store the values in variables so that we can use it in the debug output below.
178 const bool enabled = action ? action->isEnabled()
179 : subMenu ? subMenu->isEnabled()
180 : menuItem && menuItem->isEnabled();
181 m_handle->setEnabled(enabled);
182// m_handle->setVisible(isVisible());
183
184 const bool isSeparator = separator != nullptr;
185 m_handle->setIsSeparator(isSeparator);
186
187 const bool checkable = action ? action->isCheckable() : menuItem && menuItem->isCheckable();
188 m_handle->setCheckable(checkable);
189
190 const bool checked = action ? action->isChecked() : menuItem && menuItem->isChecked();
191 m_handle->setChecked(checked);
192
193 m_handle->setRole(QPlatformMenuItem::TextHeuristicRole);
194
195 const QString text = action ? action->text()
196 : subMenu ? subMenu->title()
197 : menuItem ? menuItem->text() : QString();
198 m_handle->setText(text);
199
200// m_handle->setFont(m_font);
201// m_handle->setHasExclusiveGroup(m_group && m_group->isExclusive());
202 m_handle->setHasExclusiveGroup(false);
203
204 const QQuickIcon icon = effectiveIcon();
205 const auto *menuPrivate = QQuickMenuPrivate::get(m_parentMenu);
206
207 // We should reload the icon if the window's DPR has changed, regardless if its properties have changed.
208 // We can't check for ItemDevicePixelRatioHasChanged in QQuickMenu::itemChange,
209 // because that isn't sent when the menu isn't visible, and will never
210 // be sent for native menus. We instead store lastDevicePixelRatio in QQuickMenu
211 // (to avoid storing it for each menu item) and set it whenever it's opened.
212 bool dprChanged = false;
213 if (!qGuiApp->topLevelWindows().isEmpty()) {
214 const auto *window = qGuiApp->topLevelWindows().first();
215 dprChanged = !qFuzzyCompare(window->devicePixelRatio(), menuPrivate->lastDevicePixelRatio);
216 }
217
218 if (!icon.isEmpty() && (icon != iconLoader()->icon() || dprChanged)) {
219 // This will load the icon, which will call sync() recursively, hence the m_syncing check.
220 reloadIcon();
221 }
222
223 if (subMenu) {
224 // Sync first as dynamically created menus may need to get the handle recreated.
225 auto *subMenuPrivate = QQuickMenuPrivate::get(subMenu);
226 subMenuPrivate->syncWithNativeMenu();
227 if (subMenuPrivate->handle)
228 m_handle->setMenu(subMenuPrivate->handle.get());
229 }
230
231#if QT_CONFIG(shortcut)
232 if (action)
233 m_handle->setShortcut(action->shortcut());
234#endif
235
236 if (m_parentMenu) {
237 auto *menuPrivate = QQuickMenuPrivate::get(m_parentMenu);
238 if (menuPrivate->handle)
239 menuPrivate->handle->syncMenuItem(m_handle.get());
240 }
241
242 qCDebug(lcNativeMenuItem) << "sync called on" << debugText() << "handle" << m_handle.get()
243 << "enabled:" << enabled << "isSeparator" << isSeparator << "checkable" << checkable
244 << "checked" << checked << "text" << text;
245}
246
247QQuickIcon QQuickNativeMenuItem::effectiveIcon() const
248{
249 if (const auto *action = this->action())
250 return action->icon();
251 if (const auto *subMenu = this->subMenu())
252 return subMenu->icon();
253 if (const auto *menuItem = qobject_cast<QQuickMenuItem *>(m_nonNativeItem))
254 return menuItem->icon();
255 return {};
256}
257
258QQuickNativeIconLoader *QQuickNativeMenuItem::iconLoader() const
259{
260 if (!m_iconLoader) {
261 QQuickNativeMenuItem *that = const_cast<QQuickNativeMenuItem *>(this);
262 static int slot = staticMetaObject.indexOfSlot("updateIcon()");
263 // We're created from C++, without a QML context, so look up
264 // the engine (needed to load icons) via the parent menu.
265 m_iconLoader = new QQuickNativeIconLoader(slot, that, qmlEngine(m_parentMenu));
266 // Qt Labs Platform's QQuickMenuItem would call m_iconLoader->setEnabled(m_complete) here,
267 // but since QQuickMenuPrivate::maybeCreateAndInsertNativeItem asserts that the menu's
268 // completed loading, we can just set it to true.
269 m_iconLoader->setEnabled(true);
270 }
271 return m_iconLoader;
272}
273
274void QQuickNativeMenuItem::reloadIcon()
275{
276 iconLoader()->setIcon(effectiveIcon());
277 m_handle->setIcon(iconLoader()->toQIcon());
278}
279
280void QQuickNativeMenuItem::updateIcon()
281{
282 sync();
283}
284
285void QQuickNativeMenuItem::addShortcut()
286{
287#if QT_CONFIG(shortcut)
288 const auto *action = this->action();
289 const QKeySequence sequence = action ? action->shortcut() : QKeySequence();
290 if (!sequence.isEmpty() && action->isEnabled()) {
291 m_shortcutId = QGuiApplicationPrivate::instance()->shortcutMap.addShortcut(this, sequence,
292 Qt::WindowShortcut, QQuickShortcutContext::matcher);
293 } else {
294 m_shortcutId = -1;
295 }
296#endif
297}
298
299void QQuickNativeMenuItem::removeShortcut()
300{
301#if QT_CONFIG(shortcut)
302 if (m_shortcutId == -1)
303 return;
304
305 QKeySequence sequence;
306 switch (m_type) {
307 case Type::Action:
308 sequence = action()->shortcut();
309 break;
310 default:
311 // TODO
312 break;
313 }
314
315 QGuiApplicationPrivate::instance()->shortcutMap.removeShortcut(m_shortcutId, this, sequence);
316#endif
317}
318
319QString QQuickNativeMenuItem::debugText() const
320{
321 // Prepend below to avoid extra lines from break statements.
322 QString text = QDebug::toString(m_handle.get());
323
324 switch (m_type) {
325 case Type::Action:
326 return text.prepend(QString::fromLatin1("Action(text = %1) ").arg(action()->text()));
327 case Type::SubMenu:
328 return text.prepend(QString::fromLatin1("Sub-menu(title = %1) ").arg(subMenu()->title()));
329 case Type::MenuItem:
330 return text.prepend(QString::fromLatin1("MenuItem(text = %1) ").arg(
331 qobject_cast<QQuickMenuItem *>(m_nonNativeItem)->text()));
332 case Type::Separator:
333 return text.prepend(QStringLiteral("Separator "));
334 case Type::Unknown:
335 return text.prepend(QStringLiteral("(Unknown) "));
336 }
337
338 Q_UNREACHABLE();
339}
340
341QT_END_NAMESPACE
342
343#include "moc_qquicknativemenuitem_p.cpp"
Combined button and popup list for selecting options.