2016-08-30 20:01:07 +02:00
|
|
|
#include "./traymenu.h"
|
2016-09-24 16:19:23 +02:00
|
|
|
#include "./trayicon.h"
|
2017-05-01 03:34:43 +02:00
|
|
|
#include "./traywidget.h"
|
2016-08-30 20:01:07 +02:00
|
|
|
|
2021-01-25 19:48:11 +01:00
|
|
|
#include <syncthingwidgets/settings/settings.h>
|
2016-09-01 16:34:30 +02:00
|
|
|
|
2019-05-04 22:18:50 +02:00
|
|
|
#include <qtutilities/misc/dialogutils.h>
|
|
|
|
|
2016-12-26 19:50:10 +01:00
|
|
|
#include <QApplication>
|
2017-05-01 03:34:43 +02:00
|
|
|
#include <QHBoxLayout>
|
2016-08-30 20:01:07 +02:00
|
|
|
|
2019-06-10 22:48:26 +02:00
|
|
|
using namespace QtUtilities;
|
|
|
|
|
2016-08-30 20:01:07 +02:00
|
|
|
namespace QtGui {
|
|
|
|
|
2018-05-15 22:01:03 +02:00
|
|
|
TrayMenu::TrayMenu(TrayIcon *trayIcon, QWidget *parent)
|
2017-05-01 03:34:43 +02:00
|
|
|
: QMenu(parent)
|
2018-01-27 23:27:50 +01:00
|
|
|
, m_trayIcon(trayIcon)
|
2022-10-07 23:15:25 +02:00
|
|
|
, m_pinned(false)
|
2016-08-30 20:01:07 +02:00
|
|
|
{
|
2019-07-31 20:47:19 +02:00
|
|
|
setObjectName(QStringLiteral("QtGui::TrayMenu"));
|
2018-04-02 20:26:00 +02:00
|
|
|
auto *const menuLayout = new QHBoxLayout;
|
2019-06-25 13:49:53 +02:00
|
|
|
menuLayout->setContentsMargins(0, 0, 0, 0);
|
2018-04-02 20:26:00 +02:00
|
|
|
menuLayout->setSpacing(0);
|
2018-05-15 22:01:03 +02:00
|
|
|
menuLayout->addWidget(m_trayWidget = new TrayWidget(this));
|
2016-08-30 20:01:07 +02:00
|
|
|
setLayout(menuLayout);
|
|
|
|
setPlatformMenu(nullptr);
|
2019-07-28 10:56:59 +02:00
|
|
|
setWindowFlags(Qt::FramelessWindowHint | Qt::Popup);
|
2016-08-30 20:01:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QSize TrayMenu::sizeHint() const
|
|
|
|
{
|
2016-11-02 20:03:38 +01:00
|
|
|
return Settings::values().appearance.trayMenuSize;
|
2016-08-30 20:01:07 +02:00
|
|
|
}
|
|
|
|
|
2016-12-26 19:50:10 +01:00
|
|
|
/*!
|
|
|
|
* \brief Moves the specified \a innerRect at the specified \a point into the specified \a outerRect
|
|
|
|
* by altering \a point.
|
|
|
|
*/
|
|
|
|
void moveInside(QPoint &point, const QSize &innerRect, const QRect &outerRect)
|
|
|
|
{
|
2017-05-01 03:34:43 +02:00
|
|
|
if (point.y() < outerRect.top()) {
|
2016-12-26 19:50:10 +01:00
|
|
|
point.setY(outerRect.top());
|
2017-05-01 03:34:43 +02:00
|
|
|
} else if (point.y() + innerRect.height() > outerRect.bottom()) {
|
2016-12-26 19:50:10 +01:00
|
|
|
point.setY(outerRect.bottom() - innerRect.height());
|
|
|
|
}
|
2017-05-01 03:34:43 +02:00
|
|
|
if (point.x() < outerRect.left()) {
|
2016-12-26 19:50:10 +01:00
|
|
|
point.setX(outerRect.left());
|
2017-05-01 03:34:43 +02:00
|
|
|
} else if (point.x() + innerRect.width() > outerRect.right()) {
|
2016-12-26 19:50:10 +01:00
|
|
|
point.setX(outerRect.right() - innerRect.width());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-22 17:28:09 +02:00
|
|
|
void TrayMenu::showUsingPositioningSettings()
|
2016-12-26 19:50:10 +01:00
|
|
|
{
|
|
|
|
resize(sizeHint());
|
2019-06-22 17:28:09 +02:00
|
|
|
auto pos = Settings::values().appearance.positioning.positionToUse();
|
2019-06-10 22:48:26 +02:00
|
|
|
moveInside(pos, size(), availableScreenGeometryAtPoint(pos));
|
2016-12-26 19:50:10 +01:00
|
|
|
popup(pos);
|
2022-10-07 01:06:13 +02:00
|
|
|
activateWindow();
|
2016-12-26 19:50:10 +01:00
|
|
|
}
|
2022-10-07 01:06:13 +02:00
|
|
|
|
|
|
|
void TrayMenu::setPinned(bool pinned)
|
|
|
|
{
|
|
|
|
setWindowFlags(Qt::FramelessWindowHint | ((m_pinned = pinned) ? Qt::Window : Qt::Popup));
|
2022-10-07 23:22:15 +02:00
|
|
|
show();
|
|
|
|
activateWindow();
|
2022-10-07 01:06:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TrayMenu::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (!m_pinned) {
|
|
|
|
QMenu::mousePressEvent(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrayMenu::mouseReleaseEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (!m_pinned) {
|
|
|
|
QMenu::mouseReleaseEvent(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 21:48:15 +02:00
|
|
|
} // namespace QtGui
|