kwin/src/windowitem.cpp
Vlad Zahorodnii 47113e09b8 scene: Introduce window items
Currently, dealing with sub-surfaces is very difficult due to the scene
design being heavily influenced by X11 requirements.

The goal of this change is to re-work scene abstractions to make improving
the wayland support easier.

The Item class is based on the QQuickItem class. My hope is that one day
we will be able to transition to QtQuick for painting scene, but in
meanwhile it makes more sense to have a minimalistic internal item class.

The WindowItem class represents a window. The SurfaceItem class represents
the contents of either an X11, or a Wayland, or an internal surface. The
DecorationItem and the ShadowItem class represent the server-side deco and
drop-shadow, respectively.

At the moment, the SurfaceItem is bound to the scene window, but the long
term plan is to break that connection so we could re-use the SurfaceItem
for things such as software cursors and drag-and-drop additional icons.

One of the responsibilities of the Item is to schedule repaints as needed.
Ideally, there shouldn't be any addRepaint() calls in the core code. The
Item class schedules repaints on geometry updates. In the future, it also
has to request an update if its opacity or visibility changes.
2021-03-31 13:56:55 +00:00

143 lines
3.9 KiB
C++

/*
SPDX-FileCopyrightText: 2021 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "windowitem.h"
#include "abstract_client.h"
#include "decorationitem.h"
#include "shadowitem.h"
#include "surfaceitem_internal.h"
#include "surfaceitem_wayland.h"
#include "surfaceitem_x11.h"
namespace KWin
{
WindowItem::WindowItem(Scene::Window *window, Item *parent)
: Item(window, parent)
{
AbstractClient *client = qobject_cast<AbstractClient *>(window->window());
if (client) {
connect(client, &AbstractClient::decorationChanged, this, &WindowItem::updateDecorationItem);
updateDecorationItem();
}
}
SurfaceItem *WindowItem::surfaceItem() const
{
return m_surfaceItem.data();
}
DecorationItem *WindowItem::decorationItem() const
{
return m_decorationItem.data();
}
ShadowItem *WindowItem::shadowItem() const
{
return m_shadowItem.data();
}
void WindowItem::updateSurfaceItem(SurfaceItem *surfaceItem)
{
Toplevel *toplevel = window()->window();
m_surfaceItem.reset(surfaceItem);
connect(toplevel, &Toplevel::bufferGeometryChanged, this, &WindowItem::updateSurfacePosition);
connect(toplevel, &Toplevel::frameGeometryChanged, this, &WindowItem::updateSurfacePosition);
updateSurfacePosition();
}
void WindowItem::updateSurfacePosition()
{
const Toplevel *toplevel = window()->window();
const QRect bufferGeometry = toplevel->bufferGeometry();
const QRect frameGeometry = toplevel->frameGeometry();
m_surfaceItem->setPosition(bufferGeometry.topLeft() - frameGeometry.topLeft());
}
void WindowItem::setShadow(Shadow *shadow)
{
if (shadow) {
if (!m_shadowItem || m_shadowItem->shadow() != shadow) {
m_shadowItem.reset(new ShadowItem(shadow, window(), this));
}
if (m_decorationItem) {
m_shadowItem->stackBefore(m_decorationItem.data());
} else if (m_surfaceItem) {
m_shadowItem->stackBefore(m_decorationItem.data());
}
} else {
m_shadowItem.reset();
}
}
void WindowItem::updateDecorationItem()
{
AbstractClient *client = qobject_cast<AbstractClient *>(window()->window());
if (!client || client->isZombie()) {
return;
}
if (client->decoration()) {
m_decorationItem.reset(new DecorationItem(client->decoration(), window(), this));
if (m_shadowItem) {
m_decorationItem->stackAfter(m_shadowItem.data());
} else if (m_surfaceItem) {
m_decorationItem->stackBefore(m_surfaceItem.data());
}
} else {
m_decorationItem.reset();
}
}
WindowItemX11::WindowItemX11(Scene::Window *window, Item *parent)
: WindowItem(window, parent)
{
Toplevel *toplevel = window->window();
switch (kwinApp()->operationMode()) {
case Application::OperationModeX11:
initialize();
break;
case Application::OperationModeXwayland:
// Xwayland windows and Wayland surfaces are associated asynchronously.
if (toplevel->surface()) {
initialize();
} else {
connect(toplevel, &Toplevel::surfaceChanged, this, &WindowItemX11::initialize);
}
break;
case Application::OperationModeWaylandOnly:
Q_UNREACHABLE();
}
}
void WindowItemX11::initialize()
{
if (!window()->window()->surface()) {
updateSurfaceItem(new SurfaceItemX11(window(), this));
} else {
updateSurfaceItem(new SurfaceItemXwayland(window(), this));
}
}
WindowItemWayland::WindowItemWayland(Scene::Window *window, Item *parent)
: WindowItem(window, parent)
{
Toplevel *toplevel = window->window();
updateSurfaceItem(new SurfaceItemWayland(toplevel->surface(), window, this));
}
WindowItemInternal::WindowItemInternal(Scene::Window *window, Item *parent)
: WindowItem(window, parent)
{
updateSurfaceItem(new SurfaceItemInternal(window, this));
}
} // namespace KWin