Skip to content

Commit 6ceee06

Browse files
committed
debug: add lint for zero sized items
1 parent 66b494d commit 6ceee06

File tree

7 files changed

+109
-0
lines changed

7 files changed

+109
-0
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ install(TARGETS quickshell RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
55
add_subdirectory(build)
66
add_subdirectory(launch)
77
add_subdirectory(core)
8+
add_subdirectory(debug)
89
add_subdirectory(ipc)
910
add_subdirectory(window)
1011
add_subdirectory(io)

src/debug/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
qt_add_library(quickshell-debug STATIC
2+
lint.cpp
3+
)
4+
5+
qs_pch(quickshell-debug)
6+
target_link_libraries(quickshell-debug PRIVATE Qt::Quick)
7+
target_link_libraries(quickshell PRIVATE quickshell-debug)

src/debug/lint.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

src/debug/lint.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
#include <qobject.h>
4+
#include <qquickitem.h>
5+
6+
namespace qs::debug {
7+
8+
void lintObjectTree(QObject* object);
9+
void lintItemTree(QQuickItem* item);
10+
11+
} // namespace qs::debug

src/window/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ target_link_libraries(quickshell-window PRIVATE
2222
Qt::Core Qt::Gui Qt::Quick Qt6::QuickPrivate
2323
)
2424

25+
qs_add_link_dependencies(quickshell-window quickshell-debug)
26+
2527
target_link_libraries(quickshell-window-init PRIVATE Qt::Qml)
2628

2729
qs_module_pch(quickshell-window SET large)

src/window/proxywindow.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "../core/qmlscreen.hpp"
2020
#include "../core/region.hpp"
2121
#include "../core/reload.hpp"
22+
#include "../debug/lint.hpp"
2223
#include "windowinterface.hpp"
2324

2425
ProxyWindowBase::ProxyWindowBase(QObject* parent)
@@ -214,6 +215,11 @@ void ProxyWindowBase::polishItems() {
214215
// This hack manually polishes the item tree right before showing the window so it will
215216
// always be created with the correct size.
216217
QQuickWindowPrivate::get(this->window)->polishItems();
218+
219+
if (!this->ranLints) {
220+
qs::debug::lintItemTree(this->mContentItem);
221+
this->ranLints = true;
222+
}
217223
}
218224

219225
qint32 ProxyWindowBase::x() const {

src/window/proxywindow.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ protected slots:
130130
QQuickWindow* window = nullptr;
131131
QQuickItem* mContentItem = nullptr;
132132
bool reloadComplete = false;
133+
bool ranLints = false;
133134

134135
private:
135136
void polishItems();

0 commit comments

Comments
 (0)