Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.polyfrost.oneconfig.api.config.v1.annotations.Slider
import org.polyfrost.oneconfig.api.config.v1.annotations.Switch
import org.polyfrost.oneconfig.internal.OneConfigConfig
import org.polyfrost.polyplus.PolyPlusConstants
import org.polyfrost.polyplus.client.gui.widgets.ClockFormat
import org.polyfrost.polyplus.client.gui.widgets.WidgetMigration
import org.polyfrost.polyplus.client.lobby.LobbyCaptureFrequency
import org.polyfrost.polyplus.client.lobby.LobbyCaptureView
Expand Down Expand Up @@ -135,6 +136,14 @@ object PolyPlusLobbyConfig : Config(
@JvmStatic @Include
var widgetsMigrated = false

@JvmStatic
@Dropdown(
title = "Clock Format",
description = "Show the clock widget in 12-hour or 24-hour time. Automatic follows your system language.",
options = ["Automatic", "12-hour", "24-hour"],
)
var clockFormat: ClockFormat = ClockFormat.AUTOMATIC

@JvmStatic
@Dropdown(
title = "Scene Capture",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.polyfrost.polyplus.client.gui.widgets

enum class ClockFormat(private val label: String) {
AUTOMATIC("Automatic"),
TWELVE_HOUR("12-hour"),
TWENTY_FOUR_HOUR("24-hour");

override fun toString(): String = label
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import kotlinx.coroutines.delay
import org.polyfrost.oneconfig.api.ui.v1.ExperimentalOneConfigUiApi
import org.polyfrost.oneconfig.api.ui.v1.components.Text
import org.polyfrost.oneconfig.api.ui.v1.theme.OneConfigTheme
import org.polyfrost.polyplus.client.PolyPlusLobbyConfig
import org.polyfrost.polyplus.client.gui.SocialTextPrimary
import org.polyfrost.polyplus.client.gui.pausescreen.asset
import org.polyfrost.polyplus.client.gui.pausescreen.rememberFrame
Expand Down Expand Up @@ -85,6 +86,8 @@ private const val CLOCK_FADE_MILLIS = 180
private const val CLOCK_DATE_SKELETON = "MMMEd"
private val WEEKDAY = Regex("([Ec])\\1*[.]?")
private const val CLOCK_DATE_FALLBACK = "EEEE, d MMM"
private const val CLOCK_TIME_12 = "h:mm a"
private const val CLOCK_TIME_24 = "HH:mm"
private const val CLOCK_ICON = "clock"
private const val CLOCK_ICON_FALLBACK = "moon-star"

Expand Down Expand Up @@ -165,16 +168,22 @@ private fun clockIcon(): String {
return if (found) path else asset(CLOCK_ICON_FALLBACK)
}

private class ClockFormats(locale: Locale) {
val time: DateTimeFormatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT).withLocale(locale)
private class ClockFormats(locale: Locale, format: ClockFormat) {
private val localized: String? = runCatching {
DateTimeFormatterBuilder.getLocalizedDateTimePattern(null, FormatStyle.SHORT, IsoChronology.INSTANCE, locale)
}.getOrNull()
private val pattern: String? = when (format) {
ClockFormat.AUTOMATIC -> null
ClockFormat.TWELVE_HOUR -> localized?.takeIf { 'a' in it } ?: CLOCK_TIME_12
ClockFormat.TWENTY_FOUR_HOUR -> localized?.takeIf { 'a' !in it } ?: CLOCK_TIME_24
}
val time: DateTimeFormatter = pattern?.let { DateTimeFormatter.ofPattern(it, locale) }
?: DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT).withLocale(locale)
val date: DateTimeFormatter = runCatching {
val pattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(CLOCK_DATE_SKELETON, IsoChronology.INSTANCE, locale)
DateTimeFormatter.ofPattern(pattern.replace(WEEKDAY) { it.groupValues[1].repeat(4) }, locale)
}.getOrElse { DateTimeFormatter.ofPattern(CLOCK_DATE_FALLBACK, locale) }
val marker: DateTimeFormatter? = runCatching {
val pattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(null, FormatStyle.SHORT, IsoChronology.INSTANCE, locale)
if ('a' in pattern) DateTimeFormatter.ofPattern("a", locale) else null
}.getOrNull()
val marker: DateTimeFormatter? = (pattern ?: localized)?.takeIf { 'a' in it }?.let { DateTimeFormatter.ofPattern("a", locale) }
}

private class TimeParts(val digits: String, val marker: String, val markerFirst: Boolean)
Expand Down Expand Up @@ -238,9 +247,10 @@ private fun readable(color: Color, lightTheme: Boolean): Color {

@Composable
private fun ClockCard(reduced: Boolean) {
val formats = remember { ClockFormats(Locale.getDefault(Locale.Category.FORMAT)) }
val minute = rememberMinute().value
val parts = remember(minute) { timeParts(formats, minute) }
val format = PolyPlusLobbyConfig.clockFormat
val formats = remember(format) { ClockFormats(Locale.getDefault(Locale.Category.FORMAT), format) }
val parts = remember(minute, formats) { timeParts(formats, minute) }
val date = remember(minute.toLocalDate()) { plainSpaces(runCatching { formats.date.format(minute) }.getOrDefault("")) }
val ofDay = minuteOfDay(minute)
val lightTheme = OneConfigTheme.current.isLight
Expand Down