Skip to content

Commit 86575d9

Browse files
committed
Add system tray icon ordering preference
Add a "Systray items ordering" setting to Preferences with four modes: Automatic (default), Interfaces -> Stats, Stats -> Interfaces, and Grouped by interface. When a non-automatic order is selected, QNetStats destroys and recreates only the diverged suffix of visible QSystemTrayIcon instances and re-shows them sequentially (30 ms spacing) so XEmbed and D-Bus SNI tray managers receive registrations in the requested order while avoiding unnecessary flicker on prefix matches or icon removals.
1 parent 0efcbce commit 86575d9

6 files changed

Lines changed: 256 additions & 36 deletions

File tree

‎src/preferences.cpp‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "projectinfo.h"
33

44
#include <QCheckBox>
5+
#include <QComboBox>
56
#include <QCoreApplication>
67
#include <QDialogButtonBox>
78
#include <QDir>
@@ -48,6 +49,30 @@ QString macosLaunchAgentPath() {
4849
return homeDir + QStringLiteral("/Library/LaunchAgents/") + QString::fromUtf8(ProjectInfo::Id) + QStringLiteral(".plist");
4950
}
5051
#endif
52+
53+
QString trayOrderToString(TrayIconOrder order) {
54+
switch (order) {
55+
case TrayIconOrder::InterfacesFirst:
56+
return QStringLiteral("interfaces-first");
57+
case TrayIconOrder::StatsFirst:
58+
return QStringLiteral("stats-first");
59+
case TrayIconOrder::PerInterface:
60+
return QStringLiteral("per-interface");
61+
case TrayIconOrder::Automatic:
62+
break;
63+
}
64+
return QStringLiteral("automatic");
65+
}
66+
67+
TrayIconOrder trayOrderFromString(const QString &value) {
68+
if (value == QLatin1String("interfaces-first"))
69+
return TrayIconOrder::InterfacesFirst;
70+
if (value == QLatin1String("stats-first"))
71+
return TrayIconOrder::StatsFirst;
72+
if (value == QLatin1String("per-interface"))
73+
return TrayIconOrder::PerInterface;
74+
return TrayIconOrder::Automatic;
75+
}
5176
}
5277

5378
GlobalPreferences GlobalPreferences::load() {
@@ -60,6 +85,8 @@ GlobalPreferences GlobalPreferences::load() {
6085
prefs.mChartFrameRate = std::clamp(
6186
settings.value(QStringLiteral("ChartFrameRate"), DefaultChartFrameRate).toInt(),
6287
MinChartFrameRate, MaxChartFrameRate);
88+
prefs.mTrayIconOrder = trayOrderFromString(
89+
settings.value(QStringLiteral("TrayIconOrder"), QStringLiteral("automatic")).toString());
6390
settings.endGroup();
6491
prefs.mStartOnLogin = isAutoStartEnabled();
6592
return prefs;
@@ -76,6 +103,10 @@ void GlobalPreferences::save() const {
76103
settings.remove(QStringLiteral("ChartFrameRate"));
77104
else
78105
settings.setValue(QStringLiteral("ChartFrameRate"), mChartFrameRate);
106+
if (mTrayIconOrder == TrayIconOrder::Automatic)
107+
settings.remove(QStringLiteral("TrayIconOrder"));
108+
else
109+
settings.setValue(QStringLiteral("TrayIconOrder"), trayOrderToString(mTrayIconOrder));
79110
settings.endGroup();
80111
}
81112

@@ -215,6 +246,13 @@ Preferences::Preferences(QWidget *parent) : QDialog(parent) {
215246
auto *generalForm = new QFormLayout(generalGroup);
216247
mStartOnLogin = new QCheckBox(tr("Start automatically on login"), generalGroup);
217248
generalForm->addRow(mStartOnLogin);
249+
mTrayIconOrder = new QComboBox(generalGroup);
250+
mTrayIconOrder->addItem(tr("Automatic (system default)"), int(TrayIconOrder::Automatic));
251+
mTrayIconOrder->addItem(tr("Interfaces -> Stats"), int(TrayIconOrder::InterfacesFirst));
252+
mTrayIconOrder->addItem(tr("Stats -> Interfaces"), int(TrayIconOrder::StatsFirst));
253+
mTrayIconOrder->addItem(tr("Grouped by interface"), int(TrayIconOrder::PerInterface));
254+
mTrayIconOrder->setToolTip(tr("Controls the relative order of QNetStats tray icons. Non-automatic modes hide and re-register affected icons when an icon is added or reordered; support depends on the system tray panel."));
255+
generalForm->addRow(tr("Systray items ordering:"), mTrayIconOrder);
218256
layout->addWidget(generalGroup);
219257

220258
auto *chartGroup = new QGroupBox(tr("Statistics Chart"), this);
@@ -253,13 +291,16 @@ void Preferences::showEvent(QShowEvent *event) {
253291
void Preferences::loadFromSystem() {
254292
const GlobalPreferences prefs = GlobalPreferences::load();
255293
mStartOnLogin->setChecked(prefs.mStartOnLogin);
294+
const int orderIndex = mTrayIconOrder->findData(int(prefs.mTrayIconOrder));
295+
mTrayIconOrder->setCurrentIndex(orderIndex >= 0 ? orderIndex : 0);
256296
mChartHistorySize->setValue(prefs.mChartHistorySize);
257297
mChartFrameRate->setValue(prefs.mChartFrameRate);
258298
}
259299

260300
GlobalPreferences Preferences::preferences() const {
261301
GlobalPreferences prefs;
262302
prefs.mStartOnLogin = mStartOnLogin->isChecked();
303+
prefs.mTrayIconOrder = static_cast<TrayIconOrder>(mTrayIconOrder->currentData().toInt());
263304
prefs.mChartHistorySize = mChartHistorySize->value();
264305
prefs.mChartFrameRate = mChartFrameRate->value();
265306
return prefs;

‎src/preferences.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,17 @@
44
#include <QDialog>
55

66
class QCheckBox;
7+
class QComboBox;
78
class QShowEvent;
89
class QSpinBox;
910

11+
enum class TrayIconOrder {
12+
Automatic = 0,
13+
InterfacesFirst = 1,
14+
StatsFirst = 2,
15+
PerInterface = 3
16+
};
17+
1018
struct GlobalPreferences {
1119
static constexpr int DefaultChartHistorySize = 50;
1220
static constexpr int MinChartHistorySize = 10;
@@ -19,6 +27,7 @@ struct GlobalPreferences {
1927
int mChartHistorySize = DefaultChartHistorySize;
2028
int mChartFrameRate = DefaultChartFrameRate;
2129
bool mStartOnLogin = false;
30+
TrayIconOrder mTrayIconOrder = TrayIconOrder::Automatic;
2231

2332
static GlobalPreferences load();
2433
static bool isAutoStartEnabled();
@@ -44,6 +53,7 @@ Q_OBJECT
4453
void applyChanges();
4554

4655
QCheckBox *mStartOnLogin;
56+
QComboBox *mTrayIconOrder;
4757
QSpinBox *mChartHistorySize;
4858
QSpinBox *mChartFrameRate;
4959
};

‎src/qnetstats.cpp‎

Lines changed: 108 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ void saveColorOverride(QSettings &settings, const char *key,
8484
}
8585

8686
QNetStats::QNetStats() : QDialog(nullptr, Qt::Window), mConfigure(nullptr) {
87+
mTrayIconOrder = GlobalPreferences::load().mTrayIconOrder;
8788
// read the current views from config file
8889
QSettings settings;
8990
QStringList views = settings.value("CurrentViews", QStringList()).toStringList();
@@ -154,18 +155,15 @@ void QNetStats::checkTrayIconsAvailable() {
154155
mBackupStatisticsMenu->clear();
155156
QStringList names = mViews.keys();
156157
names.sort();
158+
bool anyVisible = false;
157159
for (const QString &name : names) {
158160
auto *view = mViews.value(name);
159161
mBackupStatisticsMenu->addAction(view->displayName(), view, &QNetStatsView::showStatistics);
162+
anyVisible |= view->trayIconVisible();
160163
}
161164
mBackupStatisticsMenu->setEnabled(!names.isEmpty());
162-
for (auto view: mViews) {
163-
if (view->trayIconVisible()) {
164-
mBackupTrayIcon->hide();
165-
return;
166-
}
167-
}
168-
mBackupTrayIcon->show();
165+
mBackupTrayIcon->setVisible(!anyVisible);
166+
syncTrayIconOrder(false);
169167
}
170168

171169
void QNetStats::setup() {
@@ -180,6 +178,9 @@ void QNetStats::setup() {
180178
mConfigure = new Configure(nullptr);
181179
mPreferences = new Preferences(nullptr);
182180
connect(mPreferences, &Preferences::applied, this, &QNetStats::preferencesApply);
181+
mReorderTimer = new QTimer(this);
182+
mReorderTimer->setSingleShot(true);
183+
connect(mReorderTimer, &QTimer::timeout, this, &QNetStats::stepTrayReorder);
183184
setupBackupTrayIcon();
184185
setupFallbackWindow();
185186

@@ -325,8 +326,108 @@ void QNetStats::showPreferences() {
325326

326327
void QNetStats::preferencesApply() {
327328
const GlobalPreferences prefs = mPreferences->preferences();
329+
const bool orderChanged = prefs.mTrayIconOrder != mTrayIconOrder;
330+
mTrayIconOrder = prefs.mTrayIconOrder;
328331
for (auto *view : mViews)
329332
view->applyPreferences(prefs);
333+
if (orderChanged)
334+
syncTrayIconOrder(true);
335+
}
336+
337+
void QNetStats::syncTrayIconOrder(bool force) {
338+
if (mTrayIconOrder == TrayIconOrder::Automatic) {
339+
mReorderTimer->stop();
340+
mPendingShowIcons.clear();
341+
mLastOrderedIcons.clear();
342+
for (auto *view : mViews) {
343+
if (view->shouldShowActivityIcon() && !view->isTrayIconShown(false))
344+
view->showRecreatedTrayIcon(false);
345+
if (view->shouldShowTextIcon() && !view->isTrayIconShown(true))
346+
view->showRecreatedTrayIcon(true);
347+
}
348+
return;
349+
}
350+
351+
QStringList names = mViews.keys();
352+
names.sort();
353+
QList<TrayIconSlot> desired;
354+
auto appendSlot = [&desired](QNetStatsView *view, bool isText) {
355+
if (isText ? view->shouldShowTextIcon() : view->shouldShowActivityIcon())
356+
desired.append({view, isText});
357+
};
358+
if (mTrayIconOrder == TrayIconOrder::InterfacesFirst) {
359+
for (const QString &name : names)
360+
appendSlot(mViews.value(name), false);
361+
for (const QString &name : names)
362+
appendSlot(mViews.value(name), true);
363+
} else if (mTrayIconOrder == TrayIconOrder::StatsFirst) {
364+
for (const QString &name : names)
365+
appendSlot(mViews.value(name), true);
366+
for (const QString &name : names)
367+
appendSlot(mViews.value(name), false);
368+
} else {
369+
for (const QString &name : names) {
370+
QNetStatsView *view = mViews.value(name);
371+
appendSlot(view, false);
372+
appendSlot(view, true);
373+
}
374+
}
375+
376+
QList<TrayIconSlot> current;
377+
for (const TrayIconSlot &slot : mLastOrderedIcons) {
378+
if (slot.view && desired.contains(slot) && slot.view->isTrayIconShown(slot.isText))
379+
current.append(slot);
380+
}
381+
382+
int keep = 0;
383+
if (!force) {
384+
while (keep < current.size() && keep < desired.size() && current[keep] == desired[keep])
385+
++keep;
386+
}
387+
388+
if (keep == desired.size() && current.size() == desired.size() && !mReorderTimer->isActive()) {
389+
mLastOrderedIcons = desired;
390+
return;
391+
}
392+
393+
mReorderTimer->stop();
394+
mPendingShowIcons.clear();
395+
396+
bool hidAny = false;
397+
for (int i = keep; i < current.size(); ++i) {
398+
if (current[i].view && current[i].view->isTrayIconShown(current[i].isText)) {
399+
current[i].view->hideAndDestroyTrayIcon(current[i].isText);
400+
hidAny = true;
401+
}
402+
}
403+
for (int i = keep; i < desired.size(); ++i) {
404+
const TrayIconSlot &slot = desired[i];
405+
if (slot.view && slot.view->isTrayIconShown(slot.isText)) {
406+
slot.view->hideAndDestroyTrayIcon(slot.isText);
407+
hidAny = true;
408+
}
409+
mPendingShowIcons.append(slot);
410+
}
411+
412+
mLastOrderedIcons = desired.mid(0, keep);
413+
if (!mPendingShowIcons.isEmpty())
414+
mReorderTimer->start(hidAny ? 30 : 0);
415+
}
416+
417+
void QNetStats::stepTrayReorder() {
418+
while (!mPendingShowIcons.isEmpty()) {
419+
const TrayIconSlot slot = mPendingShowIcons.takeFirst();
420+
if (!slot.view)
421+
continue;
422+
const bool wanted = slot.isText ? slot.view->shouldShowTextIcon() : slot.view->shouldShowActivityIcon();
423+
if (!wanted)
424+
continue;
425+
slot.view->showRecreatedTrayIcon(slot.isText);
426+
mLastOrderedIcons.append(slot);
427+
if (!mPendingShowIcons.isEmpty())
428+
mReorderTimer->start(30);
429+
return;
430+
}
330431
}
331432

332433
void QNetStats::showAbout() {
@@ -483,7 +584,6 @@ void QNetStats::saveConfig(const OptionsMap &options) {
483584
mViews.erase(trayIcon);
484585
}
485586
}
486-
checkTrayIconsAvailable();
487587
}
488588

489589
settings.setValue("CurrentViews", QStringList(mViews.keys()));

‎src/qnetstats.h‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
#define QNETSTATS_H
33

44
#include "configure.h"
5+
#include "preferences.h"
56
#include <QHash>
7+
#include <QList>
68
#include <QPointer>
79
#include <QSystemTrayIcon>
810

@@ -11,7 +13,7 @@ class QListWidget;
1113
class QPushButton;
1214
class QMenu;
1315
class QAction;
14-
class Preferences;
16+
class QTimer;
1517

1618
class QNetStats : public QDialog {
1719
Q_OBJECT
@@ -26,6 +28,7 @@ Q_OBJECT
2628
static void readInterfaceConfig(const QString &ifName, ViewOptions *opts);
2729

2830
void checkTrayIconsAvailable();
31+
TrayIconOrder trayIconOrder() const { return mTrayIconOrder; }
2932
void addInterfaceHistoryMenu(QMenu *menu);
3033
void showInterfaceNotification(const QString &message,
3134
QSystemTrayIcon::MessageIcon icon, int milliseconds);
@@ -43,6 +46,14 @@ public slots:
4346
void preferencesApply();
4447

4548
private:
49+
struct TrayIconSlot {
50+
QPointer<QNetStatsView> view;
51+
bool isText = false;
52+
bool operator==(const TrayIconSlot &other) const {
53+
return view == other.view && isText == other.isText;
54+
}
55+
};
56+
4657
typedef QHash<QString, QNetStatsView *> TrayIconMap;
4758
QSystemTrayIcon *mBackupTrayIcon;
4859
QMenu *mBackupStatisticsMenu;
@@ -54,13 +65,19 @@ public slots:
5465
QPushButton *mFallbackStatistics;
5566
QAction *mDonationAction = nullptr;
5667
QPointer<QDialog> mAboutDialog;
68+
TrayIconOrder mTrayIconOrder = TrayIconOrder::Automatic;
69+
QTimer *mReorderTimer = nullptr;
70+
QList<TrayIconSlot> mLastOrderedIcons;
71+
QList<TrayIconSlot> mPendingShowIcons;
5772

5873
void setup();
5974

6075
void setupBackupTrayIcon();
6176
void setupFallbackWindow();
6277
void updateFallbackWindow();
6378
void showSelectedStatistics();
79+
void syncTrayIconOrder(bool force = false);
80+
void stepTrayReorder();
6481

6582
void saveConfig(const OptionsMap &options);
6683
};

0 commit comments

Comments
 (0)