This repository has been archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1879370 - Add a ClippingBehavior supporting multiple toolbars
- Loading branch information
mike a
committed
Mar 12, 2024
1 parent
0bbdec6
commit 5494464
Showing
14 changed files
with
491 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...p/src/main/java/org/mozilla/fenix/components/toolbar/navbar/EngineViewClippingBehavior.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package org.mozilla.fenix.components.toolbar.navbar | ||
|
||
import android.content.Context | ||
import android.util.AttributeSet | ||
import android.view.View | ||
import androidx.annotation.VisibleForTesting | ||
import androidx.coordinatorlayout.widget.CoordinatorLayout | ||
import mozilla.components.browser.toolbar.BrowserToolbar | ||
import mozilla.components.concept.engine.EngineView | ||
import mozilla.components.concept.toolbar.ScrollableToolbar | ||
import mozilla.components.support.ktx.android.view.findViewInHierarchy | ||
|
||
/** | ||
* A [CoordinatorLayout.Behavior] implementation that allows the [EngineView] to automatically | ||
* size its content and itself in relation to the Y translation of one or multiple [ScrollableToolbar]s. | ||
* | ||
* This is useful in combination with [ScrollableToolbar] ensuring the web content is displayed | ||
* immediately below / above the toolbar even when it is being animated. | ||
* | ||
* @param context [Context] used for various Android interactions. | ||
* @param attrs XML set attributes configuring this. | ||
* @param engineViewParent the parent [View] of the [EngineView]. | ||
* @param topToolbarHeight size of [ScrollableToolbar] when it is placed above the [EngineView]. | ||
* @param hasTopToolbar whether [EngineView] has a toolbar at the top. | ||
*/ | ||
class EngineViewClippingBehavior( | ||
context: Context?, | ||
attrs: AttributeSet?, | ||
private val engineViewParent: View, | ||
private val topToolbarHeight: Int, | ||
private val hasTopToolbar: Boolean, | ||
) : CoordinatorLayout.Behavior<View>(context, attrs) { | ||
|
||
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) | ||
internal var engineView = engineViewParent.findViewInHierarchy { it is EngineView } as EngineView? | ||
|
||
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) | ||
internal var recentBottomToolbarTranslation = 0f | ||
|
||
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) | ||
internal var recentTopToolbarTranslation = 0f | ||
|
||
override fun layoutDependsOn(parent: CoordinatorLayout, child: View, dependency: View): Boolean { | ||
if (dependency is ScrollableToolbar) { | ||
return true | ||
} | ||
|
||
return super.layoutDependsOn(parent, child, dependency) | ||
} | ||
|
||
override fun onDependentViewChanged(parent: CoordinatorLayout, child: View, dependency: View): Boolean { | ||
// I can't come up with a scenario where the translationY | ||
// would return a NaN, but given that it was added to the | ||
// previous version of the behaviour and there was a test | ||
// written for it, I played it safe. | ||
if (dependency.translationY.isNaN()) { | ||
return true | ||
} | ||
|
||
when (dependency) { | ||
is BrowserToolbar -> { | ||
if (hasTopToolbar) { | ||
recentTopToolbarTranslation = dependency.translationY | ||
} else { | ||
recentBottomToolbarTranslation = dependency.translationY | ||
} | ||
} | ||
is ToolbarContainerView -> recentBottomToolbarTranslation = dependency.translationY | ||
} | ||
|
||
engineView?.let { | ||
if (hasTopToolbar) { | ||
// Here we are adjusting the vertical position of | ||
// the engine view container to be directly under | ||
// the toolbar. The top toolbar is shifting up, so | ||
// its translation will be either negative or zero. | ||
// It might be safe to use the child view here, but the original | ||
// implementation was adjusting the size of the parent passed | ||
// to the class directly, and I feel cautious to change that | ||
// considering possible side effects. | ||
engineViewParent.translationY = recentTopToolbarTranslation + topToolbarHeight | ||
} | ||
|
||
// We want to position the engine view popup content | ||
// right above the bottom toolbar when the toolbar | ||
// is being shifted down. The top of the bottom toolbar | ||
// is either positive or zero, but for clipping | ||
// the values should be negative because the baseline | ||
// for clipping is bottom toolbar height. | ||
val contentBottomClipping = recentTopToolbarTranslation - recentBottomToolbarTranslation | ||
it.setVerticalClipping(contentBottomClipping.toInt()) | ||
} | ||
return true | ||
} | ||
} |
Oops, something went wrong.