From 94b04c13652ef5d2347c5d730a6d3c83440c42c4 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Fri, 23 Oct 2026 15:45:54 +0200 Subject: [PATCH] Month widget grid: palette-following colours, day ripples, bold today, strike via span, lane cap (#372) --- .../widget/month/MonthWidgetPayloadTest.kt | 18 ++-- .../calendula/widget/month/MonthGrid.kt | 20 +++-- .../calendula/widget/month/MonthGridViews.kt | 83 +++++++------------ .../calendula/widget/month/MonthWidget.kt | 37 ++++----- app/src/main/res/layout/widget_month_week.xml | 21 +++-- .../calendula/widget/month/MonthGridTest.kt | 12 +++ floret-kit | 2 +- 7 files changed, 98 insertions(+), 95 deletions(-) diff --git a/app/src/androidTest/java/de/jeanlucmakiola/calendula/widget/month/MonthWidgetPayloadTest.kt b/app/src/androidTest/java/de/jeanlucmakiola/calendula/widget/month/MonthWidgetPayloadTest.kt index 301155a7..5b02619c 100644 --- a/app/src/androidTest/java/de/jeanlucmakiola/calendula/widget/month/MonthWidgetPayloadTest.kt +++ b/app/src/androidTest/java/de/jeanlucmakiola/calendula/widget/month/MonthWidgetPayloadTest.kt @@ -25,6 +25,7 @@ import org.junit.Before import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith +import de.jeanlucmakiola.calendula.R import java.time.LocalDate import java.time.ZoneId @@ -97,7 +98,7 @@ class MonthWidgetPayloadTest { @Test fun aPackedMonthRendersWithinItsPayload() = runBlocking { - for (size in listOf(DpSize(250.dp, 250.dp), DpSize(320.dp, 360.dp), DpSize(420.dp, 520.dp))) { + for (size in listOf(DpSize(250.dp, 250.dp), DpSize(320.dp, 360.dp), DpSize(420.dp, 520.dp), DpSize(600.dp, 1000.dp))) { val views = MonthWidget().compose(context, size = size) val bytes = Parcel.obtain().let { parcel -> try { @@ -107,18 +108,21 @@ class MonthWidgetPayloadTest { parcel.recycle() } } - var texts = 0 + var dayNumbers = 0 + var bars = 0 instrumentation.runOnMainSync { val root = views.apply(context, FrameLayout(context)) fun walk(view: View) { - if (view is TextView) texts += 1 + if (view.id in DAY_IDS && view is TextView && view.text.isNotEmpty()) dayNumbers += 1 + if (view.id == R.id.bar_title) bars += 1 if (view is ViewGroup) for (i in 0 until view.childCount) walk(view.getChildAt(i)) } walk(root) } - Log.i(TAG, "size=$size bytes=$bytes texts=$texts") - // The grid itself rendered, not Glance's error layout: every day number is there. - assertThat(texts).isAtLeast(42) + Log.i(TAG, "size=$size bytes=$bytes days=$dayNumbers bars=$bars") + // The grid itself rendered, not Glance's error layout: a number in every day of every week. + assertThat(dayNumbers % 7).isEqualTo(0) + assertThat(dayNumbers).isAtLeast(28) assertThat(bytes).isAtMost(MAX_BYTES_PER_SIZE) } } @@ -128,6 +132,8 @@ class MonthWidgetPayloadTest { const val ACCOUNT = "calendula-widget-payload-test" const val EVENTS_PER_DAY = 6 + val DAY_IDS = setOf(R.id.day_0, R.id.day_1, R.id.day_2, R.id.day_3, R.id.day_4, R.id.day_5, R.id.day_6) + /** A quarter of the host's buffer, leaving room for a second size and every other widget. */ const val MAX_BYTES_PER_SIZE = 256 * 1024 } 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 d567e4ff..e43d95c6 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 @@ -36,8 +36,11 @@ internal fun mergeGaps(columns: Int, cellAt: (Int) -> GridCell?): L return cells } -/** One week of the grid seated with [laneCap] lanes. */ -internal class WeekLayout(val week: MonthWeek, val laneCap: Int) { +/** + * One week of the grid seated with [laneCap] lanes. [showsOverflow] is off when + * the week has no room for even the "+N" row. + */ +internal class WeekLayout(val week: MonthWeek, val laneCap: Int, val showsOverflow: Boolean = true) { private val seating: List = week.days.indices.map { week.seating(it, laneCap) } /** Lanes filled from the top; empty lanes below are not drawn. */ @@ -59,19 +62,26 @@ internal class WeekLayout(val week: MonthWeek, val laneCap: Int) { } } -/** Event lanes a week row [weekHeight] tall holds under its day number. */ +/** + * 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). + */ internal fun lanesFitting(weekHeight: Dp): Int = - ((weekHeight.value - DAY_ROW_DP - LANES_TOP_DP) / LANE_ROW_DP).toInt().coerceAtLeast(0) + ((weekHeight.value - DAY_ROW_DP - LANES_TOP_DP) / LANE_ROW_DP).toInt().coerceIn(0, MAX_LANES) /** * Seats each week in [lanes] lanes. A week whose events need more gives its * last lane to the "+N" row instead. */ internal fun fitWeeks(weeks: List, lanes: Int): List = weeks.map { week -> + if (lanes == 0) return@map WeekLayout(week, laneCap = 0, showsOverflow = false) val needed = WeekLayout(week, UNCAPPED_LANES).usedLanes - WeekLayout(week, if (needed <= lanes) lanes else (lanes - 1).coerceAtLeast(0)) + WeekLayout(week, if (needed <= lanes) lanes else lanes - 1) } +/** Lanes a week shows at most, however tall the widget. */ +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 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 88db8c49..29215f7d 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 @@ -2,14 +2,13 @@ package de.jeanlucmakiola.calendula.widget.month import android.app.PendingIntent import android.content.Context -import android.content.res.Configuration -import android.graphics.Paint -import android.os.Build +import android.graphics.Typeface +import android.text.SpannableString +import android.text.Spanned +import android.text.style.StrikethroughSpan +import android.text.style.StyleSpan import android.view.View import android.widget.RemoteViews -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.graphics.toArgb -import androidx.glance.unit.ColorProvider import de.jeanlucmakiola.calendula.MainActivity import de.jeanlucmakiola.calendula.R import de.jeanlucmakiola.calendula.domain.EventInstance @@ -17,45 +16,22 @@ import de.jeanlucmakiola.calendula.domain.isDeclined import de.jeanlucmakiola.calendula.ui.common.CalendarView import de.jeanlucmakiola.calendula.ui.common.eventFill import de.jeanlucmakiola.calendula.ui.common.eventInk +import de.jeanlucmakiola.floret.glance.RemoteColor import de.jeanlucmakiola.floret.glance.WidgetThemeMode +import de.jeanlucmakiola.floret.glance.remoteColor +import de.jeanlucmakiola.floret.glance.setImageTint +import de.jeanlucmakiola.floret.glance.setTextColor import kotlinx.datetime.LocalDate import kotlinx.datetime.Month -/** A colour for the light and the dark widget theme; equal when the widget pins one. */ -internal data class DayNight(val day: Int, val night: Int) { - fun current(context: Context): Int = - if (context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES) night else day -} - -/** The theme colours the plain-RemoteViews grid draws with, resolved out of Glance's providers. */ +/** The theme colours the plain-RemoteViews grid draws with. */ internal data class GridColors( - val onSurface: DayNight, - val onSurfaceVariant: DayNight, - val primary: DayNight, - val onPrimary: DayNight, + val onSurface: RemoteColor, + val onSurfaceVariant: RemoteColor, + val primary: RemoteColor, + val onPrimary: RemoteColor, ) -internal fun ColorProvider.dayNight(context: Context, mode: WidgetThemeMode): DayNight { - fun at(night: Boolean): Int { - val config = Configuration(context.resources.configuration).apply { - uiMode = (uiMode and Configuration.UI_MODE_NIGHT_MASK.inv()) or - if (night) Configuration.UI_MODE_NIGHT_YES else Configuration.UI_MODE_NIGHT_NO - } - return getColor(context.createConfigurationContext(config)).toArgb() - } - return when (mode) { - WidgetThemeMode.SYSTEM -> DayNight(day = at(false), night = at(true)) - WidgetThemeMode.LIGHT -> at(false).let { DayNight(it, it) } - WidgetThemeMode.DARK -> at(true).let { DayNight(it, it) } - } -} - -private fun pinned(day: Color, night: Color, mode: WidgetThemeMode): DayNight = when (mode) { - WidgetThemeMode.SYSTEM -> DayNight(day.toArgb(), night.toArgb()) - WidgetThemeMode.LIGHT -> DayNight(day.toArgb(), day.toArgb()) - WidgetThemeMode.DARK -> DayNight(night.toArgb(), night.toArgb()) -} - /** * The month's week rows as one plain RemoteViews tree, embedded in the Glance * widget so its views don't draw on Glance's view-id pool (#372): every event @@ -94,15 +70,15 @@ private fun weekViews( layout.week.days.forEachIndexed { col, date -> week.setOnClickPendingIntent(STRIP_IDS[col], openDayIntent(context, date)) val dayId = DAY_IDS[col] - week.setTextViewText(dayId, date.day.toString()) val isToday = date == today - week.setColor(context, dayId, "setTextColor", when { + week.setTextViewText(dayId, if (isToday) bold(date.day.toString()) else date.day.toString()) + week.setTextColor(context, dayId, when { isToday -> colors.onPrimary date.month == currentMonth -> colors.onSurface else -> colors.onSurfaceVariant }) week.setViewVisibility(TODAY_IDS[col], if (isToday) View.VISIBLE else View.GONE) - if (isToday) week.setColor(context, TODAY_IDS[col], "setColorFilter", colors.primary) + if (isToday) week.setImageTint(context, TODAY_IDS[col], colors.primary) } layout.lanes.forEach { cells -> val lane = laneViews(context) @@ -112,7 +88,7 @@ private fun weekViews( } week.addView(R.id.week_lanes, lane) } - if (layout.overflow.isNotEmpty()) { + if (layout.showsOverflow && layout.overflow.isNotEmpty()) { val lane = laneViews(context) layout.overflow.forEach { cell -> val count = cell.item @@ -121,7 +97,7 @@ private fun weekViews( } else { val more = RemoteViews(context.packageName, R.layout.widget_month_more) more.setTextViewText(R.id.more, "+$count") - more.setColor(context, R.id.more, "setTextColor", colors.onSurfaceVariant) + more.setTextColor(context, R.id.more, colors.onSurfaceVariant) lane.addView(R.id.lane, more) } } @@ -139,21 +115,18 @@ private fun bar(context: Context, event: EventInstance, cols: Int, soften: Boole val views = RemoteViews(context.packageName, BAR_LAYOUTS[cols - 1]) val dayFill = eventFill(event.color, dark = false, soften) val nightFill = eventFill(event.color, dark = true, soften) - views.setColor(context, R.id.bar_bg, "setColorFilter", pinned(dayFill, nightFill, mode)) - views.setTextViewText(R.id.bar_title, event.title.ifBlank { context.getString(R.string.event_untitled) }) - views.setColor(context, R.id.bar_title, "setTextColor", pinned(eventInk(dayFill), eventInk(nightFill), mode)) - val strike = if (event.isDeclined) Paint.STRIKE_THRU_TEXT_FLAG else 0 - views.setInt(R.id.bar_title, "setPaintFlags", strike or Paint.ANTI_ALIAS_FLAG or Paint.SUBPIXEL_TEXT_FLAG) + views.setImageTint(context, R.id.bar_bg, mode.remoteColor(dayFill, nightFill)) + val title = event.title.ifBlank { context.getString(R.string.event_untitled) } + // A span, like Glance's own decoration: paint flags would drop the text's defaults. + views.setTextViewText(R.id.bar_title, if (event.isDeclined) span(title, StrikethroughSpan()) else title) + views.setTextColor(context, R.id.bar_title, mode.remoteColor(eventInk(dayFill), eventInk(nightFill))) return views } -private fun RemoteViews.setColor(context: Context, id: Int, method: String, color: DayNight) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && color.day != color.night) { - setColorInt(id, method, color.day, color.night) - } else { - setInt(id, method, color.current(context)) - } -} +private fun bold(text: String): CharSequence = span(text, StyleSpan(Typeface.BOLD)) + +private fun span(text: String, span: Any): CharSequence = + SpannableString(text).apply { setSpan(span, 0, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) } private fun openDayIntent(context: Context, date: LocalDate): PendingIntent = PendingIntent.getActivity( context, 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 b756d097..8ce7fe8e 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 @@ -1,7 +1,5 @@ package de.jeanlucmakiola.calendula.widget.month -import de.jeanlucmakiola.calendula.domain.isDeclined -import de.jeanlucmakiola.calendula.widget.glanceDeclinedDecoration import android.content.Context import androidx.compose.runtime.Composable import androidx.compose.runtime.SideEffect @@ -21,25 +19,21 @@ import androidx.glance.LocalContext import androidx.glance.LocalSize import androidx.glance.action.ActionParameters import androidx.glance.action.actionParametersOf -import androidx.glance.action.clickable import androidx.glance.appwidget.GlanceAppWidget import androidx.glance.appwidget.AndroidRemoteViews import androidx.glance.appwidget.SizeMode import androidx.glance.appwidget.action.ActionCallback import androidx.glance.appwidget.action.actionRunCallback import androidx.glance.appwidget.action.actionStartActivity -import androidx.glance.appwidget.cornerRadius import androidx.glance.appwidget.provideContent import androidx.glance.appwidget.state.getAppWidgetState import androidx.glance.appwidget.state.updateAppWidgetState -import androidx.glance.background import androidx.glance.currentState import androidx.glance.layout.Alignment import androidx.glance.layout.Box import androidx.glance.layout.Column import androidx.glance.layout.Row import androidx.glance.layout.Spacer -import androidx.glance.layout.fillMaxHeight import androidx.glance.layout.fillMaxSize import androidx.glance.layout.fillMaxWidth import androidx.glance.layout.height @@ -48,29 +42,24 @@ import androidx.glance.layout.size import androidx.glance.layout.width import androidx.glance.state.PreferencesGlanceStateDefinition import de.jeanlucmakiola.floret.glance.LocalWidgetThemeMode +import de.jeanlucmakiola.floret.glance.WidgetColorRole +import de.jeanlucmakiola.floret.glance.remoteColor import de.jeanlucmakiola.floret.glance.WidgetActionStyle import de.jeanlucmakiola.floret.glance.WidgetHeader import de.jeanlucmakiola.floret.glance.WidgetHeaderAction import de.jeanlucmakiola.floret.glance.WidgetHeaderActionGap import de.jeanlucmakiola.floret.glance.WidgetHeaderTextAction -import androidx.glance.text.FontWeight import androidx.glance.text.Text import androidx.glance.text.TextAlign import androidx.glance.text.TextStyle -import androidx.glance.unit.ColorProvider import de.jeanlucmakiola.calendula.MainActivity import de.jeanlucmakiola.calendula.R -import de.jeanlucmakiola.calendula.domain.EventInstance import de.jeanlucmakiola.calendula.ui.common.CalendarView import de.jeanlucmakiola.calendula.ui.common.formatCalendarTitle -import de.jeanlucmakiola.calendula.ui.month.MonthWeek import de.jeanlucmakiola.calendula.ui.month.layoutMonthWeeks -import de.jeanlucmakiola.calendula.ui.common.eventFill -import de.jeanlucmakiola.calendula.ui.common.eventInk import de.jeanlucmakiola.calendula.widget.CalendulaWidgetRoot import de.jeanlucmakiola.calendula.widget.WidgetAppearance import de.jeanlucmakiola.calendula.widget.widgetAppearance -import de.jeanlucmakiola.floret.glance.themedColor import de.jeanlucmakiola.floret.glance.wallpaperBacking import de.jeanlucmakiola.floret.prefs.ThemeMode import de.jeanlucmakiola.calendula.widget.MonthWidgetSource @@ -97,8 +86,13 @@ private val MONTH_INDEX_KEY = intPreferencesKey("month_index") private val GRID_HPADDING = 8.dp -/** Height around the week rows: body padding, header, its spacer and the weekday row. */ -private val GRID_CHROME_HEIGHT = 62.dp +/** + * Height around the week rows: body padding, the header's bottom padding and + * spacer, then the header (its 28dp pills, or a title line in its transparent + * pill when that's taller) and the weekday row, the text lines at [fontScale]. + */ +private fun gridChromeHeight(fontScale: Float): Dp = + 18.dp + maxOf(28.dp, 19.dp * fontScale + 8.dp) + 15.dp * fontScale private fun currentMonthIndex(zone: TimeZone): Int = monthIndexOf(today(zone)) @@ -244,15 +238,16 @@ private fun MonthWidgetBody(source: MonthWidgetSource, index: Int) { // Column width from the live widget size, minus our H padding. val colW = (LocalSize.current.width - GRID_HPADDING * 2) / 7 val weeks = layoutMonthWeeks(ym, source.weekStart, source.instances, zone) - val weekHeight = (LocalSize.current.height - GRID_CHROME_HEIGHT) / weeks.size - val layouts = fitWeeks(weeks, lanesFitting(weekHeight)) val context = LocalContext.current + val fontScale = context.resources.configuration.fontScale + val weekHeight = (LocalSize.current.height - gridChromeHeight(fontScale)) / weeks.size + val layouts = fitWeeks(weeks, lanesFitting(weekHeight)) val mode = LocalWidgetThemeMode.current val colors = GridColors( - onSurface = GlanceTheme.colors.onSurface.dayNight(context, mode), - onSurfaceVariant = GlanceTheme.colors.onSurfaceVariant.dayNight(context, mode), - primary = GlanceTheme.colors.primary.dayNight(context, mode), - onPrimary = GlanceTheme.colors.onPrimary.dayNight(context, mode), + onSurface = remoteColor(WidgetColorRole.ON_SURFACE), + onSurfaceVariant = remoteColor(WidgetColorRole.ON_SURFACE_VARIANT), + primary = remoteColor(WidgetColorRole.PRIMARY), + onPrimary = remoteColor(WidgetColorRole.ON_PRIMARY), ) MonthHeader(label = monthLabel(ym, source.today.year), today = source.today) diff --git a/app/src/main/res/layout/widget_month_week.xml b/app/src/main/res/layout/widget_month_week.xml index e44c7d9d..40bf2ff7 100644 --- a/app/src/main/res/layout/widget_month_week.xml +++ b/app/src/main/res/layout/widget_month_week.xml @@ -12,37 +12,44 @@ android:id="@+id/strip_0" android:layout_width="0dp" android:layout_height="match_parent" - android:layout_weight="1" /> + android:layout_weight="1" + android:background="?android:attr/selectableItemBackground" /> + android:layout_weight="1" + android:background="?android:attr/selectableItemBackground" /> + android:layout_weight="1" + android:background="?android:attr/selectableItemBackground" /> + android:layout_weight="1" + android:background="?android:attr/selectableItemBackground" /> + android:layout_weight="1" + android:background="?android:attr/selectableItemBackground" /> + android:layout_weight="1" + android:background="?android:attr/selectableItemBackground" /> + android:layout_weight="1" + android:background="?android:attr/selectableItemBackground" />