fix: notification tips label and context menu

Add notification settings action. Notification tips label will display
notification count.

Log: fix notification tips label and context menu
This commit is contained in:
Yixue Wang 2024-01-09 18:12:35 +08:00 committed by Tsic
parent 263e5a4358
commit 01e2377d86
7 changed files with 136 additions and 18 deletions

View File

@ -23,6 +23,7 @@ DCORE_USE_NAMESPACE;
Notification::Notification(QWidget *parent) Notification::Notification(QWidget *parent)
: QWidget(parent) : QWidget(parent)
, m_icon(QIcon::fromTheme("notification")) , m_icon(QIcon::fromTheme("notification"))
, m_notificationCount(0)
, m_dbus(nullptr) , m_dbus(nullptr)
, m_dndMode(false) , m_dndMode(false)
{ {
@ -36,10 +37,10 @@ Notification::Notification(QWidget *parent)
QDBusReply<QDBusVariant> dnd = m_dbus->call(QLatin1String("GetSystemInfo"), QVariant::fromValue(0u)); QDBusReply<QDBusVariant> dnd = m_dbus->call(QLatin1String("GetSystemInfo"), QVariant::fromValue(0u));
if (!dnd.isValid()) { if (!dnd.isValid()) {
qCWarning(qLcPluginNotification) << dnd.error(); qCWarning(qLcPluginNotification) << dnd.error();
return ; } else {
m_dndMode = dnd.value().variant().toBool();
refreshIcon();
} }
m_dndMode = dnd.value().variant().toBool();
refreshIcon();
QDBusConnection::sessionBus().connect("org.deepin.dde.Notification1", QDBusConnection::sessionBus().connect("org.deepin.dde.Notification1",
"/org/deepin/dde/Notification1", "/org/deepin/dde/Notification1",
"org.deepin.dde.Notification1", "org.deepin.dde.Notification1",
@ -47,6 +48,19 @@ Notification::Notification(QWidget *parent)
this, this,
SLOT(onSystemInfoChanged(quint32,QDBusVariant)) SLOT(onSystemInfoChanged(quint32,QDBusVariant))
); );
auto recordCountVariant = m_dbus->property("recordCount");
if (!recordCountVariant.isValid()) {
qCWarning(qLcPluginNotification) << dnd.error();
} else {
setNotificationCount(recordCountVariant.toUInt());
}
QDBusConnection::sessionBus().connect("org.deepin.dde.Notification1",
"/org/deepin/dde/Notification1",
"org.deepin.dde.Notification1",
"recordCountChanged",
this,
SLOT(setNotificationCount(uint))
);
}); });
} }
@ -57,7 +71,7 @@ QIcon Notification::icon() const
void Notification::refreshIcon() void Notification::refreshIcon()
{ {
m_icon = QIcon::fromTheme(m_dndMode ? "notification-off" : "notification"); m_icon = QIcon::fromTheme(dndMode() ? "notification-off" : "notification");
Q_EMIT iconRefreshed(); Q_EMIT iconRefreshed();
} }
@ -73,6 +87,11 @@ void Notification::setDndMode(bool dnd)
} }
} }
uint Notification::notificationCount() const
{
return m_notificationCount;
}
void Notification::paintEvent(QPaintEvent *e) void Notification::paintEvent(QPaintEvent *e)
{ {
Q_UNUSED(e) Q_UNUSED(e)
@ -88,3 +107,12 @@ void Notification::onSystemInfoChanged(quint32 info, QDBusVariant value)
Q_EMIT dndModeChanged(m_dndMode); Q_EMIT dndModeChanged(m_dndMode);
} }
} }
void Notification::setNotificationCount(uint count)
{
if (m_notificationCount == count) {
return;
}
m_notificationCount = count;
Q_EMIT this->notificationCountChanged(count);
}

View File

@ -15,28 +15,35 @@ class Notification : public QWidget
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(bool dndMode READ dndMode WRITE setDndMode NOTIFY dndModeChanged)
Q_PROPERTY(uint notificationCount READ notificationCount NOTIFY notificationCountChanged)
public: public:
explicit Notification(QWidget *parent = nullptr); explicit Notification(QWidget *parent = nullptr);
QIcon icon() const; QIcon icon() const;
bool dndMode() const; bool dndMode() const;
void setDndMode(bool dnd); void setDndMode(bool dnd);
uint notificationCount() const;
Q_SIGNALS: Q_SIGNALS:
void iconRefreshed(); void iconRefreshed();
void dndModeChanged(bool dnd); void dndModeChanged(bool dnd);
void notificationCountChanged(uint count);
public Q_SLOTS: public Q_SLOTS:
void refreshIcon(); void refreshIcon();
private Q_SLOTS: private Q_SLOTS:
void onSystemInfoChanged(quint32 info, QDBusVariant value); void onSystemInfoChanged(quint32 info, QDBusVariant value);
void setNotificationCount(uint count);
protected: protected:
void paintEvent(QPaintEvent *e) override; void paintEvent(QPaintEvent *e) override;
private: private:
QIcon m_icon; QIcon m_icon;
uint m_notificationCount;
QScopedPointer<QDBusInterface> m_dbus; QScopedPointer<QDBusInterface> m_dbus;
bool m_dndMode; bool m_dndMode;
}; };

View File

@ -4,6 +4,7 @@
#include "notificationplugin.h" #include "notificationplugin.h"
#include <DGuiApplicationHelper> #include <DGuiApplicationHelper>
#include <DDBusSender>
#include <QIcon> #include <QIcon>
#include <QSettings> #include <QSettings>
@ -11,8 +12,9 @@
Q_LOGGING_CATEGORY(qLcPluginNotification, "dock.plugin.notification") Q_LOGGING_CATEGORY(qLcPluginNotification, "dock.plugin.notification")
#define PLUGIN_STATE_KEY "enable" #define PLUGIN_STATE_KEY "enable"
#define TOGGLE_DND "toggle-dnd" #define TOGGLE_DND "toggle-dnd"
#define NOTIFICATION_SETTINGS "notification-settings"
DGUI_USE_NAMESPACE DGUI_USE_NAMESPACE
using namespace Dock; using namespace Dock;
@ -23,7 +25,7 @@ NotificationPlugin::NotificationPlugin(QObject *parent)
, m_notification(nullptr) , m_notification(nullptr)
, m_tipsLabel(new TipsWidget) , m_tipsLabel(new TipsWidget)
{ {
m_tipsLabel->setText(tr("Notification")); m_tipsLabel->setText(tr("No messages"));
m_tipsLabel->setVisible(false); m_tipsLabel->setVisible(false);
m_tipsLabel->setAccessibleName("Notification"); m_tipsLabel->setAccessibleName("Notification");
m_tipsLabel->setObjectName("NotificationTipsLabel"); m_tipsLabel->setObjectName("NotificationTipsLabel");
@ -72,14 +74,19 @@ const QString NotificationPlugin::itemContextMenu(const QString &itemKey)
QList<QVariant> items; QList<QVariant> items;
QMap<QString, QVariant> toggleDnd; QMap<QString, QVariant> toggleDnd;
toggleDnd["itemId"] = TOGGLE_DND; toggleDnd["itemId"] = TOGGLE_DND;
toggleDnd["itemText"] = tr("Do Not Disturb"); toggleDnd["itemText"] = toggleDndText();
toggleDnd["isCheckable"] = true; toggleDnd["isCheckable"] = false;
toggleDnd["isActive"] = true; toggleDnd["isActive"] = true;
toggleDnd["checked"] = m_notification->dndMode();
items.push_back(toggleDnd); items.push_back(toggleDnd);
QMap<QString, QVariant> notificationSettings;
notificationSettings["itemId"] = NOTIFICATION_SETTINGS;
notificationSettings["itemText"] = tr("Notification settings");
notificationSettings["isCheckable"] = false;
notificationSettings["isActive"] = true;
items.push_back(notificationSettings);
QMap<QString, QVariant> menu; QMap<QString, QVariant> menu;
menu["items"] = items; menu["items"] = items;
menu["checkableMenu"] = true; menu["checkableMenu"] = false;
menu["singleCheck"] = false; menu["singleCheck"] = false;
return QJsonDocument::fromVariant(menu).toJson(); return QJsonDocument::fromVariant(menu).toJson();
} }
@ -87,8 +94,15 @@ const QString NotificationPlugin::itemContextMenu(const QString &itemKey)
void NotificationPlugin::invokedMenuItem(const QString &itemKey, const QString &menuId, const bool checked) void NotificationPlugin::invokedMenuItem(const QString &itemKey, const QString &menuId, const bool checked)
{ {
Q_UNUSED(itemKey) Q_UNUSED(itemKey)
Q_UNUSED(checked)
if (menuId == TOGGLE_DND) { if (menuId == TOGGLE_DND) {
m_notification->setDndMode(checked); m_notification->setDndMode(!m_notification->dndMode());
} else if (menuId == NOTIFICATION_SETTINGS) {
DDBusSender().service("org.deepin.dde.ControlCenter1")
.path("/org/deepin/dde/ControlCenter1")
.interface("org.deepin.dde.ControlCenter1")
.method("ShowPage")
.arg(QString("notification")).call();
} }
} }
@ -125,6 +139,7 @@ void NotificationPlugin::loadPlugin()
m_pluginLoaded = true; m_pluginLoaded = true;
m_notification.reset(new Notification); m_notification.reset(new Notification);
connect(m_notification.data(), &Notification::iconRefreshed, this, [this]() { m_proxyInter->itemUpdate(this, pluginName()); }); connect(m_notification.data(), &Notification::iconRefreshed, this, [this]() { m_proxyInter->itemUpdate(this, pluginName()); });
connect(m_notification.data(), &Notification::notificationCountChanged, this, &NotificationPlugin::updateTipsText);
m_proxyInter->itemAdded(this, pluginName()); m_proxyInter->itemAdded(this, pluginName());
} }
@ -142,6 +157,24 @@ void NotificationPlugin::refreshPluginItemsVisible()
} }
} }
void NotificationPlugin::updateTipsText(uint notificationCount)
{
if (notificationCount == 0) {
m_tipsLabel->setText(tr("No messages"));
} else {
m_tipsLabel->setText(QString("%1 %2").arg(notificationCount).arg(tr("Notifications")));
}
}
QString NotificationPlugin::toggleDndText() const
{
if (m_notification->dndMode()) {
return tr("Turn off DND mode");
} else {
return tr("Turn on DND mode");
}
}
void NotificationPlugin::refreshIcon(const QString &itemKey) void NotificationPlugin::refreshIcon(const QString &itemKey)
{ {
Q_UNUSED(itemKey) Q_UNUSED(itemKey)

View File

@ -43,6 +43,8 @@ public:
private: private:
void loadPlugin(); void loadPlugin();
void refreshPluginItemsVisible(); void refreshPluginItemsVisible();
void updateTipsText(uint notificationCount);
QString toggleDndText() const;
private: private:
bool m_pluginLoaded; bool m_pluginLoaded;

View File

@ -336,8 +336,24 @@
<translation>Notification</translation> <translation>Notification</translation>
</message> </message>
<message> <message>
<source>Do Not Disturb</source> <source>No messages</source>
<translation>Do Not Disturb</translation> <translation>No messages</translation>
</message>
<message>
<source>Notification settings</source>
<translation>Notification settings</translation>
</message>
<message>
<source>Notifications</source>
<translation>Notifications</translation>
</message>
<message>
<source>Turn off DND mode</source>
<translation>Turn off DND mode</translation>
</message>
<message>
<source>Turn on DND mode</source>
<translation>Turn on DND mode</translation>
</message> </message>
</context> </context>
<context> <context>

View File

@ -308,8 +308,24 @@
<translation>Notification</translation> <translation>Notification</translation>
</message> </message>
<message> <message>
<source>Do Not Disturb</source> <source>No messages</source>
<translation>Do Not Disturb</translation> <translation>No messages</translation>
</message>
<message>
<source>Notification settings</source>
<translation>Notification settings</translation>
</message>
<message>
<source>Notifications</source>
<translation>Notifications</translation>
</message>
<message>
<source>Turn off DND mode</source>
<translation>Turn off DND mode</translation>
</message>
<message>
<source>Turn on DND mode</source>
<translation>Turn on DND mode</translation>
</message> </message>
</context> </context>
<context> <context>

View File

@ -336,8 +336,24 @@
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<source>Do Not Disturb</source> <source>No messages</source>
<translation></translation> <translation></translation>
</message>
<message>
<source>Notification settings</source>
<translation></translation>
</message>
<message>
<source>Notifications</source>
<translation></translation>
</message>
<message>
<source>Turn off DND mode</source>
<translation></translation>
</message>
<message>
<source>Turn on DND mode</source>
<translation></translation>
</message> </message>
</context> </context>
<context> <context>