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