commit 047efccb3f3ce58b416ebf5efaf3506692537f28 parent a035f0ec1096d589d1fa3df9c3395137f3471126 Author: rhunk <101876869+rhunk@users.noreply.github.com> Date: Sun, 8 Dec 2024 17:17:13 +0100 fix(core/ui): typeface Diffstat:
4 files changed, 17 insertions(+), 24 deletions(-)
diff --git a/core/src/main/kotlin/me/rhunk/snapenhance/core/features/impl/ui/FriendFeedMessagePreview.kt b/core/src/main/kotlin/me/rhunk/snapenhance/core/features/impl/ui/FriendFeedMessagePreview.kt @@ -81,10 +81,6 @@ class FriendFeedMessagePreview : Feature("FriendFeedMessagePreview") { textSize = secondaryTextSize } - val typeface by lazy { - context.userInterface.avenirNextTypeface - } - context.event.subscribe(BuildMessageEvent::class) { param -> val conversationId = param.message.messageDescriptor?.conversationId?.toString() ?: return@subscribe val cachedView = cachedLayouts[conversationId] ?: return@subscribe @@ -132,7 +128,7 @@ class FriendFeedMessagePreview : Feature("FriendFeedMessagePreview") { val offsetY = canvas.height.toFloat() - previewContainerHeight paint.textSize = secondaryTextSize paint.color = context.userInterface.colorPrimary - paint.typeface = typeface + paint.typeface = context.userInterface.avenirNextTypeface messageCache[conversationId]?.forEachIndexed { index, messageString -> canvas.drawText(messageString, diff --git a/core/src/main/kotlin/me/rhunk/snapenhance/core/ui/UserInterface.kt b/core/src/main/kotlin/me/rhunk/snapenhance/core/ui/UserInterface.kt @@ -1,26 +1,25 @@ package me.rhunk.snapenhance.core.ui -import android.content.res.Resources import android.graphics.Typeface -import android.util.TypedValue import android.view.Gravity import android.widget.TextView +import androidx.core.content.res.ResourcesCompat import me.rhunk.snapenhance.core.ModContext import me.rhunk.snapenhance.core.util.hook.HookStage import me.rhunk.snapenhance.core.util.hook.hook +import me.rhunk.snapenhance.core.util.hook.hookConstructor import me.rhunk.snapenhance.core.util.ktx.isDarkTheme class UserInterface( private val context: ModContext ) { - private val fontMap = mutableMapOf<Int, Int>() + private val fontMap = mutableMapOf<Int, Typeface>() val colorPrimary get() = if (context.androidContext.isDarkTheme()) 0xfff5f5f5.toInt() else 0xff212121.toInt() val actionSheetBackground get() = if (context.androidContext.isDarkTheme()) 0xff1e1e1e.toInt() else 0xffffffff.toInt() - val avenirNextTypeface: Typeface by lazy { - fontMap[600]?.let { context.resources.getFont(it) } ?: Typeface.MONOSPACE - } + val avenirNextFontId = 500 + val avenirNextTypeface get() = fontMap[avenirNextFontId] ?: fontMap.entries.sortedBy { it.key }.firstOrNull()?.value ?: Typeface.DEFAULT fun dpToPx(dp: Int): Int { return (dp * context.resources.displayMetrics.density).toInt() @@ -31,10 +30,6 @@ class UserInterface( return (px / context.resources.displayMetrics.density).toInt() } - fun getFontResource(weight: Int): Int? { - return fontMap[weight] - } - fun applyActionButtonTheme(view: TextView) { view.apply { setTextColor(colorPrimary) @@ -50,13 +45,16 @@ class UserInterface( } fun init() { - Resources::class.java.hook("getValue", HookStage.AFTER) { param -> - val typedValue = param.arg<TypedValue>(1) - val path = typedValue.string ?: return@hook - if (!path.startsWith("res/") || !path.endsWith(".ttf")) return@hook + ResourcesCompat::class.java.hook("getFont", HookStage.BEFORE) { param -> + val id = param.arg<Int>(1) + if (fontMap.containsKey(id)) { + param.setResult(fontMap[id]) + } + } - val typeface = context.resources.getFont(typedValue.resourceId) - fontMap.getOrPut(typeface.weight) { typedValue.resourceId } + Typeface::class.java.hookConstructor(HookStage.AFTER) { param -> + val typeface = param.thisObject<Typeface>() + fontMap[typeface.weight] = typeface } } } \ No newline at end of file diff --git a/core/src/main/kotlin/me/rhunk/snapenhance/core/ui/menu/impl/FriendFeedInfoMenu.kt b/core/src/main/kotlin/me/rhunk/snapenhance/core/ui/menu/impl/FriendFeedInfoMenu.kt @@ -547,7 +547,7 @@ class FriendFeedInfoMenu : AbstractMenu() { createComposeView(actionSheetItemsContainer.context) { CompositionLocalProvider( LocalTextStyle provides LocalTextStyle.current.merge(TextStyle(fontFamily = FontFamily( - Font(context.userInterface.getFontResource(600) ?: throw IllegalStateException("Avenir Next font not found"), FontWeight.Medium) + Font(context.userInterface.avenirNextFontId, FontWeight.Medium) ))) ) { ComposeFriendFeedMenu() diff --git a/core/src/main/kotlin/me/rhunk/snapenhance/core/ui/menu/impl/NewChatActionMenu.kt b/core/src/main/kotlin/me/rhunk/snapenhance/core/ui/menu/impl/NewChatActionMenu.kt @@ -51,7 +51,6 @@ import me.rhunk.snapenhance.core.ui.debugEditText import me.rhunk.snapenhance.core.ui.iterateParent import me.rhunk.snapenhance.core.ui.menu.AbstractMenu import me.rhunk.snapenhance.core.ui.triggerCloseTouchEvent -import me.rhunk.snapenhance.core.util.ktx.getIdentifier import me.rhunk.snapenhance.core.util.ktx.isDarkTheme import me.rhunk.snapenhance.core.util.ktx.setObjectField import me.rhunk.snapenhance.core.util.ktx.vibrateLongPress @@ -300,7 +299,7 @@ class NewChatActionMenu : AbstractMenu() { val primaryColor = remember { if (event.view.context.isDarkTheme()) Color.White else Color.Black } val avenirNextMediumFont = remember { FontFamily( - Font(context.userInterface.getFontResource(600) ?: throw IllegalStateException("Font not found"), FontWeight.Medium) + Font(context.userInterface.avenirNextFontId, FontWeight.Medium) ) }