feat: add plugin notification

Add plugin notification to show notification center. Notification is a
fixed plugin.

Log: add plugin notification
Issue: https://github.com/linuxdeepin/developer-center/issues/6695
This commit is contained in:
Yixue Wang 2024-01-04 14:18:15 +08:00 committed by asterwyx
parent cf909cd4d7
commit 4eecd92395
16 changed files with 464 additions and 74 deletions

View File

@ -174,7 +174,7 @@
"visibility":"public"
},
"Dock_Quick_Plugins": {
"value": ["power", "network", "shutdown", "show-desktop", "multitasking"],
"value": ["power", "network", "shutdown", "show-desktop", "multitasking", "notification"],
"serial": 0,
"flags": [],
"name": "Quick Plugins show on dock",

View File

@ -9,4 +9,5 @@ usr/lib/dde-dock/plugins/quick-trays
usr/lib/dde-dock/plugins/libmultitasking.so
usr/lib/dde-dock/plugins/libshow-desktop.so
usr/lib/dde-dock/plugins/libkeyboard-layout.so
usr/lib/dde-dock/plugins/libnotification.so
usr/share/dsg/configs/dde-dock/

View File

@ -1068,5 +1068,5 @@ QPoint QuickDockItem::popupMarkPoint() const
void QuickDockItem::onMenuActionClicked(QAction *action)
{
m_pluginItem->invokedMenuItem(m_itemKey, action->data().toString(), true);
m_pluginItem->invokedMenuItem(m_itemKey, action->data().toString(), action->isCheckable() ? action->isChecked() : true);
}

View File

@ -12,3 +12,4 @@ add_subdirectory("show-desktop")
add_subdirectory("multitasking")
add_subdirectory("bluetooth")
add_subdirectory("airplane-mode")
add_subdirectory("notification")

View File

@ -0,0 +1,28 @@
set(PLUGIN_NAME "notification")
find_package(Qt5 REQUIRED COMPONENTS DBus)
find_package(Dtk REQUIRED COMPONENTS Widget)
add_library(${PLUGIN_NAME} SHARED
notification.h
notification.cpp
notificationplugin.h
notificationplugin.cpp
${CMAKE_SOURCE_DIR}/widgets/tipswidget.h
${CMAKE_SOURCE_DIR}/widgets/tipswidget.cpp
notification.qrc
)
target_compile_definitions(${PLUGIN_NAME} PRIVATE QT_PLUGIN)
set_target_properties(${PLUGIN_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/plugins)
target_include_directories(${PLUGIN_NAME} PRIVATE
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/interfaces>
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/widgets>
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/frame/qtdbusextended>
)
target_link_libraries(${PLUGIN_NAME} PRIVATE
Dtk::Widget
Qt5::DBus
)
install(TARGETS ${PLUGIN_NAME} LIBRARY DESTINATION lib/dde-dock/plugins)

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,90 @@
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "notification.h"
#include "constants.h"
#include <QPainter>
#include <QPainterPath>
#include <QMouseEvent>
#include <QApplication>
#include <QIcon>
#include <QDBusInterface>
#include <QDBusReply>
#include <QtConcurrent/QtConcurrent>
#include <DStyle>
#include <DGuiApplicationHelper>
Q_DECLARE_LOGGING_CATEGORY(qLcPluginNotification)
DWIDGET_USE_NAMESPACE;
DCORE_USE_NAMESPACE;
Notification::Notification(QWidget *parent)
: QWidget(parent)
, m_icon(QIcon::fromTheme("notification"))
, m_dbus(nullptr)
, m_dndMode(false)
{
setMinimumSize(PLUGIN_BACKGROUND_MIN_SIZE, PLUGIN_BACKGROUND_MIN_SIZE);
connect(this, &Notification::dndModeChanged, this, &Notification::refreshIcon);
QtConcurrent::run([this](){
m_dbus = new QDBusInterface("org.deepin.dde.Notification1", "/org/deepin/dde/Notification1", "org.deepin.dde.Notification1", QDBusConnection::sessionBus(), this);
// Refresh icon for the first time, cause org.deepin.dde.Notification1 might depend on dock's DBus,
// we should not call org.deepin.dde.Notification1 in the main thread before dock's dbus is initialized.
// Just refresh icon in the other thread.
QDBusReply<QDBusVariant> dnd = m_dbus->call(QLatin1String("GetSystemInfo"), QVariant::fromValue(0u));
if (!dnd.isValid()) {
qCWarning(qLcPluginNotification) << dnd.error();
return ;
}
m_dndMode = dnd.value().variant().toBool();
refreshIcon();
QDBusConnection::sessionBus().connect("org.deepin.dde.Notification1",
"/org/deepin/dde/Notification1",
"org.deepin.dde.Notification1",
"SystemInfoChanged",
this,
SLOT(onSystemInfoChanged(quint32,QDBusVariant))
);
});
}
QIcon Notification::icon() const
{
return m_icon;
}
void Notification::refreshIcon()
{
m_icon = QIcon::fromTheme(m_dndMode ? "notification-off" : "notification");
Q_EMIT iconRefreshed();
}
bool Notification::dndMode() const
{
return m_dndMode;
}
void Notification::setDndMode(bool dnd)
{
if (m_dbus) {
m_dbus->call(QLatin1String("SetSystemInfo"), QVariant::fromValue(0u), QVariant::fromValue(QDBusVariant(dnd)));
}
}
void Notification::paintEvent(QPaintEvent *e)
{
Q_UNUSED(e)
QPainter p(this);
m_icon.paint(&p, rect());
}
void Notification::onSystemInfoChanged(quint32 info, QDBusVariant value)
{
if (info == 0) {
// DND mode
m_dndMode = value.variant().toBool();
Q_EMIT dndModeChanged(m_dndMode);
}
}

View File

@ -0,0 +1,44 @@
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#ifndef NOTIFICATION_H
#define NOTIFICATION_H
#include <DGuiApplicationHelper>
#include <QWidget>
#include <QIcon>
#include <QDBusVariant>
#include <QDBusInterface>
class Notification : public QWidget
{
Q_OBJECT
public:
explicit Notification(QWidget *parent = nullptr);
QIcon icon() const;
bool dndMode() const;
void setDndMode(bool dnd);
Q_SIGNALS:
void iconRefreshed();
void dndModeChanged(bool dnd);
public Q_SLOTS:
void refreshIcon();
private Q_SLOTS:
void onSystemInfoChanged(quint32 info, QDBusVariant value);
protected:
void paintEvent(QPaintEvent *e) override;
private:
QIcon m_icon;
QDBusInterface *m_dbus;
bool m_dndMode;
};
#endif // NOTIFICATION_H

View File

@ -0,0 +1,4 @@
{
"api": "2.0.0",
"depends-daemon-dbus-service": "org.deepin.dde.Notification1"
}

View File

@ -0,0 +1,6 @@
<RCC>
<qresource prefix="/dsg/built-in-icons">
<file alias="notification.dci">icons/notification.dci</file>
<file alias="notification-off.dci">icons/notification-off.dci</file>
</qresource>
</RCC>

View File

@ -0,0 +1,149 @@
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "notificationplugin.h"
#include <DGuiApplicationHelper>
#include <QIcon>
#include <QSettings>
#include <QPainter>
Q_LOGGING_CATEGORY(qLcPluginNotification, "dock.plugin.notification")
#define PLUGIN_STATE_KEY "enable"
#define TOGGLE_DND "toggle-dnd"
DGUI_USE_NAMESPACE
using namespace Dock;
NotificationPlugin::NotificationPlugin(QObject *parent)
: QObject(parent)
, m_pluginLoaded(false)
, m_notification(nullptr)
, m_tipsLabel(new TipsWidget)
{
m_tipsLabel->setText(tr("Notification"));
m_tipsLabel->setVisible(false);
m_tipsLabel->setAccessibleName("Notification");
m_tipsLabel->setObjectName("NotificationTipsLabel");
}
QWidget *NotificationPlugin::itemWidget(const QString &itemKey)
{
Q_UNUSED(itemKey)
return m_notification.data();
}
QWidget *NotificationPlugin::itemTipsWidget(const QString &itemKey)
{
Q_UNUSED(itemKey);
return m_tipsLabel.data();
}
void NotificationPlugin::init(PluginProxyInterface *proxyInter)
{
m_proxyInter = proxyInter;
if (!pluginIsDisable()) {
loadPlugin();
}
}
void NotificationPlugin::pluginStateSwitched()
{
m_proxyInter->saveValue(this, PLUGIN_STATE_KEY, pluginIsDisable());
refreshPluginItemsVisible();
}
bool NotificationPlugin::pluginIsDisable()
{
return !(m_proxyInter->getValue(this, PLUGIN_STATE_KEY, true).toBool());
}
const QString NotificationPlugin::itemCommand(const QString &itemKey)
{
Q_UNUSED(itemKey);
return QString("dbus-send --session --print-reply --dest=org.deepin.dde.Widgets1 /org/deepin/dde/Widgets1 org.deepin.dde.Widgets1.Toggle");
}
const QString NotificationPlugin::itemContextMenu(const QString &itemKey)
{
QList<QVariant> items;
QMap<QString, QVariant> toggleDnd;
toggleDnd["itemId"] = TOGGLE_DND;
toggleDnd["itemText"] = tr("Do Not Disturb");
toggleDnd["isCheckable"] = true;
toggleDnd["isActive"] = true;
toggleDnd["checked"] = m_notification->dndMode();
items.push_back(toggleDnd);
QMap<QString, QVariant> menu;
menu["items"] = items;
menu["checkableMenu"] = true;
menu["singleCheck"] = false;
return QJsonDocument::fromVariant(menu).toJson();
}
void NotificationPlugin::invokedMenuItem(const QString &itemKey, const QString &menuId, const bool checked)
{
Q_UNUSED(itemKey)
if (menuId == TOGGLE_DND) {
m_notification->setDndMode(checked);
}
}
int NotificationPlugin::itemSortKey(const QString &itemKey)
{
const QString key = QString("pos_%1_%2").arg(itemKey).arg(Dock::Efficient);
return m_proxyInter->getValue(this, key, 3).toInt();
}
void NotificationPlugin::setSortKey(const QString &itemKey, const int order)
{
const QString key = QString("pos_%1_%2").arg(itemKey).arg(Dock::Efficient);
m_proxyInter->saveValue(this, key, order);
}
void NotificationPlugin::pluginSettingsChanged()
{
refreshPluginItemsVisible();
}
QIcon NotificationPlugin::icon(const DockPart &dockPart, DGuiApplicationHelper::ColorType themeType)
{
Q_UNUSED(themeType)
if (dockPart == DockPart::DCCSetting)
return QIcon::fromTheme("notification");
return m_notification->icon();
}
void NotificationPlugin::loadPlugin()
{
if (m_pluginLoaded) {
return;
}
m_pluginLoaded = true;
m_notification.reset(new Notification);
connect(m_notification.data(), &Notification::iconRefreshed, this, [this]() { m_proxyInter->itemUpdate(this, pluginName()); });
m_proxyInter->itemAdded(this, pluginName());
}
void NotificationPlugin::refreshPluginItemsVisible()
{
if (pluginIsDisable())
{
m_proxyInter->itemRemoved(this, pluginName());
} else {
if (!m_pluginLoaded) {
loadPlugin();
return;
}
m_proxyInter->itemAdded(this, pluginName());
}
}
void NotificationPlugin::refreshIcon(const QString &itemKey)
{
Q_UNUSED(itemKey)
m_notification->refreshIcon();
}

View File

@ -0,0 +1,53 @@
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#ifndef NOTIFICATIONPLUGIN_H
#define NOTIFICATIONPLUGIN_H
#include "pluginsiteminterface.h"
#include "notification.h"
#include "tipswidget.h"
class NotificationPlugin : public QObject, public PluginsItemInterface
{
Q_OBJECT
Q_INTERFACES(PluginsItemInterface)
Q_PLUGIN_METADATA(IID "com.deepin.dock.PluginsItemInterface" FILE "notification.json")
public:
explicit NotificationPlugin(QObject *parent = nullptr);
const QString pluginName() const override { return "notification"; }
const QString pluginDisplayName() const override { return tr("Notification"); }
PluginMode status() const override { return PluginMode::Active; }
PluginType type() override { return PluginType::Normal; }
PluginFlags flags() const override { return PluginFlag::Type_Common | PluginFlag::Attribute_CanSetting; }
QString description() const override { return pluginDisplayName(); }
bool pluginIsAllowDisable() override { return true; }
void init(PluginProxyInterface *proxyInter) override;
void pluginStateSwitched() override;
bool pluginIsDisable() override;
QWidget *itemWidget(const QString &itemKey) override;
QWidget *itemTipsWidget(const QString &itemKey) override;
const QString itemCommand(const QString &itemKey) override;
const QString itemContextMenu(const QString &itemKey) override;
void invokedMenuItem(const QString &itemKey, const QString &menuId, const bool checked) override;
int itemSortKey(const QString &itemKey) override;
void setSortKey(const QString &itemKey, const int order) override;
void pluginSettingsChanged() override;
QIcon icon(const DockPart &dockPart, DGuiApplicationHelper::ColorType themeType) override;
void refreshIcon(const QString &itemKey) override;
private:
void loadPlugin();
void refreshPluginItemsVisible();
private:
bool m_pluginLoaded;
QScopedPointer<Notification> m_notification;
QScopedPointer<Dock::TipsWidget> m_tipsLabel;
};
#endif // NOTIFICATIONPLUGIN_H

View File

@ -138,6 +138,17 @@
<translation>Add keyboard layout</translation>
</message>
</context>
<context>
<name>DBusHandler</name>
<message>
<source>failed to dock </source>
<translation>failed to dock </translation>
</message>
<message>
<source>Unrecognized application, unable to dock</source>
<translation>Unrecognized application, unable to dock</translation>
</message>
</context>
<context>
<name>DateTimeDisplayer</name>
<message>
@ -153,39 +164,6 @@
<translation>Time settings</translation>
</message>
</context>
<context>
<name>DatetimePlugin</name>
<message>
<source>Datetime</source>
<translation>Datetime</translation>
</message>
<message>
<source>12-hour time</source>
<translation>12-hour time</translation>
</message>
<message>
<source>24-hour time</source>
<translation>24-hour time</translation>
</message>
<message>
<source>Time settings</source>
<translation>Time settings</translation>
</message>
</context>
<context>
<name>DisplayPlugin</name>
<message>
<source>Brightness</source>
<translation type="unfinished"/>
</message>
</context>
<context>
<name>DisplayPlugin</name>
<message>
<source>Brightness</source>
<translation>Brightness</translation>
</message>
</context>
<context>
<name>DevCollaborationWidget</name>
<message>
@ -212,6 +190,13 @@
<translation>This action cannot be restored</translation>
</message>
</context>
<context>
<name>DisplayPlugin</name>
<message>
<source>Brightness</source>
<translation type="unfinished">Brightness</translation>
</message>
</context>
<context>
<name>DisplaySettingWidget</name>
<message>
@ -226,6 +211,33 @@
<translation>The plugin %1 is not compatible with the system.</translation>
</message>
</context>
<context>
<name>Entry</name>
<message>
<source>Open</source>
<translation>Open</translation>
</message>
<message>
<source>Close All</source>
<translation>Close All</translation>
</message>
<message>
<source>Force Quit</source>
<translation>Force Quit</translation>
</message>
<message>
<source>Dock</source>
<translation>Dock</translation>
</message>
<message>
<source>Undock</source>
<translation>Undock</translation>
</message>
<message>
<source>All Windows</source>
<translation>All Windows</translation>
</message>
</context>
<context>
<name>HomeMonitorPlugin</name>
<message>
@ -244,6 +256,13 @@
<translation>Launcher</translation>
</message>
</context>
<context>
<name>MediaPlayerModel</name>
<message>
<source>Unknown</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>MenuWorker</name>
<message>
@ -310,6 +329,17 @@
<translation>Undock</translation>
</message>
</context>
<context>
<name>NotificationPlugin</name>
<message>
<source>Notification</source>
<translation>Notification</translation>
</message>
<message>
<source>Do Not Disturb</source>
<translation>Do Not Disturb</translation>
</message>
</context>
<context>
<name>OnboardPlugin</name>
<message>
@ -473,42 +503,4 @@
<translation>The Dock is in safe mode, please exit to show it properly</translation>
</message>
</context>
<context>
<name>Entry</name>
<message>
<source>Open</source>
<translation>Open</translation>
</message>
<message>
<source>Close All</source>
<translation>Close All</translation>
</message>
<message>
<source>Force Quit</source>
<translation>Force Quit</translation>
</message>
<message>
<source>Dock</source>
<translation>Dock</translation>
</message>
<message>
<source>Undock</source>
<translation>Undock</translation>
</message>
<message>
<source>All Windows</source>
<translation>All Windows</translation>
</message>
</context>
<context>
<name>DBusHandler</name>
<message>
<source>failed to dock </source>
<translation>failed to dock </translation>
</message>
<message>
<source>Unrecognized application, unable to dock</source>
<translation>Unrecognized application, unable to dock</translation>
</message>
</context>
</TS>

View File

@ -301,6 +301,17 @@
<translation>Undock</translation>
</message>
</context>
<context>
<name>NotificationPlugin</name>
<message>
<source>Notification</source>
<translation>Notification</translation>
</message>
<message>
<source>Do Not Disturb</source>
<translation>Do Not Disturb</translation>
</message>
</context>
<context>
<name>OnboardPlugin</name>
<message>
@ -491,4 +502,4 @@
<translation>All Windows</translation>
</message>
</context>
</TS>
</TS>

View File

@ -329,6 +329,17 @@
<translation></translation>
</message>
</context>
<context>
<name>NotificationPlugin</name>
<message>
<source>Notification</source>
<translation></translation>
</message>
<message>
<source>Do Not Disturb</source>
<translation></translation>
</message>
</context>
<context>
<name>OnboardPlugin</name>
<message>