Support more options for popup menu.

This commit is contained in:
John Preston 2020-01-31 15:11:56 +03:00
parent 0ec26f2e10
commit 18ba60ec86
3 changed files with 30 additions and 6 deletions

View file

@ -90,7 +90,9 @@ void Menu::init() {
setMouseTracking(true);
if (_st.itemBg->c.alpha() == 255) {
setAttribute(Qt::WA_OpaquePaintEvent);
}
}
not_null<QAction*> Menu::addAction(const QString &text, const QObject *receiver, const char* member, const style::icon *icon, const style::icon *iconOver) {

View file

@ -300,7 +300,11 @@ void PopupMenu::childHiding(PopupMenu *child) {
}
void PopupMenu::setOrigin(PanelAnimation::Origin origin) {
_origin = origin;
_origin = _forcedOrigin.value_or(origin);
}
void PopupMenu::setForcedOrigin(PanelAnimation::Origin origin) {
_forcedOrigin = origin;
}
void PopupMenu::showAnimated(PanelAnimation::Origin origin) {
@ -457,14 +461,28 @@ void PopupMenu::showMenu(const QPoint &p, PopupMenu *parent, TriggeredSource sou
}
_parent = parent;
auto origin = PanelAnimation::Origin::TopLeft;
using Origin = PanelAnimation::Origin;
auto origin = Origin::TopLeft;
const auto forceLeft = _forcedOrigin
&& (*_forcedOrigin == Origin::TopLeft
|| *_forcedOrigin == Origin::BottomLeft);
const auto forceTop = _forcedOrigin
&& (*_forcedOrigin == Origin::TopLeft
|| *_forcedOrigin == Origin::TopRight);
const auto forceRight = _forcedOrigin
&& (*_forcedOrigin == Origin::TopRight
|| *_forcedOrigin == Origin::BottomRight);
const auto forceBottom = _forcedOrigin
&& (*_forcedOrigin == Origin::BottomLeft
|| *_forcedOrigin == Origin::BottomRight);
auto w = p - QPoint(0, _padding.top());
auto r = QApplication::desktop()->screenGeometry(p);
_useTransparency = Platform::TranslucentWindowsSupported(p);
setAttribute(Qt::WA_OpaquePaintEvent, !_useTransparency);
handleCompositingUpdate();
if (style::RightToLeft()) {
if (w.x() - width() < r.x() - _padding.left()) {
const auto badLeft = (w.x() - width() < r.x() - _padding.left());
if (forceRight || (badLeft && !forceLeft)) {
if (_parent && w.x() + _parent->width() - _padding.left() - _padding.right() + width() - _padding.right() <= r.x() + r.width()) {
w.setX(w.x() + _parent->width() - _padding.left() - _padding.right());
} else {
@ -474,7 +492,8 @@ void PopupMenu::showMenu(const QPoint &p, PopupMenu *parent, TriggeredSource sou
w.setX(w.x() - width());
}
} else {
if (w.x() + width() - _padding.right() > r.x() + r.width()) {
const auto badLeft = (w.x() + width() - _padding.right() > r.x() + r.width());
if (forceRight || (badLeft && !forceLeft)) {
if (_parent && w.x() - _parent->width() + _padding.left() + _padding.right() - width() + _padding.right() >= r.x() - _padding.left()) {
w.setX(w.x() + _padding.left() + _padding.right() - _parent->width() - width() + _padding.left() + _padding.right());
} else {
@ -483,7 +502,8 @@ void PopupMenu::showMenu(const QPoint &p, PopupMenu *parent, TriggeredSource sou
origin = PanelAnimation::Origin::TopRight;
}
}
if (w.y() + height() - _padding.bottom() > r.y() + r.height()) {
const auto badTop = (w.y() + height() - _padding.bottom() > r.y() + r.height());
if (forceBottom || (badTop && !forceTop)) {
if (_parent) {
w.setY(r.y() + r.height() - height() + _padding.bottom());
} else {

View file

@ -32,6 +32,7 @@ public:
void deleteOnHide(bool del);
void popup(const QPoint &p);
void hideMenu(bool fast = false);
void setForcedOrigin(PanelAnimation::Origin origin);
void setDestroyedCallback(Fn<void()> callback) {
_destroyedCallback = std::move(callback);
@ -109,6 +110,7 @@ private:
SubmenuPointer _activeSubmenu;
PanelAnimation::Origin _origin = PanelAnimation::Origin::TopLeft;
std::optional<PanelAnimation::Origin> _forcedOrigin;
std::unique_ptr<PanelAnimation> _showAnimation;
Animations::Simple _a_show;