File indexing completed on 2024-05-19 15:21:18

0001 /*
0002     SPDX-FileCopyrightText: 2019 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "kwindowshadow.h"
0008 #include "kwindowshadow_dummy_p.h"
0009 #include "kwindowshadow_p.h"
0010 #include "kwindowsystem_debug.h"
0011 #include "pluginwrapper_p.h"
0012 
0013 #include <array>
0014 
0015 KWindowShadowTile::KWindowShadowTile()
0016     : d(KWindowSystemPluginWrapper::self().createWindowShadowTile())
0017 {
0018 }
0019 
0020 KWindowShadowTile::~KWindowShadowTile()
0021 {
0022     if (d->isCreated) {
0023         d->destroy();
0024     }
0025 }
0026 
0027 QImage KWindowShadowTile::image() const
0028 {
0029     return d->image;
0030 }
0031 
0032 void KWindowShadowTile::setImage(const QImage &image)
0033 {
0034     if (d->isCreated) {
0035         qCWarning(LOG_KWINDOWSYSTEM,
0036                   "Cannot change the image on a tile that already has native "
0037                   "platform resources allocated.");
0038         return;
0039     }
0040     d->image = image;
0041 }
0042 
0043 bool KWindowShadowTile::isCreated() const
0044 {
0045     return d->isCreated;
0046 }
0047 
0048 bool KWindowShadowTile::create()
0049 {
0050     if (d->isCreated) {
0051         return true;
0052     }
0053     d->isCreated = d->create();
0054     return d->isCreated;
0055 }
0056 
0057 KWindowShadow::KWindowShadow(QObject *parent)
0058     : QObject(parent)
0059     , d(KWindowSystemPluginWrapper::self().createWindowShadow())
0060 {
0061 }
0062 
0063 KWindowShadow::~KWindowShadow()
0064 {
0065     destroy();
0066 }
0067 
0068 KWindowShadowTile::Ptr KWindowShadow::leftTile() const
0069 {
0070     return d->leftTile;
0071 }
0072 
0073 void KWindowShadow::setLeftTile(KWindowShadowTile::Ptr tile)
0074 {
0075     if (d->isCreated) {
0076         qCWarning(LOG_KWINDOWSYSTEM,
0077                   "Cannot attach a left tile to a shadow that already has "
0078                   "native platform resources allocated. To do so, destroy() the shadow and then "
0079                   "setLeftTile() and create()");
0080         return;
0081     }
0082     d->leftTile = tile;
0083 }
0084 
0085 KWindowShadowTile::Ptr KWindowShadow::topLeftTile() const
0086 {
0087     return d->topLeftTile;
0088 }
0089 
0090 void KWindowShadow::setTopLeftTile(KWindowShadowTile::Ptr tile)
0091 {
0092     if (d->isCreated) {
0093         qCWarning(LOG_KWINDOWSYSTEM,
0094                   "Cannot attach a top-left tile to a shadow that already has "
0095                   "native platform resources allocated. To do so, destroy() the shadow and then "
0096                   "setTopLeftTile() and create()");
0097         return;
0098     }
0099     d->topLeftTile = tile;
0100 }
0101 
0102 KWindowShadowTile::Ptr KWindowShadow::topTile() const
0103 {
0104     return d->topTile;
0105 }
0106 
0107 void KWindowShadow::setTopTile(KWindowShadowTile::Ptr tile)
0108 {
0109     if (d->isCreated) {
0110         qCWarning(LOG_KWINDOWSYSTEM,
0111                   "Cannot attach a top tile to a shadow that already has "
0112                   "native platform resources allocated. To do so, destroy() the shadow and then "
0113                   "setTopTile() and create()");
0114         return;
0115     }
0116     d->topTile = tile;
0117 }
0118 
0119 KWindowShadowTile::Ptr KWindowShadow::topRightTile() const
0120 {
0121     return d->topRightTile;
0122 }
0123 
0124 void KWindowShadow::setTopRightTile(KWindowShadowTile::Ptr tile)
0125 {
0126     if (d->isCreated) {
0127         qCWarning(LOG_KWINDOWSYSTEM,
0128                   "Cannot attach a top-right tile to a shadow that already "
0129                   "has native platform resources allocated. To do so, destroy() the shadow and "
0130                   "then setTopRightTile() and create()");
0131         return;
0132     }
0133     d->topRightTile = tile;
0134 }
0135 
0136 KWindowShadowTile::Ptr KWindowShadow::rightTile() const
0137 {
0138     return d->rightTile;
0139 }
0140 
0141 void KWindowShadow::setRightTile(KWindowShadowTile::Ptr tile)
0142 {
0143     if (d->isCreated) {
0144         qCWarning(LOG_KWINDOWSYSTEM,
0145                   "Cannot attach a right tile to a shadow that already has "
0146                   "native platform resources allocated. To do so, destroy() the shadow and then "
0147                   "setRightTile() and create()");
0148         return;
0149     }
0150     d->rightTile = tile;
0151 }
0152 
0153 KWindowShadowTile::Ptr KWindowShadow::bottomRightTile() const
0154 {
0155     return d->bottomRightTile;
0156 }
0157 
0158 void KWindowShadow::setBottomRightTile(KWindowShadowTile::Ptr tile)
0159 {
0160     if (d->isCreated) {
0161         qCWarning(LOG_KWINDOWSYSTEM,
0162                   "Cannot attach a bottom-right tile to a shadow that already "
0163                   "has native platform resources allocated. To do so, destroy() the shadow and "
0164                   "then setBottomRightTile() and create()");
0165         return;
0166     }
0167     d->bottomRightTile = tile;
0168 }
0169 
0170 KWindowShadowTile::Ptr KWindowShadow::bottomTile() const
0171 {
0172     return d->bottomTile;
0173 }
0174 
0175 void KWindowShadow::setBottomTile(KWindowShadowTile::Ptr tile)
0176 {
0177     if (d->isCreated) {
0178         qCWarning(LOG_KWINDOWSYSTEM,
0179                   "Cannot attach a bottom tile to a shadow that already has "
0180                   "native platform resources allocated. To do so, destroy() the shadow and then "
0181                   "setBottomTile() and create()");
0182         return;
0183     }
0184     d->bottomTile = tile;
0185 }
0186 
0187 KWindowShadowTile::Ptr KWindowShadow::bottomLeftTile() const
0188 {
0189     return d->bottomLeftTile;
0190 }
0191 
0192 void KWindowShadow::setBottomLeftTile(KWindowShadowTile::Ptr tile)
0193 {
0194     if (d->isCreated) {
0195         qCWarning(LOG_KWINDOWSYSTEM,
0196                   "Cannot attach a bottom-left tile to a shadow that already "
0197                   "has native platform resources allocated. To do so, destroy() the shadow and "
0198                   "then setBottomLeftTile() and create()");
0199         return;
0200     }
0201     d->bottomLeftTile = tile;
0202 }
0203 
0204 QMargins KWindowShadow::padding() const
0205 {
0206     return d->padding;
0207 }
0208 
0209 void KWindowShadow::setPadding(const QMargins &padding)
0210 {
0211     if (d->isCreated) {
0212         qCWarning(LOG_KWINDOWSYSTEM,
0213                   "Cannot set the padding on a shadow that already has "
0214                   "native platform resources allocated. To do so, destroy() the shadow and "
0215                   "then setPadding() and create()");
0216         return;
0217     }
0218     d->padding = padding;
0219 }
0220 
0221 QWindow *KWindowShadow::window() const
0222 {
0223     return d->window;
0224 }
0225 
0226 void KWindowShadow::setWindow(QWindow *window)
0227 {
0228     if (d->isCreated) {
0229         qCWarning(LOG_KWINDOWSYSTEM,
0230                   "Cannot set the target window on a shadow that already has "
0231                   "native platform resources allocated. To do so, destroy() the shadow and then "
0232                   "setWindow() and create()");
0233         return;
0234     }
0235     d->window = window;
0236 }
0237 
0238 bool KWindowShadow::isCreated() const
0239 {
0240     return d->isCreated;
0241 }
0242 
0243 bool KWindowShadow::create()
0244 {
0245     if (d->isCreated) {
0246         return true;
0247     }
0248     if (!d->window) {
0249         qCWarning(LOG_KWINDOWSYSTEM,
0250                   "Cannot allocate the native platform resources for the shadow "
0251                   "because the target window is not specified.");
0252         return false;
0253     }
0254     if (!d->prepareTiles()) {
0255         return false;
0256     }
0257     d->isCreated = d->create();
0258     return d->isCreated;
0259 }
0260 
0261 void KWindowShadow::destroy()
0262 {
0263     if (!d->isCreated) {
0264         return;
0265     }
0266     d->destroy();
0267     d->isCreated = false;
0268 }
0269 
0270 KWindowShadowTilePrivate::~KWindowShadowTilePrivate()
0271 {
0272 }
0273 
0274 KWindowShadowTilePrivate *KWindowShadowTilePrivate::get(const KWindowShadowTile *tile)
0275 {
0276     return tile->d.data();
0277 }
0278 
0279 KWindowShadowPrivate::~KWindowShadowPrivate()
0280 {
0281 }
0282 
0283 bool KWindowShadowPrivate::create()
0284 {
0285     return false;
0286 }
0287 
0288 void KWindowShadowPrivate::destroy()
0289 {
0290 }
0291 
0292 bool KWindowShadowPrivate::prepareTiles()
0293 {
0294     const std::array<KWindowShadowTile *, 8> tiles{
0295         leftTile.data(),
0296         topLeftTile.data(),
0297         topTile.data(),
0298         topRightTile.data(),
0299         rightTile.data(),
0300         bottomRightTile.data(),
0301         bottomTile.data(),
0302         bottomLeftTile.data(),
0303     };
0304 
0305     for (KWindowShadowTile *tile : tiles) {
0306         if (!tile) {
0307             continue;
0308         }
0309         if (tile->isCreated()) {
0310             continue;
0311         }
0312         if (!tile->create()) {
0313             return false;
0314         }
0315     }
0316 
0317     return true;
0318 }
0319 
0320 bool KWindowShadowTilePrivateDummy::create()
0321 {
0322     return false;
0323 }
0324 
0325 void KWindowShadowTilePrivateDummy::destroy()
0326 {
0327 }
0328 
0329 bool KWindowShadowPrivateDummy::create()
0330 {
0331     return false;
0332 }
0333 
0334 void KWindowShadowPrivateDummy::destroy()
0335 {
0336 }
0337 
0338 #include "moc_kwindowshadow.cpp"