|
| 1 | +#include "lint.hpp" |
| 2 | +#include <algorithm> |
| 3 | + |
| 4 | +#include <qlogging.h> |
| 5 | +#include <qloggingcategory.h> |
| 6 | +#include <qobject.h> |
| 7 | +#include <qqmlcontext.h> |
| 8 | +#include <qqmlengine.h> |
| 9 | +#include <qqmlinfo.h> |
| 10 | +#include <qquickframebufferobject.h> |
| 11 | +#include <qquickitem.h> |
| 12 | +#include <qquickpainteditem.h> |
| 13 | +#include <qquickrhiitem.h> |
| 14 | +#include <qquickwindow.h> |
| 15 | +#include <qstringliteral.h> |
| 16 | + |
| 17 | +namespace qs::debug { |
| 18 | + |
| 19 | +Q_LOGGING_CATEGORY(logLint, "quickshell.linter", QtWarningMsg); |
| 20 | + |
| 21 | +void lintZeroSized(QQuickItem* item); |
| 22 | +bool isRenderable(QQuickItem* item); |
| 23 | + |
| 24 | +void lintObjectTree(QObject* object) { |
| 25 | + if (!logLint().isWarningEnabled()) return; |
| 26 | + |
| 27 | + qCDebug(logLint) << "Walking children of object" << object; |
| 28 | + |
| 29 | + for (auto* child: object->children()) { |
| 30 | + if (child->isQuickItemType()) { |
| 31 | + auto* item = static_cast<QQuickItem*>(child); // NOLINT; |
| 32 | + lintItemTree(item); |
| 33 | + } else { |
| 34 | + lintObjectTree(child); |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +void lintItemTree(QQuickItem* item) { |
| 40 | + if (!logLint().isWarningEnabled()) return; |
| 41 | + |
| 42 | + qCDebug(logLint) << "Running lints for item" << item; |
| 43 | + lintZeroSized(item); |
| 44 | + |
| 45 | + qCDebug(logLint) << "Walking visual children of item" << item; |
| 46 | + for (auto* child: item->childItems()) { |
| 47 | + lintItemTree(child); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +void lintZeroSized(QQuickItem* item) { |
| 52 | + if (!item->isEnabled() || !item->isVisible()) return; |
| 53 | + if (item->childItems().isEmpty()) return; |
| 54 | + |
| 55 | + auto zeroWidth = item->width() == 0; |
| 56 | + auto zeroHeight = item->height() == 0; |
| 57 | + |
| 58 | + if (!zeroWidth && !zeroHeight) return; |
| 59 | + |
| 60 | + if (!isRenderable(item)) return; |
| 61 | + |
| 62 | + auto* ctx = QQmlEngine::contextForObject(item); |
| 63 | + if (!ctx || ctx->baseUrl().scheme() != QStringLiteral("qsintercept")) return; |
| 64 | + |
| 65 | + qmlWarning(item) << "Item is visible and has visible children, but has zero " |
| 66 | + << (zeroWidth && zeroHeight ? "width and height" |
| 67 | + : zeroWidth ? "width" |
| 68 | + : "height"); |
| 69 | +} |
| 70 | + |
| 71 | +bool isRenderable(QQuickItem* item) { |
| 72 | + if (!item->isEnabled() || !item->isVisible()) return false; |
| 73 | + |
| 74 | + if (item->flags().testFlags(QQuickItem::ItemHasContents)) { |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + return std::ranges::any_of(item->childItems(), [](auto* item) { return isRenderable(item); }); |
| 79 | +} |
| 80 | + |
| 81 | +} // namespace qs::debug |
0 commit comments