点击月份标题跳转到今天

This commit is contained in:
meyou 2026-05-16 14:54:35 +08:00
parent fcad070800
commit b65856a4ae
2 changed files with 16 additions and 1 deletions

View File

@ -27,6 +27,7 @@ import kotlinx.datetime.TimeZone
import kotlinx.datetime.number
import kotlinx.datetime.plus
import kotlinx.datetime.todayIn
import kotlinx.coroutines.launch
import plus.rua.project.CalendarViewModel
import kotlin.math.abs
import kotlin.time.Clock
@ -144,6 +145,17 @@ fun CalendarMonthView(
year = currentYear,
month = currentMonth,
weekNumber = viewModel.getIsoWeekNumber(viewModel.selectedDate),
onClick = {
viewModel.selectDate(today)
@Suppress("DEPRECATION") // monthNumber 无替代 API
val targetPage = yearMonthToPage(
today.year, today.month.number,
today.year, today.month.number
)
if (targetPage != pagerState.currentPage) {
coroutineScope.launch { pagerState.animateScrollToPage(targetPage) }
}
},
modifier = Modifier.onSizeChanged { size ->
monthHeaderHeightPx = size.height
}

View File

@ -7,6 +7,7 @@ import androidx.compose.animation.fadeOut
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.animation.togetherWith
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
@ -32,12 +33,14 @@ fun MonthHeader(
year: Int,
month: Int,
weekNumber: Int,
onClick: (() -> Unit)? = null,
modifier: Modifier = Modifier
) {
Row(
modifier = modifier
.fillMaxWidth()
.padding(vertical = 14.dp, horizontal = 12.dp),
.padding(vertical = 14.dp, horizontal = 12.dp)
.then(if (onClick != null) Modifier.clickable(onClick = onClick) else Modifier),
verticalAlignment = Alignment.CenterVertically
) {
AnimatedContent(