Skip to content

Commit

Permalink
Revert "fix(core/ui): typeface"
Browse files Browse the repository at this point in the history
This reverts commit 047efcc.
  • Loading branch information
bocajthomas committed Dec 17, 2024
1 parent 93ce901 commit d2a1da8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ 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
Expand Down Expand Up @@ -128,7 +132,7 @@ class FriendFeedMessagePreview : Feature("FriendFeedMessagePreview") {
val offsetY = canvas.height.toFloat() - previewContainerHeight
paint.textSize = secondaryTextSize
paint.color = context.userInterface.colorPrimary
paint.typeface = context.userInterface.avenirNextTypeface
paint.typeface = typeface

messageCache[conversationId]?.forEachIndexed { index, messageString ->
canvas.drawText(messageString,
Expand Down
30 changes: 16 additions & 14 deletions core/src/main/kotlin/me/rhunk/snapenhance/core/ui/UserInterface.kt
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
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, Typeface>()
private val fontMap = mutableMapOf<Int, Int>()

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 avenirNextFontId = 500
val avenirNextTypeface get() = fontMap[avenirNextFontId] ?: fontMap.entries.sortedBy { it.key }.firstOrNull()?.value ?: Typeface.DEFAULT
val avenirNextTypeface: Typeface by lazy {
fontMap[600]?.let { context.resources.getFont(it) } ?: Typeface.MONOSPACE
}

fun dpToPx(dp: Int): Int {
return (dp * context.resources.displayMetrics.density).toInt()
Expand All @@ -30,6 +31,10 @@ 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)
Expand All @@ -45,16 +50,13 @@ class UserInterface(
}

fun init() {
ResourcesCompat::class.java.hook("getFont", HookStage.BEFORE) { param ->
val id = param.arg<Int>(1)
if (fontMap.containsKey(id)) {
param.setResult(fontMap[id])
}
}
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

Typeface::class.java.hookConstructor(HookStage.AFTER) { param ->
val typeface = param.thisObject<Typeface>()
fontMap[typeface.weight] = typeface
val typeface = context.resources.getFont(typedValue.resourceId)
fontMap.getOrPut(typeface.weight) { typedValue.resourceId }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ class FriendFeedInfoMenu : AbstractMenu() {
createComposeView(actionSheetItemsContainer.context) {
CompositionLocalProvider(
LocalTextStyle provides LocalTextStyle.current.merge(TextStyle(fontFamily = FontFamily(
Font(context.userInterface.avenirNextFontId, FontWeight.Medium)
Font(context.userInterface.getFontResource(600) ?: throw IllegalStateException("Avenir Next font not found"), FontWeight.Medium)
)))
) {
ComposeFriendFeedMenu()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ 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
Expand Down Expand Up @@ -299,7 +300,7 @@ class NewChatActionMenu : AbstractMenu() {
val primaryColor = remember { if (event.view.context.isDarkTheme()) Color.White else Color.Black }
val avenirNextMediumFont = remember {
FontFamily(
Font(context.userInterface.avenirNextFontId, FontWeight.Medium)
Font(context.userInterface.getFontResource(600) ?: throw IllegalStateException("Font not found"), FontWeight.Medium)
)
}

Expand Down

0 comments on commit d2a1da8

Please sign in to comment.