fix(core/ui): typeface

This commit is contained in:
rhunk 2024-12-08 17:17:13 +01:00
parent a035f0ec10
commit 047efccb3f
4 changed files with 17 additions and 24 deletions

View File

@ -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,

View File

@ -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
}
}
}

View File

@ -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()

View File

@ -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)
)
}