Use KWayland protocol to close the active window

As a nice side-effect this allows us to enable/disable the close button
depending on whether we have a window to close.
This commit is contained in:
Martin Gräßlin 2015-06-19 01:52:22 +02:00
parent bf4959d63e
commit 8f7f23321f
4 changed files with 34 additions and 42 deletions

View file

@ -1,2 +0,0 @@
workspace.activeClient.closeWindow();

View file

@ -73,7 +73,8 @@ PlasmaCore.ColorScope {
width: parent.width/3 width: parent.width/3
anchors.right: parent.right anchors.right: parent.right
iconSource: "window-close" iconSource: "window-close"
onClicked: plasmoid.nativeInterface.executeScript("close"); enabled: plasmoid.nativeInterface.hasCloseableActiveWindow;
onClicked: plasmoid.nativeInterface.closeActiveWindow();
} }
} }
} }

View file

@ -21,10 +21,6 @@
#include <QtQml> #include <QtQml>
#include <QDebug> #include <QDebug>
#include <QDBusMessage>
#include <QDBusConnection>
#include <QDBusPendingCallWatcher>
#include <QDBusPendingReply>
#include <Plasma/Package> #include <Plasma/Package>
@ -47,35 +43,6 @@ TaskPanel::~TaskPanel()
{ {
} }
void TaskPanel::executeScript(const QString &script)
{
//Plasma::Package p =
QDBusMessage message = QDBusMessage::createMethodCall(s_kwinService, "/Scripting", QString(), "loadScript");
QList<QVariant> arguments;
arguments << QVariant(package().filePath("scripts", script + ".js"));
message.setArguments(arguments);
QDBusPendingReply<void> asyncCall = QDBusConnection::sessionBus().asyncCall(message);
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(asyncCall, this);
QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
this, SLOT(loadScriptFinishedSlot(QDBusPendingCallWatcher*)));
}
void TaskPanel::loadScriptFinishedSlot(QDBusPendingCallWatcher *watcher)
{
QDBusMessage reply = watcher->reply();
if (reply.type() == QDBusMessage::ErrorMessage) {
qWarning() << reply.errorMessage();
} else {
const int id = reply.arguments().first().toInt();
QDBusConnection::sessionBus().connect(s_kwinService, "/" + QString::number(id), QString(), "print", this, SLOT(print(QString)));
QDBusConnection::sessionBus().connect(s_kwinService, "/" + QString::number(id), QString(), "printError", this, SLOT(print(QString)));
QDBusMessage message = QDBusMessage::createMethodCall(s_kwinService, "/" + QString::number(id), QString(), "run");
//fire blindly the call for now
QDBusConnection::sessionBus().asyncCall(message);
}
}
void TaskPanel::requestShowingDesktop(bool showingDesktop) void TaskPanel::requestShowingDesktop(bool showingDesktop)
{ {
if (!m_windowManagement) { if (!m_windowManagement) {
@ -108,11 +75,35 @@ void TaskPanel::initWayland()
emit showingDesktopChanged(m_showingDesktop); emit showingDesktopChanged(m_showingDesktop);
} }
); );
connect(m_windowManagement, &PlasmaWindowManagement::activeWindowChanged, this, &TaskPanel::updateActiveWindow);
updateActiveWindow();
} }
); );
registry->setup(); registry->setup();
} }
void TaskPanel::updateActiveWindow()
{
if (!m_windowManagement) {
return;
}
m_activeWindow = m_windowManagement->activeWindow();
// TODO: connect to closeableChanged, not needed right now as KWin doesn't provide this changeable
emit hasCloseableActiveWindowChanged();
}
bool TaskPanel::hasCloseableActiveWindow() const
{
return m_activeWindow && m_activeWindow->isCloseable();
}
void TaskPanel::closeActiveWindow()
{
if (m_activeWindow) {
m_activeWindow->requestClose();
}
}
K_EXPORT_PLASMA_APPLET_WITH_JSON(taskpanel, TaskPanel, "metadata.json") K_EXPORT_PLASMA_APPLET_WITH_JSON(taskpanel, TaskPanel, "metadata.json")
#include "taskpanel.moc" #include "taskpanel.moc"

View file

@ -24,13 +24,12 @@
#include <Plasma/Containment> #include <Plasma/Containment>
class QDBusPendingCallWatcher;
namespace KWayland namespace KWayland
{ {
namespace Client namespace Client
{ {
class PlasmaWindowManagement; class PlasmaWindowManagement;
class PlasmaWindow;
} }
} }
@ -38,28 +37,31 @@ class TaskPanel : public Plasma::Containment
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(bool showDesktop READ isShowingDesktop WRITE requestShowingDesktop NOTIFY showingDesktopChanged) Q_PROPERTY(bool showDesktop READ isShowingDesktop WRITE requestShowingDesktop NOTIFY showingDesktopChanged)
Q_PROPERTY(bool hasCloseableActiveWindow READ hasCloseableActiveWindow NOTIFY hasCloseableActiveWindowChanged)
public: public:
TaskPanel( QObject *parent, const QVariantList &args ); TaskPanel( QObject *parent, const QVariantList &args );
~TaskPanel(); ~TaskPanel();
Q_INVOKABLE void executeScript(const QString &script); Q_INVOKABLE void closeActiveWindow();
bool isShowingDesktop() const { bool isShowingDesktop() const {
return m_showingDesktop; return m_showingDesktop;
} }
void requestShowingDesktop(bool showingDesktop); void requestShowingDesktop(bool showingDesktop);
bool hasCloseableActiveWindow() const;
Q_SIGNALS: Q_SIGNALS:
void showingDesktopChanged(bool); void showingDesktopChanged(bool);
void hasCloseableActiveWindowChanged();
private Q_SLOTS:
void loadScriptFinishedSlot(QDBusPendingCallWatcher *watcher);
private: private:
void initWayland(); void initWayland();
void updateActiveWindow();
bool m_showingDesktop; bool m_showingDesktop;
KWayland::Client::PlasmaWindowManagement *m_windowManagement; KWayland::Client::PlasmaWindowManagement *m_windowManagement;
KWayland::Client::PlasmaWindow *m_activeWindow = nullptr;
}; };