Files
claude-usage-widget/app/src/main/java/me/khodak/claudeusage/BarRenderer.kt
T
amir d6d7daa30f v1.12: restore reset-time labels, bolder pace tick
Fix v1.11 regression where the pace tag overwrote the reset-time label
(gone entirely on the small widget). The widget reset lines now show the
actual reset time again; pace is conveyed by the bar tick.

Make the pace tick more prominent: wider core + white halo so it stands
out against any fill color.

versionCode 13 / versionName 1.12. Includes rebuilt signed release APK.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-04 02:42:45 +00:00

64 lines
2.3 KiB
Kotlin

package me.khodak.claudeusage
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
/**
* Draws a usage bar as a Bitmap: rounded track + rounded fill + an optional vertical pace tick.
* Used both in the home-screen widget (setImageViewBitmap) and the in-app card, so the two render
* identically. Bitmaps are rendered at a fixed width and stretched horizontally via ImageView
* scaleType=fitXY; height is fixed so there is no vertical distortion.
*/
object BarRenderer {
private const val TRACK_COLOR = 0xFF252525.toInt()
fun render(
usedPct: Int,
markerPct: Int?,
fillColor: Int,
tierColor: Int?,
wPx: Int = 500,
hPx: Int = 14,
cornerPx: Float = 7f
): Bitmap {
val bmp = Bitmap.createBitmap(wPx, hPx, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bmp)
val paint = Paint(Paint.ANTI_ALIAS_FLAG)
// Track
paint.color = TRACK_COLOR
val full = RectF(0f, 0f, wPx.toFloat(), hPx.toFloat())
canvas.drawRoundRect(full, cornerPx, cornerPx, paint)
// Fill
val pct = usedPct.coerceIn(0, 100)
if (pct > 0) {
paint.color = fillColor
val fillW = (wPx * pct / 100f).coerceAtLeast(cornerPx * 2)
val fill = RectF(0f, 0f, fillW, hPx.toFloat())
canvas.drawRoundRect(fill, cornerPx, cornerPx, paint)
}
// Pace tick — "where you should be right now".
// Wider than before and wrapped in a white halo so it stands out on any fill color.
if (markerPct != null && tierColor != null) {
val m = markerPct.coerceIn(0, 100)
val coreW = (wPx * 0.022f).coerceIn(9f, 14f) // tier-colored core
val halo = coreW * 0.6f // white outline on each side
var x = wPx * m / 100f
x = x.coerceIn(coreW / 2f + halo, wPx - coreW / 2f - halo)
// white halo
paint.color = 0xFFFFFFFF.toInt()
canvas.drawRect(x - coreW / 2f - halo, 0f, x + coreW / 2f + halo, hPx.toFloat(), paint)
// tier-colored core
paint.color = tierColor
canvas.drawRect(x - coreW / 2f, 0f, x + coreW / 2f, hPx.toFloat(), paint)
}
return bmp
}
}