Android: don't use OnPreDrawListener in setOnApplyWindowInsetsListener

Firstly, this was adding a new OnPreDrawListener listener with each
onApplyWindowInsets call and it wasn't cleaned afterwards. In this
case it's not even necessary to have those nested listeners.

Furthermore, don't set m_firstSafeMarginsDelivered from within the
onApplyWindowInsetsListener so that we're certain that some safe
margin values are retrieved from sources other than the
onApplyWindowInsetsListener just a guarantee.

If the window moves or resizes under the root while the root
doesn't change, signals like setOnApplyWindowInsetsListener
won't be sent thus we'll end up with out-dated safe margins,
so add addOnLayoutChangeListener to fix that.
Along the way move the expanded show after the normal show
because that's how I found this case handling was missing.

Pick-to: 6.9 6.10 6.10.0
Task-number: QTBUG-135808
Change-Id: I57c74cbd8ec7a0c190dc97ba9a92a0292a535240
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
This commit is contained in:
Assam Boudjelthia 2025-09-08 20:16:04 +03:00
parent cc984a60fd
commit 13a1cf98e6
2 changed files with 11 additions and 18 deletions

View File

@ -89,15 +89,7 @@ class QtWindow extends QtLayout implements QtSurfaceInterface {
setOnApplyWindowInsetsListener((view, insets) -> { setOnApplyWindowInsetsListener((view, insets) -> {
WindowInsets windowInsets = view.onApplyWindowInsets(insets); WindowInsets windowInsets = view.onApplyWindowInsets(insets);
ViewTreeObserver.OnPreDrawListener listener = new ViewTreeObserver.OnPreDrawListener() { reportSafeAreaMargins(windowInsets, getId());
@Override
public boolean onPreDraw() {
reportSafeAreaMargins(windowInsets, getId());
m_firstSafeMarginsDelivered = true;
return true;
}
};
getViewTreeObserver().addOnPreDrawListener(listener);
return windowInsets; return windowInsets;
}); });
@ -127,11 +119,6 @@ class QtWindow extends QtLayout implements QtSurfaceInterface {
ViewTreeObserver.OnPreDrawListener listener = new ViewTreeObserver.OnPreDrawListener() { ViewTreeObserver.OnPreDrawListener listener = new ViewTreeObserver.OnPreDrawListener() {
@Override @Override
public boolean onPreDraw() { public boolean onPreDraw() {
if (m_firstSafeMarginsDelivered) {
getViewTreeObserver().removeOnPreDrawListener(this);
return true;
}
if (isAttachedToWindow()) { if (isAttachedToWindow()) {
WindowInsets insets = getRootWindowInsets(); WindowInsets insets = getRootWindowInsets();
if (insets != null) { if (insets != null) {
@ -149,6 +136,12 @@ class QtWindow extends QtLayout implements QtSurfaceInterface {
}; };
getViewTreeObserver().addOnPreDrawListener(listener); getViewTreeObserver().addOnPreDrawListener(listener);
} }
addOnLayoutChangeListener((view, l, t, r, b, oldl, oldt, oldr, oldb) -> {
WindowInsets insets = getRootWindowInsets();
if (insets != null)
getRootView().post(() -> reportSafeAreaMargins(insets, getId()));
});
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")

View File

@ -232,14 +232,14 @@ void tst_Android::safeAreaWithWindowFlagsAndStates_data()
<< Qt::WindowStates(Qt::WindowNoState) << Qt::WindowStates(Qt::WindowNoState)
<< Qt::WindowFlags(); << Qt::WindowFlags();
QTest::newRow("Fullscreen")
<< Qt::WindowStates(Qt::WindowFullScreen)
<< Qt::WindowFlags();
QTest::newRow("Expanded Client Area") QTest::newRow("Expanded Client Area")
<< Qt::WindowStates(Qt::WindowNoState) << Qt::WindowStates(Qt::WindowNoState)
<< Qt::WindowFlags(Qt::ExpandedClientAreaHint); << Qt::WindowFlags(Qt::ExpandedClientAreaHint);
QTest::newRow("Fullscreen")
<< Qt::WindowStates(Qt::WindowFullScreen)
<< Qt::WindowFlags();
QTest::newRow("Fullscreen and Expanded Client Area") QTest::newRow("Fullscreen and Expanded Client Area")
<< Qt::WindowStates(Qt::WindowFullScreen) << Qt::WindowStates(Qt::WindowFullScreen)
<< Qt::WindowFlags(Qt::ExpandedClientAreaHint); << Qt::WindowFlags(Qt::ExpandedClientAreaHint);