From d9ffac0c890c0d58cd62f00693a45322c030f96d Mon Sep 17 00:00:00 2001 From: Kainalu Hagiwara Date: Mon, 10 Aug 2020 10:43:30 -0700 Subject: [PATCH] For #13405 - Use toolbar location relative to the whole screen instead of window. --- .../fenix/browser/ToolbarGestureHandler.kt | 3 ++- .../main/java/org/mozilla/fenix/ext/View.kt | 14 ++++++++++++++ .../java/org/mozilla/fenix/ext/ViewTest.kt | 18 ++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/mozilla/fenix/browser/ToolbarGestureHandler.kt b/app/src/main/java/org/mozilla/fenix/browser/ToolbarGestureHandler.kt index ccd2b3ce4..cd507391b 100644 --- a/app/src/main/java/org/mozilla/fenix/browser/ToolbarGestureHandler.kt +++ b/app/src/main/java/org/mozilla/fenix/browser/ToolbarGestureHandler.kt @@ -23,6 +23,7 @@ import mozilla.components.browser.session.Session import mozilla.components.browser.session.SessionManager import mozilla.components.support.ktx.android.util.dpToPx import mozilla.components.support.ktx.android.view.getRectWithViewLocation +import org.mozilla.fenix.ext.getRectWithScreenLocation import org.mozilla.fenix.ext.getWindowInsets import org.mozilla.fenix.ext.isKeyboardVisible import org.mozilla.fenix.ext.sessionsOfType @@ -304,7 +305,7 @@ class ToolbarGestureHandler( } private fun PointF.isInToolbar(): Boolean { - val toolbarLocation = toolbarLayout.getRectWithViewLocation() + val toolbarLocation = toolbarLayout.getRectWithScreenLocation() // In Android 10, the system gesture touch area overlaps the bottom of the toolbar, so // lets make our swipe area taller by that amount activity.window.decorView.getWindowInsets()?.let { insets -> diff --git a/app/src/main/java/org/mozilla/fenix/ext/View.kt b/app/src/main/java/org/mozilla/fenix/ext/View.kt index 05c2e8de7..683d9b65d 100644 --- a/app/src/main/java/org/mozilla/fenix/ext/View.kt +++ b/app/src/main/java/org/mozilla/fenix/ext/View.kt @@ -31,6 +31,20 @@ fun View.removeTouchDelegate() { } } +/** + * Fills a [Rect] with data about a view's location in the screen. + * + * @see View.getLocationOnScreen + * @see View.getRectWithViewLocation for a version of this that is relative to a window +*/ +fun View.getRectWithScreenLocation(): Rect { + val locationOnScreen = IntArray(2).apply { getLocationOnScreen(this) } + return Rect(locationOnScreen[0], + locationOnScreen[1], + locationOnScreen[0] + width, + locationOnScreen[1] + height) +} + /** * A safer version of [ViewCompat.getRootWindowInsets] that does not throw a NullPointerException * if the view is not attached. diff --git a/app/src/test/java/org/mozilla/fenix/ext/ViewTest.kt b/app/src/test/java/org/mozilla/fenix/ext/ViewTest.kt index c9da170fb..003312d3d 100644 --- a/app/src/test/java/org/mozilla/fenix/ext/ViewTest.kt +++ b/app/src/test/java/org/mozilla/fenix/ext/ViewTest.kt @@ -148,4 +148,22 @@ class ViewTest { every { view.getKeyboardHeight() } returns 100 assertEquals(true, view.isKeyboardVisible()) } + + @Test + fun `getRectWithScreenLocation should transform getLocationInScreen method values`() { + val locationOnScreen = slot() + every { view.getLocationOnScreen(capture(locationOnScreen)) } answers { + locationOnScreen.captured[0] = 100 + locationOnScreen.captured[1] = 200 + } + every { view.width } returns 150 + every { view.height } returns 250 + + val outRect = view.getRectWithScreenLocation() + + assertEquals(100, outRect.left) + assertEquals(200, outRect.top) + assertEquals(250, outRect.right) + assertEquals(450, outRect.bottom) + } }