diff --git a/ui/widgets/dropdown_menu.cpp b/ui/widgets/dropdown_menu.cpp index 7ee28ed..a8f1960 100644 --- a/ui/widgets/dropdown_menu.cpp +++ b/ui/widgets/dropdown_menu.cpp @@ -134,9 +134,9 @@ bool DropdownMenu::popupSubmenuFromAction(const Menu::CallbackData &data) { // } //} -void DropdownMenu::forwardKeyPress(int key) { - if (!handleKeyPress(key)) { - _menu->handleKeyPress(key); +void DropdownMenu::forwardKeyPress(not_null e) { + if (!handleKeyPress(e->key())) { + _menu->handleKeyPress(e); } } @@ -193,7 +193,7 @@ void DropdownMenu::hideEvent(QHideEvent *e) { } void DropdownMenu::keyPressEvent(QKeyEvent *e) { - forwardKeyPress(e->key()); + forwardKeyPress(e); } void DropdownMenu::mouseMoveEvent(QMouseEvent *e) { diff --git a/ui/widgets/dropdown_menu.h b/ui/widgets/dropdown_menu.h index 0f5a1a9..166616f 100644 --- a/ui/widgets/dropdown_menu.h +++ b/ui/widgets/dropdown_menu.h @@ -59,7 +59,7 @@ private: using TriggeredSource = Menu::TriggeredSource; void handleActivated(const Menu::CallbackData &data); void handleTriggered(const Menu::CallbackData &data); - void forwardKeyPress(int key); + void forwardKeyPress(not_null e); bool handleKeyPress(int key); void forwardMouseMove(QPoint globalPosition) { _menu->handleMouseMove(globalPosition); diff --git a/ui/widgets/menu/menu.cpp b/ui/widgets/menu/menu.cpp index 3701532..217e8f0 100644 --- a/ui/widgets/menu/menu.cpp +++ b/ui/widgets/menu/menu.cpp @@ -229,7 +229,7 @@ void Menu::itemPressed(TriggeredSource source) { void Menu::keyPressEvent(QKeyEvent *e) { const auto key = e->key(); if (!_keyPressDelegate || !_keyPressDelegate(key)) { - handleKeyPress(key); + handleKeyPress(e); } } @@ -238,26 +238,17 @@ ItemBase *Menu::findSelectedAction() const { return (it == end(_actionWidgets)) ? nullptr : it->get(); } -void Menu::handleKeyPress(int key) { - if (key == Qt::Key_Enter || key == Qt::Key_Return) { - itemPressed(TriggeredSource::Keyboard); - return; - } - if (key == (style::RightToLeft() ? Qt::Key_Left : Qt::Key_Right)) { - if (_selected >= 0 && _actionWidgets[_selected]->hasSubmenu()) { - itemPressed(TriggeredSource::Keyboard); - return; - } else if (_selected < 0 && !_actions.empty()) { - _mouseSelection = false; - setSelected(0); - } - } +void Menu::handleKeyPress(not_null e) { + const auto key = e->key(); + const auto selected = findSelectedAction(); if ((key != Qt::Key_Up && key != Qt::Key_Down) || _actions.empty()) { + if (selected) { + selected->handleKeyPress(e); + } return; } const auto delta = (key == Qt::Key_Down ? 1 : -1); - const auto selected = findSelectedAction(); auto start = selected ? selected->index() : -1; if (start < 0 || start >= _actions.size()) { start = (delta > 0) ? (_actions.size() - 1) : 0; diff --git a/ui/widgets/menu/menu.h b/ui/widgets/menu/menu.h index f838a1b..9f3d265 100644 --- a/ui/widgets/menu/menu.h +++ b/ui/widgets/menu/menu.h @@ -59,7 +59,7 @@ public: void setKeyPressDelegate(Fn delegate) { _keyPressDelegate = std::move(delegate); } - void handleKeyPress(int key); + void handleKeyPress(not_null e); void setMouseMoveDelegate(Fn delegate) { _mouseMoveDelegate = std::move(delegate); diff --git a/ui/widgets/menu/menu_action.cpp b/ui/widgets/menu/menu_action.cpp index 955a722..e8f8273 100644 --- a/ui/widgets/menu/menu_action.cpp +++ b/ui/widgets/menu/menu_action.cpp @@ -9,6 +9,8 @@ #include "ui/effects/ripple_animation.h" #include "ui/painter.h" +#include + namespace Ui::Menu { namespace { @@ -173,4 +175,21 @@ int Action::contentHeight() const { return _height; } +void Action::handleKeyPress(not_null e) { + if (!isSelected()) { + return; + } + const auto key = e->key(); + if (key == Qt::Key_Enter || key == Qt::Key_Return) { + setClicked(TriggeredSource::Keyboard); + return; + } + if (key == (style::RightToLeft() ? Qt::Key_Left : Qt::Key_Right)) { + if (hasSubmenu()) { + setClicked(TriggeredSource::Keyboard); + return; + } + } +} + } // namespace Ui::Menu diff --git a/ui/widgets/menu/menu_action.h b/ui/widgets/menu/menu_action.h index c274c03..448cd66 100644 --- a/ui/widgets/menu/menu_action.h +++ b/ui/widgets/menu/menu_action.h @@ -26,6 +26,8 @@ public: bool isEnabled() const override; not_null action() const override; + void handleKeyPress(not_null e) override; + protected: QPoint prepareRippleStartPosition() const override; QImage prepareRippleMask() const override; diff --git a/ui/widgets/menu/menu_item_base.h b/ui/widgets/menu/menu_item_base.h index 4dbe242..8dcdc63 100644 --- a/ui/widgets/menu/menu_item_base.h +++ b/ui/widgets/menu/menu_item_base.h @@ -39,6 +39,9 @@ public: bool hasSubmenu() const; void setHasSubmenu(bool value); + virtual void handleKeyPress(not_null e) { + } + virtual not_null action() const = 0; virtual bool isEnabled() const = 0; diff --git a/ui/widgets/popup_menu.cpp b/ui/widgets/popup_menu.cpp index 394a8c4..38d0acc 100644 --- a/ui/widgets/popup_menu.cpp +++ b/ui/widgets/popup_menu.cpp @@ -211,9 +211,9 @@ void PopupMenu::popupSubmenu(SubmenuPointer submenu, int actionTop, TriggeredSou } } -void PopupMenu::forwardKeyPress(int key) { - if (!handleKeyPress(key)) { - _menu->handleKeyPress(key); +void PopupMenu::forwardKeyPress(not_null e) { + if (!handleKeyPress(e->key())) { + _menu->handleKeyPress(e); } } @@ -270,7 +270,7 @@ void PopupMenu::hideEvent(QHideEvent *e) { } void PopupMenu::keyPressEvent(QKeyEvent *e) { - forwardKeyPress(e->key()); + forwardKeyPress(e); } void PopupMenu::mouseMoveEvent(QMouseEvent *e) { diff --git a/ui/widgets/popup_menu.h b/ui/widgets/popup_menu.h index 9e05866..9caa56d 100644 --- a/ui/widgets/popup_menu.h +++ b/ui/widgets/popup_menu.h @@ -78,7 +78,7 @@ private: void handleMenuResize(); void handleActivated(const Menu::CallbackData &data); void handleTriggered(const Menu::CallbackData &data); - void forwardKeyPress(int key); + void forwardKeyPress(not_null e); bool handleKeyPress(int key); void forwardMouseMove(QPoint globalPosition) { _menu->handleMouseMove(globalPosition);