From 16bf34140782b27c17013720482ea8877528fcd2 Mon Sep 17 00:00:00 2001 From: reionwong Date: Sun, 30 Aug 2026 05:22:06 -0400 Subject: [PATCH] fix(model): keep the selection while the context menu is open --- model/foldermodel.cpp | 10 ++++++++++ model/foldermodel.h | 8 ++++++++ 2 files changed, 18 insertions(+) diff --git a/model/foldermodel.cpp b/model/foldermodel.cpp index 6be2fdc..e07af5f 100644 --- a/model/foldermodel.cpp +++ b/model/foldermodel.cpp @@ -858,6 +858,13 @@ void FolderModel::updateSelection(const QVariantList &rows, bool toggle) void FolderModel::clearSelection() { + // Keep the selection while our own context menu is open: the menu is a + // separate window, so showing it makes the view lose focus, and the desktop + // clears the selection on focus loss. Without this the menu would be built + // from the selection and then act on an empty one. + if (m_contextMenu) + return; + if (m_selectionModel->hasSelection()) m_selectionModel->clear(); } @@ -1335,7 +1342,10 @@ void FolderModel::openContextMenu(QQuickItem *visualParent, Qt::KeyboardModifier menu->installEventFilter(this); menu->setAttribute(Qt::WA_TranslucentBackground); menu->winId(); + + m_contextMenu = menu; menu->popup(position); + connect(menu, &QMenu::aboutToHide, [menu]() { menu->deleteLater(); }); diff --git a/model/foldermodel.h b/model/foldermodel.h index a4fb1af..a1ee61b 100644 --- a/model/foldermodel.h +++ b/model/foldermodel.h @@ -41,6 +41,8 @@ #include #include +class QMenu; + class QDrag; class CFileSizeJob; class FolderModel : public QSortFilterProxyModel, public QQmlParserStatus @@ -309,6 +311,12 @@ private: QString m_selectedItemSize; + // The context menu is a window of its own, so opening it takes the focus + // away from the view. The desktop reacts to losing focus by clearing the + // selection, which would leave every menu entry acting on nothing, so the + // selection is held for as long as this menu is up. + QPointer m_contextMenu; + KActionCollection m_actionCollection; QHash m_dragImages; QModelIndexList m_dragIndexes;