Reorganize selection toolbar
1. Selection toolbar no longer relies on recompose.
2. Renamed function names in SelectionRegistrar for better readibility.
Bug: 161183209
Test: ./gradlew test
RelNote: "Introduced several new functions in SelectionRegistrar and also renamed onPositionChange to notifyPositionChange."
Change-Id: Ifbaf754c0ee3f485869115bba8dbcc1a8b7f5b88
diff --git a/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/selection/SelectionManager.kt b/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/selection/SelectionManager.kt
index b6cbf8e..ce33357 100644
--- a/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/selection/SelectionManager.kt
+++ b/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/selection/SelectionManager.kt
@@ -31,6 +31,7 @@
import androidx.compose.ui.platform.TextToolbar
import androidx.compose.ui.platform.TextToolbarStatus
import androidx.compose.ui.text.AnnotatedString
+import androidx.compose.ui.text.ExperimentalTextApi
import androidx.compose.ui.text.InternalTextApi
import androidx.compose.ui.text.length
import androidx.compose.ui.text.subSequence
@@ -40,7 +41,10 @@
/**
* A bridge class between user interaction to the text composables for text selection.
*/
-@OptIn(InternalTextApi::class)
+@OptIn(
+ InternalTextApi::class,
+ ExperimentalTextApi::class
+)
internal class SelectionManager(private val selectionRegistrar: SelectionRegistrarImpl) {
/**
* The current selection.
@@ -49,7 +53,6 @@
set(value) {
field = value
updateHandleOffsets()
- hideSelectionToolbar()
}
/**
@@ -123,9 +126,20 @@
init {
selectionRegistrar.>
updateHandleOffsets()
+ updateSelectionToolbarPosition()
+ }
+
+ selectionRegistrar. layoutCoordinates, startPosition ->
+ updateSelection(
+ startPosition = convertToContainerCoordinates(layoutCoordinates, startPosition),
+ endPosition = convertToContainerCoordinates(layoutCoordinates, startPosition),
+ isStartHandle = true,
+ longPress = true
+ )
hideSelectionToolbar()
}
- selectionRegistrar.>
+
+ selectionRegistrar.>
{ layoutCoordinates, startPosition, endPosition ->
updateSelection(
startPosition = convertToContainerCoordinates(layoutCoordinates, startPosition),
@@ -134,6 +148,10 @@
longPress = true
)
}
+
+ selectionRegistrar.>
+ showSelectionToolbar()
+ }
}
private fun updateHandleOffsets() {
@@ -265,12 +283,9 @@
}
}
- private fun hideSelectionToolbar() {
+ internal fun hideSelectionToolbar() {
if (textToolbar?.status == TextToolbarStatus.Shown) {
- val selection = selection
- if (selection == null) {
- textToolbar?.hide()
- }
+ textToolbar?.hide()
}
}
@@ -356,12 +371,14 @@
endPosition = Offset(-1f, -1f),
previousSelection = selection
)
+ hideSelectionToolbar()
if (selection != null) onSelectionChange(null)
}
fun handleDragObserver(isStartHandle: Boolean): DragObserver {
return object : DragObserver {
override fun onStart(downPosition: Offset) {
+ hideSelectionToolbar()
val selection = selection!!
// The LayoutCoordinates of the composable where the drag gesture should begin. This
// is used to convert the position of the beginning of the drag gesture from the
@@ -375,13 +392,15 @@
// The position of the character where the drag gesture should begin. This is in
// the composable coordinates.
val beginCoordinates = getAdjustedCoordinates(
- if (isStartHandle)
+ if (isStartHandle) {
selection.start.selectable.getHandlePosition(
selection = selection, isStartHandle = true
- ) else
+ )
+ } else {
selection.end.selectable.getHandlePosition(
selection = selection, isStartHandle = false
)
+ }
)
// Convert the position where drag gesture begins from composable coordinates to
@@ -434,6 +453,14 @@
)
return dragDistance
}
+
+ override fun onStop(velocity: Offset) {
+ showSelectionToolbar()
+ }
+
+ override fun onCancel() {
+ showSelectionToolbar()
+ }
}
}
@@ -468,6 +495,7 @@
return lhs?.merge(rhs) ?: rhs
}
+@OptIn(ExperimentalTextApi::class)
internal fun getCurrentSelectedText(
selectable: Selectable,
selection: Selection