Use KStatusNotifierItem on the indicator

Seems to work a bit better than QSystemTrayIcon
This commit is contained in:
Aleix Pol 2017-03-27 20:44:12 +02:00
parent a5ff478896
commit 32002e3c46
2 changed files with 29 additions and 7 deletions

View file

@ -1,4 +1,6 @@
find_package(KF5Notifications REQUIRED)
add_executable(kdeconnect-indicator main.cpp deviceindicator.cpp) add_executable(kdeconnect-indicator main.cpp deviceindicator.cpp)
target_link_libraries(kdeconnect-indicator Qt5::Widgets KF5::CoreAddons KF5::I18n kdeconnectinterfaces) target_link_libraries(kdeconnect-indicator Qt5::Widgets KF5::CoreAddons KF5::I18n KF5::Notifications kdeconnectinterfaces)
install(TARGETS kdeconnect-indicator ${INSTALL_TARGETS_DEFAULT_ARGS}) install(TARGETS kdeconnect-indicator ${INSTALL_TARGETS_DEFAULT_ARGS})

View file

@ -24,7 +24,12 @@
#include <QDBusConnection> #include <QDBusConnection>
#include <QApplication> #include <QApplication>
#include <QTextStream> #include <QTextStream>
#ifdef QSYSTRAY
#include <QSystemTrayIcon> #include <QSystemTrayIcon>
#else
#include <KStatusNotifierItem>
#endif
#include <KAboutData> #include <KAboutData>
#include <KLocalizedString> #include <KLocalizedString>
@ -49,15 +54,10 @@ int main(int argc, char** argv)
DevicesModel model; DevicesModel model;
model.setDisplayFilter(DevicesModel::Reachable | DevicesModel::Paired); model.setDisplayFilter(DevicesModel::Reachable | DevicesModel::Paired);
QSystemTrayIcon systray;
systray.setIcon(QIcon::fromTheme("kdeconnect"));
systray.setVisible(true);
systray.setToolTip("KDE Connect");
QMenu *menu = new QMenu; QMenu *menu = new QMenu;
DaemonDbusInterface iface; DaemonDbusInterface iface;
auto refreshMenu = [&systray, &iface, &model, &menu]() { auto refreshMenu = [&iface, &model, &menu]() {
menu->clear(); menu->clear();
auto configure = menu->addAction(i18n("Configure...")); auto configure = menu->addAction(i18n("Configure..."));
QObject::connect(configure, &QAction::triggered, configure, [](){ QObject::connect(configure, &QAction::triggered, configure, [](){
@ -87,11 +87,31 @@ int main(int argc, char** argv)
QObject::connect(&model, &DevicesModel::rowsInserted, &model, refreshMenu); QObject::connect(&model, &DevicesModel::rowsInserted, &model, refreshMenu);
QObject::connect(&model, &DevicesModel::rowsRemoved, &model, refreshMenu); QObject::connect(&model, &DevicesModel::rowsRemoved, &model, refreshMenu);
#ifdef QSYSTRAY
QSystemTrayIcon systray;
systray.setIcon(QIcon::fromTheme("kdeconnect"));
systray.setVisible(true);
systray.setToolTip("KDE Connect");
QObject::connect(&model, &DevicesModel::rowsChanged, &model, [&systray, &model]() { QObject::connect(&model, &DevicesModel::rowsChanged, &model, [&systray, &model]() {
systray.setToolTip(i18np("%1 device connected", "%1 devices connected", model.rowCount())); systray.setToolTip(i18np("%1 device connected", "%1 devices connected", model.rowCount()));
}); });
systray.setContextMenu(menu); systray.setContextMenu(menu);
#else
KStatusNotifierItem systray;
systray.setIconByName(QStringLiteral("kdeconnect"));
systray.setToolTip(QStringLiteral("kdeconnect"), "KDE Connect", "KDE Connect");
systray.setCategory(KStatusNotifierItem::Communications);
systray.setStatus(KStatusNotifierItem::Passive);
systray.setStandardActionsEnabled(false);
QObject::connect(&model, &DevicesModel::rowsChanged, &model, [&systray, &model]() {
const auto count = model.rowCount();
systray.setStatus(count == 0 ? KStatusNotifierItem::Passive : KStatusNotifierItem::Active);
systray.setToolTip(QStringLiteral("kdeconnect"), QStringLiteral("KDE Connect"), i18np("%1 device connected", "%1 devices connected", count));
});
systray.setContextMenu(menu);
#endif
refreshMenu(); refreshMenu();