Files
PostPad/src/main/java/com/shitoucuo/postpad/Ui.kt

133 lines
5.7 KiB
Kotlin
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.shitoucuo.postpad
import android.content.Context
import android.content.res.Configuration
import android.graphics.Color
import android.graphics.Paint
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.InsetDrawable
import android.text.TextPaint
import android.util.TypedValue
/** 通用 UI 小工具 */
object Ui {
/**
* 弹窗配色板:背景、文字、分隔线、高亮色一次性取齐,且**全部来自同一个 Resources 实例**。
*
* 这是「深色切浅色后下拉菜单黑底黑字」的根因修复。旧实现
* `popupBackground(context, night)` 会按 night 参数 `createConfigurationContext()`
* 另造一份资源去取 surface,而菜单项文字仍旧由页面按当前资源染色(`R.color.text`)。
* 一旦这两份资源对深浅色的判断不一致——切到浅色时恰好发生了——就成了
* 背景取深色 surface、文字取浅色 text,即黑底黑字。
*
* 现在统一走 `context.resources`,即弹出弹窗的那个页面自己的资源:
* 页面是什么色,弹窗就是什么色,背景与文字必然同一套,不可能再同色。
*/
class PopupPalette internal constructor(
val surface: Int,
val stroke: Int,
val text: Int,
val textSecondary: Int,
val divider: Int,
val brand: Int,
val night: Boolean,
private val density: Float,
) {
/** 圆角面板 + 1px 描边,6dp 内边距让文字不压到圆角与描边 */
fun background(cornerDp: Int = 12): Drawable {
val shape = GradientDrawable().apply {
shape = GradientDrawable.RECTANGLE
cornerRadius = cornerDp * density
setColor(surface)
setStroke((1 * density).toInt().coerceAtLeast(1), stroke)
}
return InsetDrawable(shape, (6 * density).toInt())
}
}
/**
* 取当前页面的弹窗配色板。
*
* 必须传 Activity(不能用 applicationContext):Activity 的资源才是页面真正
* 生效的那份,只有取自同一份资源,弹窗与页面才不会深浅错位。
*/
fun popupPalette(context: Context): PopupPalette {
val res = context.resources
val surface = res.getColor(R.color.surface, null)
var text = res.getColor(R.color.text, null)
var brand = res.getColor(R.color.brand, null)
// 兜底:同源取色时对比度必然充足,正常不会进这里。万一将来又有人在别处
// 混用两套资源(或厂商 ROM 的资源缓存出问题),这里至少保证文字与背景
// 分得开,绝不会再出现"背景黑、字也黑"这种完全看不见的情况。
if (contrastRatio(surface, text) < 3.0) {
text = if (luminance(surface) < 0.5) Color.WHITE else Color.BLACK
}
if (contrastRatio(surface, brand) < 2.0) brand = text
return PopupPalette(
surface = surface,
stroke = res.getColor(R.color.card_stroke, null),
text = text,
textSecondary = res.getColor(R.color.text_secondary, null),
divider = res.getColor(R.color.divider, null),
brand = brand,
night = (res.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) ==
Configuration.UI_MODE_NIGHT_YES,
density = res.displayMetrics.density,
)
}
/** 相对亮度(WCAG 2.0),用于判断前景与背景是否够区分 */
private fun luminance(color: Int): Double {
fun channel(value: Int): Double {
val s = value / 255.0
return if (s <= 0.03928) s / 12.92 else Math.pow((s + 0.055) / 1.055, 2.4)
}
return 0.2126 * channel(Color.red(color)) +
0.7152 * channel(Color.green(color)) +
0.0722 * channel(Color.blue(color))
}
/** 对比度(1~21),正文可读的经验下限约 3.0 */
private fun contrastRatio(a: Int, b: Int): Double {
val la = luminance(a)
val lb = luminance(b)
return (maxOf(la, lb) + 0.05) / (minOf(la, lb) + 0.05)
}
/** 只要背景时的便捷入口(与 popupPalette(context).background() 等价) */
fun popupBackground(context: Context, cornerDp: Int = 12): Drawable =
popupPalette(context).background(cornerDp)
/**
* 按最宽文本计算下拉菜单宽度:比文字稍宽即可,不再与锚点控件等宽。
*
* @param textSizeSp 菜单文字字号(与 item 布局保持一致)
* @param extraDp 额外预留:左右内边距 + 背景 + 滚动条
* @param minDp/maxDp 宽度上下限
*/
fun menuWidth(
context: Context,
texts: List<String>,
textSizeSp: Float = 15f,
extraDp: Int = 32,
minDp: Int = 96,
maxDp: Int = 240,
): Int {
val dm = context.resources.displayMetrics
val paint = TextPaint(Paint.ANTI_ALIAS_FLAG).apply {
// 走 applyDimension 而不是直接乘已废弃的 scaledDensity:
// 系统字号放大后(尤其 Android 14 的非线性字体缩放),手乘得到的宽度
// 会小于实际渲染宽度,菜单项又带 ellipsize,四五个字就会被截成两三个。
textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, textSizeSp, dm)
}
var text = 0f
for (t in texts) text = maxOf(text, paint.measureText(t))
val d = dm.density
// +0.5f 是四舍五入:直接 toInt() 会向下取整,宽度总比算出来的少 1px
return (text + extraDp * d + 0.5f).toInt()
.coerceIn((minDp * d).toInt(), (maxDp * d).toInt())
}
}