Layershellqt

window.cpp
1/*
2 * SPDX-FileCopyrightText: 2021 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6
7#include "window.h"
8#include "../qwaylandlayershellintegration_p.h"
9
10#include <layershellqt_logging.h>
11
12#include <QPointer>
13#include <optional>
14
15#include <QtWaylandClient/private/qwaylandwindow_p.h>
16
17using namespace LayerShellQt;
18
19class LayerShellQt::WindowPrivate
20{
21public:
22 WindowPrivate(QWindow *window)
23 : parentWindow(window)
24 {
25 }
26
27 QWindow *parentWindow;
28 QString scope = QStringLiteral("window");
29 Window::Anchors anchors = {Window::AnchorTop | Window::AnchorBottom | Window::AnchorLeft | Window::AnchorRight};
30 int32_t exclusionZone = 0;
31 Window::Anchor exclusiveEdge = Window::AnchorNone;
32 Window::KeyboardInteractivity keyboardInteractivity = Window::KeyboardInteractivityOnDemand;
33 Window::Layer layer = Window::LayerTop;
34 QMargins margins;
35 Window::ScreenConfiguration screenConfiguration = Window::ScreenFromQWindow;
36 bool closeOnDismissed = true;
37};
38
39static QMap<QWindow *, Window *> s_map;
40
41Window::~Window()
42{
43 s_map.remove(d->parentWindow);
44}
45
46void Window::setAnchors(Anchors anchors)
47{
48 if (d->anchors != anchors) {
49 d->anchors = anchors;
50 Q_EMIT anchorsChanged();
51 }
52}
53
54Window::Anchors Window::anchors() const
55{
56 return d->anchors;
57}
58
59void Window::setExclusiveZone(int32_t zone)
60{
61 if (d->exclusionZone != zone) {
62 d->exclusionZone = zone;
63 Q_EMIT exclusionZoneChanged();
64 }
65}
66
67int32_t Window::exclusionZone() const
68{
69 return d->exclusionZone;
70}
71
72void Window::setExclusiveEdge(Window::Anchor edge)
73{
74 if (d->exclusiveEdge == edge) {
75 return;
76 }
77
78 d->exclusiveEdge = edge;
79 Q_EMIT exclusiveEdgeChanged();
80}
81
82Window::Anchor Window::exclusiveEdge() const
83{
84 return d->exclusiveEdge;
85}
86
87void Window::setMargins(const QMargins &margins)
88{
89 if (d->margins != margins) {
90 d->margins = margins;
91 Q_EMIT marginsChanged();
92 }
93}
94
95QMargins Window::margins() const
96{
97 return d->margins;
98}
99
100void Window::setKeyboardInteractivity(KeyboardInteractivity interactivity)
101{
102 if (d->keyboardInteractivity != interactivity) {
103 d->keyboardInteractivity = interactivity;
104 Q_EMIT keyboardInteractivityChanged();
105 }
106}
107
108Window::KeyboardInteractivity Window::keyboardInteractivity() const
109{
110 return d->keyboardInteractivity;
111}
112
113void Window::setLayer(Layer layer)
114{
115 if (d->layer != layer) {
116 d->layer = layer;
117 Q_EMIT layerChanged();
118 }
119}
120
121void Window::setScope(const QString &scope)
122{
123 d->scope = scope;
124 // this is static and must be set before the platform window is created
125}
126
127QString Window::scope() const
128{
129 return d->scope;
130}
131
132Window::Layer Window::layer() const
133{
134 return d->layer;
135}
136
137Window::ScreenConfiguration Window::screenConfiguration() const
138{
139 return d->screenConfiguration;
140}
141
142void Window::setScreenConfiguration(Window::ScreenConfiguration screenConfiguration)
143{
144 d->screenConfiguration = screenConfiguration;
145}
146
147bool Window::closeOnDismissed() const
148{
149 return d->closeOnDismissed;
150}
151
152void Window::setCloseOnDismissed(bool close)
153{
154 d->closeOnDismissed = close;
155}
156
157Window::Window(QWindow *window)
158 : QObject(window)
159 , d(new WindowPrivate(window))
160{
161 s_map.insert(d->parentWindow, this);
162
163 window->create();
164
165 auto waylandWindow = dynamic_cast<QtWaylandClient::QWaylandWindow *>(window->handle());
166 if (!waylandWindow) {
167 qCWarning(LAYERSHELLQT) << window << "is not a wayland window. Not creating zwlr_layer_surface";
168 return;
169 }
170
171 static QWaylandLayerShellIntegration *shellIntegration = nullptr;
172 if (!shellIntegration) {
173 shellIntegration = new QWaylandLayerShellIntegration();
174 if (!shellIntegration->initialize(waylandWindow->display())) {
175 delete shellIntegration;
176 shellIntegration = nullptr;
177 qCWarning(LAYERSHELLQT) << "Failed to initialize layer-shell integration, possibly because compositor does not support the layer-shell protocol";
178 return;
179 }
180 }
181
182 waylandWindow->setShellIntegration(shellIntegration);
183}
184
185Window *Window::get(QWindow *window)
186{
187 if (!window) {
188 return nullptr;
189 }
190
191 auto layerShellWindow = s_map.value(window);
192 if (layerShellWindow) {
193 return layerShellWindow;
194 }
195 return new Window(window);
196}
197
198Window *Window::qmlAttachedProperties(QObject *object)
199{
200 return get(qobject_cast<QWindow *>(object));
201}
void close()
KIOCORE_EXPORT TransferJob * get(const QUrl &url, LoadType reload=NoReload, JobFlags flags=DefaultFlags)
KGUIADDONS_EXPORT QWindow * window(QObject *job)
Q_EMITQ_EMIT
T qobject_cast(QObject *object)
void create()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:02:40 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.