Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

Commit

Permalink
Bug 1812843 - Maintain aspect ratio when resizing a bitmap for the 'a…
Browse files Browse the repository at this point in the history
…dd' button in CustomTabsToolbarFeature
  • Loading branch information
t-p-white committed Feb 2, 2024
1 parent cb7e919 commit e864696
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:importantForAccessibility="no"
app:layout_constraintEnd_toStartOf="@+id/mozac_browser_toolbar_browser_actions"
app:layout_constraintStart_toEndOf="@+id/mozac_browser_toolbar_navigation_actions"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toTopOf="parent"
app:layout_goneMarginEnd="0dp" />

<!-- URL indicators (lock, tracking protection, ..) -->

Expand Down Expand Up @@ -116,6 +118,7 @@
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginTop="4dp"
android:scaleType="center"
app:layout_constraintEnd_toEndOf="@+id/mozac_browser_toolbar_background"
app:layout_constraintTop_toTopOf="parent"
mozac:actionContainerItemSize="48dp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package mozilla.components.feature.customtabs

import android.app.PendingIntent
import android.graphics.Bitmap
import android.util.Size
import android.view.Window
import androidx.annotation.ColorInt
import androidx.annotation.VisibleForTesting
Expand Down Expand Up @@ -200,13 +201,15 @@ class CustomTabsToolbarFeature(
buttonConfig: CustomTabActionButtonConfig?,
) {
buttonConfig?.let { config ->
val icon = config.icon
val scaledIconSize = icon.resizeMaintainingAspectRatio()
val drawableIcon = Bitmap.createScaledBitmap(
config.icon,
ACTION_BUTTON_DRAWABLE_WIDTH_DP.dpToPx(context.resources.displayMetrics),
ACTION_BUTTON_DRAWABLE_HEIGHT_DP.dpToPx(context.resources.displayMetrics),
icon,
scaledIconSize.width.dpToPx(context.resources.displayMetrics),
scaledIconSize.height.dpToPx(context.resources.displayMetrics),
true,
)
.toDrawable(context.resources)
).toDrawable(context.resources)

if (config.tint || forceActionButtonTinting) {
drawableIcon.setTint(readableColor)
}
Expand Down Expand Up @@ -301,8 +304,25 @@ class CustomTabsToolbarFeature(
}
}

private fun Bitmap.resizeMaintainingAspectRatio(): Size {
val iconWidth = width.toFloat()
val iconHeight = height.toFloat()

return if (iconWidth > iconHeight) {
// Scale a wide bitmap
val aspectRatio = iconHeight / iconWidth
val scaledHeight = (ACTION_BUTTON_DRAWABLE_WIDTH_DP * aspectRatio).toInt()
Size(ACTION_BUTTON_DRAWABLE_WIDTH_DP, scaledHeight)
} else {
// Scale square or tall bitmap
val aspectRatio = iconWidth / iconHeight
val scaledWidth = (ACTION_BUTTON_DRAWABLE_HEIGHT_DP * aspectRatio).toInt()
Size(scaledWidth, ACTION_BUTTON_DRAWABLE_HEIGHT_DP)
}
}

companion object {
private const val ACTION_BUTTON_DRAWABLE_WIDTH_DP = 24
private const val ACTION_BUTTON_DRAWABLE_WIDTH_DP = 48
private const val ACTION_BUTTON_DRAWABLE_HEIGHT_DP = 24
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ class CustomTabsToolbarFeatureTest {
}

@Test
fun `action button is scaled to 24 width and 24 height`() {
fun `square action button drawable is scaled to 24 width and 24 height`() {
val captor = argumentCaptor<Toolbar.ActionButton>()
val size = 48
val pendingIntent: PendingIntent = mock()
Expand Down Expand Up @@ -441,6 +441,84 @@ class CustomTabsToolbarFeatureTest {
assertEquals(24, button.drawable.intrinsicWidth)
}

@Test
fun `wide action button drawable is scaled to 48 width and 24 height`() {
val captor = argumentCaptor<Toolbar.ActionButton>()
val width = 96
val height = 48
val pendingIntent: PendingIntent = mock()
val tab = createCustomTab(
"https://www.mozilla.org",
id = "mozilla",
config = CustomTabConfig(
actionButtonConfig = CustomTabActionButtonConfig(
description = "Button",
icon = Bitmap.createBitmap(IntArray(width * height), width, height, Bitmap.Config.ARGB_8888),
pendingIntent = pendingIntent,
),
),
)
val store = BrowserStore(
BrowserState(
customTabs = listOf(tab),
),
)
val toolbar = spy(BrowserToolbar(testContext))
val useCases = CustomTabsUseCases(
store = store,
loadUrlUseCase = SessionUseCases(store).loadUrl,
)
val feature = spy(CustomTabsToolbarFeature(store, toolbar, sessionId = "mozilla", useCases = useCases) {})

feature.start()

verify(feature).addActionButton(anyInt(), any())
verify(toolbar).addBrowserAction(captor.capture())

val button = captor.value.createView(FrameLayout(testContext))
assertEquals(24, (button as ImageButton).drawable.intrinsicHeight)
assertEquals(48, button.drawable.intrinsicWidth)
}

@Test
fun `tall action button drawable is scaled to 12 width and 24 height`() {
val captor = argumentCaptor<Toolbar.ActionButton>()
val width = 24
val height = 48
val pendingIntent: PendingIntent = mock()
val tab = createCustomTab(
"https://www.mozilla.org",
id = "mozilla",
config = CustomTabConfig(
actionButtonConfig = CustomTabActionButtonConfig(
description = "Button",
icon = Bitmap.createBitmap(IntArray(width * height), width, height, Bitmap.Config.ARGB_8888),
pendingIntent = pendingIntent,
),
),
)
val store = BrowserStore(
BrowserState(
customTabs = listOf(tab),
),
)
val toolbar = spy(BrowserToolbar(testContext))
val useCases = CustomTabsUseCases(
store = store,
loadUrlUseCase = SessionUseCases(store).loadUrl,
)
val feature = spy(CustomTabsToolbarFeature(store, toolbar, sessionId = "mozilla", useCases = useCases) {})

feature.start()

verify(feature).addActionButton(anyInt(), any())
verify(toolbar).addBrowserAction(captor.capture())

val button = captor.value.createView(FrameLayout(testContext))
assertEquals(24, (button as ImageButton).drawable.intrinsicHeight)
assertEquals(12, button.drawable.intrinsicWidth)
}

@Test
fun `action button uses updated url`() {
val size = 48
Expand Down

0 comments on commit e864696

Please sign in to comment.