ChipMonogramIcon.kt

1.7 KB · raw

1package com.peachpasswords.autofill23import android.graphics.Bitmap4import android.graphics.Canvas5import android.graphics.Paint6import android.graphics.Typeface7import android.graphics.drawable.Icon8import android.util.LruCache9import androidx.compose.ui.graphics.toArgb10import com.peachpasswords.ui.components.titleBackground11import com.peachpasswords.ui.components.titleInitials1213/**14 * Monogram chip icons for inline autofill suggestions — the same initials and15 * deterministic background as the vault list (titleInitials/titleBackground),16 * rendered once per source and cached. The IME renders these outside any17 * Peach window, so they must be plain bitmaps.18 */19object ChipMonogramIcon {20    private const val SIZE_PX = 6421    private val cache = LruCache<String, Icon>(32)2223    fun icon(source: String): Icon {24        cache.get(source)?.let { return it }25        val bitmap = Bitmap.createBitmap(SIZE_PX, SIZE_PX, Bitmap.Config.ARGB_8888)26        val canvas = Canvas(bitmap)27        val background = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = titleBackground(source).toArgb() }28        val radius = SIZE_PX * 0.23f29        canvas.drawRoundRect(0f, 0f, SIZE_PX.toFloat(), SIZE_PX.toFloat(), radius, radius, background)30        val text = Paint(Paint.ANTI_ALIAS_FLAG).apply {31            // T0 ink at ~92%, matching the vault-list initials chip.32            color = 0xEBF0E8D8.toInt()33            textSize = SIZE_PX * 0.34f34            typeface = Typeface.MONOSPACE35            textAlign = Paint.Align.CENTER36        }37        val centerY = SIZE_PX / 2f - (text.descent() + text.ascent()) / 2f38        canvas.drawText(titleInitials(source), SIZE_PX / 2f, centerY, text)39        return Icon.createWithBitmap(bitmap).also { cache.put(source, it) }40    }41}