window: Improve how we tell the window to do layer-shell

Instead of telling it in the construction after forcing the window
creation, install an event handler that sets it when we get the
PlatformSurface event.

It has the advantage that it will also trigger in subsequent platform
surface events (e.g. after hide and show).
This commit is contained in:
Aleix Pol
2025-09-09 00:43:58 +02:00
parent 2b8544f5a0
commit cb79d3f60a
3 changed files with 70 additions and 17 deletions

View File

@ -186,27 +186,36 @@ Window::Window(QWindow *window)
, d(new WindowPrivate(window)) , d(new WindowPrivate(window))
{ {
s_map.insert(d->parentWindow, this); s_map.insert(d->parentWindow, this);
window->installEventFilter(this);
}
window->create(); bool Window::eventFilter(QObject *watched, QEvent *event)
{
auto waylandWindow = dynamic_cast<QtWaylandClient::QWaylandWindow *>(window->handle()); auto window = qobject_cast<QWindow *>(watched);
if (!waylandWindow) { if (!window) {
qCWarning(LAYERSHELLQT) << window << "is not a wayland window. Not creating zwlr_layer_surface"; return false;
return;
} }
if (event->type() == QEvent::PlatformSurface) {
static QWaylandLayerShellIntegration *shellIntegration = nullptr; auto waylandWindow = dynamic_cast<QtWaylandClient::QWaylandWindow *>(window->handle());
if (!shellIntegration) { if (!waylandWindow) {
shellIntegration = new QWaylandLayerShellIntegration(); qCWarning(LAYERSHELLQT) << window << "is not a wayland window. Not creating zwlr_layer_surface";
if (!shellIntegration->initialize(waylandWindow->display())) { return false;
delete shellIntegration;
shellIntegration = nullptr;
qCWarning(LAYERSHELLQT) << "Failed to initialize layer-shell integration, possibly because compositor does not support the layer-shell protocol";
return;
} }
}
waylandWindow->setShellIntegration(shellIntegration); static QWaylandLayerShellIntegration *shellIntegration = nullptr;
if (!shellIntegration) {
shellIntegration = new QWaylandLayerShellIntegration();
if (!shellIntegration->initialize(waylandWindow->display())) {
delete shellIntegration;
shellIntegration = nullptr;
qCWarning(LAYERSHELLQT)
<< "Failed to initialize layer-shell integration, possibly because compositor does not support the layer-shell protocol";
return false;
}
}
waylandWindow->setShellIntegration(shellIntegration);
}
return false;
} }
Window *Window::get(QWindow *window) Window *Window::get(QWindow *window)

View File

@ -138,6 +138,8 @@ public:
static Window *qmlAttachedProperties(QObject *object); static Window *qmlAttachedProperties(QObject *object);
bool eventFilter(QObject *watched, QEvent *event) override;
Q_SIGNALS: Q_SIGNALS:
void anchorsChanged(); void anchorsChanged();
void exclusionZoneChanged(); void exclusionZoneChanged();

View File

@ -0,0 +1,42 @@
/*
* SPDX-FileCopyrightText: 2025 Aleix Pol i Gonzalez <aleixpol@kde.org>
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
import QtQuick
import QtQuick.Controls
import org.kde.layershell 1.0 as LayerShell
Item
{
Button {
text: "Convert"
anchors.centerIn: parent
onClicked: {
win.close()
win.LayerShell.Window.anchors = LayerShell.Window.AnchorLeft;
win.LayerShell.Window.layer = LayerShell.Window.LayerTop;
win.LayerShell.Window.exclusionZone = -1;
win.show()
}
}
Window {
id: win
width: 100
height: 100
Rectangle {
anchors.fill: parent
color: "red"
Text {
anchors.centerIn: parent
text: "top"
}
}
visible: true
}
}