From f267744c1c8891b779efc29043fa7727adcc284a Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Mon, 9 Nov 2026 18:40:30 +0100 Subject: [PATCH] Month widget: keep the single lane's bars, compact lane only at normal font scale (#491) --- .../calendula/widget/month/MonthGrid.kt | 37 ++++++++++++------- .../calendula/widget/month/MonthGridViews.kt | 8 ++-- .../calendula/widget/month/MonthWidget.kt | 5 +-- .../calendula/widget/month/MonthGridTest.kt | 26 ++++++++----- 4 files changed, 45 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/widget/month/MonthGrid.kt b/app/src/main/java/de/jeanlucmakiola/calendula/widget/month/MonthGrid.kt index b6b007a0..2a60fe14 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/widget/month/MonthGrid.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/widget/month/MonthGrid.kt @@ -37,10 +37,16 @@ internal fun mergeGaps(columns: Int, cellAt: (Int) -> GridCell?): 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 = 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, lanes: Int): List = weeks.map { week -> - if (lanes == 0) return@map WeekLayout(week, laneCap = 0, showsOverflow = false) +internal fun fitWeeks(weeks: List, fit: LaneFit): List = 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 diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/widget/month/MonthGridViews.kt b/app/src/main/java/de/jeanlucmakiola/calendula/widget/month/MonthGridViews.kt index 82566f49..bbd280ba 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/widget/month/MonthGridViews.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/widget/month/MonthGridViews.kt @@ -46,7 +46,6 @@ internal data class GridColors( internal fun monthGridViews( context: Context, layouts: List, - 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) { diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/widget/month/MonthWidget.kt b/app/src/main/java/de/jeanlucmakiola/calendula/widget/month/MonthWidget.kt index 49388525..abe674b5 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/widget/month/MonthWidget.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/widget/month/MonthWidget.kt @@ -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(), ) } diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/widget/month/MonthGridTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/widget/month/MonthGridTest.kt index 9223f521..946af7c8 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/widget/month/MonthGridTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/widget/month/MonthGridTest.kt @@ -42,14 +42,14 @@ class MonthGridTest { @Test fun `a week that fits keeps every lane`() { - val layouts = fitWeeks(busyWeeks(perDay = 2), lanes = 3) + val layouts = fitWeeks(busyWeeks(perDay = 2), LaneFit(3)) assertThat(layouts.map { it.laneCap }.distinct()).containsExactly(3) assertThat(layouts.all { it.overflow.isEmpty() }).isTrue() } @Test fun `a week that doesn't fit gives its last lane to the overflow`() { - val layouts = fitWeeks(busyWeeks(perDay = 5), lanes = 3) + val layouts = fitWeeks(busyWeeks(perDay = 5), LaneFit(3)) assertThat(layouts.map { it.laneCap }.distinct()).containsExactly(2) assertThat(layouts.all { it.overflow.isNotEmpty() }).isTrue() } @@ -62,15 +62,23 @@ class MonthGridTest { } @Test - fun `the last lane may lose its bottom gap`() { - assertThat(lanesFitting(34.dp)).isEqualTo(LaneFit(1)) - assertThat(lanesFitting(49.dp)).isEqualTo(LaneFit(2)) + fun `a row too short for a full lane gets a compact one`() { + assertThat(lanesFitting(34.dp)).isEqualTo(LaneFit(1, compact = true)) + assertThat(lanesFitting(32.dp)).isEqualTo(LaneFit(1, compact = true)) + assertThat(lanesFitting(31.dp)).isEqualTo(LaneFit(0)) + assertThat(lanesFitting(35.dp)).isEqualTo(LaneFit(1)) } @Test - fun `a row too short for a full lane gets a compact one`() { - assertThat(lanesFitting(31.dp)).isEqualTo(LaneFit(1, compact = true)) - assertThat(lanesFitting(30.dp)).isEqualTo(LaneFit(0)) + fun `no compact lane at a large font scale`() { + assertThat(lanesFitting(34.dp, fontScale = 1.3f)).isEqualTo(LaneFit(0)) + } + + @Test + fun `a single lane keeps its bars in a busy week`() { + val layouts = fitWeeks(busyWeeks(perDay = 5), LaneFit(1, compact = true)) + assertThat(layouts.all { it.laneCap == 1 && it.lanes.size == 1 && it.compact }).isTrue() + assertThat(layouts.none { it.showsOverflow }).isTrue() } @Test @@ -80,7 +88,7 @@ class MonthGridTest { @Test fun `a week with no room for a lane draws no overflow row either`() { - val layouts = fitWeeks(busyWeeks(perDay = 5), lanes = 0) + val layouts = fitWeeks(busyWeeks(perDay = 5), LaneFit(0)) assertThat(layouts.none { it.showsOverflow }).isTrue() assertThat(layouts.all { it.lanes.isEmpty() }).isTrue() }