Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix/UX) Track menu: Left key in Search related & Crates menus closes them again #13602

Open
wants to merge 1 commit into
base: 2.5
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion src/widget/wsearchrelatedtracksmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ void WSearchRelatedTracksMenu::addTriggerSearchAction(
auto pAction = make_parented<QWidgetAction>(this);
pAction->setDefaultWidget(pCheckBox.get());
// While the checkbox is selected (via keyboard, not hovered by pointer)
// pressing Space will toggle it whereas pressing Return triggers the action.
// pressing Space will toggle it whereas pressing Return triggers the action
// and closes the menu (see WTrackMenu).
connect(pAction.get(),
&QAction::triggered,
this,
Expand Down
32 changes: 32 additions & 0 deletions src/widget/wtrackmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <QCheckBox>
#include <QDialogButtonBox>
#include <QInputDialog>
#include <QKeyEvent>
#include <QList>
#include <QListWidget>
#include <QModelIndex>
Expand Down Expand Up @@ -152,6 +153,25 @@
emit trackMenuVisible(false);
}

bool WTrackMenu::eventFilter(QObject* pObj, QEvent* e) {
// If a checkbox in a QWidgetAction is focused, Left/Right keys are translated
// to Up/Down which prevents closing the submenus with Left key like in other
// submenus. We simply call hide() of the submenu if Left is pressed.
if (e->type() == QEvent::KeyPress) {
QCheckBox* cbx = qobject_cast<QCheckBox*>(pObj);
if (cbx) {
// Note: if we ever add checkboxes to the top level we need to
// assert that cbx->parentWidget() != this.
QKeyEvent* pKe = static_cast<QKeyEvent*>(e);
if (pKe && pKe->key() == Qt::Key_Left) {
cbx->parentWidget()->hide();
return true;
}
}
}
return QObject::eventFilter(pObj, e);
}

void WTrackMenu::popup(const QPoint& pos, QAction* at) {
if (isEmpty()) {
return;
Expand Down Expand Up @@ -236,6 +256,16 @@
}
m_pSearchRelatedMenu->setEnabled(
!m_pSearchRelatedMenu->isEmpty());
if (!m_pSearchRelatedMenu->isEmpty()) {
// We're interested in keypress Qt::Key_Left, so use our
// event filter like we do for the crate checkboxes.
for (const auto ch : m_pSearchRelatedMenu->children()) {

Check warning on line 262 in src/widget/wtrackmenu.cpp

View workflow job for this annotation

GitHub Actions / clang-tidy

'const auto ch' can be declared as 'auto *const ch' [readability-qualified-auto]
QCheckBox* cb = qobject_cast<QCheckBox*>(ch);
if (cb) {
cb->installEventFilter(this);
}
}
}
});
connect(m_pSearchRelatedMenu,
&WSearchRelatedTracksMenu::triggerSearch,
Expand Down Expand Up @@ -1499,6 +1529,8 @@
m_pCrateMenu);
pCheckBox->setProperty("crateId", QVariant::fromValue(crate.getId()));
pCheckBox->setEnabled(!crate.isLocked());
// We're interested in keypress Qt::Key_Left
pCheckBox->installEventFilter(this);
// Strangely, the normal styling of QActions does not automatically
// apply to QWidgetActions. The :selected pseudo-state unfortunately
// does not work with QWidgetAction. :hover works for selecting items
Expand Down
2 changes: 2 additions & 0 deletions src/widget/wtrackmenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class WTrackMenu : public QMenu {
void slotRemoveFromDisk();
const QString getDeckGroup() const;

bool eventFilter(QObject* pObj, QEvent* e) override;

signals:
void loadTrackToPlayer(TrackPointer pTrack, const QString& group, bool play = false);
void trackMenuVisible(bool visible);
Expand Down