Skip to content

Commit

Permalink
feat: scale only foreground or the whole icon
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardLuo0 committed Feb 20, 2025
1 parent 84889d1 commit c775af4
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/src/main/kotlin/com/richardluo/globalIconPack/Pref.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ object Pref {
val SHORTCUT = Pair("shortcut", true)

val ICON_FALLBACK = Pair("iconFallback", true)
val SCALE_ONLY_FOREGROUND = Pair("scaleOnlyForeground", true)
val OVERRIDE_ICON_FALLBACK = Pair("overrideIconFallback", false)
val ICON_PACK_SCALE = Pair("iconPackScale", 1f)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ abstract class IconPack(pref: SharedPreferences, val pack: String, val resources
}

protected val iconPackAsFallback = pref.get(Pref.ICON_PACK_AS_FALLBACK)
protected val scaleOnlyForeground = pref.get(Pref.SCALE_ONLY_FOREGROUND)
protected var iconFallback: IconFallback? = null

protected fun initFallbackSettings(fs: FallbackSettings, pref: SharedPreferences) {
Expand Down Expand Up @@ -64,6 +65,7 @@ abstract class IconPack(pref: SharedPreferences, val pack: String, val resources
iconUpons.randomOrNull(),
iconMasks.randomOrNull(),
iconScale,
scaleOnlyForeground,
staticIcon,
)
} ?: baseIcon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ class MainActivity : ComponentActivity() {
title = { Text(stringResource(R.string.iconFallback)) },
summary = { Text(stringResource(R.string.iconFallbackSummary)) },
)
switchPreference(
key = Pref.SCALE_ONLY_FOREGROUND.first,
defaultValue = Pref.SCALE_ONLY_FOREGROUND.second,
title = { Text(stringResource(R.string.scaleOnlyForeground)) },
)
item {
val enableState =
rememberPreferenceState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ class MainVM(app: Application) : AndroidViewModel(app) {
val iconFallback: Boolean,
val overrideIconFallback: Boolean,
val iconPackScale: Float,
val scaleOnlyForeground: Boolean,
)
flow
.map {
CachePref(
it.get(Pref.ICON_FALLBACK),
it.get(Pref.OVERRIDE_ICON_FALLBACK),
it.get(Pref.ICON_PACK_SCALE),
it.get(Pref.SCALE_ONLY_FOREGROUND),
)
}
.distinctUntilChanged()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ object IconHelper {
upon: Bitmap?,
mask: Bitmap?,
iconScale: Float = 1f,
scaleOnlyForeground: Boolean = true,
): Drawable =
(if (iconScale != 1f && drawable is AdaptiveIconDrawable)
(if (scaleOnlyForeground && iconScale != 1f && drawable is AdaptiveIconDrawable)
UnClipAdaptiveIconDrawable(
drawable.background,
createScaledDrawable(drawable.foreground, iconScale),
Expand All @@ -143,10 +144,13 @@ object IconHelper {
upon: Bitmap?,
mask: Bitmap?,
iconScale: Float = 1f,
scaleOnlyForeground: Boolean = true,
): Drawable =
if (drawable is AdaptiveIconDrawable)
CustomAdaptiveIconDrawable(
drawable.background,
drawable.background?.let {
if (scaleOnlyForeground) it else createScaledDrawable(it, iconScale)
},
drawable.foreground?.let { createScaledDrawable(it, iconScale) },
back,
upon,
Expand All @@ -160,7 +164,7 @@ object IconHelper {
upon,
mask,
)
else processIconToStatic(res, drawable, back, upon, null, iconScale)
else processIconToStatic(res, drawable, back, upon, null, iconScale, scaleOnlyForeground)

fun processIcon(
res: Resources,
Expand All @@ -169,10 +173,11 @@ object IconHelper {
upon: Bitmap?,
mask: Bitmap?,
iconScale: Float = 1f,
scaleOnlyForeground: Boolean = true,
static: Boolean = false,
) =
if (static) processIconToStatic(res, drawable, back, upon, mask, iconScale)
else processIconToAdaptive(res, drawable, back, upon, mask, iconScale)
if (static) processIconToStatic(res, drawable, back, upon, mask, iconScale, scaleOnlyForeground)
else processIconToAdaptive(res, drawable, back, upon, mask, iconScale, scaleOnlyForeground)

fun makeAdaptive(drawable: Drawable, static: Boolean = false) =
if (!static && drawable !is AdaptiveIconDrawable)
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<string name="iconVariantSummary">Choose different variants from icon pack.</string>
<string name="iconFallback">Generate fallback icons</string>
<string name="iconFallbackSummary">Apply background, mask, and scale from the icon pack to apps without dedicated icons.</string>
<string name="scaleOnlyForeground">Scale only the foreground for adaptive icons</string>
<string name="overrideIconFallback">Override fallback settings</string>
<string name="overrideIconFallbackSummary">Use new values for the fallback settings.</string>
<string name="iconPackScale">Scale</string>
Expand Down

0 comments on commit c775af4

Please sign in to comment.