Moved key press handling from menu to menu item base.
This commit is contained in:
parent
3bd07cf837
commit
43f09755e5
9 changed files with 42 additions and 27 deletions
|
|
@ -134,9 +134,9 @@ bool DropdownMenu::popupSubmenuFromAction(const Menu::CallbackData &data) {
|
||||||
// }
|
// }
|
||||||
//}
|
//}
|
||||||
|
|
||||||
void DropdownMenu::forwardKeyPress(int key) {
|
void DropdownMenu::forwardKeyPress(not_null<QKeyEvent*> e) {
|
||||||
if (!handleKeyPress(key)) {
|
if (!handleKeyPress(e->key())) {
|
||||||
_menu->handleKeyPress(key);
|
_menu->handleKeyPress(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -193,7 +193,7 @@ void DropdownMenu::hideEvent(QHideEvent *e) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void DropdownMenu::keyPressEvent(QKeyEvent *e) {
|
void DropdownMenu::keyPressEvent(QKeyEvent *e) {
|
||||||
forwardKeyPress(e->key());
|
forwardKeyPress(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DropdownMenu::mouseMoveEvent(QMouseEvent *e) {
|
void DropdownMenu::mouseMoveEvent(QMouseEvent *e) {
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,7 @@ private:
|
||||||
using TriggeredSource = Menu::TriggeredSource;
|
using TriggeredSource = Menu::TriggeredSource;
|
||||||
void handleActivated(const Menu::CallbackData &data);
|
void handleActivated(const Menu::CallbackData &data);
|
||||||
void handleTriggered(const Menu::CallbackData &data);
|
void handleTriggered(const Menu::CallbackData &data);
|
||||||
void forwardKeyPress(int key);
|
void forwardKeyPress(not_null<QKeyEvent*> e);
|
||||||
bool handleKeyPress(int key);
|
bool handleKeyPress(int key);
|
||||||
void forwardMouseMove(QPoint globalPosition) {
|
void forwardMouseMove(QPoint globalPosition) {
|
||||||
_menu->handleMouseMove(globalPosition);
|
_menu->handleMouseMove(globalPosition);
|
||||||
|
|
|
||||||
|
|
@ -229,7 +229,7 @@ void Menu::itemPressed(TriggeredSource source) {
|
||||||
void Menu::keyPressEvent(QKeyEvent *e) {
|
void Menu::keyPressEvent(QKeyEvent *e) {
|
||||||
const auto key = e->key();
|
const auto key = e->key();
|
||||||
if (!_keyPressDelegate || !_keyPressDelegate(key)) {
|
if (!_keyPressDelegate || !_keyPressDelegate(key)) {
|
||||||
handleKeyPress(key);
|
handleKeyPress(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -238,26 +238,17 @@ ItemBase *Menu::findSelectedAction() const {
|
||||||
return (it == end(_actionWidgets)) ? nullptr : it->get();
|
return (it == end(_actionWidgets)) ? nullptr : it->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Menu::handleKeyPress(int key) {
|
void Menu::handleKeyPress(not_null<QKeyEvent*> e) {
|
||||||
if (key == Qt::Key_Enter || key == Qt::Key_Return) {
|
const auto key = e->key();
|
||||||
itemPressed(TriggeredSource::Keyboard);
|
const auto selected = findSelectedAction();
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ((key != Qt::Key_Up && key != Qt::Key_Down) || _actions.empty()) {
|
if ((key != Qt::Key_Up && key != Qt::Key_Down) || _actions.empty()) {
|
||||||
|
if (selected) {
|
||||||
|
selected->handleKeyPress(e);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto delta = (key == Qt::Key_Down ? 1 : -1);
|
const auto delta = (key == Qt::Key_Down ? 1 : -1);
|
||||||
const auto selected = findSelectedAction();
|
|
||||||
auto start = selected ? selected->index() : -1;
|
auto start = selected ? selected->index() : -1;
|
||||||
if (start < 0 || start >= _actions.size()) {
|
if (start < 0 || start >= _actions.size()) {
|
||||||
start = (delta > 0) ? (_actions.size() - 1) : 0;
|
start = (delta > 0) ? (_actions.size() - 1) : 0;
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,7 @@ public:
|
||||||
void setKeyPressDelegate(Fn<bool(int key)> delegate) {
|
void setKeyPressDelegate(Fn<bool(int key)> delegate) {
|
||||||
_keyPressDelegate = std::move(delegate);
|
_keyPressDelegate = std::move(delegate);
|
||||||
}
|
}
|
||||||
void handleKeyPress(int key);
|
void handleKeyPress(not_null<QKeyEvent*> e);
|
||||||
|
|
||||||
void setMouseMoveDelegate(Fn<void(QPoint globalPosition)> delegate) {
|
void setMouseMoveDelegate(Fn<void(QPoint globalPosition)> delegate) {
|
||||||
_mouseMoveDelegate = std::move(delegate);
|
_mouseMoveDelegate = std::move(delegate);
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,8 @@
|
||||||
#include "ui/effects/ripple_animation.h"
|
#include "ui/effects/ripple_animation.h"
|
||||||
#include "ui/painter.h"
|
#include "ui/painter.h"
|
||||||
|
|
||||||
|
#include <QtGui/QtEvents>
|
||||||
|
|
||||||
namespace Ui::Menu {
|
namespace Ui::Menu {
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
|
@ -173,4 +175,21 @@ int Action::contentHeight() const {
|
||||||
return _height;
|
return _height;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Action::handleKeyPress(not_null<QKeyEvent*> 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
|
} // namespace Ui::Menu
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,8 @@ public:
|
||||||
bool isEnabled() const override;
|
bool isEnabled() const override;
|
||||||
not_null<QAction*> action() const override;
|
not_null<QAction*> action() const override;
|
||||||
|
|
||||||
|
void handleKeyPress(not_null<QKeyEvent*> e) override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
QPoint prepareRippleStartPosition() const override;
|
QPoint prepareRippleStartPosition() const override;
|
||||||
QImage prepareRippleMask() const override;
|
QImage prepareRippleMask() const override;
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,9 @@ public:
|
||||||
bool hasSubmenu() const;
|
bool hasSubmenu() const;
|
||||||
void setHasSubmenu(bool value);
|
void setHasSubmenu(bool value);
|
||||||
|
|
||||||
|
virtual void handleKeyPress(not_null<QKeyEvent*> e) {
|
||||||
|
}
|
||||||
|
|
||||||
virtual not_null<QAction*> action() const = 0;
|
virtual not_null<QAction*> action() const = 0;
|
||||||
virtual bool isEnabled() const = 0;
|
virtual bool isEnabled() const = 0;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -211,9 +211,9 @@ void PopupMenu::popupSubmenu(SubmenuPointer submenu, int actionTop, TriggeredSou
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PopupMenu::forwardKeyPress(int key) {
|
void PopupMenu::forwardKeyPress(not_null<QKeyEvent*> e) {
|
||||||
if (!handleKeyPress(key)) {
|
if (!handleKeyPress(e->key())) {
|
||||||
_menu->handleKeyPress(key);
|
_menu->handleKeyPress(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -270,7 +270,7 @@ void PopupMenu::hideEvent(QHideEvent *e) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void PopupMenu::keyPressEvent(QKeyEvent *e) {
|
void PopupMenu::keyPressEvent(QKeyEvent *e) {
|
||||||
forwardKeyPress(e->key());
|
forwardKeyPress(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PopupMenu::mouseMoveEvent(QMouseEvent *e) {
|
void PopupMenu::mouseMoveEvent(QMouseEvent *e) {
|
||||||
|
|
|
||||||
|
|
@ -78,7 +78,7 @@ private:
|
||||||
void handleMenuResize();
|
void handleMenuResize();
|
||||||
void handleActivated(const Menu::CallbackData &data);
|
void handleActivated(const Menu::CallbackData &data);
|
||||||
void handleTriggered(const Menu::CallbackData &data);
|
void handleTriggered(const Menu::CallbackData &data);
|
||||||
void forwardKeyPress(int key);
|
void forwardKeyPress(not_null<QKeyEvent*> e);
|
||||||
bool handleKeyPress(int key);
|
bool handleKeyPress(int key);
|
||||||
void forwardMouseMove(QPoint globalPosition) {
|
void forwardMouseMove(QPoint globalPosition) {
|
||||||
_menu->handleMouseMove(globalPosition);
|
_menu->handleMouseMove(globalPosition);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue