Skip to content

Commit

Permalink
Fix broken icon state colors
Browse files Browse the repository at this point in the history
Set drawable state as desired before rendering to bitmap.
That's for both default favicons and error page icon.
  • Loading branch information
Slion committed Oct 2, 2023
1 parent 229c1f3 commit 09fb94a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ import androidx.annotation.StringRes
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.content.ContextCompat
import androidx.core.graphics.ColorUtils
import androidx.core.graphics.drawable.toBitmap
import androidx.core.net.toUri
import androidx.core.view.children
import androidx.core.view.isVisible
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ fun Context.isDarkTheme(): Boolean {
*/
fun Context.createDefaultFavicon(): Bitmap {
Timber.v("createDefaultFavicon")
//val background = MaterialColors.getColor(context, com.google.android.material.R.attr.colorSurface, Color.BLACK)
return ResourcesCompat.getDrawable(resources, R.drawable.ic_web, theme)!!.toBitmap(256,256)
return getDrawable(R.drawable.ic_web, android.R.attr.state_enabled).toBitmap()
}

/**
* Load drawable [aId] from resources using theme and apply given [aStates].
*
* See drawable state: https://stackoverflow.com/questions/11943795
*/
fun Context.getDrawable(@DrawableRes aId: Int, vararg aStates: Int): Drawable {
Timber.v("getDrawable")
return ResourcesCompat.getDrawable(resources, aId, theme)!!.apply{ state = intArrayOf(*aStates) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import androidx.core.graphics.component2
import androidx.core.graphics.component3
import androidx.core.graphics.component4
import androidx.core.graphics.drawable.toBitmapOrNull
import com.google.android.material.color.MaterialColors
import java.lang.reflect.Method

/**
Expand Down Expand Up @@ -69,7 +68,7 @@ fun StateListDrawable.drawableForState(aState: Int) : Drawable {
* bitmap
* @see toBitmapOrNull
*/
public fun Drawable.toBitmap(
fun Drawable.toBitmap(
@Px width: Int = intrinsicWidth,
@Px height: Int = intrinsicHeight,
@ColorInt aBackground: Int = Color.TRANSPARENT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import acr.browser.lightning.constant.FILE
import acr.browser.lightning.controller.UIController
import acr.browser.lightning.di.HiltEntryPoint
import acr.browser.lightning.di.configPrefs
import acr.browser.lightning.extensions.getDrawable
import acr.browser.lightning.extensions.getText
import acr.browser.lightning.extensions.ihs
import acr.browser.lightning.extensions.makeSnackbar
Expand Down Expand Up @@ -377,8 +378,8 @@ class LightningWebClient(

//Encode image to base64 string
val output = ByteArrayOutputStream()
val bitmap = ResourcesCompat.getDrawable(activity.resources, R.drawable.ic_about, activity.theme)?.toBitmap()
bitmap?.compress(Bitmap.CompressFormat.PNG, 100, output)
val bitmap = activity.getDrawable(R.drawable.ic_about, android.R.attr.state_enabled).toBitmap()
bitmap.compress(Bitmap.CompressFormat.PNG, 100, output)
val imageBytes: ByteArray = output.toByteArray()
val imageString = "data:image/png;base64,"+Base64.encodeToString(imageBytes, Base64.NO_WRAP)

Expand Down
6 changes: 3 additions & 3 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ colorControlNormal:
<!-- <item name="appColorControlEnabled">@color/md_grey_650</item>-->
<!-- <item name="appColorControlSelected">?attr/colorOnPrimary</item>-->
<!-- <item name="appColorControlDisabled">@color/md_grey_400</item>-->
<item name="colorControlNormal">?attr/colorOnSurface</item>
<!-- <item name="colorControlNormal">?attr/colorOnSurface</item>-->
<!-- Fix dialog issues -->
<!-- <item name="materialAlertDialogTheme">@style/ThemeOverlay.App.MaterialAlertDialog</item>-->
<!-- <item name="alertDialogTheme">@style/ThemeOverlay.App.MaterialAlertDialog</item>-->
Expand Down Expand Up @@ -381,7 +381,7 @@ colorControlNormal:
<!-- <item name="appColorControlEnabled">@color/md_grey_350</item>-->
<!-- <item name="appColorControlSelected">?attr/colorOnPrimary</item>-->
<!-- <item name="appColorControlDisabled">@color/md_grey_600</item>-->
<item name="colorControlNormal">?attr/colorOnBackground</item>
<!-- <item name="colorControlNormal">?attr/colorOnBackground</item>-->
<!-- Fix dialog issues -->
<!-- <item name="materialAlertDialogTheme">@style/ThemeOverlay.App.MaterialAlertDialog</item>-->
<!-- <item name="alertDialogTheme">@style/ThemeOverlay.App.MaterialAlertDialog</item>-->
Expand Down Expand Up @@ -549,7 +549,7 @@ colorControlNormal:
<!-- <item name="appColorControlEnabled">@color/md_grey_350</item>-->
<!-- <item name="appColorControlSelected">?attr/colorOnPrimary</item>-->
<!-- <item name="appColorControlDisabled">@color/md_grey_600</item>-->
<item name="colorControlNormal">?attr/colorOnBackground</item>
<!-- <item name="colorControlNormal">?attr/colorOnBackground</item>-->
<!-- Fix dialog issues -->
<!-- <item name="materialAlertDialogTheme">@style/ThemeOverlay.App.MaterialAlertDialog</item>-->
<!-- <item name="alertDialogTheme">@style/ThemeOverlay.App.MaterialAlertDialog</item>-->
Expand Down

0 comments on commit 09fb94a

Please sign in to comment.