File: traymenu.cpp

package info (click to toggle)
syncthingtray 1.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,804 kB
  • sloc: cpp: 31,085; xml: 1,694; java: 570; sh: 81; javascript: 53; makefile: 25
file content (283 lines) | stat: -rw-r--r-- 8,851 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include "./traymenu.h"
#include "./trayicon.h"
#include "./traywidget.h"

#include <syncthingwidgets/settings/settings.h>

#include <qtutilities/misc/dialogutils.h>

#include <QApplication>
#include <QHBoxLayout>
#include <QPaintEvent>
#include <QPainter>
#include <QWindow>

#ifdef TRAY_MENU_HANDLE_WINDOWS11_STYLE
#include <QStyle>
#if (QT_VERSION < QT_VERSION_CHECK(6, 9, 0))
#include <QLibraryInfo>
#endif
#endif

#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
#define QT_SUPPORTS_SYSTEM_WINDOW_COMMANDS
#endif

using namespace QtUtilities;

namespace QtGui {

static constexpr auto border = 10;

#ifdef TRAY_MENU_HANDLE_WINDOWS11_STYLE
static bool isWindows11Style(const QWidget *widget)
{
    const auto *const s = widget->style();
    return s && s->name().compare(QLatin1String("windows11"), Qt::CaseInsensitive) == 0;
}
#endif

TrayMenu::TrayMenu(TrayIcon *trayIcon, QWidget *parent)
    : QMenu(parent)
    , m_layout(new QHBoxLayout)
    , m_trayIcon(trayIcon)
    , m_windowType(WindowType::Popup)
    , m_startedSystemWindowCommand(false)
#ifdef TRAY_MENU_HANDLE_WINDOWS11_STYLE
    , m_isWindows11Style(isWindows11Style(this))
#endif
{
    setObjectName(QStringLiteral("QtGui::TrayMenu"));
    setLayout(m_layout);
    updateContentMargins();
    m_layout->setSpacing(0);
    m_layout->addWidget(m_trayWidget = new TrayWidget(this));
    setPlatformMenu(nullptr);
    setWindowFlags(Qt::FramelessWindowHint | Qt::Popup);
    setWindowIcon(m_trayWidget->windowIcon());
}

QSize TrayMenu::sizeHint() const
{
    return Settings::values().appearance.trayMenuSize;
}

/*!
 * \brief Moves the specified \a innerRect at the specified \a point into the specified \a outerRect
 *        by altering \a point.
 */
static void moveInside(QPoint &point, const QSize &innerRect, const QRect &outerRect)
{
    if (point.y() < outerRect.top()) {
        point.setY(outerRect.top());
    } else if (point.y() + innerRect.height() > outerRect.bottom()) {
        point.setY(outerRect.bottom() - innerRect.height());
    }
    if (point.x() < outerRect.left()) {
        point.setX(outerRect.left());
    } else if (point.x() + innerRect.width() > outerRect.right()) {
        point.setX(outerRect.right() - innerRect.width());
    }
}

/*!
 * \brief Shows the menu using positioning settings.
 * \remarks
 * Not using `popup(pos.value())` because as of Qt 6.9.0 this function returns early if there is not visible action. This could be worked
 * around using `addAction(QString())` but in some styles (e.g. the classic "Windows" style) this leads to an empty action being drawn. So
 * this function just uses `show()` in any case now.
 */
void TrayMenu::showUsingPositioningSettings()
{
    if (m_windowType == WindowType::None) {
        widget().showWebUI();
        return;
    }
    resize(sizeHint());
    if (auto pos = Settings::values().appearance.positioning.positionToUse(); pos.has_value()) {
        const auto popupSize = size();
        moveInside(pos.value(), popupSize, availableScreenGeometryAtPoint(pos.value()));
        setGeometry(QRect(pos.value(), popupSize));
    }
    show();
    activateWindow();
}

bool TrayMenu::event(QEvent *event)
{
#ifdef TRAY_MENU_HANDLE_WINDOWS11_STYLE
    switch (event->type()) {
    case QEvent::StyleChange:
        m_isWindows11Style = isWindows11Style(this);
        updateContentMargins();
        break;
    case QEvent::PolishRequest:
    case QEvent::Polish:
        if (m_windowType != TrayMenu::WindowType::Popup && m_isWindows11Style) {
            // avoid polishing via the Windows 11 style as it would break behavior if we don't actually show this as popup
            event->accept();
            return true;
        }
        break;
    default:;
    }
#endif
    return QMenu::event(event);
}

void TrayMenu::setWindowType(int windowType)
{
    if (windowType >= 0 && windowType <= 3) {
        setWindowType(static_cast<WindowType>(windowType));
    }
}

void TrayMenu::setWindowType(WindowType windowType)
{
    if (m_windowType == windowType) {
        return;
    }
    auto flags = Qt::WindowFlags();
    switch (m_windowType = windowType) {
    case WindowType::Popup:
        flags = Qt::FramelessWindowHint | Qt::Popup;
        break;
    case WindowType::NormalWindow:
        flags = Qt::Window;
        break;
    case WindowType::CustomWindow:
        flags = Qt::Dialog | Qt::CustomizeWindowHint;
        break;
    case WindowType::None:
        break;
    }
    setWindowFlags(flags);

#ifdef TRAY_MENU_HANDLE_WINDOWS11_STYLE
    // ensure correct margins and polishing when using Windows 11 style
    if (m_isWindows11Style) {
        updateContentMargins();
        if (windowType == WindowType::Popup) {
            if (auto *const s = style()) {
                s->polish(this);
            }
        }
    }
#endif
}

void TrayMenu::mousePressEvent(QMouseEvent *event)
{
    // skip any special behavior if the tray menu is shown as a regular window
    if (m_windowType == TrayMenu::WindowType::NormalWindow) {
        return;
    }

    // try starting a system window resize/move to allow resizing/moving the borderless window
#ifdef QT_SUPPORTS_SYSTEM_WINDOW_COMMANDS
    if (auto *const window = this->windowHandle()) {
        // keep default behavior on X11 for popups as the start functions don't work there (even though they return true)
#if defined(Q_OS_UNIX) && !(defined(Q_OS_ANDROID) || defined(Q_OS_DARWIN))
        if (m_windowType == TrayMenu::WindowType::Popup) {
            static const auto platform = QGuiApplication::platformName();
            if (!platform.compare(QLatin1String("xcb"), Qt::CaseInsensitive)) {
                QMenu::mousePressEvent(event);
                return;
            }
        }
#endif
        // check relevant edges and start the appropriate system resize/move
        const auto pos = event->pos();
        auto edges = Qt::Edges();
        if (pos.x() < border)
            edges |= Qt::LeftEdge;
        if (pos.x() >= width() - border)
            edges |= Qt::RightEdge;
        if (pos.y() < border)
            edges |= Qt::TopEdge;
        if (pos.y() >= height() - border)
            edges |= Qt::BottomEdge;
        m_startedSystemWindowCommand = edges ? window->startSystemResize(edges) : window->startSystemMove();
    }
#endif

    // fallback to the default behavior for the current window type if system window resize/move is not possible
    if (!m_startedSystemWindowCommand) {
        QMenu::mousePressEvent(event);
    }
}

void TrayMenu::mouseReleaseEvent(QMouseEvent *event)
{
    // cover cases analogous to TrayMenu::mousePressEvent()
    if (m_windowType == TrayMenu::WindowType::NormalWindow) {
        return;
    }
    if (m_startedSystemWindowCommand) {
        m_startedSystemWindowCommand = false;
    } else {
        QMenu::mouseReleaseEvent(event);
    }
}

void TrayMenu::moveEvent(QMoveEvent *event)
{
    auto &settings = Settings::values().appearance.positioning;
    if (settings.useAssumedIconPosition) {
        Settings::values().appearance.positioning.assumedIconPosition = event->pos();
        emit positioningSettingsChanged();
    }
}

void TrayMenu::resizeEvent(QResizeEvent *event)
{
    Settings::values().appearance.trayMenuSize = event->size();
    emit positioningSettingsChanged();
}

void TrayMenu::paintEvent(QPaintEvent *event)
{
    if (m_windowType == WindowType::Popup) {
        QMenu::paintEvent(event);
    } else {
#ifdef TRAY_MENU_HANDLE_WINDOWS11_STYLE
        const auto p = m_windowType != TrayMenu::WindowType::Popup && m_isWindows11Style ? QGuiApplication::palette() : QPalette(palette());
#else
        const auto &p = palette();
#endif
        QPainter(this).fillRect(event->rect(), p.color(backgroundRole()));
        QWidget::paintEvent(event);
    }
}

void TrayMenu::focusOutEvent(QFocusEvent *)
{
    if (m_windowType == WindowType::CustomWindow) {
        if (const auto *fw = focusWidget(); fw->hasFocus()) {
            return;
        }
        close();
    }
}

void TrayMenu::updateContentMargins()
{
#ifdef TRAY_MENU_HANDLE_WINDOWS11_STYLE
    // set higher margins to account for the shadow effects of the Windows 11 style
    // note: Not sure whether there's a way to determine the required margins dynamically.
    if (m_windowType == TrayMenu::WindowType::Popup && m_isWindows11Style) {
        static constexpr auto normalMargin = 2;
#if (QT_VERSION >= QT_VERSION_CHECK(6, 9, 0))
        static constexpr auto extraMargin = normalMargin;
#else
        static const auto needsExtraMargin = QLibraryInfo::version() < QVersionNumber(6, 8, 1);
        static const auto extraMargin = needsExtraMargin ? 10 : normalMargin;
#endif
        m_layout->setContentsMargins(normalMargin, normalMargin, extraMargin, normalMargin);
        return;
    }
#endif
    m_layout->setContentsMargins(0, 0, 0, 0);
}

} // namespace QtGui