Skip to content

Commit

Permalink
chore: set plugin visible
Browse files Browse the repository at this point in the history
set plugin visible

Log:
  • Loading branch information
wangfei committed Jul 5, 2024
1 parent b92b515 commit 6ea1f29
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/loader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ file(GLOB SRCS "*.h" "*.cpp" "../../interfaces/*.h" "utils/*.h" "utils/*.cpp")

add_executable(dockplugin-loader
main.cpp
dockdbusproxy.cpp
dockdbusproxy.h
widgetplugin.h
widgetplugin.cpp
pluginitem.h
Expand Down
67 changes: 67 additions & 0 deletions src/loader/dockdbusproxy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
//
//SPDX-License-Identifier: GPL-3.0-or-later
#include "dockdbusproxy.h"

#include <QDBusConnection>
#include <QDBusInterface>
#include <QDBusMetaType>

const static QString DockService = "org.deepin.dde.Dock1";
const static QString DockPath = "/org/deepin/dde/Dock1";
const static QString DockInterface = "org.deepin.dde.Dock1";

const static QString PropertiesInterface = "org.freedesktop.DBus.Properties";
const static QString PropertiesChanged = "PropertiesChanged";

QDBusArgument &operator<<(QDBusArgument &arg, const DockItemInfo &info)
{
arg.beginStructure();
arg << info.name << info.displayName << info.itemKey << info.settingKey << info.dcc_icon << info.visible;
arg.endStructure();
return arg;
}

const QDBusArgument &operator>>(const QDBusArgument &arg, DockItemInfo &info)
{
arg.beginStructure();
arg >> info.name >> info.displayName >> info.itemKey >> info.settingKey >> info.dcc_icon >> info.visible;
arg.endStructure();
return arg;
}

DockDBusProxy::DockDBusProxy(QObject *parent)
: QObject(parent)
, m_dockInter(new QDBusInterface(DockService, DockPath, DockInterface, QDBusConnection::sessionBus(), this))
{
QDBusConnection::sessionBus().connect(DockService, DockPath, DockInterface, "pluginVisibleChanged", this, SLOT(pluginVisibleChanged(const QString &, bool)));

registerDockItemType();
}

void DockDBusProxy::registerDockItemType()
{
static bool isRegister = false;
if (isRegister)
return;

qRegisterMetaType<DockItemInfo>("DockItemInfo");
qDBusRegisterMetaType<DockItemInfo>();
qRegisterMetaType<DockItemInfos>("DockItemInfos");
qDBusRegisterMetaType<DockItemInfos>();
isRegister = true;
}

QDBusPendingReply<DockItemInfos> DockDBusProxy::plugins()
{
QDBusPendingReply<DockItemInfos> reply = m_dockInter->asyncCall(QStringLiteral("plugins"));
reply.waitForFinished();
return reply;
}

QDBusPendingReply<> DockDBusProxy::setItemOnDock(const QString settingKey, const QString &itemKey, bool visible)

Check warning on line 62 in src/loader/dockdbusproxy.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Parameter 'settingKey' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++.
{
QList<QVariant> argumengList;
argumengList << settingKey << itemKey << QVariant::fromValue(visible);
return m_dockInter->asyncCallWithArgumentList("setItemOnDock", argumengList);
}
52 changes: 52 additions & 0 deletions src/loader/dockdbusproxy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
//
//SPDX-License-Identifier: GPL-3.0-or-later
#ifndef DOCKDBUSPROXY_H
#define DOCKDBUSPROXY_H

#include <QObject>
#include <QDBusPendingReply>

class QDBusInterface;
class QDBusMessage;

struct DockItemInfo
{
QString name;
QString displayName;
QString itemKey;
QString settingKey;
QString dcc_icon;
bool visible;
};

QDBusArgument &operator<<(QDBusArgument &arg, const DockItemInfo &info);
const QDBusArgument &operator>>(const QDBusArgument &arg, DockItemInfo &info);

Q_DECLARE_METATYPE(DockItemInfo)

typedef QList<DockItemInfo> DockItemInfos;

Q_DECLARE_METATYPE(DockItemInfos)

class DockDBusProxy : public QObject
{
Q_OBJECT

public:
explicit DockDBusProxy(QObject *parent = nullptr);

static void registerDockItemType();

public Q_SLOTS:
QDBusPendingReply<DockItemInfos> plugins();
QDBusPendingReply<> setItemOnDock(const QString settingKey, const QString &itemKey, bool visible);

Check warning on line 43 in src/loader/dockdbusproxy.h

View workflow job for this annotation

GitHub Actions / cppcheck

Parameter 'settingKey' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++.

Q_SIGNALS:
void pluginVisibleChanged(const QString &itemKey, bool visible) const;

private:
QDBusInterface *m_dockInter;
};

#endif // DOCKDBUSPROXY_H
21 changes: 13 additions & 8 deletions src/loader/pluginitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,7 @@ PluginItem::PluginItem(PluginsItemInterface *pluginItemInterface, const QString
connect(m_menu, &QMenu::triggered, this, [this](QAction *action){
QString actionStr = action->data().toString();
if (actionStr == Dock::dockMenuItemId || actionStr == Dock::unDockMenuItemId) {
auto widget = m_pluginsItemInterface->itemWidget(m_itemKey);
if (widget && widget->window() && widget->window()->windowHandle()) {
if (actionStr == Dock::dockMenuItemId) {
widget->window()->windowHandle()->show();
} else if (actionStr == Dock::unDockMenuItemId) {
widget->window()->windowHandle()->hide();
}
}
setPluginVisible(actionStr == Dock::dockMenuItemId);
} else {
m_pluginsItemInterface->invokedMenuItem(m_itemKey, action->data().toString(), action->isCheckable() ? action->isChecked() : true);
}
Expand Down Expand Up @@ -302,3 +295,15 @@ bool PluginItem::executeCommand()
}
return false;
}

void PluginItem::setPluginVisible(bool isVisible)
{
auto widget = m_pluginsItemInterface->itemWidget(m_itemKey);
if (widget && widget->window() && widget->window()->windowHandle()) {
if (isVisible) {
widget->window()->windowHandle()->show();
} else {
widget->window()->windowHandle()->hide();
}
}
}
1 change: 1 addition & 0 deletions src/loader/pluginitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class PluginItem : public QWidget
void initPluginMenu();
QWidget *itemTooltip(const QString &itemKey);
bool executeCommand();
void setPluginVisible(bool isvisible);

private:
QWidget *itemPopupApplet();
Expand Down
32 changes: 31 additions & 1 deletion src/loader/quickpluginitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <QMouseEvent>
#include <QMenu>

const static QString DockQuickPlugins = "Dock_Quick_Plugins";

class Q_DECL_HIDDEN EventFilter : public QObject
{
public:
Expand Down Expand Up @@ -65,7 +67,10 @@ QuickPluginItem::QuickPluginItem(PluginsItemInterface *pluginInterface, const QS
: PluginItem(pluginInterface, itemKey, parent)
, m_onDockAction(nullptr)
{
if (m_dbusProxy.isNull())
m_dbusProxy.reset(new DockDBusProxy);
qApp->installEventFilter(new EventFilter(this));
connect(m_dbusProxy.data(), &DockDBusProxy::pluginVisibleChanged, this, &QuickPluginItem::onPluginVisibleChanged);
}

QWidget *QuickPluginItem::centralWidget()
Expand Down Expand Up @@ -110,10 +115,13 @@ QMenu *QuickPluginItem::pluginContextMenu()
}

if (m_onDockAction) {
bool onDock = true; // TODO 要找到对应插件是否在任务栏上显示,1070通过dbus获取
bool onDock = pluginIsVisible();
QString itemText = onDock ? tr("Remove from dock") : tr("Pin to dock");
m_onDockAction->setText(itemText);
m_onDockAction->setData(onDock ? Dock::unDockMenuItemId : Dock::dockMenuItemId);
connect(m_onDockAction, &QAction::trigger, this, [this, onDock]{
m_dbusProxy->setItemOnDock(DockQuickPlugins, PluginItem::itemKey(), !onDock);
});
}

m_menu->setAttribute(Qt::WA_TranslucentBackground, true);
Expand Down Expand Up @@ -141,3 +149,25 @@ QString QuickPluginItem::itemKey() const
{
return Dock::QUICK_ITEM_KEY;
}

void QuickPluginItem::onPluginVisibleChanged(const QString &itemKey, bool visible)
{
if (itemKey == PluginItem::itemKey()) {
setPluginVisible(visible);
}
}

bool QuickPluginItem::pluginIsVisible()
{
const QString itemKey = PluginItem::itemKey();

QDBusPendingReply<DockItemInfos> reply = m_dbusProxy->plugins();
DockItemInfos plugins = reply.value();
for (const DockItemInfo &info : plugins) {
if (info.itemKey == itemKey) {

Check warning on line 167 in src/loader/quickpluginitem.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Consider using std::find_if algorithm instead of a raw loop.
return info.visible;
}
}
return false;
}

8 changes: 8 additions & 0 deletions src/loader/quickpluginitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define QUICK_PLUGIN_ITEM_H

#include "pluginitem.h"
#include "dockdbusproxy.h"

class QuickPluginItem : public PluginItem
{
Expand All @@ -22,8 +23,15 @@ class QuickPluginItem : public PluginItem
virtual QWidget *pluginTooltip() override;
virtual QString itemKey() const override;

public Q_SLOTS:
void onPluginVisibleChanged(const QString &itemKey, bool visible);

private:
bool pluginIsVisible();

private:
QAction *m_onDockAction;
QScopedPointer<DockDBusProxy> m_dbusProxy;
};

#endif

0 comments on commit 6ea1f29

Please sign in to comment.