Procházet zdrojové kódy

Fix top insets when using pan keyboard mode

Janic Duplessis před 4 roky
rodič
revize
f0228a9068

+ 14
- 8
android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaView.java Zobrazit soubor

@@ -1,6 +1,7 @@
1 1
 package com.th3rdwave.safeareacontext;
2 2
 
3 3
 import android.content.Context;
4
+import android.graphics.Rect;
4 5
 import android.os.Build;
5 6
 import android.view.Surface;
6 7
 import android.view.View;
@@ -65,16 +66,21 @@ public class SafeAreaView extends ReactViewGroup implements ViewTreeObserver.OnG
65 66
     View contentView = rootView.findViewById(android.R.id.content);
66 67
     float windowWidth = rootView.getWidth();
67 68
     float windowHeight = rootView.getHeight();
68
-    int[] windowLocation = new int[2];
69
-    contentView.getLocationInWindow(windowLocation);
70
-    windowInsets.top = Math.max(windowInsets.top - windowLocation[1], 0);
71
-    windowInsets.left = Math.max(windowInsets.left - windowLocation[0], 0);
72
-    windowInsets.bottom = Math.max(windowLocation[1] + contentView.getHeight() + windowInsets.bottom - windowHeight, 0);
73
-    windowInsets.right = Math.max(windowLocation[0] + contentView.getWidth() + windowInsets.right - windowWidth, 0);
69
+    Rect visibleRect = new Rect();
70
+    contentView.getGlobalVisibleRect(visibleRect);
71
+
72
+    windowInsets.top = Math.max(windowInsets.top - visibleRect.top, 0);
73
+    windowInsets.left = Math.max(windowInsets.left - visibleRect.left, 0);
74
+    windowInsets.bottom = Math.max(visibleRect.top + contentView.getHeight() + windowInsets.bottom - windowHeight, 0);
75
+    windowInsets.right = Math.max(visibleRect.left + contentView.getWidth() + windowInsets.right - windowWidth, 0);
74 76
     return windowInsets;
75 77
   }
76 78
 
77 79
   private void maybeUpdateInsets() {
80
+    if (!isAttachedToWindow()) {
81
+      return;
82
+    }
83
+
78 84
     EdgeInsets edgeInsets = getSafeAreaInsets();
79 85
     if (mLastInsets == null || !mLastInsets.equalsToEdgeInsets(edgeInsets)) {
80 86
       Assertions.assertNotNull(mInsetsChangeListener).onInsetsChange(this, edgeInsets);
@@ -86,7 +92,7 @@ public class SafeAreaView extends ReactViewGroup implements ViewTreeObserver.OnG
86 92
   protected void onAttachedToWindow() {
87 93
     super.onAttachedToWindow();
88 94
 
89
-    getRootView().getViewTreeObserver().addOnGlobalLayoutListener(this);
95
+    getViewTreeObserver().addOnGlobalLayoutListener(this);
90 96
     maybeUpdateInsets();
91 97
   }
92 98
 
@@ -94,7 +100,7 @@ public class SafeAreaView extends ReactViewGroup implements ViewTreeObserver.OnG
94 100
   protected void onDetachedFromWindow() {
95 101
     super.onDetachedFromWindow();
96 102
 
97
-    getRootView().getViewTreeObserver().removeOnGlobalLayoutListener(this);
103
+    getViewTreeObserver().removeOnGlobalLayoutListener(this);
98 104
   }
99 105
 
100 106
   @Override