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
qquickfluentwinui3focusframe.cpp
Go to the documentation of this file.
1// Copyright (C) 2024 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 <private/qquickitem_p.h>
8
9#include <QtCore/qmetaobject.h>
10
11#include <QtGui/qguiapplication.h>
12
13#include <QtQml/qqmlengine.h>
14#include <QtQml/qqmlcontext.h>
15#include <QtQml/qqmlcomponent.h>
16
17
19
20QScopedPointer<QQuickItem> QQuickFluentWinUI3FocusFrame::m_focusFrame;
21
22QQuickFluentWinUI3FocusFrame::QQuickFluentWinUI3FocusFrame()
23{
24 connect(qGuiApp, &QGuiApplication::focusObjectChanged, this, [this](QObject *focusObject){
25 if (QQuickControl *control = qobject_cast<QQuickControl *>(focusObject);
26 control && (control->focusReason() == Qt::FocusReason::TabFocusReason
27 || control->focusReason() == Qt::FocusReason::BacktabFocusReason
28 || control->focusReason() == Qt::FocusReason::OtherFocusReason)) {
29 moveToItem(control);
30 } else {
31 moveToItem(nullptr);
32 }
33 });
34}
35
36QQuickItem *QQuickFluentWinUI3FocusFrame::createFocusFrame(QQmlContext *context)
37{
38 QQmlComponent component(context->engine(), "QtQuick.Controls.FluentWinUI3.impl", "FocusFrame");
39 auto frame = qobject_cast<QQuickItem *>(component.create());
40 if (!frame)
41 return nullptr;
42 return frame;
43}
44
45void QQuickFluentWinUI3FocusFrame::moveToItem(QQuickControl *item)
46{
47 if (!m_focusFrame) {
48 const auto context = QQmlEngine::contextForObject(item);
49 // In certain cases like QQuickWebEngineView, the item
50 // gets focus even though it has no QQmlEngine associated with its context.
51 // We need the engine for creating the focus frame component.
52 if (!context || !context->engine())
53 return;
54 m_focusFrame.reset(createFocusFrame(context));
55 if (!m_focusFrame) {
56 qWarning() << "Failed to create FocusFrame";
57 return;
58 }
59 QQuickItemPrivate::get(m_focusFrame.get())->setTransparentForPositioner(true);
60 }
61
62 const auto target = getFocusTarget(item);
63 QMetaObject::invokeMethod(m_focusFrame.data(), "moveToItem",
64 Q_ARG(QVariant, QVariant::fromValue(target)));
65}
66
67QQuickControl *QQuickFluentWinUI3FocusFrame::getFocusTarget(QQuickControl *focusItem) const
68{
69 if (!focusItem)
70 return nullptr;
71
72 const auto parentItem = focusItem->parentItem();
73 if (!parentItem)
74 return nullptr;
75
76 // The control that gets active focus can be a child of the control (e.g
77 // editable ComboBox). In that case, resolve the actual control first.
78 const auto proxy = focusItem->property("__focusFrameControl").value<QQuickControl *>();
79 const auto control = proxy ? proxy : focusItem;
80 auto target = control->property("__focusFrameTarget").value<QQuickControl *>();
81
82 return target;
83}
84
85QT_END_NAMESPACE
86
87#include "moc_qquickfluentwinui3focusframe_p.cpp"