Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions qt6/src/qml/Popup.qml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ T.Popup {
id: control

palette: D.DTK.palette
focus: popupType === Popup.Window

property bool closeOnInactive: true
readonly property bool active: parent && parent.Window.active
Expand Down
64 changes: 63 additions & 1 deletion src/private/dpopupwindowhandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
#include <QGuiApplication>
#include <QDebug>
#include <QMouseEvent>
#include <algorithm>

Check warning on line 15 in src/private/dpopupwindowhandle.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <algorithm> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <private/qquickitem_p.h>

Check warning on line 17 in src/private/dpopupwindowhandle.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <private/qquickitem_p.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <qpa/qwindowsysteminterface.h>

Check warning on line 18 in src/private/dpopupwindowhandle.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <qpa/qwindowsysteminterface.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

DQUICK_BEGIN_NAMESPACE

Expand Down Expand Up @@ -52,6 +53,7 @@
popupItemReparented();

connect(popup, SIGNAL(popupTypeChanged()), this, SLOT(updateEnabled()));
connect(popup, SIGNAL(visibleChanged()), this, SLOT(onPopupVisibleChanged()));
}

DQuickWindowAttached *DPopupWindowHandle::qmlAttachedProperties(QObject *object)
Expand Down Expand Up @@ -124,6 +126,9 @@

void DPopupWindowHandle::onWindowChanged(QQuickWindow *window)
{
if (!window)
restoreParentFocus();

// Cleanup old filters
if (m_popupWin) {
m_popupWin->removeEventFilter(this);
Expand Down Expand Up @@ -154,12 +159,24 @@
}
}

void DPopupWindowHandle::onPopupVisibleChanged()
{
if (!m_popup || m_popup->property("visible").toBool())
return;

restoreParentFocus();
}

bool DPopupWindowHandle::eventFilter(QObject *watched, QEvent *event)
{
if (watched == m_popupWin && event->type() == QEvent::Move) {
adjustPopupPosition();
}

if (watched == m_popupWin && event->type() == QEvent::Show) {
requestPopupFocus();
}

// Close popup on parent window click (only while popup is visible)
if (watched == m_parentWindow && event->type() == QEvent::MouseButtonPress
&& m_popup->property("visible").toBool()) {
Expand Down Expand Up @@ -280,7 +297,52 @@
m_popupWin->setPosition(newPos);
}

void DPopupWindowHandle::requestPopupFocus()
{
if (!m_popup || !isEnabled() || !m_popupWin || !m_popupWin->isVisible())
return;
if (!m_popup->property("focus").toBool())
return;

m_restoreFocusItem = m_parentWindow ? m_parentWindow->activeFocusItem() : nullptr;

QMetaObject::invokeMethod(this, [this] {
if (!isEnabled() || !m_popup->property("focus").toBool() || !m_popupWin || !m_popupWin->isVisible())
return;

QPointer<QQuickItem> item = popupItem();
if (!item || item->window() != m_popupWin)
return;

m_popupFocusRequested = true;
m_popupWin->requestActivate();
}, Qt::QueuedConnection);
}
Comment thread
sourcery-ai[bot] marked this conversation as resolved.

void DPopupWindowHandle::restoreParentFocus()
{
if (!m_popupFocusRequested)
return;

m_popupFocusRequested = false;

if (!m_popupWin || !m_parentWindow || !m_parentWindow->isVisible()) {
m_restoreFocusItem.clear();
return;
}

m_parentWindow->requestActivate();
if (QGuiApplication::focusWindow() == m_popupWin)
QWindowSystemInterface::handleFocusWindowChanged(m_parentWindow, Qt::PopupFocusReason);

if (m_restoreFocusItem && m_restoreFocusItem->window() == m_parentWindow
&& m_restoreFocusItem->isVisible() && m_restoreFocusItem->isEnabled()) {
m_restoreFocusItem->forceActiveFocus(Qt::OtherFocusReason);
}

m_restoreFocusItem.clear();
}

DQUICK_END_NAMESPACE

#include "moc_dpopupwindowhandle_p.cpp"

Check warning on line 348 in src/private/dpopupwindowhandle.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "moc_dpopupwindowhandle_p.cpp" not found.

5 changes: 5 additions & 0 deletions src/private/dpopupwindowhandle_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class DPopupWindowHandle : public QObject, public QQuickItemChangeListener
private Q_SLOTS:
void updateEnabled();
void onWindowChanged(QQuickWindow *window);
void onPopupVisibleChanged();

private:
QQuickWindow *popupWindow() const;
Expand All @@ -59,6 +60,8 @@ private Q_SLOTS:

bool isEnabled() const;
void adjustPopupPosition();
void requestPopupFocus();
void restoreParentFocus();

private:
QObject *m_popup = nullptr;
Expand All @@ -68,6 +71,8 @@ private Q_SLOTS:
QPointer<QQuickWindow> m_parentWindow = nullptr;
QPointer<QQuickWindow> m_popupWin = nullptr;
QPointer<QQuickItem> m_trackedItem = nullptr;
QPointer<QQuickItem> m_restoreFocusItem = nullptr;
bool m_popupFocusRequested = false;
};

DQUICK_END_NAMESPACE
Expand Down
Loading