commonMain.androidx.compose.foundation.text.selection.TextSelectionDelegate.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of foundation Show documentation
Show all versions of foundation Show documentation
Higher level abstractions of the Compose UI primitives. This library is design system agnostic, providing the high-level building blocks for both application and design-system developers
/*
* Copyright 2019 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.selection
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.text.TextLayoutResult
import kotlin.math.max
/**
* This method returns the graphical position where the selection handle should be based on the
* offset and other information.
*
* @param textLayoutResult a result of the text layout.
* @param offset character offset to be calculated
* @param isStart true if called for selection start handle
* @param areHandlesCrossed true if the selection handles are crossed
*
* @return the graphical position where the selection handle should be.
*/
internal fun getSelectionHandleCoordinates(
textLayoutResult: TextLayoutResult,
offset: Int,
isStart: Boolean,
areHandlesCrossed: Boolean
): Offset {
val line = textLayoutResult.getLineForOffset(offset)
// This happens if maxLines is set but the offset is on a line >= maxLines.
if (line >= textLayoutResult.lineCount) return Offset.Unspecified
val x = textLayoutResult.getHorizontalPosition(offset, isStart, areHandlesCrossed)
val y = textLayoutResult.getLineBottom(line)
return Offset(x, y)
}
internal fun TextLayoutResult.getHorizontalPosition(
offset: Int,
isStart: Boolean,
areHandlesCrossed: Boolean
): Float {
val offsetToCheck =
if (isStart && !areHandlesCrossed || !isStart && areHandlesCrossed) offset
else max(offset - 1, 0)
val bidiRunDirection = getBidiRunDirection(offsetToCheck)
val paragraphDirection = getParagraphDirection(offset)
return getHorizontalPosition(
offset = offset,
usePrimaryDirection = bidiRunDirection == paragraphDirection
)
}