From a267aed86fe5c3752e6314b6514c3216edcc09b9 Mon Sep 17 00:00:00 2001 From: Damocles Date: Wed, 22 Apr 2026 22:34:27 +0200 Subject: [PATCH] fix transparent bg, responsive compact layout for single-row clock --- .../nova/android/widget/ClockWidget.kt | 153 +++++++++++------- 1 file changed, 96 insertions(+), 57 deletions(-) diff --git a/app/src/main/java/space/darkest/nova/android/widget/ClockWidget.kt b/app/src/main/java/space/darkest/nova/android/widget/ClockWidget.kt index 709d277..f5ecc82 100644 --- a/app/src/main/java/space/darkest/nova/android/widget/ClockWidget.kt +++ b/app/src/main/java/space/darkest/nova/android/widget/ClockWidget.kt @@ -5,18 +5,20 @@ import android.content.Context import android.content.Intent import android.provider.AlarmClock import androidx.compose.runtime.Composable +import androidx.compose.ui.unit.DpSize import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.glance.GlanceId import androidx.glance.GlanceModifier import androidx.glance.GlanceTheme +import androidx.glance.LocalSize import androidx.glance.action.clickable import androidx.glance.appwidget.GlanceAppWidget import androidx.glance.appwidget.GlanceAppWidgetReceiver +import androidx.glance.appwidget.SizeMode import androidx.glance.appwidget.action.actionStartActivity import androidx.glance.appwidget.provideContent import androidx.glance.background -import androidx.glance.color.ColorProvider import androidx.glance.color.DynamicThemeColorProviders import space.darkest.nova.android.data.WidgetPreferences import androidx.glance.layout.Alignment @@ -41,6 +43,8 @@ import java.util.Locale class ClockWidget : GlanceAppWidget() { + override val sizeMode = SizeMode.Exact + override suspend fun provideGlance(context: Context, id: GlanceId) { val alarmManager = context.getSystemService(AlarmManager::class.java) val nextAlarm = alarmManager.nextAlarmClock?.let { alarm -> @@ -59,6 +63,8 @@ class ClockWidget : GlanceAppWidget() { @Composable private fun ClockContent(alarmTime: LocalTime?, alarmDate: LocalDate?, transparentBg: Boolean) { + val size = LocalSize.current + val compact = size.height < 120.dp val now = LocalTime.now() val today = LocalDate.now() val hours = now.format(DateTimeFormatter.ofPattern("HH")) @@ -68,81 +74,114 @@ class ClockWidget : GlanceAppWidget() { val date = it.format(DateTimeFormatter.ofPattern("d MMM")) "$day, $date" } + val alarmStr = if (alarmTime != null && alarmDate != null) { + "\u23F0 ${formatAlarm(alarmTime, alarmDate)}" + } else null - val bgModifier = if (transparentBg) { - GlanceModifier - } else { - GlanceModifier.background(GlanceTheme.colors.widgetBackground) - } Box( - modifier = bgModifier + modifier = GlanceModifier .fillMaxSize() + .let { if (transparentBg) it else it.background(GlanceTheme.colors.widgetBackground) } .padding(16.dp) .clickable(actionStartActivity(Intent(AlarmClock.ACTION_SHOW_ALARMS))), contentAlignment = Alignment.CenterStart, ) { + if (compact) { + CompactLayout(hours, minutes, dateStr, alarmStr) + } else { + FullLayout(hours, minutes, dateStr, alarmStr) + } + } + } + + @Composable + private fun TimeRow(hours: String, minutes: String, fontSize: Int = 48) { + Row(verticalAlignment = Alignment.Bottom) { + Text( + text = hours, + style = TextStyle( + color = GlanceTheme.colors.primary, + fontSize = fontSize.sp, + fontWeight = FontWeight.Bold, + ), + ) + Text( + text = ":", + style = TextStyle( + color = GlanceTheme.colors.onSurfaceVariant, + fontSize = fontSize.sp, + fontWeight = FontWeight.Bold, + ), + ) + Text( + text = minutes, + style = TextStyle( + color = GlanceTheme.colors.tertiary, + fontSize = fontSize.sp, + fontWeight = FontWeight.Bold, + ), + ) + } + } + + @Composable + private fun CompactLayout(hours: String, minutes: String, dateStr: String, alarmStr: String?) { + Row( + verticalAlignment = Alignment.CenterVertically, + modifier = GlanceModifier.fillMaxSize(), + ) { + TimeRow(hours, minutes, fontSize = 36) + Spacer(GlanceModifier.width(12.dp)) Column { - // time row: hours (primary) : minutes (tertiary) - Row(verticalAlignment = Alignment.Bottom) { - Text( - text = hours, - style = TextStyle( - color = GlanceTheme.colors.primary, - fontSize = 48.sp, - fontWeight = FontWeight.Bold, - ), - ) - Text( - text = ":", - style = TextStyle( - color = GlanceTheme.colors.onSurfaceVariant, - fontSize = 48.sp, - fontWeight = FontWeight.Bold, - ), - ) - Text( - text = minutes, - style = TextStyle( - color = GlanceTheme.colors.tertiary, - fontSize = 48.sp, - fontWeight = FontWeight.Bold, - ), - ) - } - - Spacer(GlanceModifier.height(2.dp)) - - // date Text( text = dateStr, style = TextStyle( color = GlanceTheme.colors.onSurfaceVariant, - fontSize = 14.sp, + fontSize = 12.sp, ), ) - - // next alarm - if (alarmTime != null && alarmDate != null) { - Spacer(GlanceModifier.height(4.dp)) - Row(verticalAlignment = Alignment.CenterVertically) { - Text( - text = "\u23F0", - style = TextStyle(fontSize = 12.sp), - ) - Spacer(GlanceModifier.width(4.dp)) - Text( - text = formatAlarm(alarmTime, alarmDate), - style = TextStyle( - color = GlanceTheme.colors.secondary, - fontSize = 12.sp, - ), - ) - } + if (alarmStr != null) { + Spacer(GlanceModifier.height(2.dp)) + Text( + text = alarmStr, + style = TextStyle( + color = GlanceTheme.colors.secondary, + fontSize = 11.sp, + ), + ) } } } } + @Composable + private fun FullLayout(hours: String, minutes: String, dateStr: String, alarmStr: String?) { + Column { + TimeRow(hours, minutes) + + Spacer(GlanceModifier.height(2.dp)) + + Text( + text = dateStr, + style = TextStyle( + color = GlanceTheme.colors.onSurfaceVariant, + fontSize = 14.sp, + ), + ) + + if (alarmStr != null) { + Spacer(GlanceModifier.height(4.dp)) + Text( + text = alarmStr, + style = TextStyle( + color = GlanceTheme.colors.secondary, + fontSize = 12.sp, + ), + ) + } + } + } + private fun formatAlarm(time: LocalTime, date: LocalDate): String { val today = LocalDate.now() val timeStr = time.format(DateTimeFormatter.ofPattern("HH:mm"))