Skip to content

Fix: Hide omnibar shadow in focus mode #6353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import androidx.activity.OnBackPressedCallback
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.VisibleForTesting
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.core.view.postDelayed
import androidx.lifecycle.Lifecycle.State.STARTED
Expand Down Expand Up @@ -1303,12 +1302,7 @@ open class BrowserActivity : DuckDuckGoActivity() {
}

if (Build.VERSION.SDK_INT >= 28) {
singleToolBarMockupBinding.mockOmniBarContainerShadow.addBottomShadow(
shadowSizeDp = 12f,
offsetYDp = 3f,
insetDp = 3f,
shadowColor = ContextCompat.getColor(this, com.duckduckgo.mobile.android.R.color.background_omnibar_shadow),
)
singleToolBarMockupBinding.mockOmniBarContainerShadow.addBottomShadow()
}

binding.bottomMockupToolbar.appBarLayoutMockup.gone()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import android.os.Build
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.view.ViewOutlineProvider
import android.view.ViewTreeObserver.OnGlobalLayoutListener
import android.view.animation.DecelerateInterpolator
import android.widget.ImageView
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.core.view.updatePadding
import com.duckduckgo.anvil.annotations.InjectWith
Expand Down Expand Up @@ -71,6 +71,8 @@ class SingleOmnibarLayout @JvmOverloads constructor(
FindInPageImpl(IncludeFadeOmnibarFindInPageBinding.bind(findViewById(R.id.findInPage)))
}
private var isFindInPageVisible = false
private var outlineProvider: ViewOutlineProvider? = null

private val findInPageLayoutVisibilityChangeListener = OnGlobalLayoutListener {
val isVisible = findInPage.findInPageContainer.isVisible
if (isFindInPageVisible != isVisible) {
Expand Down Expand Up @@ -101,32 +103,13 @@ class SingleOmnibarLayout @JvmOverloads constructor(

AndroidSupportInjection.inject(this)

if (Build.VERSION.SDK_INT >= 28) {
omnibarCardShadow.addBottomShadow(
shadowSizeDp = 12f,
offsetYDp = 3f,
insetDp = 3f,
shadowColor = ContextCompat.getColor(context, CommonR.color.background_omnibar_shadow),
)
}
showShadow()

when (omnibarPosition) {
OmnibarPosition.TOP -> {
if (Build.VERSION.SDK_INT < 28) {
omnibarCardShadow.cardElevation = 2f.toPx(context)
}
}
OmnibarPosition.BOTTOM -> {
// When omnibar is at the bottom, we're adding an additional space at the top
toolbarContainer.updatePadding(
top = toolbarContainerPaddingTopWhenAtBottom,
)

// Try to reduce the bottom omnibar material shadow when not using the custom shadow
if (Build.VERSION.SDK_INT < 28) {
omnibarCardShadow.cardElevation = 0.5f.toPx(context)
}
}
if (omnibarPosition == OmnibarPosition.BOTTOM) {
// When omnibar is at the bottom, we're adding an additional space at the top
toolbarContainer.updatePadding(
top = toolbarContainerPaddingTopWhenAtBottom,
)
}
}

Expand All @@ -149,6 +132,8 @@ class SingleOmnibarLayout @JvmOverloads constructor(
} else {
animateOmnibarFocusedState(focused = false)
}

omnibarCardShadow.isVisible = viewState.viewMode !is ViewMode.CustomTab
}

override fun renderButtons(viewState: ViewState) {
Expand Down Expand Up @@ -199,6 +184,41 @@ class SingleOmnibarLayout @JvmOverloads constructor(

animator.start()
focusAnimator = animator

if (focused) {
disableShadow()
} else {
enableShadow()
}
}

private fun disableShadow() {
if (omnibarCardShadow.outlineProvider != null) {
omnibarCardShadow.outlineProvider = null
}
}

private fun enableShadow() {
if (omnibarCardShadow.outlineProvider == null) {
omnibarCardShadow.outlineProvider = outlineProvider
}
}

private fun showShadow() {
if (Build.VERSION.SDK_INT >= 28) {
omnibarCardShadow.addBottomShadow()
} else {
when (omnibarPosition) {
OmnibarPosition.TOP -> {
omnibarCardShadow.cardElevation = 2f.toPx(context)
}
OmnibarPosition.BOTTOM -> {
// Try to reduce the bottom omnibar material shadow when not using the custom shadow
omnibarCardShadow.cardElevation = 0.5f.toPx(context)
}
}
}
outlineProvider = omnibarCardShadow.outlineProvider
}

private fun onFindInPageShown() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,16 @@ import com.google.android.material.card.MaterialCardView
* @param shadowColor Optional shadow color (Android P and above only)
*/
@RequiresApi(28)
fun View.addBottomShadow(
shadowSizeDp: Float,
offsetYDp: Float = 2f,
insetDp: Float = 0f,
@ColorInt shadowColor: Int,
fun MaterialCardView.addBottomShadow(
shadowSizeDp: Float = 12f,
offsetYDp: Float = 3f,
insetDp: Float = 3f,
@ColorInt shadowColor: Int = ContextCompat.getColor(this.context, com.duckduckgo.mobile.android.R.color.background_omnibar_shadow),
) {
val shadowSize = shadowSizeDp.toPx(context)
val offsetY = offsetYDp.toPx(context)
val inset = insetDp.toPx(context).toInt()

// Get corner radius if view is a card
val cornerRadius = when (this) {
is MaterialCardView -> this.radius
else -> 0f
}

outlineProvider = object : ViewOutlineProvider() {
override fun getOutline(view: View, outline: Outline) {
// Create outline with rounded corners that match the view
Expand All @@ -58,7 +52,7 @@ fun View.addBottomShadow(
0,
view.width + inset,
view.height,
cornerRadius,
radius,
)
// Make the shadow appear only below the view
outline.offset(0, offsetY.toInt())
Expand Down
Loading