Month widget: keep the single lane's bars, compact lane only at normal font scale (#491)

This commit is contained in:
makiolaj committed 2026-11-09 18:40:30 +01:00
1 parent f574f039f5
commit f267744c1c
4 files changed
+45 -31

No files matched your search

@@ -37,10 +37,16 @@ internal fun <T : Any> mergeGaps(columns: Int, cellAt: (Int) -> GridCell<T>?): L
}
/**
* One week of the grid seated with [laneCap] lanes. [showsOverflow] is off when
* the week has no room for even the "+N" row.
* One week of the grid seated with [laneCap] lanes, drawn [compact] when the
* row is too short for a full one. [showsOverflow] is off when the week has no
* room for the "+N" row.
*/
internal class WeekLayout(val week: MonthWeek, val laneCap: Int, val showsOverflow: Boolean = true) {
internal class WeekLayout(
val week: MonthWeek,
val laneCap: Int,
val showsOverflow: Boolean = true,
val compact: Boolean = false,
) {
private val seating: List<DaySeating> = week.days.indices.map { week.seating(it, laneCap) }
/** Lanes filled from the top; empty lanes below are not drawn. */
@@ -67,22 +73,24 @@ internal data class LaneFit(val lanes: Int, val compact: Boolean = false)
/**
* Event lanes a week row [weekHeight] tall holds under its day number, held to
* [MAX_LANES] so a tall widget's payload stays bounded (#214). The last lane's
* bottom gap may be cut off, and a row too short for one full lane still gets
* one compact lane when that fits (#491).
* [MAX_LANES] so a tall widget's payload stays bounded (#214). A row too short
* for one full lane still gets a compact one when its text fits at [fontScale] (#491).
*/
internal fun lanesFitting(weekHeight: Dp): LaneFit {
val room = weekHeight.value - DAY_ROW_DP - LANES_TOP_DP + LANE_GAP_DP
internal fun lanesFitting(weekHeight: Dp, fontScale: Float = 1f): LaneFit {
val room = weekHeight.value - DAY_ROW_DP - LANES_TOP_DP
val full = (room / LANE_ROW_DP).toInt().coerceIn(0, MAX_LANES)
return if (full == 0 && room >= COMPACT_LANE_ROW_DP) LaneFit(1, compact = true) else LaneFit(full)
val compactFits = room >= COMPACT_LANE_ROW_DP && fontScale <= COMPACT_MAX_FONT_SCALE
return if (full == 0 && compactFits) LaneFit(1, compact = true) else LaneFit(full)
}
/**
* Seats each week in [lanes] lanes. A week whose events need more gives its
* last lane to the "+N" row instead.
* Seats each week in [fit]'s lanes. A week whose events need more gives its
* last lane to the "+N" row instead, unless that would leave it no lane at all:
* a single lane keeps its bars and drops the count (#491).
*/
internal fun fitWeeks(weeks: List<MonthWeek>, lanes: Int): List<WeekLayout> = weeks.map { week ->
if (lanes == 0) return@map WeekLayout(week, laneCap = 0, showsOverflow = false)
internal fun fitWeeks(weeks: List<MonthWeek>, fit: LaneFit): List<WeekLayout> = weeks.map { week ->
val lanes = fit.lanes
if (lanes <= 1) return@map WeekLayout(week, laneCap = lanes, showsOverflow = false, compact = fit.compact)
val needed = WeekLayout(week, UNCAPPED_LANES).usedLanes
WeekLayout(week, if (needed <= lanes) lanes else lanes - 1)
}
@@ -93,6 +101,7 @@ internal const val MAX_LANES = 8
private const val DAY_ROW_DP = 18f
private const val LANES_TOP_DP = 2f
private const val LANE_ROW_DP = 15f
/** Matches `widget_month_lane_compact`'s height; its 9sp text clips past [COMPACT_MAX_FONT_SCALE]. */
private const val COMPACT_LANE_ROW_DP = 12f
private const val LANE_GAP_DP = 1f
private const val COMPACT_MAX_FONT_SCALE = 1.1f
private const val UNCAPPED_LANES = 64
@@ -46,7 +46,6 @@ internal data class GridColors(
internal fun monthGridViews(
context: Context,
layouts: List<WeekLayout>,
compact: Boolean,
currentMonth: Month,
today: LocalDate,
soften: Boolean,
@@ -55,14 +54,13 @@ internal fun monthGridViews(
): RemoteViews {
val grid = RemoteViews(context.packageName, R.layout.widget_month_grid)
grid.removeAllViews(R.id.month_grid)
layouts.forEach { grid.addView(R.id.month_grid, weekViews(context, it, compact, currentMonth, today, soften, colors, mode)) }
layouts.forEach { grid.addView(R.id.month_grid, weekViews(context, it, currentMonth, today, soften, colors, mode)) }
return grid
}
private fun weekViews(
context: Context,
layout: WeekLayout,
compact: Boolean,
currentMonth: Month,
today: LocalDate,
soften: Boolean,
@@ -94,7 +92,7 @@ private fun weekViews(
if (isToday) week.setImageTint(context, TODAY_IDS[col], colors.primary)
}
layout.lanes.forEach { cells ->
val lane = laneViews(context, compact)
val lane = laneViews(context, layout.compact)
cells.forEach { cell ->
val event = cell.item
lane.addView(R.id.lane, if (event == null) gap(context, cell.cols) else bar(context, event, cell.cols, soften, mode))
@@ -102,7 +100,7 @@ private fun weekViews(
week.addView(R.id.week_lanes, lane)
}
if (layout.showsOverflow && layout.overflow.isNotEmpty()) {
val lane = laneViews(context, compact)
val lane = laneViews(context, layout.compact)
layout.overflow.forEach { cell ->
val count = cell.item
if (count == null) {
@@ -254,8 +254,7 @@ private fun MonthWidgetBody(source: MonthWidgetSource, index: Int) {
val context = LocalContext.current
val fontScale = context.resources.configuration.fontScale
val weekHeight = (LocalSize.current.height - gridChromeHeight(fontScale)) / weeks.size
val fit = lanesFitting(weekHeight)
val layouts = fitWeeks(weeks, fit.lanes)
val layouts = fitWeeks(weeks, lanesFitting(weekHeight, fontScale))
val mode = LocalWidgetThemeMode.current
val colors = GridColors(
onSurface = remoteColor(WidgetColorRole.ON_SURFACE),
@@ -273,7 +272,7 @@ private fun MonthWidgetBody(source: MonthWidgetSource, index: Int) {
Column(modifier = GlanceModifier.fillMaxWidth().defaultWeight()) {
WeekdayHeader(weekStart = source.weekStart)
AndroidRemoteViews(
remoteViews = monthGridViews(context, layouts, fit.compact, ym.month, source.today, source.soften, colors, mode),
remoteViews = monthGridViews(context, layouts, ym.month, source.today, source.soften, colors, mode),
modifier = GlanceModifier.fillMaxWidth().defaultWeight(),
)
}