decoratedclient.cpp 8.02 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/********************************************************************
 KWin - the KDE window manager
 This file is part of the KDE project.

Copyright (C) 2014 Martin Gräßlin <mgraesslin@kde.org>

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 2 of the License, or
(at your option) 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 <http://www.gnu.org/licenses/>.
*********************************************************************/
#include "decoratedclient.h"
21
#include "decorationpalette.h"
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "decorationrenderer.h"
#include "client.h"
#include "composite.h"
#include "cursor.h"
#include "options.h"
#include "scene.h"
#include "workspace.h"

#include <KDecoration2/DecoratedClient>
#include <KDecoration2/Decoration>

#include <QDebug>

namespace KWin
{
namespace Decoration
{

DecoratedClientImpl::DecoratedClientImpl(Client *client, KDecoration2::DecoratedClient *decoratedClient, KDecoration2::Decoration *decoration)
    : QObject()
    , DecoratedClientPrivate(decoratedClient, decoration)
    , m_client(client)
    , m_renderer(nullptr)
{
    createRenderer();
47
    client->setDecoratedClient(QPointer<DecoratedClientImpl>(this));
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
    connect(client, &Client::activeChanged, this,
        [decoratedClient, client]() {
            emit decoratedClient->activeChanged(client->isActive());
        }
    );
    connect(client, &Client::geometryChanged, this,
        [decoratedClient, client]() {
            emit decoratedClient->widthChanged(client->clientSize().width());
            emit decoratedClient->heightChanged(client->clientSize().height());
        }
    );
    connect(client, &Client::desktopChanged, this,
        [decoratedClient, client]() {
            emit decoratedClient->onAllDesktopsChanged(client->isOnAllDesktops());
        }
    );
    connect(client, &Client::captionChanged, this,
        [decoratedClient, client]() {
            emit decoratedClient->captionChanged(client->caption());
        }
    );
    connect(client, &Client::iconChanged, this,
        [decoratedClient, client]() {
            emit decoratedClient->iconChanged(client->icon());
        }
    );
    connect(client, &Client::shadeChanged, this,
        [decoratedClient, client]() {
            // TODO: geometry is wrong
            emit decoratedClient->shadedChanged(client->isShade());
        }
    );
    connect(client, &Client::keepAboveChanged, decoratedClient, &KDecoration2::DecoratedClient::keepAboveChanged);
    connect(client, &Client::keepBelowChanged, decoratedClient, &KDecoration2::DecoratedClient::keepBelowChanged);
    connect(Compositor::self(), &Compositor::compositingToggled, this,
        [this, decoration]() {
            delete m_renderer;
            m_renderer = nullptr;
            createRenderer();
            decoration->update();
        }
    );
90
91
    connect(client, &Client::quickTileModeChanged, decoratedClient,
        [this, decoratedClient]() {
92
            emit decoratedClient->adjacentScreenEdgesChanged(adjacentScreenEdges());
93
94
        }
    );
95
96
    connect(client, &Client::closeableChanged, decoratedClient, &KDecoration2::DecoratedClient::closeableChanged);
    connect(client, &Client::shadeableChanged, decoratedClient, &KDecoration2::DecoratedClient::shadeableChanged);
97
98
    connect(client, &Client::minimizeableChanged, decoratedClient, &KDecoration2::DecoratedClient::minimizeableChanged);
    connect(client, &Client::maximizeableChanged, decoratedClient, &KDecoration2::DecoratedClient::maximizeableChanged);
99
100

    connect(client, &Client::paletteChanged, decoratedClient, &KDecoration2::DecoratedClient::paletteChanged);
101
102
103
104
}

DecoratedClientImpl::~DecoratedClientImpl() = default;

105
#define DELEGATE(type, name, clientName) \
106
107
    type DecoratedClientImpl::name() const \
    { \
108
        return m_client->clientName(); \
109
110
    }

111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#define DELEGATE2(type, name) DELEGATE(type, name, name)

DELEGATE2(QString, caption)
DELEGATE2(bool, isActive)
DELEGATE2(bool, isCloseable)
DELEGATE(bool, isMaximizeable, isMaximizable)
DELEGATE(bool, isMinimizeable, isMinimizable)
DELEGATE2(bool, isModal)
DELEGATE(bool, isMoveable, isMovable)
DELEGATE(bool, isResizeable, isResizable)
DELEGATE2(bool, isShadeable)
DELEGATE2(bool, providesContextHelp)
DELEGATE2(int, desktop)
DELEGATE2(bool, isOnAllDesktops)
DELEGATE2(QPalette, palette)
DELEGATE2(QIcon, icon)
127

128
#undef DELEGATE2
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#undef DELEGATE

#define DELEGATE(type, name, clientName) \
    type DecoratedClientImpl::name() const \
    { \
        return m_client->clientName(); \
    }

DELEGATE(bool, isKeepAbove, keepAbove)
DELEGATE(bool, isKeepBelow, keepBelow)
DELEGATE(bool, isShaded, isShade)
DELEGATE(WId, windowId, window)
DELEGATE(WId, decorationId, frameId)

#undef DELEGATE

#define DELEGATE(name, op) \
    void DecoratedClientImpl::name() \
    { \
148
        Workspace::self()->performWindowOperation(m_client, Options::op); \
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
    }

DELEGATE(requestToggleShade, ShadeOp)
DELEGATE(requestToggleOnAllDesktops, OnAllDesktopsOp)
DELEGATE(requestToggleKeepAbove, KeepAboveOp)
DELEGATE(requestToggleKeepBelow, KeepBelowOp)

#undef DELEGATE

#define DELEGATE(name, clientName) \
    void DecoratedClientImpl::name() \
    { \
        m_client->clientName(); \
    }

DELEGATE(requestContextHelp, showContextHelp)
DELEGATE(requestMinimize, minimize)

#undef DELEGATE

169
170
171
172
173
void DecoratedClientImpl::requestClose()
{
    QMetaObject::invokeMethod(m_client, "closeWindow", Qt::QueuedConnection);
}

174
175
176
177
178
179
180
181
182
183
QColor DecoratedClientImpl::color(KDecoration2::ColorGroup group, KDecoration2::ColorRole role) const
{
    auto dp = m_client->decorationPalette();
    if (dp) {
        return dp->color(group, role);
    }

    return QColor();
}

184
185
186
187
188
189
void DecoratedClientImpl::requestShowWindowMenu()
{
    // TODO: add rect to requestShowWindowMenu
    Workspace::self()->showWindowMenu(QRect(Cursor::pos(), Cursor::pos()), m_client);
}

190
void DecoratedClientImpl::requestToggleMaximization(Qt::MouseButtons buttons)
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
{
    Workspace::self()->performWindowOperation(m_client, options->operationMaxButtonClick(buttons));
}

int DecoratedClientImpl::width() const
{
    return m_client->clientSize().width();
}

int DecoratedClientImpl::height() const
{
    return m_client->clientSize().height();
}

bool DecoratedClientImpl::isMaximizedVertically() const
{
207
    return m_client->maximizeMode() & MaximizeVertical;
208
209
210
211
212
213
214
215
216
}

bool DecoratedClientImpl::isMaximized() const
{
    return isMaximizedHorizontally() && isMaximizedVertically();
}

bool DecoratedClientImpl::isMaximizedHorizontally() const
{
217
    return m_client->maximizeMode() & MaximizeHorizontal;
218
219
}

220
Qt::Edges DecoratedClientImpl::adjacentScreenEdges() const
221
222
{
    Qt::Edges edges;
223
224
    const Client::QuickTileMode mode = m_client->quickTileMode();
    if (mode.testFlag(Client::QuickTileLeft)) {
225
        edges |= Qt::LeftEdge;
226
        if (!mode.testFlag(Client::QuickTileTop) && !mode.testFlag(Client::QuickTileBottom)) {
227
228
229
230
            // using complete side
            edges |= Qt::TopEdge | Qt::BottomEdge;
        }
    }
231
    if (mode.testFlag(Client::QuickTileTop)) {
232
233
        edges |= Qt::TopEdge;
    }
234
    if (mode.testFlag(Client::QuickTileRight)) {
235
        edges |= Qt::RightEdge;
236
        if (!mode.testFlag(Client::QuickTileTop) && !mode.testFlag(Client::QuickTileBottom)) {
237
238
239
240
            // using complete side
            edges |= Qt::TopEdge | Qt::BottomEdge;
        }
    }
241
    if (mode.testFlag(Client::QuickTileBottom)) {
242
243
244
245
246
        edges |= Qt::BottomEdge;
    }
    return edges;
}

247
248
249
250
251
252
253
254
255
void DecoratedClientImpl::createRenderer()
{
    if (Compositor::self()->hasScene()) {
        m_renderer = Compositor::self()->scene()->createDecorationRenderer(this);
    } else {
        m_renderer = new X11Renderer(this);
    }
}

256
257
258
259
260
261
void DecoratedClientImpl::destroyRenderer()
{
    delete m_renderer;
    m_renderer = nullptr;
}

262
263
}
}