2016-10-26 16:43:13 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-10-26 16:43:13 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-10-26 16:43:13 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "styles/style_widgets.h"
|
|
|
|
|
|
|
|
namespace Ui {
|
|
|
|
|
2017-06-29 12:29:00 +00:00
|
|
|
class ToggleView;
|
2016-11-16 16:04:25 +00:00
|
|
|
class RippleAnimation;
|
|
|
|
|
2016-10-26 16:43:13 +00:00
|
|
|
class Menu : public TWidget {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
Menu(QWidget *parent, const style::Menu &st = st::defaultMenu);
|
|
|
|
Menu(QWidget *parent, QMenu *menu, const style::Menu &st = st::defaultMenu);
|
|
|
|
|
2016-11-01 12:46:34 +00:00
|
|
|
QAction *addAction(const QString &text, const QObject *receiver, const char* member, const style::icon *icon = nullptr, const style::icon *iconOver = nullptr);
|
2018-06-04 15:35:11 +00:00
|
|
|
QAction *addAction(const QString &text, Fn<void()> callback, const style::icon *icon = nullptr, const style::icon *iconOver = nullptr);
|
2016-10-26 16:43:13 +00:00
|
|
|
QAction *addSeparator();
|
|
|
|
void clearActions();
|
2017-09-30 19:20:40 +00:00
|
|
|
void finishAnimating();
|
2016-10-26 16:43:13 +00:00
|
|
|
|
|
|
|
void clearSelection();
|
|
|
|
|
|
|
|
enum class TriggeredSource {
|
|
|
|
Mouse,
|
|
|
|
Keyboard,
|
|
|
|
};
|
|
|
|
void setChildShown(bool shown) {
|
|
|
|
_childShown = shown;
|
|
|
|
}
|
|
|
|
void setShowSource(TriggeredSource source);
|
2017-06-29 12:29:00 +00:00
|
|
|
void setForceWidth(int forceWidth);
|
2016-10-26 16:43:13 +00:00
|
|
|
|
|
|
|
using Actions = QList<QAction*>;
|
|
|
|
Actions &actions();
|
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
void setResizedCallback(Fn<void()> callback) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_resizedCallback = std::move(callback);
|
2016-10-26 16:43:13 +00:00
|
|
|
}
|
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
void setActivatedCallback(Fn<void(QAction *action, int actionTop, TriggeredSource source)> callback) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_activatedCallback = std::move(callback);
|
2016-10-26 16:43:13 +00:00
|
|
|
}
|
2018-06-04 15:35:11 +00:00
|
|
|
void setTriggeredCallback(Fn<void(QAction *action, int actionTop, TriggeredSource source)> callback) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_triggeredCallback = std::move(callback);
|
2016-10-26 16:43:13 +00:00
|
|
|
}
|
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
void setKeyPressDelegate(Fn<bool(int key)> delegate) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_keyPressDelegate = std::move(delegate);
|
2016-10-26 16:43:13 +00:00
|
|
|
}
|
|
|
|
void handleKeyPress(int key);
|
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
void setMouseMoveDelegate(Fn<void(QPoint globalPosition)> delegate) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_mouseMoveDelegate = std::move(delegate);
|
2016-10-26 16:43:13 +00:00
|
|
|
}
|
|
|
|
void handleMouseMove(QPoint globalPosition);
|
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
void setMousePressDelegate(Fn<void(QPoint globalPosition)> delegate) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_mousePressDelegate = std::move(delegate);
|
2016-10-26 16:43:13 +00:00
|
|
|
}
|
|
|
|
void handleMousePress(QPoint globalPosition);
|
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
void setMouseReleaseDelegate(Fn<void(QPoint globalPosition)> delegate) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_mouseReleaseDelegate = std::move(delegate);
|
2016-11-16 16:04:25 +00:00
|
|
|
}
|
|
|
|
void handleMouseRelease(QPoint globalPosition);
|
|
|
|
|
2016-10-26 16:43:13 +00:00
|
|
|
protected:
|
|
|
|
void paintEvent(QPaintEvent *e) override;
|
|
|
|
void keyPressEvent(QKeyEvent *e) override;
|
|
|
|
void mouseMoveEvent(QMouseEvent *e) override;
|
|
|
|
void mousePressEvent(QMouseEvent *e) override;
|
2016-11-16 16:04:25 +00:00
|
|
|
void mouseReleaseEvent(QMouseEvent *e) override;
|
2017-02-11 11:24:37 +00:00
|
|
|
void enterEventHook(QEvent *e) override;
|
|
|
|
void leaveEventHook(QEvent *e) override;
|
2016-10-26 16:43:13 +00:00
|
|
|
|
|
|
|
private slots:
|
|
|
|
void actionChanged();
|
|
|
|
|
|
|
|
private:
|
2017-07-22 08:35:18 +00:00
|
|
|
struct ActionData {
|
|
|
|
ActionData() = default;
|
|
|
|
ActionData(const ActionData &other) = delete;
|
|
|
|
ActionData &operator=(const ActionData &other) = delete;
|
|
|
|
ActionData(ActionData &&other) = default;
|
|
|
|
ActionData &operator=(ActionData &&other) = default;
|
|
|
|
~ActionData();
|
|
|
|
|
|
|
|
bool hasSubmenu = false;
|
|
|
|
QString text;
|
|
|
|
QString shortcut;
|
|
|
|
const style::icon *icon = nullptr;
|
|
|
|
const style::icon *iconOver = nullptr;
|
|
|
|
std::unique_ptr<RippleAnimation> ripple;
|
|
|
|
std::unique_ptr<ToggleView> toggle;
|
|
|
|
};
|
|
|
|
|
2016-10-26 16:43:13 +00:00
|
|
|
void updateSelected(QPoint globalPosition);
|
|
|
|
void init();
|
|
|
|
|
|
|
|
// Returns the new width.
|
|
|
|
int processAction(QAction *action, int index, int width);
|
2016-11-01 12:46:34 +00:00
|
|
|
QAction *addAction(QAction *a, const style::icon *icon = nullptr, const style::icon *iconOver = nullptr);
|
2016-10-26 16:43:13 +00:00
|
|
|
|
|
|
|
void setSelected(int selected);
|
2017-06-29 19:09:10 +00:00
|
|
|
void setPressed(int pressed);
|
2016-10-26 16:43:13 +00:00
|
|
|
void clearMouseSelection();
|
|
|
|
|
|
|
|
int itemTop(int index);
|
2016-11-16 16:04:25 +00:00
|
|
|
void updateItem(int index);
|
2016-10-26 16:43:13 +00:00
|
|
|
void updateSelectedItem();
|
|
|
|
void itemPressed(TriggeredSource source);
|
2016-11-16 16:04:25 +00:00
|
|
|
void itemReleased(TriggeredSource source);
|
2016-10-26 16:43:13 +00:00
|
|
|
|
|
|
|
const style::Menu &_st;
|
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<void()> _resizedCallback;
|
|
|
|
Fn<void(QAction *action, int actionTop, TriggeredSource source)> _activatedCallback;
|
|
|
|
Fn<void(QAction *action, int actionTop, TriggeredSource source)> _triggeredCallback;
|
|
|
|
Fn<bool(int key)> _keyPressDelegate;
|
|
|
|
Fn<void(QPoint globalPosition)> _mouseMoveDelegate;
|
|
|
|
Fn<void(QPoint globalPosition)> _mousePressDelegate;
|
|
|
|
Fn<void(QPoint globalPosition)> _mouseReleaseDelegate;
|
2016-10-26 16:43:13 +00:00
|
|
|
|
|
|
|
QMenu *_wappedMenu = nullptr;
|
|
|
|
Actions _actions;
|
2017-06-29 12:29:00 +00:00
|
|
|
std::vector<ActionData> _actionsData;
|
2016-10-26 16:43:13 +00:00
|
|
|
|
2017-06-29 12:29:00 +00:00
|
|
|
int _forceWidth = 0;
|
2016-10-26 16:43:13 +00:00
|
|
|
int _itemHeight, _separatorHeight;
|
|
|
|
|
|
|
|
bool _mouseSelection = false;
|
|
|
|
|
|
|
|
int _selected = -1;
|
2016-11-16 16:04:25 +00:00
|
|
|
int _pressed = -1;
|
2016-10-26 16:43:13 +00:00
|
|
|
bool _childShown = false;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Ui
|