From af15879b0aa2a9eaca02fadc1938fbfa2e4583bd Mon Sep 17 00:00:00 2001 From: Alexander Maryanovsky Date: Tue, 29 Oct 2024 14:22:16 +0200 Subject: [PATCH] Make DefaultCursorThickness an `expect val` and draw cursor without blurring. This is needed to allow a different cursor thickness on the desktop. See https://github.com/JetBrains/compose-multiplatform-core/pull/103 RelNote: "We are ever slightly changing the way cursor is drawn. You can use LocalCursorBlinkEnabled to disable the cursor drawing in tests." Test: No additional tests needed; TextFieldCursorTest is passing. Change-Id: I4c6979903bec329287ec25093f55b83668d674ea --- .../foundation/text/TextFieldCursor.android.kt | 18 +++++++++++++++++- .../compose/foundation/text/TextFieldCursor.kt | 2 +- .../selection/TextFieldSelectionState.kt | 2 +- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/compose/foundation/foundation/src/androidMain/kotlin/androidx/compose/foundation/text/TextFieldCursor.android.kt b/compose/foundation/foundation/src/androidMain/kotlin/androidx/compose/foundation/text/TextFieldCursor.android.kt index 3151c0e83508a..728f3de6ec11d 100644 --- a/compose/foundation/foundation/src/androidMain/kotlin/androidx/compose/foundation/text/TextFieldCursor.android.kt +++ b/compose/foundation/foundation/src/androidMain/kotlin/androidx/compose/foundation/text/TextFieldCursor.android.kt @@ -1,6 +1,22 @@ +/* + * Copyright 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package androidx.compose.foundation.text import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp -internal actual val DefaultCursorThickness: Dp = 2.dp \ No newline at end of file +internal actual val DefaultCursorThickness: Dp = 2.dp diff --git a/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text/TextFieldCursor.kt b/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text/TextFieldCursor.kt index bd33cea1d28ad..c12f34d5fbc7a 100644 --- a/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text/TextFieldCursor.kt +++ b/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text/TextFieldCursor.kt @@ -80,7 +80,7 @@ internal fun Modifier.cursor( // When cursor width is odd, draw it in the middle of a pixel, // to avoid blurring due to antialiasing. if (cursorWidth.toInt() % 2 == 1) { - floor(it) + 0.5f // round to nearest n+0.5 + floor(it) + 0.5f // round to nearest n+0.5 } else round(it) } diff --git a/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text/input/internal/selection/TextFieldSelectionState.kt b/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text/input/internal/selection/TextFieldSelectionState.kt index 4f29869add308..262a02e6b4d4d 100644 --- a/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text/input/internal/selection/TextFieldSelectionState.kt +++ b/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text/input/internal/selection/TextFieldSelectionState.kt @@ -323,7 +323,7 @@ internal class TextFieldSelectionState( // When cursor width is odd, draw it in the middle of a pixel, // to avoid blurring due to antialiasing. if (cursorWidth.toInt() % 2 == 1) { - floor(it) + 0.5f // round to nearest n+0.5 + floor(it) + 0.5f // round to nearest n+0.5 } else round(it) }