1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
// Qt-Security score:significant reason:default
#include <QtQuick/private/qquicksafearea_p.h>
#include <QtQuick/private/qquickanchors_p_p.h>
#include <QtQuick/private/qquickitem_p.h>
#include <QtQuick/private/qquickflickable_p.h>
#include <QtQuick/qquickwindow.h>
#include <QtQuick/qquickitem.h>
QT_BEGIN_NAMESPACE
Q_STATIC_LOGGING_CATEGORY(lcSafeArea, "qt.quick.safearea", QtWarningMsg)
/*!
\qmltype SafeArea
\nativetype QQuickSafeArea
\inqmlmodule QtQuick
\ingroup qtquick-visual
\since 6.9
\brief Provides access to the safe area properties of the item or window.
The SafeArea attached type provides information about the areas of
an Item or Window where content may risk being overlapped by other
UI elements, such as system title bars or status bars.
This information can be used to lay out children of an item within
the safe area of the item, while still allowing a background color
or effect to span the entire item.
\table
\row
\li \snippet qml/safearea/basic.qml 0
\li \inlineimage safearea-ios.webp
\endtable
The SafeArea margins are relative to the item they attach to. If an
ancestor item has laid out its children within the safe area margins,
any descendant item with its own SafeArea attached will report zero
margins, unless \l{Additional margins}{additional margins} have been
added.
\note An item should not be positioned based on \e{its own} safe area,
as that would result in a binding loop.
\section2 Additional margins
Sometimes an item's layout involves child items that overlap each other,
for example in a window with a semi transparent header, where the rest
of the window content flows underneath the header.
In this scenario, the item may reflect the header's position and size
to the child items via the additionalMargins property.
The additional margins will be added to any margins that the
item already picks up from its parent hierarchy (including system
margins, such as title bars or status bars), and child items will
reflect the combined margins accordingly.
\table
\row
\li \snippet qml/safearea/additional.qml 0
\li \br \inlineimage safearea-ios-header.webp
\endtable
In the example above, the header item is positioned at the top of
the window, which may potentially overlap with existing safe area
margins coming from the window. To account for this we only add
additional margins for the part of the header that extends beyond
the window's safe area margins.
\note In this example the header item does not overlap the child item,
as the goal is to show how the items are positioned and resized in
response to safe area margin changes.
\section2 Controls
Applying safe area margins to a Control is straightforward,
as Control already offers properties to add padding to the
control's content item.
\snippet qml/safearea/controls.qml 0
*/
QQuickSafeArea *QQuickSafeArea::qmlAttachedProperties(QObject *attachee)
{
auto *item = qobject_cast<QQuickItem*>(attachee);
if (!item) {
if (auto *window = qobject_cast<QQuickWindow*>(attachee))
item = window->contentItem();
}
if (!item) {
if (auto *safeAreaAttachable = qobject_cast<QQuickSafeAreaAttachable*>(attachee))
item = safeAreaAttachable->safeAreaAttachmentItem();
}
if (!item) {
qmlWarning(attachee) << "SafeArea can not be attached to this type";
return nullptr;
}
// We may already have created a safe area for Window, and are now
// requesting one for Window.contentItem (or the other way around).
// As both map to the same safe area item, we need to check first
// if we already have created one for this item.
if (auto *safeArea = item->findChild<QQuickSafeArea*>(Qt::FindDirectChildrenOnly))
return safeArea;
return new QQuickSafeArea(item);
}
QQuickSafeArea::QQuickSafeArea(QQuickItem *item)
: QObject(item)
{
qCInfo(lcSafeArea) << "Creating" << this;
connect(item, &QQuickItem::windowChanged,
this, &QQuickSafeArea::windowChanged);
item->setFlag(QQuickItem::ItemObservesViewport);
QQuickItemPrivate::get(item)->addItemChangeListener(
this, QQuickItemPrivate::Matrix);
updateSafeArea();
}
QQuickSafeArea::~QQuickSafeArea()
{
qCInfo(lcSafeArea) << "Destroying" << this;
const auto listenedItems = m_listenedItems;
for (const auto &item : listenedItems) {
if (!item)
continue;
auto *itemPrivate = QQuickItemPrivate::get(item);
itemPrivate->removeItemChangeListener(this,
QQuickItemPrivate::Matrix);
itemPrivate->removeItemChangeListener(this,
QQuickItemPrivate::Geometry);
}
}
/*!
\qmlpropertygroup QtQuick::SafeArea::margins
\qmlproperty real QtQuick::SafeArea::margins.top
\qmlproperty real QtQuick::SafeArea::margins.left
\qmlproperty real QtQuick::SafeArea::margins.right
\qmlproperty real QtQuick::SafeArea::margins.bottom
\readonly
This property holds the safe area margins, relative
to the attached item.
\sa additionalMargins
*/
QMarginsF QQuickSafeArea::margins() const
{
return m_safeAreaMargins;
}
/*!
\qmlpropertygroup QtQuick::SafeArea::additionalMargins
\qmlproperty real QtQuick::SafeArea::additionalMargins.top
\qmlproperty real QtQuick::SafeArea::additionalMargins.left
\qmlproperty real QtQuick::SafeArea::additionalMargins.right
\qmlproperty real QtQuick::SafeArea::additionalMargins.bottom
This property holds the additional safe area margins for the item.
The additional safe area margins can not be negative, and will be
automatically clamped to 0.
The resulting safe area margins of the item are the sum of the inherited
margins (for example from title bars or status bar) and the additional
margins applied to the item.
\sa margins
*/
void QQuickSafeArea::setAdditionalMargins(const QMarginsF &additionalMargins)
{
// Additional margins should never be negative
auto newMargins = additionalMargins | QMarginsF();
if (newMargins == m_additionalMargins)
return;
m_additionalMargins = newMargins;
emit additionalMarginsChanged();
auto *attachedItem = qobject_cast<QQuickItem*>(parent());
updateSafeAreasRecursively(attachedItem);
}
QMarginsF QQuickSafeArea::additionalMargins() const
{
return m_additionalMargins;
}
/*
Maps the safe area \a margins from \a fromItem to \a toItem
*/
static QMarginsF toLocalMargins(const QMarginsF &margins, QQuickItem *fromItem, QQuickItem *toItem)
{
if (margins.isNull())
return margins;
const auto localMarginRect = fromItem->mapRectToItem(toItem,
QRectF(margins.left(), margins.top(),
fromItem->width() - margins.left() - margins.right(),
fromItem->height() - margins.top() - margins.bottom()));
// Only return a mapped margin if there was an original margin
return QMarginsF(
margins.left() > 0 ? localMarginRect.left() : 0,
margins.top() > 0 ? localMarginRect.top() : 0,
margins.right() > 0 ? toItem->width() - localMarginRect.right() : 0,
margins.bottom() > 0 ? toItem->height() - localMarginRect.bottom() : 0
) | QMarginsF();
}
void QQuickSafeArea::updateSafeArea()
{
qCDebug(lcSafeArea) << "✨ Updating" << this;
auto *attachedItem = qobject_cast<QQuickItem*>(parent());
if (!QQuickItemPrivate::get(attachedItem)->componentComplete) {
qCDebug(lcSafeArea) << attachedItem << "is not complete. Deferring";
return;
}
QMarginsF inheritedMargins;
auto *parentItem = attachedItem->parentItem();
while (parentItem) {
if (qobject_cast<QQuickFlickable*>(parentItem)) {
// Stop propagation of safe areas when we hit a Flickable,
// as items within the content item that account for safe
// area margins will continuously update when the content
// item is moved, which is not necessarily what the user
// expects.
qCDebug(lcSafeArea) << "Stopping safe area margin propagation on" << parentItem;
break;
}
// We attach the safe area to the relevant item for an attachee
// such as QQuickWindow or QQuickPopup, so we can't go via
// qmlAttachedPropertiesObject to find the safe area for an
// item, as the attached object cache is based on the original
// attachee.
if (auto *safeArea = parentItem->findChild<QQuickSafeArea*>(Qt::FindDirectChildrenOnly)) {
inheritedMargins = safeArea->margins();
break;
}
parentItem = parentItem->parentItem();
}
const auto *window = attachedItem->window();
if (!parentItem && window) {
// We didn't find a parent item with a safe area,
// so inherit the margins from the window.
parentItem = window->contentItem();
inheritedMargins = window->safeAreaMargins();
}
auto inheritedMarginsMapped = toLocalMargins(inheritedMargins, parentItem, attachedItem);
// Make sure margins are never negative
const QMarginsF newMargins = QMarginsF() | (inheritedMarginsMapped + additionalMargins());
if (newMargins != m_safeAreaMargins) {
qCDebug(lcSafeArea) << "Margins changed from" << m_safeAreaMargins
<< "to" << newMargins
<< "based on inherited" << inheritedMargins
<< "mapped to local" << inheritedMarginsMapped
<< "and additional" << additionalMargins();
m_safeAreaMargins = newMargins;
if (emittingMarginsUpdate) {
// We are already in the process of emitting an update for this
// safe area, which resulted in the safe area margins changing.
// This can be a binding loop if the margins do not stabilize,
// which we'll detect when we return from the root emit below.
qCDebug(lcSafeArea) << "Already emitting update for" << this;
return;
}
QScopedValueRollback blocker(emittingMarginsUpdate, true);
emit marginsChanged();
if (m_safeAreaMargins != newMargins) {
qCDebug(lcSafeArea) << "⚠️ Possible binding loop for" << this
<< newMargins << "changed to" << m_safeAreaMargins;
QScopedValueRollback blocker(detectedPossibleBindingLoop, true);
for (int i = 0; i < 5; ++i) {
auto marginsBeforeEmit = m_safeAreaMargins;
emit marginsChanged();
if (m_safeAreaMargins == marginsBeforeEmit) {
qCDebug(lcSafeArea) << "✅ Margins stabilized for" << this;
return;
}
qCDebug(lcSafeArea) << qPrintable(QStringLiteral("‼️").repeated(i + 1))
<< marginsBeforeEmit << "changed to" << m_safeAreaMargins;
}
qmlWarning(attachedItem) << "Safe area binding loop detected";
}
}
}
void QQuickSafeArea::windowChanged()
{
updateSafeArea();
}
void QQuickSafeArea::itemTransformChanged(QQuickItem *item, QQuickItem *transformedItem)
{
Q_ASSERT(item == parent());
auto *transformedItemPrivate = QQuickItemPrivate::get(transformedItem);
qCDebug(lcSafeArea) << "📏 Transform changed for" << transformedItem
<< "with dirty state" << transformedItemPrivate->dirtyToString();
if (qobject_cast<QQuickFlickable*>(transformedItem->parentItem())) {
qCDebug(lcSafeArea) << "Ignoring transform change for Flickable content item";
return;
}
// The order of transform and geometry change callbacks may not be in paint order,
// so to ensure we update the safe areas in paint order we find the item closest
// to the transformed item with a safe area, and let that safe area trigger the
// update recursively in paint order.
if (transformedItem != item) {
for (auto *parent = item->parentItem(); parent; parent = parent->parentItem()) {
if (parent->findChild<QQuickSafeArea*>(Qt::FindDirectChildrenOnly))
item = parent;
if (parent == transformedItem)
break;
}
}
if (item != parent()) {
qCDebug(lcSafeArea) << "Found" << item << "closer to transformed item than" << this;
return;
}
// The dirtying of position and size will be followed by a geometry change,
// which via anchors or event listeners may result in an ancestor invalidating
// its transform, which might invalidate the margins we're about to compute.
// Instead of processing the margin change now, possibly resulting in a flip-
// flop of the margins, we wait for the geometry notification, where the item
// hierarchy has already reacted to the geometry change of the transformed item.
// This accounts for anchors, and items that listen to geometry changes, but not
// property bindings, as those are emitted after notifying listeners (us) about
// the geometry change.
auto dirtyAttributes = transformedItemPrivate->dirtyAttributes;
if (dirtyAttributes & (QQuickItemPrivate::Position | QQuickItemPrivate::Size)) {
qCDebug(lcSafeArea) << "Deferring update of" << this << "until geometry change";
transformedItemPrivate->addItemChangeListener(
this, QQuickItemPrivate::Geometry);
return;
}
updateSafeAreasRecursively(item);
}
void QQuickSafeArea::itemGeometryChanged(QQuickItem *item, QQuickGeometryChange change, const QRectF &oldGeometry)
{
Q_UNUSED(change);
Q_UNUSED(oldGeometry);
auto *itemPrivate = QQuickItemPrivate::get(item);
itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
qCDebug(lcSafeArea) << "📐 Geometry changed for" << item << "from" << oldGeometry
<< "to" << QRectF(item->position(), item->size());
updateSafeAreasRecursively(item);
}
void QQuickSafeArea::updateSafeAreasRecursively(QQuickItem *item)
{
Q_ASSERT(item);
if (auto *safeArea = item->findChild<QQuickSafeArea*>(Qt::FindDirectChildrenOnly))
safeArea->updateSafeArea();
auto *itemPrivate = QQuickItemPrivate::get(item);
const auto paintOrderChildItems = itemPrivate->paintOrderChildItems();
for (auto *child : paintOrderChildItems)
updateSafeAreasRecursively(child);
}
void QQuickSafeArea::addSourceItem(QQuickItem *item)
{
m_listenedItems << item;
}
void QQuickSafeArea::removeSourceItem(QQuickItem *item)
{
m_listenedItems.removeAll(item);
}
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug debug, const QQuickSafeArea *safeArea)
{
QDebugStateSaver saver(debug);
debug.nospace();
if (!safeArea) {
debug << "QQuickSafeArea(nullptr)";
return debug;
}
debug << safeArea->metaObject()->className() << '(' << static_cast<const void *>(safeArea);
debug << ", attachedItem=" << safeArea->parent();
debug << ", safeAreaMargins=" << safeArea->m_safeAreaMargins;
debug << ", additionalMargins=" << safeArea->additionalMargins();
debug << ')';
return debug;
}
#endif // QT_NO_DEBUG_STREAM
QQuickSafeAreaAttachable::~QQuickSafeAreaAttachable() = default;
QT_END_NAMESPACE
#include "moc_qquicksafearea_p.cpp"
|