diff options
| author | Assam Boudjelthia <[email protected]> | 2025-06-17 15:50:37 +0000 |
|---|---|---|
| committer | Assam Boudjelthia <[email protected]> | 2025-06-24 14:42:05 +0000 |
| commit | 3ad1bc6731f35b1bff8fc9192ac4e28424273c90 (patch) | |
| tree | 9a6a0e8508efd92eddb3241c11ba9b4f02bd9bb4 /src | |
| parent | 5e405060431cae3cec450a4a73adcb650d3f86af (diff) | |
Revert "Revert "Android: consider DecorView insets at app startup""
This reverts commit 05f8abc61dd2429d7041a87d5bfc7bffbb105f12.
Reason for revert: Quick tests fixed to account for safe margins.
Pick-to: 6.10 6.9
Change-Id: I3c9d182267bb22b36ed0031b0fe744f331559b3d
Reviewed-by: Tor Arne Vestbø <[email protected]>
Diffstat (limited to 'src')
| -rw-r--r-- | src/android/jar/src/org/qtproject/qt/android/QtWindow.java | 58 | ||||
| -rw-r--r-- | src/plugins/platforms/android/qandroidplatformwindow.cpp | 5 |
2 files changed, 49 insertions, 14 deletions
diff --git a/src/android/jar/src/org/qtproject/qt/android/QtWindow.java b/src/android/jar/src/org/qtproject/qt/android/QtWindow.java index 27d206b98cb..22d387945c1 100644 --- a/src/android/jar/src/org/qtproject/qt/android/QtWindow.java +++ b/src/android/jar/src/org/qtproject/qt/android/QtWindow.java @@ -77,27 +77,57 @@ class QtWindow extends QtLayout implements QtSurfaceInterface { }); m_gestureDetector.setIsLongpressEnabled(true); }); + } - if (getContext() instanceof QtActivityBase) { - setOnApplyWindowInsetsListener((view, insets) -> { - Insets safeInsets; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - int types = WindowInsets.Type.displayCutout() | WindowInsets.Type.systemBars(); - safeInsets = insets.getInsets(types); - } else { - safeInsets = getSafeInsetsPreAndroidR(view, insets); - } - QtNative.runAction(() -> safeAreaMarginsChanged(safeInsets, getId())); - return insets; - }); + @UsedFromNativeCode + void registerSafeAreaMarginsListner(boolean isTopLevel, boolean isSameWindowAndScreenSize) + { + if (!(getContext() instanceof QtActivityBase)) + return; + + setOnApplyWindowInsetsListener((view, insets) -> { + Insets safeInsets = getSafeInsets(view, insets); + safeAreaMarginsChanged(safeInsets, getId()); + return getConsumedInsets(insets); + }); - QtNative.runAction(() -> requestApplyInsets()); + // NOTE: if the window size fits the screen geometry (i.e. edge-to-edge case), + // assume this window is the main window and initialize its safe margins with + // the insets of the decor view. + if (isTopLevel && isSameWindowAndScreenSize) { + QtNative.runAction(() -> { + // NOTE: The callback onApplyWindowInsetsListener() is not being triggered during + // startup, so this is a Workaround to get the safe area margins at startup. + // Initially, set the root view insets to the current window, then if the insets + // change later, we can rely on setOnApplyWindowInsetsListener() being called. + View decorView = ((Activity) getContext()).getWindow().getDecorView(); + WindowInsets rootInsets = decorView.getRootWindowInsets(); + Insets rootSafeInsets = getSafeInsets(decorView, rootInsets); + safeAreaMarginsChanged(rootSafeInsets, getId()); + }); } + + QtNative.runAction(() -> requestApplyInsets()); } @SuppressWarnings("deprecation") - Insets getSafeInsetsPreAndroidR(View view, WindowInsets insets) + WindowInsets getConsumedInsets(WindowInsets insets) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) + return WindowInsets.CONSUMED; + else + return insets.consumeSystemWindowInsets(); + } + + @SuppressWarnings("deprecation") + Insets getSafeInsets(View view, WindowInsets insets) + { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + int types = WindowInsets.Type.displayCutout() | WindowInsets.Type.systemBars(); + return insets.getInsets(types); + } + + // Android R and older int left = 0; int top = 0; int right = 0; diff --git a/src/plugins/platforms/android/qandroidplatformwindow.cpp b/src/plugins/platforms/android/qandroidplatformwindow.cpp index c85917049d6..4105b1030c8 100644 --- a/src/plugins/platforms/android/qandroidplatformwindow.cpp +++ b/src/plugins/platforms/android/qandroidplatformwindow.cpp @@ -96,6 +96,11 @@ void QAndroidPlatformWindow::initialize() } qCDebug(lcQpaWindow) << "Window" << m_nativeViewId << "using surface container type" << static_cast<int>(m_surfaceContainerType); + + const bool isSameWindowAndScreenSize = geometry().size() == screen()->geometry().size(); + m_nativeQtWindow.callMethod("registerSafeAreaMarginsListner", + window->isTopLevel(), isSameWindowAndScreenSize); + } QAndroidPlatformWindow::~QAndroidPlatformWindow() |
