From 11c6e2db4bb503834aefde3731a54d94a7983447 Mon Sep 17 00:00:00 2001 From: Yixue Wang Date: Thu, 4 Jan 2024 14:18:15 +0800 Subject: [PATCH] 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 --- plugins/CMakeLists.txt | 1 + plugins/notification/CMakeLists.txt | 28 ++++ plugins/notification/icons/notification.dci | Bin 0 -> 997 bytes plugins/notification/notification.cpp | 33 ++++ plugins/notification/notification.h | 29 ++++ plugins/notification/notification.json | 3 + plugins/notification/notification.qrc | 5 + plugins/notification/notificationplugin.cpp | 160 ++++++++++++++++++++ plugins/notification/notificationplugin.h | 51 +++++++ 9 files changed, 310 insertions(+) create mode 100644 plugins/notification/CMakeLists.txt create mode 100644 plugins/notification/icons/notification.dci create mode 100644 plugins/notification/notification.cpp create mode 100644 plugins/notification/notification.h create mode 100644 plugins/notification/notification.json create mode 100644 plugins/notification/notification.qrc create mode 100644 plugins/notification/notificationplugin.cpp create mode 100644 plugins/notification/notificationplugin.h diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 074adc668..78d46d085 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -12,3 +12,4 @@ add_subdirectory("show-desktop") add_subdirectory("multitasking") add_subdirectory("bluetooth") add_subdirectory("airplane-mode") +add_subdirectory("notification") diff --git a/plugins/notification/CMakeLists.txt b/plugins/notification/CMakeLists.txt new file mode 100644 index 000000000..02201f5f0 --- /dev/null +++ b/plugins/notification/CMakeLists.txt @@ -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 + $ + $ + $ +) + +target_link_libraries(${PLUGIN_NAME} PRIVATE + Dtk::Widget + Qt5::DBus +) + +install(TARGETS ${PLUGIN_NAME} LIBRARY DESTINATION lib/dde-dock/plugins) diff --git a/plugins/notification/icons/notification.dci b/plugins/notification/icons/notification.dci new file mode 100644 index 0000000000000000000000000000000000000000..2f5b99480229192de5cde03787eeb06de6cec9c1 GIT binary patch literal 997 zcmZ>9_GDmWWME)2G-IFyOl5}XX3EPi%1zACOGzxsCda(BP)$t6bacE^o$aHV||#}Fi(TUNirUyID!d6Q|f5}s5y)yuKiWCBxA}$^ zeK(rDjW=>~Sowl84Zm=%2v(dZ~uICWpsl5vm^T%&m4Pt z`iIVjhvgU9zjPGxUwE!i%AcHM$@X1yFQWzL?H2z8hkW*3M|N?SU2&3==iGgSJt>oK+;N`Mqp# zYur^4?eENT&F=Ev;@Ne2?~J`q?wstn*-_(sf7QuV@2xxPFa16< T`^#x_U0-=ydwJjKPvUt1+Sl*n literal 0 HcmV?d00001 diff --git a/plugins/notification/notification.cpp b/plugins/notification/notification.cpp new file mode 100644 index 000000000..7026815da --- /dev/null +++ b/plugins/notification/notification.cpp @@ -0,0 +1,33 @@ +// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: LGPL-3.0-or-later +#include "notification.h" + +#include +#include +#include +#include +#include + +#include +#include + +DWIDGET_USE_NAMESPACE; +Notification::Notification(QWidget *parent) + : QWidget(parent) + , m_icon(QIcon::fromTheme("notification")) +{ + setMinimumSize(PLUGIN_BACKGROUND_MIN_SIZE, PLUGIN_BACKGROUND_MIN_SIZE); +} + +QIcon Notification::icon() const +{ + return m_icon; +} + +void Notification::paintEvent(QPaintEvent *e) +{ + Q_UNUSED(e) + QPainter p(this); + m_icon.paint(&p, rect()); +} diff --git a/plugins/notification/notification.h b/plugins/notification/notification.h new file mode 100644 index 000000000..d78e9207e --- /dev/null +++ b/plugins/notification/notification.h @@ -0,0 +1,29 @@ +// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: LGPL-3.0-or-later +#ifndef NOTIFICATION_H +#define NOTIFICATION_H +#include "constants.h" + +#include + +#include +#include + +DGUI_USE_NAMESPACE +class Notification : public QWidget +{ + Q_OBJECT + +public: + explicit Notification(QWidget *parent = nullptr); + QIcon icon() const; + +protected: + void paintEvent(QPaintEvent *e) override; + +private: + QIcon m_icon; +}; + +#endif // NOTIFICATION_H diff --git a/plugins/notification/notification.json b/plugins/notification/notification.json new file mode 100644 index 000000000..1e8c6b553 --- /dev/null +++ b/plugins/notification/notification.json @@ -0,0 +1,3 @@ +{ + "api": "2.0.0" +} \ No newline at end of file diff --git a/plugins/notification/notification.qrc b/plugins/notification/notification.qrc new file mode 100644 index 000000000..9d9f8ce09 --- /dev/null +++ b/plugins/notification/notification.qrc @@ -0,0 +1,5 @@ + + + icons/notification.dci + + diff --git a/plugins/notification/notificationplugin.cpp b/plugins/notification/notificationplugin.cpp new file mode 100644 index 000000000..ebc9215ec --- /dev/null +++ b/plugins/notification/notificationplugin.cpp @@ -0,0 +1,160 @@ +// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: LGPL-3.0-or-later +#include "notificationplugin.h" + +#include + +#include +#include +#include + +Q_LOGGING_CATEGORY(qLcPluginNotification, "dock.plugin.notification") + +#define PLUGIN_STATE_KEY "enable" + +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"); +} + +const QString NotificationPlugin::pluginName() const +{ + return "notification"; +} + +const QString NotificationPlugin::pluginDisplayName() const +{ + return tr("Notification"); +} + +QWidget *NotificationPlugin::itemWidget(const QString &itemKey) +{ + if (itemKey == pluginName()) + return m_notification.data(); + + return nullptr; +} + +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"); +} + +void NotificationPlugin::displayModeChanged(const Dock::DisplayMode displayMode) +{ + Q_UNUSED(displayMode); + + if (!pluginIsDisable()) { + m_notification->update(); + } +} + +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) +{ + return m_notification->icon(); +} + +PluginsItemInterface::PluginMode NotificationPlugin::status() const +{ + return PluginsItemInterface::PluginMode::Active; +} + +QString NotificationPlugin::description() const +{ + return pluginDisplayName(); +} + +void NotificationPlugin::loadPlugin() +{ + if (m_pluginLoaded) { + return; + } + m_pluginLoaded = true; + + m_notification.reset(new Notification); + + m_proxyInter->itemAdded(this, pluginName()); + displayModeChanged(displayMode()); +} + +void NotificationPlugin::refreshPluginItemsVisible() +{ + if (pluginIsDisable()) + { + m_proxyInter->itemRemoved(this, pluginName()); + } else { + if (!m_pluginLoaded) { + loadPlugin(); + return; + } + m_proxyInter->itemAdded(this, pluginName()); + } +} + +NotificationPlugin::PluginType NotificationPlugin::type() +{ + return PluginType::Normal; +} + +PluginFlags NotificationPlugin::flags() const +{ + return PluginFlag::Type_Common | Attribute_ForceDock; +} diff --git a/plugins/notification/notificationplugin.h b/plugins/notification/notificationplugin.h new file mode 100644 index 000000000..037258a3d --- /dev/null +++ b/plugins/notification/notificationplugin.h @@ -0,0 +1,51 @@ +// 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; + const QString pluginDisplayName() const override; + void init(PluginProxyInterface *proxyInter) override; + void pluginStateSwitched() override; + bool pluginIsAllowDisable() override { return true; } + bool pluginIsDisable() override; + + QWidget *itemWidget(const QString &itemKey) override; + QWidget *itemTipsWidget(const QString &itemKey) override; + const QString itemCommand(const QString &itemKey) override; + void displayModeChanged(const Dock::DisplayMode displayMode) 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; + PluginMode status() const override; + PluginType type() override; + PluginFlags flags() const override; + QString description() const override; + +private: + void loadPlugin(); + void refreshPluginItemsVisible(); + +private: + bool m_pluginLoaded; + + QScopedPointer m_notification; + QScopedPointer m_tipsLabel; +}; + +#endif // NOTIFICATIONPLUGIN_H