TextSelectionDelegate.kt
/*
* 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)
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
)
}