service/tray: provide default sorting for SystemTray.items
Items are first sorted by category, and then by name.
This commit is contained in:
parent
392f56c40e
commit
69430e3873
3 changed files with 30 additions and 2 deletions
|
@ -1,5 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include <bit>
|
||||
#include <qabstractitemmodel.h>
|
||||
#include <qcontainerfwd.h>
|
||||
|
@ -98,6 +100,19 @@ public:
|
|||
this->UntypedObjectModel::insertObject(object, index);
|
||||
}
|
||||
|
||||
void insertObjectSorted(T* object, const std::function<bool(T*, T*)>& compare) {
|
||||
auto& list = this->valueList();
|
||||
auto iter = list.begin();
|
||||
|
||||
while (iter != list.end()) {
|
||||
if (!compare(object, *iter)) break;
|
||||
++iter;
|
||||
}
|
||||
|
||||
auto idx = iter - list.begin();
|
||||
this->UntypedObjectModel::insertObject(object, idx);
|
||||
}
|
||||
|
||||
void removeObject(const T* object) { this->UntypedObjectModel::removeObject(object); }
|
||||
|
||||
// Assumes only one instance of a specific value
|
||||
|
|
|
@ -17,10 +17,20 @@ SystemTray::SystemTray(QObject* parent): QObject(parent) {
|
|||
// clang-format on
|
||||
|
||||
for (auto* item: host->items()) {
|
||||
this->mItems.insertObject(item);
|
||||
this->mItems.insertObjectSorted(item, &SystemTray::compareItems);
|
||||
}
|
||||
}
|
||||
|
||||
void SystemTray::onItemRegistered(StatusNotifierItem* item) { this->mItems.insertObject(item); }
|
||||
void SystemTray::onItemRegistered(StatusNotifierItem* item) {
|
||||
this->mItems.insertObjectSorted(item, &SystemTray::compareItems);
|
||||
}
|
||||
|
||||
void SystemTray::onItemUnregistered(StatusNotifierItem* item) { this->mItems.removeObject(item); }
|
||||
ObjectModel<StatusNotifierItem>* SystemTray::items() { return &this->mItems; }
|
||||
|
||||
bool SystemTray::compareItems(
|
||||
qs::service::sni::StatusNotifierItem* a,
|
||||
qs::service::sni::StatusNotifierItem* b
|
||||
) {
|
||||
return a->category() > b->category() || a->id().compare(b->id(), Qt::CaseInsensitive) >= 0;
|
||||
}
|
||||
|
|
|
@ -31,5 +31,8 @@ private slots:
|
|||
void onItemUnregistered(qs::service::sni::StatusNotifierItem* item);
|
||||
|
||||
private:
|
||||
static bool
|
||||
compareItems(qs::service::sni::StatusNotifierItem* a, qs::service::sni::StatusNotifierItem* b);
|
||||
|
||||
ObjectModel<qs::service::sni::StatusNotifierItem> mItems {this};
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue