diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 50e447731..b98574c6c 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -11,3 +11,4 @@ add_subdirectory("keyboard-layout") add_subdirectory("onboard") add_subdirectory("overlay-warning") add_subdirectory("show-desktop") +add_subdirectory("multitasking") diff --git a/plugins/multitasking/CMakeLists.txt b/plugins/multitasking/CMakeLists.txt new file mode 100644 index 000000000..295196589 --- /dev/null +++ b/plugins/multitasking/CMakeLists.txt @@ -0,0 +1,26 @@ + +set(PLUGIN_NAME "multitasking") + +project(${PLUGIN_NAME}) + +# Sources files +file(GLOB SRCS "*.h" "*.cpp" "../../widgets/*.h" "../../widgets/*.cpp") + +find_package(PkgConfig REQUIRED) +find_package(Qt5Widgets REQUIRED) +find_package(Qt5Svg REQUIRED) +find_package(Qt5DBus REQUIRED) +find_package(DtkWidget REQUIRED) + +add_definitions("${QT_DEFINITIONS} -DQT_PLUGIN") +add_library(${PLUGIN_NAME} SHARED ${SRCS}) +set_target_properties(${PLUGIN_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ../) +target_include_directories(${PLUGIN_NAME} PUBLIC ${DtkWidget_INCLUDE_DIRS} ../../interfaces) +target_link_libraries(${PLUGIN_NAME} PRIVATE + ${Qt5DBus_LIBRARIES} + ${DtkWidget_LIBRARIES} + ${Qt5Widgets_LIBRARIES} + ${Qt5Svg_LIBRARIES} +) + +install(TARGETS ${PLUGIN_NAME} LIBRARY DESTINATION lib/dde-dock/plugins) diff --git a/plugins/multitasking/multitasking.json b/plugins/multitasking/multitasking.json new file mode 100644 index 000000000..ad498eeb3 --- /dev/null +++ b/plugins/multitasking/multitasking.json @@ -0,0 +1,3 @@ +{ + "api": "1.1.1" +} diff --git a/plugins/multitasking/multitaskingplugin.cpp b/plugins/multitasking/multitaskingplugin.cpp new file mode 100644 index 000000000..2409ee0c8 --- /dev/null +++ b/plugins/multitasking/multitaskingplugin.cpp @@ -0,0 +1,198 @@ +/* + * Copyright (C) 2019 ~ 2019 Deepin Technology Co., Ltd. + * + * Author: wangshaojun + * + * Maintainer: wangshaojun + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "multitaskingplugin.h" + +#include + +#define PLUGIN_STATE_KEY "enable" + +MultitaskingPlugin::MultitaskingPlugin(QObject *parent) + : QObject(parent) + , m_pluginLoaded(false) + , m_tipsLabel(new TipsWidget) +{ + m_tipsLabel->setVisible(false); + m_tipsLabel->setObjectName("multitasking"); +} + +const QString MultitaskingPlugin::pluginName() const +{ + return "multitasking"; +} + +const QString MultitaskingPlugin::pluginDisplayName() const +{ + return tr("Multitasking View"); +} + +QWidget *MultitaskingPlugin::itemWidget(const QString &itemKey) +{ + Q_UNUSED(itemKey); + + return m_multitaskingWidget; +} + +QWidget *MultitaskingPlugin::itemTipsWidget(const QString &itemKey) +{ + m_tipsLabel->setObjectName(itemKey); + + m_tipsLabel->setText(pluginDisplayName()); + + return m_tipsLabel; +} + +void MultitaskingPlugin::init(PluginProxyInterface *proxyInter) +{ + m_proxyInter = proxyInter; + + if (!pluginIsDisable()) { + loadPlugin(); + } +} + +void MultitaskingPlugin::pluginStateSwitched() +{ + m_proxyInter->saveValue(this, PLUGIN_STATE_KEY, pluginIsDisable()); + + refreshPluginItemsVisible(); +} + +bool MultitaskingPlugin::pluginIsDisable() +{ + return !m_proxyInter->getValue(this, PLUGIN_STATE_KEY, true).toBool(); +} + +const QString MultitaskingPlugin::itemCommand(const QString &itemKey) +{ + if (itemKey == PLUGIN_KEY) + return "dbus-send --session --dest=com.deepin.wm --print-reply /com/deepin/wm com.deepin.wm.PerformAction int32:1"; + + return ""; +} + +const QString MultitaskingPlugin::itemContextMenu(const QString &itemKey) +{ + if (itemKey != PLUGIN_KEY) { + return QString(); + } + + QList items; + items.reserve(6); + + QMap desktop; + desktop["itemId"] = "multitasking"; + desktop["itemText"] = tr("Multitasking View"); + desktop["isActive"] = true; + items.push_back(desktop); + + QMap power; + power["itemId"] = "remove"; + power["itemText"] = tr("Undock"); + power["isActive"] = true; + items.push_back(power); + + QMap menu; + menu["items"] = items; + menu["checkableMenu"] = false; + menu["singleCheck"] = false; + + return QJsonDocument::fromVariant(menu).toJson(); +} + +void MultitaskingPlugin::invokedMenuItem(const QString &itemKey, const QString &menuId, const bool checked) +{ + Q_UNUSED(itemKey) + Q_UNUSED(checked) + + if (menuId == "multitasking") { + QProcess::startDetached("dbus-send --session --dest=com.deepin.wm --print-reply /com/deepin/wm com.deepin.wm.PerformAction int32:1"); + } else if (menuId == "remove") { + pluginStateSwitched(); + } +} + +void MultitaskingPlugin::refreshIcon(const QString &itemKey) +{ + if (itemKey == PLUGIN_KEY) { + m_multitaskingWidget->refreshIcon(); + } +} + +int MultitaskingPlugin::itemSortKey(const QString &itemKey) +{ + const QString key = QString("pos_%1_%2").arg(itemKey).arg(displayMode()); + + return m_proxyInter->getValue(this, key, 2).toInt(); +} + +void MultitaskingPlugin::setSortKey(const QString &itemKey, const int order) +{ + const QString key = QString("pos_%1_%2").arg(itemKey).arg(displayMode()); + + m_proxyInter->saveValue(this, key, order); +} + +void MultitaskingPlugin::pluginSettingsChanged() +{ + refreshPluginItemsVisible(); +} + +PluginsItemInterface::PluginType MultitaskingPlugin::type() +{ + return PluginType::Fixed; +} + +void MultitaskingPlugin::updateBatteryVisible() +{ + if (pluginIsDisable()) + m_proxyInter->itemRemoved(this, PLUGIN_KEY); + else + m_proxyInter->itemAdded(this, PLUGIN_KEY); +} + +void MultitaskingPlugin::loadPlugin() +{ + if (m_pluginLoaded) { + return; + } + + m_pluginLoaded = true; + + m_multitaskingWidget = new MultitaskingWidget; + + m_proxyInter->itemAdded(this, pluginName()); + + updateBatteryVisible(); +} + +void MultitaskingPlugin::refreshPluginItemsVisible() +{ + if (pluginIsDisable()) { + m_proxyInter->itemRemoved(this, PLUGIN_KEY); + } else { + if (!m_pluginLoaded) { + loadPlugin(); + return; + } + updateBatteryVisible(); + } +} diff --git a/plugins/multitasking/multitaskingplugin.h b/plugins/multitasking/multitaskingplugin.h new file mode 100644 index 000000000..8d060e087 --- /dev/null +++ b/plugins/multitasking/multitaskingplugin.h @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2019 ~ 2019 Deepin Technology Co., Ltd. + * + * Author: wangshaojun + * + * Maintainer: wangshaojun + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef MULTITASKINGPLUGIN_H +#define MULTITASKINGPLUGIN_H + +#include "pluginsiteminterface.h" +#include "multitaskingwidget.h" +#include "../widgets/tipswidget.h" + +#include + +class MultitaskingPlugin : public QObject, PluginsItemInterface +{ + Q_OBJECT + Q_INTERFACES(PluginsItemInterface) + Q_PLUGIN_METADATA(IID "com.deepin.dock.PluginsItemInterface" FILE "multitasking.json") + +public: + explicit MultitaskingPlugin(QObject *parent = 0); + + 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; + const QString itemContextMenu(const QString &itemKey) override; + void invokedMenuItem(const QString &itemKey, const QString &menuId, const bool checked) override; + void refreshIcon(const QString &itemKey) Q_DECL_OVERRIDE; + int itemSortKey(const QString &itemKey) Q_DECL_OVERRIDE; + void setSortKey(const QString &itemKey, const int order) Q_DECL_OVERRIDE; + void pluginSettingsChanged() override; + PluginType type() override; + +private: + void updateBatteryVisible(); + void loadPlugin(); + void refreshPluginItemsVisible(); + +private: + bool m_pluginLoaded; + + MultitaskingWidget *m_multitaskingWidget; + TipsWidget *m_tipsLabel; +}; + +#endif // MULTITASKINGPLUGIN_H diff --git a/plugins/multitasking/multitaskingwidget.cpp b/plugins/multitasking/multitaskingwidget.cpp new file mode 100644 index 000000000..8a0b4d9bd --- /dev/null +++ b/plugins/multitasking/multitaskingwidget.cpp @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2019 ~ 2019 Deepin Technology Co., Ltd. + * + * Author: wangshaojun + * + * Maintainer: wangshaojun + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "multitaskingwidget.h" +#include "multitaskingplugin.h" + +#include +#include +#include + +MultitaskingWidget::MultitaskingWidget(QWidget *parent) + : QWidget(parent) +{ + setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); +} + +void MultitaskingWidget::refreshIcon() +{ + update(); +} + +QSize MultitaskingWidget::sizeHint() const +{ + return QSize(16, 16); +} + +void MultitaskingWidget::paintEvent(QPaintEvent *e) +{ + Q_UNUSED(e); + + const auto ratio = devicePixelRatioF(); + QPixmap icon; + + if (Dock::Fashion == qApp->property(PROP_DISPLAY_MODE).value()) { + icon = QIcon::fromTheme("deepin-multitasking-view").pixmap(size() * 0.8 * ratio); + } else { + icon = QIcon::fromTheme("deepin-multitasking-view").pixmap(size() * 0.7 * ratio); + } + + icon.setDevicePixelRatio(ratio); + + QPainter painter(this); + const QRectF &rf = QRectF(rect()); + const QRectF &rfp = QRectF(icon.rect()); + painter.drawPixmap(rf.center() - rfp.center() / ratio, icon); +} + +void MultitaskingWidget::resizeEvent(QResizeEvent *event) +{ + const Dock::Position position = qApp->property(PROP_POSITION).value(); + // 保持横纵比 + if (position == Dock::Bottom || position == Dock::Top) { + setMinimumWidth(height()); + setMinimumHeight(PLUGIN_ICON_MIN_SIZE); + } else { + setMinimumWidth(PLUGIN_ICON_MIN_SIZE); + setMinimumHeight(width()); + } + + QWidget::resizeEvent(event); +} diff --git a/plugins/multitasking/multitaskingwidget.h b/plugins/multitasking/multitaskingwidget.h new file mode 100644 index 000000000..3690fc1cc --- /dev/null +++ b/plugins/multitasking/multitaskingwidget.h @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2019 ~ 2019 Deepin Technology Co., Ltd. + * + * Author: wangshaojun + * + * Maintainer: wangshaojun + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef MultitaskingWidget_H +#define MultitaskingWidget_H + +#include + +#define PLUGIN_KEY "multitasking" + +class MultitaskingWidget : public QWidget +{ + Q_OBJECT + +public: + explicit MultitaskingWidget(QWidget *parent = 0); + void refreshIcon(); + QSize sizeHint() const override; + +signals: + void requestContextMenu(const QString &itemKey) const; + +protected: + void paintEvent(QPaintEvent *e) override; + void resizeEvent(QResizeEvent *event) override; +}; + +#endif // MULTITASKINGWIDGET_H