Skip to content

Commit 52ee9b0

Browse files
authored
compute Input Screen tab indicator height to always be vertically centered in the tab switcher (#6435)
Task/Issue URL: https://app.asana.com/1/137249556945/project/1208671518894266/task/1210767017822401 ### Description Previously, the dimensions picked for the tab switcher and its tab indicators often resulted in the switcher having a height of an odd number of pixels while the indicator had height of even number of pixels. This resulted in the pill not being perfectly vertically aligned within the switcher. It depends on the pixel density of the device, but happened often on the devices we tested on. This PR adds a bit of logic to subtract a pixel when needed, to allow for that perfect vertical alignment. ### Steps to test this PR - [ ] Try a commit before this change and verify that the space below the tab indicator is bigger than the space above it. - [ ] Try this change and verify that the space is now equal.
1 parent 09f4a3f commit 52ee9b0

File tree

1 file changed

+18
-1
lines changed
  • duckchat/duckchat-impl/src/main/java/com/duckduckgo/duckchat/impl/inputscreen/ui/view

1 file changed

+18
-1
lines changed

duckchat/duckchat-impl/src/main/java/com/duckduckgo/duckchat/impl/inputscreen/ui/view/InputModeTabLayout.kt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class InputModeTabLayout @JvmOverloads constructor(
5050
buildShadowedTabIndicator(
5151
context = context,
5252
heightPx = resources.getDimensionPixelSize(R.dimen.inputModeTabIndicatorHeight),
53+
containerHeightPx = resources.getDimensionPixelSize(R.dimen.inputModeSwitchHeight),
5354
horizontalInsetPx = resources.getDimensionPixelSize(R.dimen.inputModeTabIndicatorHorizontalInset),
5455
elevationPx = resources.getDimensionPixelSize(R.dimen.inputModeTabIndicatorElevation),
5556
),
@@ -75,6 +76,7 @@ class InputModeTabLayout @JvmOverloads constructor(
7576
private fun buildShadowedTabIndicator(
7677
context: Context,
7778
heightPx: Int,
79+
containerHeightPx: Int,
7880
horizontalInsetPx: Int,
7981
elevationPx: Int,
8082
): InsetDrawable {
@@ -83,8 +85,23 @@ class InputModeTabLayout @JvmOverloads constructor(
8385
.setAllCorners(CornerFamily.ROUNDED, cornerRadius)
8486
.build()
8587

88+
// Computing a pill height that will be perfectly centered in the container vertically, with equal top and bottom insets,
89+
// and matching horizontal insets, if possible.
90+
val shadowPillHeightCandidates = listOf(heightPx, heightPx - 1, heightPx + 1).filter { candidate ->
91+
// only pick values that will result in even vertical insets
92+
(containerHeightPx - candidate) % 2 == 0
93+
}
94+
val shadowPillHeight = shadowPillHeightCandidates
95+
.firstOrNull { candidate ->
96+
// prefer values that will result in equal horizontal and vertical insets
97+
val verticalInsetsPx = (containerHeightPx - candidate) / 2
98+
verticalInsetsPx == horizontalInsetPx
99+
}
100+
?: shadowPillHeightCandidates.firstOrNull()
101+
?: heightPx
102+
86103
val shadowedPill = object : MaterialShapeDrawable(pill) {
87-
override fun getIntrinsicHeight(): Int = heightPx
104+
override fun getIntrinsicHeight(): Int = shadowPillHeight
88105
}.apply {
89106
shadowCompatibilityMode = MaterialShapeDrawable.SHADOW_COMPAT_MODE_ALWAYS
90107
initializeElevationOverlay(context)

0 commit comments

Comments
 (0)