decoratedclient.cpp 10.2 KB
Newer Older
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
1
2
3
/*
    KWin - the KDE window manager
    This file is part of the KDE project.
4

Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
5
    SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
6

Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
7
8
    SPDX-License-Identifier: GPL-2.0-or-later
*/
9
#include "decoratedclient.h"
10
#include "decorationbridge.h"
11
#include "decorationpalette.h"
12
#include "decorationrenderer.h"
13
#include "abstract_client.h"
14
15
#include "composite.h"
#include "cursor.h"
16
#include "platform.h"
17
18
19
20
21
22
#include "workspace.h"

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

#include <QDebug>
23
#include <QStyle>
Martin Flöser's avatar
Martin Flöser committed
24
#include <QToolTip>
25
26
27
28
29
30

namespace KWin
{
namespace Decoration
{

31
DecoratedClientImpl::DecoratedClientImpl(AbstractClient *client, KDecoration2::DecoratedClient *decoratedClient, KDecoration2::Decoration *decoration)
32
    : QObject()
Kai Uwe Broulik's avatar
Kai Uwe Broulik committed
33
    , ApplicationMenuEnabledDecoratedClientPrivate(decoratedClient, decoration)
34
    , m_client(client)
35
    , m_clientSize(client->clientSize())
36
37
38
    , m_renderer(nullptr)
{
    createRenderer();
39
    client->setDecoratedClient(QPointer<DecoratedClientImpl>(this));
40
    connect(client, &AbstractClient::activeChanged, this,
41
42
43
44
        [decoratedClient, client]() {
            emit decoratedClient->activeChanged(client->isActive());
        }
    );
45
    connect(client, &AbstractClient::clientGeometryChanged, this,
46
47
48
49
50
51
52
53
54
55
56
57
        [decoratedClient, this]() {
            if (m_client->clientSize() == m_clientSize) {
                return;
            }
            const auto oldSize = m_clientSize;
            m_clientSize = m_client->clientSize();
            if (oldSize.width() != m_clientSize.width()) {
                emit decoratedClient->widthChanged(m_clientSize.width());
            }
            if (oldSize.height() != m_clientSize.height()) {
                emit decoratedClient->heightChanged(m_clientSize.height());
            }
58
            emit decoratedClient->sizeChanged(m_clientSize);
59
60
        }
    );
61
    connect(client, &AbstractClient::desktopChanged, this,
62
63
64
65
        [decoratedClient, client]() {
            emit decoratedClient->onAllDesktopsChanged(client->isOnAllDesktops());
        }
    );
66
    connect(client, &AbstractClient::captionChanged, this,
67
68
69
70
        [decoratedClient, client]() {
            emit decoratedClient->captionChanged(client->caption());
        }
    );
71
    connect(client, &AbstractClient::iconChanged, this,
72
73
74
75
        [decoratedClient, client]() {
            emit decoratedClient->iconChanged(client->icon());
        }
    );
76
    connect(client, &AbstractClient::shadeChanged, this,
77
            &Decoration::DecoratedClientImpl::signalShadeChange);
78
79
    connect(client, &AbstractClient::keepAboveChanged, decoratedClient, &KDecoration2::DecoratedClient::keepAboveChanged);
    connect(client, &AbstractClient::keepBelowChanged, decoratedClient, &KDecoration2::DecoratedClient::keepBelowChanged);
80
    connect(Compositor::self(), &Compositor::aboutToToggleCompositing, this, &DecoratedClientImpl::destroyRenderer);
81
    m_compositorToggledConnection = connect(Compositor::self(), &Compositor::compositingToggled, this,
82
83
84
85
86
        [this, decoration]() {
            createRenderer();
            decoration->update();
        }
    );
87
88
89
90
91
92
    connect(Compositor::self(), &Compositor::aboutToDestroy, this,
        [this] {
            disconnect(m_compositorToggledConnection);
            m_compositorToggledConnection = QMetaObject::Connection();
        }
    );
93
    connect(client, &AbstractClient::quickTileModeChanged, decoratedClient,
94
        [this, decoratedClient]() {
95
            emit decoratedClient->adjacentScreenEdgesChanged(adjacentScreenEdges());
96
97
        }
    );
98
99
100
101
    connect(client, &AbstractClient::closeableChanged, decoratedClient, &KDecoration2::DecoratedClient::closeableChanged);
    connect(client, &AbstractClient::shadeableChanged, decoratedClient, &KDecoration2::DecoratedClient::shadeableChanged);
    connect(client, &AbstractClient::minimizeableChanged, decoratedClient, &KDecoration2::DecoratedClient::minimizeableChanged);
    connect(client, &AbstractClient::maximizeableChanged, decoratedClient, &KDecoration2::DecoratedClient::maximizeableChanged);
102

103
    connect(client, &AbstractClient::paletteChanged, decoratedClient, &KDecoration2::DecoratedClient::paletteChanged);
Kai Uwe Broulik's avatar
Kai Uwe Broulik committed
104
105
106

    connect(client, &AbstractClient::hasApplicationMenuChanged, decoratedClient, &KDecoration2::DecoratedClient::hasApplicationMenuChanged);
    connect(client, &AbstractClient::applicationMenuActiveChanged, decoratedClient, &KDecoration2::DecoratedClient::applicationMenuActiveChanged);
107
108
109
110
111
112
113

    m_toolTipWakeUp.setSingleShot(true);
    connect(&m_toolTipWakeUp, &QTimer::timeout, this,
            [this]() {
                int fallAsleepDelay = QApplication::style()->styleHint(QStyle::SH_ToolTip_FallAsleepDelay);
                this->m_toolTipFallAsleep.setRemainingTime(fallAsleepDelay);

114
                QToolTip::showText(Cursors::self()->mouse()->pos(), this->m_toolTipText);
115
                m_toolTipShowing = true;
116
117
            }
    );
118
119
}

120
121
122
123
124
125
DecoratedClientImpl::~DecoratedClientImpl()
{
    if (m_toolTipShowing) {
        requestHideToolTip();
    }
}
126

127
128
129
130
void DecoratedClientImpl::signalShadeChange() {
    emit decoratedClient()->shadedChanged(m_client->isShade());
}

131
#define DELEGATE(type, name, clientName) \
132
133
    type DecoratedClientImpl::name() const \
    { \
134
        return m_client->clientName(); \
135
136
    }

137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#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)
153

154
#undef DELEGATE2
155
156
157
158
159
160
161
162
163
164
165
#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)
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
166
DELEGATE(WId, windowId, window)
167
168
169
170
171
172
173
DELEGATE(WId, decorationId, frameId)

#undef DELEGATE

#define DELEGATE(name, op) \
    void DecoratedClientImpl::name() \
    { \
174
        Workspace::self()->performWindowOperation(m_client, Options::op); \
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
    }

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

195
196
197
198
199
void DecoratedClientImpl::requestClose()
{
    QMetaObject::invokeMethod(m_client, "closeWindow", Qt::QueuedConnection);
}

200
201
202
203
204
205
206
207
208
209
QColor DecoratedClientImpl::color(KDecoration2::ColorGroup group, KDecoration2::ColorRole role) const
{
    auto dp = m_client->decorationPalette();
    if (dp) {
        return dp->color(group, role);
    }

    return QColor();
}

Martin Flöser's avatar
Martin Flöser committed
210
211
void DecoratedClientImpl::requestShowToolTip(const QString &text)
{
212
213
214
215
    if (!DecorationBridge::self()->showToolTips()) {
        return;
    }

216
217
218
219
    m_toolTipText = text;

    int wakeUpDelay = QApplication::style()->styleHint(QStyle::SH_ToolTip_WakeUpDelay);
    m_toolTipWakeUp.start(m_toolTipFallAsleep.hasExpired() ? wakeUpDelay : 20);
Martin Flöser's avatar
Martin Flöser committed
220
221
222
223
}

void DecoratedClientImpl::requestHideToolTip()
{
224
    m_toolTipWakeUp.stop();
Martin Flöser's avatar
Martin Flöser committed
225
    QToolTip::hideText();
226
    m_toolTipShowing = false;
Martin Flöser's avatar
Martin Flöser committed
227
228
}

Xaver Hugl's avatar
Xaver Hugl committed
229
void DecoratedClientImpl::requestShowWindowMenu(const QRect &rect)
230
{
Xaver Hugl's avatar
Xaver Hugl committed
231
    Workspace::self()->showWindowMenu(QRect(m_client->pos() + rect.topLeft(), m_client->pos() + rect.bottomRight()), m_client);
232
233
}

Kai Uwe Broulik's avatar
Kai Uwe Broulik committed
234
235
236
237
238
239
240
241
242
243
void DecoratedClientImpl::requestShowApplicationMenu(const QRect &rect, int actionId)
{
    Workspace::self()->showApplicationMenu(rect, m_client, actionId);
}

void DecoratedClientImpl::showApplicationMenu(int actionId)
{
    decoration()->showApplicationMenu(actionId);
}

244
void DecoratedClientImpl::requestToggleMaximization(Qt::MouseButtons buttons)
245
{
246
247
248
249
250
251
    QMetaObject::invokeMethod(this, "delayedRequestToggleMaximization", Qt::QueuedConnection, Q_ARG(Options::WindowOperation, options->operationMaxButtonClick(buttons)));
}

void DecoratedClientImpl::delayedRequestToggleMaximization(Options::WindowOperation operation)
{
    Workspace::self()->performWindowOperation(m_client, operation);
252
253
254
255
}

int DecoratedClientImpl::width() const
{
256
    return m_clientSize.width();
257
258
259
260
}

int DecoratedClientImpl::height() const
{
261
    return m_clientSize.height();
262
263
}

264
265
266
267
268
QSize DecoratedClientImpl::size() const
{
    return m_clientSize;
}

269
270
bool DecoratedClientImpl::isMaximizedVertically() const
{
271
    return m_client->requestedMaximizeMode() & MaximizeVertical;
272
273
274
275
276
277
278
279
280
}

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

bool DecoratedClientImpl::isMaximizedHorizontally() const
{
281
    return m_client->requestedMaximizeMode() & MaximizeHorizontal;
282
283
}

284
Qt::Edges DecoratedClientImpl::adjacentScreenEdges() const
285
286
{
    Qt::Edges edges;
287
288
    const QuickTileMode mode = m_client->quickTileMode();
    if (mode.testFlag(QuickTileFlag::Left)) {
289
        edges |= Qt::LeftEdge;
290
        if (!mode.testFlag(QuickTileFlag::Top) && !mode.testFlag(QuickTileFlag::Bottom)) {
291
292
293
294
            // using complete side
            edges |= Qt::TopEdge | Qt::BottomEdge;
        }
    }
295
    if (mode.testFlag(QuickTileFlag::Top)) {
296
297
        edges |= Qt::TopEdge;
    }
298
    if (mode.testFlag(QuickTileFlag::Right)) {
299
        edges |= Qt::RightEdge;
300
        if (!mode.testFlag(QuickTileFlag::Top) && !mode.testFlag(QuickTileFlag::Bottom)) {
301
302
303
304
            // using complete side
            edges |= Qt::TopEdge | Qt::BottomEdge;
        }
    }
305
    if (mode.testFlag(QuickTileFlag::Bottom)) {
306
307
308
309
310
        edges |= Qt::BottomEdge;
    }
    return edges;
}

Kai Uwe Broulik's avatar
Kai Uwe Broulik committed
311
312
313
314
315
316
317
318
319
320
bool DecoratedClientImpl::hasApplicationMenu() const
{
    return m_client->hasApplicationMenu();
}

bool DecoratedClientImpl::isApplicationMenuActive() const
{
    return m_client->applicationMenuActive();
}

321
322
void DecoratedClientImpl::createRenderer()
{
323
    m_renderer = kwinApp()->platform()->createDecorationRenderer(this);
324
325
}

326
327
328
329
330
331
void DecoratedClientImpl::destroyRenderer()
{
    delete m_renderer;
    m_renderer = nullptr;
}

332
333
}
}