Changeset View
Changeset View
Standalone View
Standalone View
src/windowsystem/windoweffects.cpp
Show All 18 Lines | |||||
19 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | 19 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
20 | */ | 20 | */ | ||
21 | 21 | | |||
22 | #include "windoweffects.h" | 22 | #include "windoweffects.h" | ||
23 | #include "waylandintegration.h" | 23 | #include "waylandintegration.h" | ||
24 | 24 | | |||
25 | #include <QDebug> | 25 | #include <QDebug> | ||
26 | #include <QWidget> | 26 | #include <QWidget> | ||
27 | #include <QGuiApplication> | ||||
28 | #include <QExposeEvent> | ||||
27 | 29 | | |||
28 | #include <KWayland/Client/connection_thread.h> | 30 | #include <KWayland/Client/connection_thread.h> | ||
29 | #include <KWayland/Client/plasmawindowmanagement.h> | 31 | #include <KWayland/Client/plasmawindowmanagement.h> | ||
30 | #include <KWayland/Client/registry.h> | 32 | #include <KWayland/Client/registry.h> | ||
31 | #include <KWayland/Client/plasmashell.h> | 33 | #include <KWayland/Client/plasmashell.h> | ||
32 | #include <KWayland/Client/compositor.h> | 34 | #include <KWayland/Client/compositor.h> | ||
33 | #include <KWayland/Client/surface.h> | 35 | #include <KWayland/Client/surface.h> | ||
34 | #include <KWayland/Client/blur.h> | 36 | #include <KWayland/Client/blur.h> | ||
35 | #include <KWayland/Client/contrast.h> | 37 | #include <KWayland/Client/contrast.h> | ||
36 | #include <KWayland/Client/region.h> | 38 | #include <KWayland/Client/region.h> | ||
37 | #include <KWayland/Client/slide.h> | 39 | #include <KWayland/Client/slide.h> | ||
38 | 40 | | |||
39 | WindowEffects::WindowEffects() | 41 | WindowEffects::WindowEffects() | ||
40 | : QObject(), | 42 | : QObject(), | ||
41 | KWindowEffectsPrivate() | 43 | KWindowEffectsPrivate() | ||
42 | { | 44 | { | ||
43 | } | 45 | } | ||
44 | 46 | | |||
45 | WindowEffects::~WindowEffects() | 47 | WindowEffects::~WindowEffects() | ||
46 | {} | 48 | {} | ||
47 | 49 | | |||
50 | QWindow *WindowEffects::windowForId(WId wid) | ||||
51 | { | ||||
52 | QWindow *window = nullptr; | ||||
53 | | ||||
54 | for (auto win : qApp->allWindows()) { | ||||
55 | if (win->winId() == wid) { | ||||
56 | window = win; | ||||
57 | break; | ||||
58 | } | ||||
59 | } | ||||
60 | return window; | ||||
61 | } | ||||
62 | | ||||
63 | void WindowEffects::trackWindow(QWindow *window) | ||||
64 | { | ||||
65 | if (!m_windowWatchers.contains(window)) { | ||||
66 | window->installEventFilter(this); | ||||
67 | auto conn = connect(window, &QObject::destroyed, this, [this, window]() { | ||||
68 | m_blurRegions.remove(window); | ||||
69 | m_backgroundConstrastRegions.remove(window); | ||||
70 | m_windowWatchers.remove(window); | ||||
71 | }); | ||||
72 | m_windowWatchers[window] = conn; | ||||
73 | } | ||||
74 | } | ||||
75 | | ||||
76 | void WindowEffects::releaseWindow(QWindow *window) | ||||
77 | { | ||||
78 | if (!m_blurRegions.contains(window) && !m_backgroundConstrastRegions.contains(window)) { | ||||
79 | disconnect(m_windowWatchers[window]); | ||||
80 | window->removeEventFilter(this); | ||||
81 | m_windowWatchers.remove(window); | ||||
82 | } | ||||
83 | } | ||||
84 | | ||||
85 | bool WindowEffects::eventFilter(QObject *watched, QEvent *event) | ||||
86 | { | ||||
87 | if (event->type() == QEvent::Expose) { | ||||
88 | auto ee = static_cast<QExposeEvent*>(event); | ||||
89 | | ||||
90 | if ((ee->region().isNull())) { | ||||
91 | return false; | ||||
92 | } | ||||
93 | | ||||
94 | auto window = qobject_cast<QWindow *>(watched); | ||||
95 | if (!window) { | ||||
96 | return false; | ||||
97 | } | ||||
98 | | ||||
99 | { | ||||
100 | auto it = m_blurRegions.constFind(window); | ||||
101 | if (it != m_blurRegions.constEnd()) { | ||||
102 | enableBlurBehind(window, true, *it); | ||||
103 | } | ||||
104 | } | ||||
105 | { | ||||
106 | auto it = m_backgroundConstrastRegions.constFind(window); | ||||
107 | if (it != m_backgroundConstrastRegions.constEnd()) { | ||||
108 | enableBackgroundContrast(window, true, it->contrast, it->intensity, it->saturation, it->region); | ||||
109 | } | ||||
110 | } | ||||
111 | } | ||||
112 | return false; | ||||
113 | } | ||||
114 | | ||||
48 | bool WindowEffects::isEffectAvailable(KWindowEffects::Effect effect) | 115 | bool WindowEffects::isEffectAvailable(KWindowEffects::Effect effect) | ||
49 | { | 116 | { | ||
50 | switch (effect) { | 117 | switch (effect) { | ||
51 | case KWindowEffects::BackgroundContrast: | 118 | case KWindowEffects::BackgroundContrast: | ||
52 | return WaylandIntegration::self()->waylandContrastManager() != nullptr; | 119 | return WaylandIntegration::self()->waylandContrastManager() != nullptr; | ||
53 | case KWindowEffects::BlurBehind: | 120 | case KWindowEffects::BlurBehind: | ||
54 | return WaylandIntegration::self()->waylandBlurManager() != nullptr; | 121 | return WaylandIntegration::self()->waylandBlurManager() != nullptr; | ||
55 | case KWindowEffects::Slide: | 122 | case KWindowEffects::Slide: | ||
▲ Show 20 Lines • Show All 67 Lines • ▼ Show 20 Line(s) | |||||
123 | } | 190 | } | ||
124 | 191 | | |||
125 | void WindowEffects::highlightWindows(WId controller, const QList<WId> &ids) | 192 | void WindowEffects::highlightWindows(WId controller, const QList<WId> &ids) | ||
126 | { | 193 | { | ||
127 | Q_UNUSED(controller) | 194 | Q_UNUSED(controller) | ||
128 | Q_UNUSED(ids) | 195 | Q_UNUSED(ids) | ||
129 | } | 196 | } | ||
130 | 197 | | |||
131 | void WindowEffects::enableBlurBehind(WId window, bool enable, const QRegion ®ion) | 198 | void WindowEffects::enableBlurBehind(WId winId, bool enable, const QRegion ®ion) | ||
199 | { | ||||
200 | auto window = windowForId(winId); | ||||
201 | if (!window) { | ||||
202 | return; | ||||
203 | } | ||||
204 | if (enable) { | ||||
205 | trackWindow(window); | ||||
206 | m_blurRegions[window] = region; | ||||
207 | } else { | ||||
208 | m_blurRegions.remove(window); | ||||
209 | releaseWindow(window); | ||||
210 | } | ||||
211 | | ||||
212 | enableBlurBehind(window, enable, region); | ||||
213 | } | ||||
214 | | ||||
215 | void WindowEffects::enableBlurBehind(QWindow *window, bool enable, const QRegion ®ion) | ||||
132 | { | 216 | { | ||
133 | if (!WaylandIntegration::self()->waylandBlurManager()) { | 217 | if (!WaylandIntegration::self()->waylandBlurManager()) { | ||
134 | return; | 218 | return; | ||
135 | } | 219 | } | ||
136 | KWayland::Client::Surface *surface = KWayland::Client::Surface::fromQtWinId(window); | 220 | KWayland::Client::Surface *surface = KWayland::Client::Surface::fromWindow(window); | ||
221 | | ||||
137 | if (surface) { | 222 | if (surface) { | ||
138 | if (enable) { | 223 | if (enable) { | ||
139 | auto blur = WaylandIntegration::self()->waylandBlurManager()->createBlur(surface, surface); | 224 | auto blur = WaylandIntegration::self()->waylandBlurManager()->createBlur(surface, surface); | ||
140 | blur->setRegion(WaylandIntegration::self()->waylandCompositor()->createRegion(region, nullptr)); | 225 | blur->setRegion(WaylandIntegration::self()->waylandCompositor()->createRegion(region, nullptr)); | ||
141 | blur->commit(); | 226 | blur->commit(); | ||
142 | } else { | 227 | } else { | ||
143 | WaylandIntegration::self()->waylandBlurManager()->removeBlur(surface); | 228 | WaylandIntegration::self()->waylandBlurManager()->removeBlur(surface); | ||
144 | } | 229 | } | ||
145 | surface->commit(KWayland::Client::Surface::CommitFlag::None); | 230 | surface->commit(KWayland::Client::Surface::CommitFlag::None); | ||
146 | 231 | | |||
147 | WaylandIntegration::self()->waylandConnection()->flush(); | 232 | WaylandIntegration::self()->waylandConnection()->flush(); | ||
148 | } | 233 | } | ||
149 | } | 234 | } | ||
150 | 235 | | |||
151 | void WindowEffects::enableBackgroundContrast(WId window, bool enable, qreal contrast, qreal intensity, qreal saturation, const QRegion ®ion) | 236 | void WindowEffects::enableBackgroundContrast(WId winId, bool enable, qreal contrast, qreal intensity, qreal saturation, const QRegion ®ion) | ||
237 | { | ||||
238 | auto window = windowForId(winId); | ||||
239 | if (!window) { | ||||
240 | return; | ||||
241 | } | ||||
242 | if (enable) { | ||||
243 | trackWindow(window); | ||||
244 | m_backgroundConstrastRegions[window].contrast = contrast; | ||||
245 | m_backgroundConstrastRegions[window].intensity = intensity; | ||||
246 | m_backgroundConstrastRegions[window].saturation = saturation; | ||||
247 | m_backgroundConstrastRegions[window].region = region; | ||||
248 | } else { | ||||
249 | m_backgroundConstrastRegions.remove(window); | ||||
250 | releaseWindow(window); | ||||
251 | } | ||||
252 | | ||||
253 | enableBackgroundContrast(window, enable, contrast, intensity, saturation, region); | ||||
254 | } | ||||
255 | | ||||
256 | void WindowEffects::enableBackgroundContrast(QWindow *window, bool enable, qreal contrast, qreal intensity, qreal saturation, const QRegion ®ion) | ||||
152 | { | 257 | { | ||
153 | if (!WaylandIntegration::self()->waylandContrastManager()) { | 258 | if (!WaylandIntegration::self()->waylandContrastManager()) { | ||
154 | return; | 259 | return; | ||
155 | } | 260 | } | ||
156 | KWayland::Client::Surface *surface = KWayland::Client::Surface::fromQtWinId(window); | 261 | KWayland::Client::Surface *surface = KWayland::Client::Surface::fromWindow(window); | ||
157 | if (surface) { | 262 | if (surface) { | ||
158 | if (enable) { | 263 | if (enable) { | ||
159 | auto backgroundContrast = WaylandIntegration::self()->waylandContrastManager()->createContrast(surface, surface); | 264 | auto backgroundContrast = WaylandIntegration::self()->waylandContrastManager()->createContrast(surface, surface); | ||
160 | backgroundContrast->setRegion(WaylandIntegration::self()->waylandCompositor()->createRegion(region, nullptr)); | 265 | backgroundContrast->setRegion(WaylandIntegration::self()->waylandCompositor()->createRegion(region, nullptr)); | ||
161 | backgroundContrast->setContrast(contrast); | 266 | backgroundContrast->setContrast(contrast); | ||
162 | backgroundContrast->setIntensity(intensity); | 267 | backgroundContrast->setIntensity(intensity); | ||
163 | backgroundContrast->setSaturation(saturation); | 268 | backgroundContrast->setSaturation(saturation); | ||
164 | backgroundContrast->commit(); | 269 | backgroundContrast->commit(); | ||
Show All 15 Lines |