Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 1 | /* |
haoyu | ac341f0 | 2021-01-22 22:01:56 -0800 | [diff] [blame] | 2 | * Copyright 2021 The Android Open Source Project |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Matvei Malkov | c287d1b | 2021-01-26 15:04:34 +0000 | [diff] [blame] | 17 | @file:Suppress("DEPRECATION") |
| 18 | |
haoyu | ac341f0 | 2021-01-22 22:01:56 -0800 | [diff] [blame] | 19 | package androidx.compose.foundation.text.selection |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 20 | |
Louis Pullen-Freilich | c6e92aa | 2021-03-08 00:42:16 +0000 | [diff] [blame] | 21 | import androidx.compose.foundation.fastFold |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 22 | import androidx.compose.foundation.focusable |
Ralston Da Silva | de62bc6 | 2021-06-02 17:46:44 -0700 | [diff] [blame] | 23 | import androidx.compose.foundation.gestures.forEachGesture |
| 24 | import androidx.compose.foundation.gestures.waitForUpOrCancellation |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame^] | 25 | import androidx.compose.foundation.text.Handle |
| 26 | import androidx.compose.foundation.text.TextDragObserver |
| 27 | import androidx.compose.foundation.text.selection.Selection.AnchorInfo |
| 28 | import androidx.compose.runtime.MutableState |
Louis Pullen-Freilich | 1f10a59 | 2020-07-24 16:35:14 +0100 | [diff] [blame] | 29 | import androidx.compose.runtime.State |
| 30 | import androidx.compose.runtime.getValue |
| 31 | import androidx.compose.runtime.mutableStateOf |
| 32 | import androidx.compose.runtime.setValue |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 33 | import androidx.compose.ui.Modifier |
| 34 | import androidx.compose.ui.focus.FocusRequester |
| 35 | import androidx.compose.ui.focus.focusRequester |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 36 | import androidx.compose.ui.focus.onFocusChanged |
Louis Pullen-Freilich | 534385a | 2020-08-14 14:10:12 +0100 | [diff] [blame] | 37 | import androidx.compose.ui.geometry.Offset |
| 38 | import androidx.compose.ui.geometry.Rect |
Louis Pullen-Freilich | a03fd6c | 2020-07-24 23:26:29 +0100 | [diff] [blame] | 39 | import androidx.compose.ui.hapticfeedback.HapticFeedback |
| 40 | import androidx.compose.ui.hapticfeedback.HapticFeedbackType |
Andrey Rudenko | 8b4981e | 2021-01-29 15:31:59 +0100 | [diff] [blame] | 41 | import androidx.compose.ui.input.key.KeyEvent |
| 42 | import androidx.compose.ui.input.key.onKeyEvent |
Ralston Da Silva | de62bc6 | 2021-06-02 17:46:44 -0700 | [diff] [blame] | 43 | import androidx.compose.ui.input.pointer.PointerInputScope |
| 44 | import androidx.compose.ui.input.pointer.pointerInput |
Louis Pullen-Freilich | 534385a | 2020-08-14 14:10:12 +0100 | [diff] [blame] | 45 | import androidx.compose.ui.layout.LayoutCoordinates |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 46 | import androidx.compose.ui.layout.boundsInWindow |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 47 | import androidx.compose.ui.layout.onGloballyPositioned |
haoyu | 7ad5ea3 | 2021-03-22 10:36:35 -0700 | [diff] [blame] | 48 | import androidx.compose.ui.layout.positionInWindow |
Louis Pullen-Freilich | 534385a | 2020-08-14 14:10:12 +0100 | [diff] [blame] | 49 | import androidx.compose.ui.platform.ClipboardManager |
Louis Pullen-Freilich | a03fd6c | 2020-07-24 23:26:29 +0100 | [diff] [blame] | 50 | import androidx.compose.ui.platform.TextToolbar |
| 51 | import androidx.compose.ui.platform.TextToolbarStatus |
Louis Pullen-Freilich | ab19475 | 2020-07-21 22:21:22 +0100 | [diff] [blame] | 52 | import androidx.compose.ui.text.AnnotatedString |
Ralston Da Silva | de62bc6 | 2021-06-02 17:46:44 -0700 | [diff] [blame] | 53 | import kotlinx.coroutines.coroutineScope |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 54 | import kotlin.math.max |
| 55 | import kotlin.math.min |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 56 | |
Qingqing Deng | 35f97ea | 2019-09-18 19:24:37 -0700 | [diff] [blame] | 57 | /** |
Louis Pullen-Freilich | 5da28bd | 2019-10-15 17:05:07 +0100 | [diff] [blame] | 58 | * A bridge class between user interaction to the text composables for text selection. |
Qingqing Deng | 35f97ea | 2019-09-18 19:24:37 -0700 | [diff] [blame] | 59 | */ |
Siyamed Sinir | 700df845 | 2019-10-22 20:23:58 -0700 | [diff] [blame] | 60 | internal class SelectionManager(private val selectionRegistrar: SelectionRegistrarImpl) { |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame^] | 61 | |
| 62 | private val _selection: MutableState<Selection?> = mutableStateOf(null) |
| 63 | |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 64 | /** |
| 65 | * The current selection. |
| 66 | */ |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame^] | 67 | var selection: Selection? |
| 68 | get() = _selection.value |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 69 | set(value) { |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame^] | 70 | _selection.value = value |
haoyu | 9085c88 | 2020-12-08 12:01:06 -0800 | [diff] [blame] | 71 | if (value != null) { |
| 72 | updateHandleOffsets() |
| 73 | } |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 74 | } |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 75 | |
| 76 | /** |
Andrey Rudenko | 8b4981e | 2021-01-29 15:31:59 +0100 | [diff] [blame] | 77 | * Is touch mode active |
| 78 | */ |
| 79 | var touchMode: Boolean = true |
| 80 | |
| 81 | /** |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 82 | * The manager will invoke this every time it comes to the conclusion that the selection should |
| 83 | * change. The expectation is that this callback will end up causing `setSelection` to get |
| 84 | * called. This is what makes this a "controlled component". |
| 85 | */ |
| 86 | var onSelectionChange: (Selection?) -> Unit = {} |
| 87 | |
| 88 | /** |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 89 | * [HapticFeedback] handle to perform haptic feedback. |
| 90 | */ |
| 91 | var hapticFeedBack: HapticFeedback? = null |
| 92 | |
| 93 | /** |
Qingqing Deng | f2d0a2d | 2020-03-12 14:19:50 -0700 | [diff] [blame] | 94 | * [ClipboardManager] to perform clipboard features. |
| 95 | */ |
| 96 | var clipboardManager: ClipboardManager? = null |
| 97 | |
| 98 | /** |
Qingqing Deng | a89450d | 2020-04-03 19:11:49 -0700 | [diff] [blame] | 99 | * [TextToolbar] to show floating toolbar(post-M) or primary toolbar(pre-M). |
| 100 | */ |
| 101 | var textToolbar: TextToolbar? = null |
| 102 | |
| 103 | /** |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 104 | * Focus requester used to request focus when selection becomes active. |
| 105 | */ |
| 106 | var focusRequester: FocusRequester = FocusRequester() |
| 107 | |
| 108 | /** |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 109 | * Return true if the corresponding SelectionContainer is focused. |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 110 | */ |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 111 | var hasFocus: Boolean by mutableStateOf(false) |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * Modifier for selection container. |
| 115 | */ |
| 116 | val modifier get() = Modifier |
Ralston Da Silva | de62bc6 | 2021-06-02 17:46:44 -0700 | [diff] [blame] | 117 | .onClearSelectionRequested { onRelease() } |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 118 | .onGloballyPositioned { containerLayoutCoordinates = it } |
| 119 | .focusRequester(focusRequester) |
| 120 | .onFocusChanged { focusState -> |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 121 | if (!focusState.isFocused && hasFocus) { |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 122 | onRelease() |
| 123 | } |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 124 | hasFocus = focusState.isFocused |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 125 | } |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 126 | .focusable() |
Andrey Rudenko | 8b4981e | 2021-01-29 15:31:59 +0100 | [diff] [blame] | 127 | .onKeyEvent { |
| 128 | if (isCopyKeyEvent(it)) { |
| 129 | copy() |
| 130 | true |
| 131 | } else { |
| 132 | false |
| 133 | } |
| 134 | } |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame^] | 135 | .selectionMagnifier(this) |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 136 | |
haoyu | 7ad5ea3 | 2021-03-22 10:36:35 -0700 | [diff] [blame] | 137 | private var previousPosition: Offset? = null |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 138 | /** |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 139 | * Layout Coordinates of the selection container. |
| 140 | */ |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 141 | var containerLayoutCoordinates: LayoutCoordinates? = null |
| 142 | set(value) { |
| 143 | field = value |
haoyu | 2c6e984 | 2021-03-30 17:39:04 -0700 | [diff] [blame] | 144 | if (hasFocus && selection != null) { |
| 145 | val positionInWindow = value?.positionInWindow() |
| 146 | if (previousPosition != positionInWindow) { |
| 147 | previousPosition = positionInWindow |
| 148 | updateHandleOffsets() |
| 149 | updateSelectionToolbarPosition() |
| 150 | } |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 151 | } |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 152 | } |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 153 | |
| 154 | /** |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 155 | * The beginning position of the drag gesture. Every time a new drag gesture starts, it wil be |
| 156 | * recalculated. |
| 157 | */ |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame^] | 158 | internal var dragBeginPosition by mutableStateOf(Offset.Zero) |
| 159 | private set |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 160 | |
| 161 | /** |
| 162 | * The total distance being dragged of the drag gesture. Every time a new drag gesture starts, |
| 163 | * it will be zeroed out. |
| 164 | */ |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame^] | 165 | internal var dragTotalDistance by mutableStateOf(Offset.Zero) |
| 166 | private set |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 167 | |
| 168 | /** |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 169 | * The calculated position of the start handle in the [SelectionContainer] coordinates. It |
| 170 | * is null when handle shouldn't be displayed. |
| 171 | * It is a [State] so reading it during the composition will cause recomposition every time |
| 172 | * the position has been changed. |
| 173 | */ |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame^] | 174 | var startHandlePosition: Offset? by mutableStateOf(null) |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 175 | private set |
| 176 | |
| 177 | /** |
| 178 | * The calculated position of the end handle in the [SelectionContainer] coordinates. It |
| 179 | * is null when handle shouldn't be displayed. |
| 180 | * It is a [State] so reading it during the composition will cause recomposition every time |
| 181 | * the position has been changed. |
| 182 | */ |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame^] | 183 | var endHandlePosition: Offset? by mutableStateOf(null) |
| 184 | private set |
| 185 | |
| 186 | /** |
| 187 | * The handle that is currently being dragged, or null when no handle is being dragged. |
| 188 | */ |
| 189 | var draggingHandle: Handle? by mutableStateOf(null) |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 190 | private set |
| 191 | |
| 192 | init { |
haoyu | 7ad5ea3 | 2021-03-22 10:36:35 -0700 | [diff] [blame] | 193 | selectionRegistrar.onPositionChangeCallback = { selectableId -> |
| 194 | if ( |
| 195 | selectableId == selection?.start?.selectableId || |
| 196 | selectableId == selection?.end?.selectableId |
| 197 | ) { |
| 198 | updateHandleOffsets() |
| 199 | updateSelectionToolbarPosition() |
| 200 | } |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 203 | selectionRegistrar.onSelectionUpdateStartCallback = |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 204 | { layoutCoordinates, position, selectionMode -> |
| 205 | val positionInContainer = convertToContainerCoordinates( |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 206 | layoutCoordinates, |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 207 | position |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 208 | ) |
| 209 | |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 210 | if (positionInContainer != null) { |
| 211 | startSelection( |
| 212 | position = positionInContainer, |
| 213 | isStartHandle = false, |
| 214 | adjustment = selectionMode |
| 215 | ) |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 216 | |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 217 | focusRequester.requestFocus() |
| 218 | hideSelectionToolbar() |
| 219 | } |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 220 | } |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 221 | |
Qingqing Deng | 6bc981c | 2021-05-11 22:43:51 -0700 | [diff] [blame] | 222 | selectionRegistrar.onSelectionUpdateSelectAll = |
| 223 | { selectableId -> |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 224 | val (newSelection, newSubselection) = selectAll( |
Qingqing Deng | 6bc981c | 2021-05-11 22:43:51 -0700 | [diff] [blame] | 225 | selectableId = selectableId, |
| 226 | previousSelection = selection, |
| 227 | ) |
| 228 | if (newSelection != selection) { |
| 229 | selectionRegistrar.subselections = newSubselection |
| 230 | onSelectionChange(newSelection) |
| 231 | } |
| 232 | |
| 233 | focusRequester.requestFocus() |
| 234 | hideSelectionToolbar() |
| 235 | } |
| 236 | |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 237 | selectionRegistrar.onSelectionUpdateCallback = |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 238 | { layoutCoordinates, newPosition, previousPosition, isStartHandle, selectionMode -> |
| 239 | val newPositionInContainer = |
| 240 | convertToContainerCoordinates(layoutCoordinates, newPosition) |
| 241 | val previousPositionInContainer = |
| 242 | convertToContainerCoordinates(layoutCoordinates, previousPosition) |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 243 | |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 244 | updateSelection( |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 245 | newPosition = newPositionInContainer, |
| 246 | previousPosition = previousPositionInContainer, |
| 247 | isStartHandle = isStartHandle, |
Haoyu Zhang | 4c6c637 | 2021-04-20 10:25:21 -0700 | [diff] [blame] | 248 | adjustment = selectionMode |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 249 | ) |
| 250 | } |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 251 | |
| 252 | selectionRegistrar.onSelectionUpdateEndCallback = { |
| 253 | showSelectionToolbar() |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame^] | 254 | // This property is set by updateSelection while dragging, so we need to clear it after |
| 255 | // the original selection drag. |
| 256 | draggingHandle = null |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 257 | } |
haoyu | 9085c88 | 2020-12-08 12:01:06 -0800 | [diff] [blame] | 258 | |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 259 | selectionRegistrar.onSelectableChangeCallback = { selectableKey -> |
| 260 | if (selectableKey in selectionRegistrar.subselections) { |
haoyu | 9085c88 | 2020-12-08 12:01:06 -0800 | [diff] [blame] | 261 | // clear the selection range of each Selectable. |
| 262 | onRelease() |
| 263 | selection = null |
| 264 | } |
| 265 | } |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 266 | |
| 267 | selectionRegistrar.afterSelectableUnsubscribe = { selectableKey -> |
| 268 | if ( |
| 269 | selectableKey == selection?.start?.selectableId || |
| 270 | selectableKey == selection?.end?.selectableId |
| 271 | ) { |
| 272 | // The selectable that contains a selection handle just unsubscribed. |
| 273 | // Hide selection handles for now |
| 274 | startHandlePosition = null |
| 275 | endHandlePosition = null |
| 276 | } |
| 277 | } |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 278 | } |
| 279 | |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame^] | 280 | /** |
| 281 | * Returns the [Selectable] responsible for managing the given [AnchorInfo], or null if the |
| 282 | * anchor is not from a currently-registered [Selectable]. |
| 283 | */ |
| 284 | internal fun getAnchorSelectable(anchor: AnchorInfo): Selectable? { |
| 285 | return selectionRegistrar.selectableMap[anchor.selectableId] |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 286 | } |
| 287 | |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 288 | private fun updateHandleOffsets() { |
| 289 | val selection = selection |
| 290 | val containerCoordinates = containerLayoutCoordinates |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame^] | 291 | val startSelectable = selection?.start?.let(::getAnchorSelectable) |
| 292 | val endSelectable = selection?.end?.let(::getAnchorSelectable) |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 293 | val startLayoutCoordinates = startSelectable?.getLayoutCoordinates() |
| 294 | val endLayoutCoordinates = endSelectable?.getLayoutCoordinates() |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 295 | if ( |
| 296 | selection == null || |
| 297 | containerCoordinates == null || |
| 298 | !containerCoordinates.isAttached || |
| 299 | startLayoutCoordinates == null || |
| 300 | endLayoutCoordinates == null |
| 301 | ) { |
| 302 | this.startHandlePosition = null |
| 303 | this.endHandlePosition = null |
| 304 | return |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 305 | } |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 306 | |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 307 | val startHandlePosition = containerCoordinates.localPositionOf( |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 308 | startLayoutCoordinates, |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 309 | startSelectable.getHandlePosition( |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 310 | selection = selection, |
| 311 | isStartHandle = true |
| 312 | ) |
| 313 | ) |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 314 | val endHandlePosition = containerCoordinates.localPositionOf( |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 315 | endLayoutCoordinates, |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 316 | endSelectable.getHandlePosition( |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 317 | selection = selection, |
| 318 | isStartHandle = false |
| 319 | ) |
| 320 | ) |
| 321 | |
| 322 | val visibleBounds = containerCoordinates.visibleBounds() |
| 323 | this.startHandlePosition = |
| 324 | if (visibleBounds.containsInclusive(startHandlePosition)) startHandlePosition else null |
| 325 | this.endHandlePosition = |
| 326 | if (visibleBounds.containsInclusive(endHandlePosition)) endHandlePosition else null |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Returns non-nullable [containerLayoutCoordinates]. |
| 331 | */ |
| 332 | internal fun requireContainerCoordinates(): LayoutCoordinates { |
| 333 | val coordinates = containerLayoutCoordinates |
| 334 | require(coordinates != null) |
| 335 | require(coordinates.isAttached) |
| 336 | return coordinates |
| 337 | } |
| 338 | |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 339 | internal fun selectAll( |
| 340 | selectableId: Long, |
| 341 | previousSelection: Selection? |
Qingqing Deng | 6bc981c | 2021-05-11 22:43:51 -0700 | [diff] [blame] | 342 | ): Pair<Selection?, Map<Long, Selection>> { |
| 343 | val subselections = mutableMapOf<Long, Selection>() |
| 344 | val newSelection = selectionRegistrar.sort(requireContainerCoordinates()) |
| 345 | .fastFold(null) { mergedSelection: Selection?, selectable: Selectable -> |
| 346 | val selection = if (selectable.selectableId == selectableId) |
| 347 | selectable.getSelectAllSelection() else null |
| 348 | selection?.let { subselections[selectable.selectableId] = it } |
| 349 | merge(mergedSelection, selection) |
| 350 | } |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 351 | if (newSelection != previousSelection) { |
| 352 | hapticFeedBack?.performHapticFeedback(HapticFeedbackType.TextHandleMove) |
| 353 | } |
Qingqing Deng | 6bc981c | 2021-05-11 22:43:51 -0700 | [diff] [blame] | 354 | return Pair(newSelection, subselections) |
| 355 | } |
| 356 | |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 357 | internal fun getSelectedText(): AnnotatedString? { |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 358 | val selectables = selectionRegistrar.sort(requireContainerCoordinates()) |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 359 | var selectedText: AnnotatedString? = null |
| 360 | |
| 361 | selection?.let { |
Louis Pullen-Freilich | c6e92aa | 2021-03-08 00:42:16 +0000 | [diff] [blame] | 362 | for (i in selectables.indices) { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 363 | val selectable = selectables[i] |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 364 | // Continue if the current selectable is before the selection starts. |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 365 | if (selectable.selectableId != it.start.selectableId && |
| 366 | selectable.selectableId != it.end.selectableId && |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 367 | selectedText == null |
| 368 | ) continue |
| 369 | |
| 370 | val currentSelectedText = getCurrentSelectedText( |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 371 | selectable = selectable, |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 372 | selection = it |
| 373 | ) |
| 374 | selectedText = selectedText?.plus(currentSelectedText) ?: currentSelectedText |
| 375 | |
| 376 | // Break if the current selectable is the last selected selectable. |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 377 | if (selectable.selectableId == it.end.selectableId && !it.handlesCrossed || |
| 378 | selectable.selectableId == it.start.selectableId && it.handlesCrossed |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 379 | ) break |
| 380 | } |
| 381 | } |
| 382 | return selectedText |
| 383 | } |
| 384 | |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 385 | internal fun copy() { |
Qingqing Deng | f2d0a2d | 2020-03-12 14:19:50 -0700 | [diff] [blame] | 386 | val selectedText = getSelectedText() |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 387 | selectedText?.let { clipboardManager?.setText(it) } |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * This function get the selected region as a Rectangle region, and pass it to [TextToolbar] |
| 392 | * to make the FloatingToolbar show up in the proper place. In addition, this function passes |
| 393 | * the copy method as a callback when "copy" is clicked. |
| 394 | */ |
| 395 | internal fun showSelectionToolbar() { |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 396 | if (hasFocus) { |
| 397 | selection?.let { |
| 398 | textToolbar?.showMenu( |
| 399 | getContentRect(), |
| 400 | onCopyRequested = { |
| 401 | copy() |
| 402 | onRelease() |
| 403 | } |
| 404 | ) |
| 405 | } |
Qingqing Deng | a89450d | 2020-04-03 19:11:49 -0700 | [diff] [blame] | 406 | } |
Qingqing Deng | f2d0a2d | 2020-03-12 14:19:50 -0700 | [diff] [blame] | 407 | } |
| 408 | |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 409 | internal fun hideSelectionToolbar() { |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 410 | if (hasFocus && textToolbar?.status == TextToolbarStatus.Shown) { |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 411 | textToolbar?.hide() |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | |
| 415 | private fun updateSelectionToolbarPosition() { |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 416 | if (hasFocus && textToolbar?.status == TextToolbarStatus.Shown) { |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 417 | showSelectionToolbar() |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * Calculate selected region as [Rect]. The top is the top of the first selected |
| 423 | * line, and the bottom is the bottom of the last selected line. The left is the leftmost |
| 424 | * handle's horizontal coordinates, and the right is the rightmost handle's coordinates. |
| 425 | */ |
| 426 | private fun getContentRect(): Rect { |
Nader Jawad | 8432b8d | 2020-07-27 14:51:23 -0700 | [diff] [blame] | 427 | val selection = selection ?: return Rect.Zero |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame^] | 428 | val startSelectable = getAnchorSelectable(selection.start) |
| 429 | val endSelectable = getAnchorSelectable(selection.end) |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 430 | val startLayoutCoordinates = startSelectable?.getLayoutCoordinates() ?: return Rect.Zero |
| 431 | val endLayoutCoordinates = endSelectable?.getLayoutCoordinates() ?: return Rect.Zero |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 432 | |
| 433 | val localLayoutCoordinates = containerLayoutCoordinates |
| 434 | if (localLayoutCoordinates != null && localLayoutCoordinates.isAttached) { |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 435 | var startOffset = localLayoutCoordinates.localPositionOf( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 436 | startLayoutCoordinates, |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 437 | startSelectable.getHandlePosition( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 438 | selection = selection, |
| 439 | isStartHandle = true |
| 440 | ) |
| 441 | ) |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 442 | var endOffset = localLayoutCoordinates.localPositionOf( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 443 | endLayoutCoordinates, |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 444 | endSelectable.getHandlePosition( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 445 | selection = selection, |
| 446 | isStartHandle = false |
| 447 | ) |
| 448 | ) |
| 449 | |
| 450 | startOffset = localLayoutCoordinates.localToRoot(startOffset) |
| 451 | endOffset = localLayoutCoordinates.localToRoot(endOffset) |
| 452 | |
| 453 | val left = min(startOffset.x, endOffset.x) |
| 454 | val right = max(startOffset.x, endOffset.x) |
| 455 | |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 456 | var startTop = localLayoutCoordinates.localPositionOf( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 457 | startLayoutCoordinates, |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 458 | Offset( |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 459 | 0f, |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 460 | startSelectable.getBoundingBox(selection.start.offset).top |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 461 | ) |
| 462 | ) |
| 463 | |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 464 | var endTop = localLayoutCoordinates.localPositionOf( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 465 | endLayoutCoordinates, |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 466 | Offset( |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 467 | 0.0f, |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 468 | endSelectable.getBoundingBox(selection.end.offset).top |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 469 | ) |
| 470 | ) |
| 471 | |
| 472 | startTop = localLayoutCoordinates.localToRoot(startTop) |
| 473 | endTop = localLayoutCoordinates.localToRoot(endTop) |
| 474 | |
| 475 | val top = min(startTop.y, endTop.y) |
Haoyu Zhang | d47f1fe | 2021-07-13 03:10:55 -0700 | [diff] [blame] | 476 | val bottom = max(startOffset.y, endOffset.y) + (HandleHeight.value * 4.0).toFloat() |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 477 | |
| 478 | return Rect( |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 479 | left, |
| 480 | top, |
| 481 | right, |
| 482 | bottom |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 483 | ) |
| 484 | } |
Nader Jawad | 8432b8d | 2020-07-27 14:51:23 -0700 | [diff] [blame] | 485 | return Rect.Zero |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Qingqing Deng | b6f5d8a | 2019-11-11 18:19:22 -0800 | [diff] [blame] | 488 | // This is for PressGestureDetector to cancel the selection. |
| 489 | fun onRelease() { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 490 | selectionRegistrar.subselections = emptyMap() |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 491 | hideSelectionToolbar() |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 492 | if (selection != null) { |
| 493 | onSelectionChange(null) |
| 494 | hapticFeedBack?.performHapticFeedback(HapticFeedbackType.TextHandleMove) |
| 495 | } |
Qingqing Deng | b6f5d8a | 2019-11-11 18:19:22 -0800 | [diff] [blame] | 496 | } |
| 497 | |
Matvei Malkov | f770a91 | 2021-03-24 18:12:41 +0000 | [diff] [blame] | 498 | fun handleDragObserver(isStartHandle: Boolean): TextDragObserver { |
| 499 | return object : TextDragObserver { |
| 500 | override fun onStart(startPoint: Offset) { |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 501 | hideSelectionToolbar() |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 502 | val selection = selection!! |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 503 | val startSelectable = |
| 504 | selectionRegistrar.selectableMap[selection.start.selectableId] |
| 505 | val endSelectable = |
| 506 | selectionRegistrar.selectableMap[selection.end.selectableId] |
Louis Pullen-Freilich | 5da28bd | 2019-10-15 17:05:07 +0100 | [diff] [blame] | 507 | // The LayoutCoordinates of the composable where the drag gesture should begin. This |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 508 | // is used to convert the position of the beginning of the drag gesture from the |
Louis Pullen-Freilich | 5da28bd | 2019-10-15 17:05:07 +0100 | [diff] [blame] | 509 | // composable coordinates to selection container coordinates. |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 510 | val beginLayoutCoordinates = if (isStartHandle) { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 511 | startSelectable?.getLayoutCoordinates()!! |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 512 | } else { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 513 | endSelectable?.getLayoutCoordinates()!! |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 516 | // The position of the character where the drag gesture should begin. This is in |
Louis Pullen-Freilich | 5da28bd | 2019-10-15 17:05:07 +0100 | [diff] [blame] | 517 | // the composable coordinates. |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 518 | val beginCoordinates = getAdjustedCoordinates( |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 519 | if (isStartHandle) { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 520 | startSelectable!!.getHandlePosition( |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 521 | selection = selection, isStartHandle = true |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 522 | ) |
| 523 | } else { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 524 | endSelectable!!.getHandlePosition( |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 525 | selection = selection, isStartHandle = false |
| 526 | ) |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 527 | } |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 528 | ) |
| 529 | |
Louis Pullen-Freilich | 5da28bd | 2019-10-15 17:05:07 +0100 | [diff] [blame] | 530 | // Convert the position where drag gesture begins from composable coordinates to |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 531 | // selection container coordinates. |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 532 | dragBeginPosition = requireContainerCoordinates().localPositionOf( |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 533 | beginLayoutCoordinates, |
| 534 | beginCoordinates |
| 535 | ) |
| 536 | |
| 537 | // Zero out the total distance that being dragged. |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 538 | dragTotalDistance = Offset.Zero |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Matvei Malkov | f770a91 | 2021-03-24 18:12:41 +0000 | [diff] [blame] | 541 | override fun onDrag(delta: Offset) { |
Matvei Malkov | f770a91 | 2021-03-24 18:12:41 +0000 | [diff] [blame] | 542 | dragTotalDistance += delta |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 543 | val endPosition = dragBeginPosition + dragTotalDistance |
| 544 | val consumed = updateSelection( |
| 545 | newPosition = endPosition, |
| 546 | previousPosition = dragBeginPosition, |
Haoyu Zhang | 5740343b | 2021-07-15 11:29:27 -0700 | [diff] [blame] | 547 | isStartHandle = isStartHandle, |
Haoyu Zhang | a78608e | 2021-08-03 17:10:18 -0700 | [diff] [blame] | 548 | adjustment = SelectionAdjustment.CharacterWithWordAccelerate |
Qingqing Deng | a5d8095 | 2019-10-11 16:46:52 -0700 | [diff] [blame] | 549 | ) |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 550 | if (consumed) { |
| 551 | dragBeginPosition = endPosition |
| 552 | dragTotalDistance = Offset.Zero |
| 553 | } |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 554 | } |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 555 | |
Matvei Malkov | f770a91 | 2021-03-24 18:12:41 +0000 | [diff] [blame] | 556 | override fun onStop() { |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 557 | showSelectionToolbar() |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame^] | 558 | draggingHandle = null |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | override fun onCancel() { |
| 562 | showSelectionToolbar() |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame^] | 563 | draggingHandle = null |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 564 | } |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 565 | } |
| 566 | } |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 567 | |
Ralston Da Silva | de62bc6 | 2021-06-02 17:46:44 -0700 | [diff] [blame] | 568 | /** |
| 569 | * Detect tap without consuming the up event. |
| 570 | */ |
| 571 | private suspend fun PointerInputScope.detectNonConsumingTap(onTap: (Offset) -> Unit) { |
| 572 | forEachGesture { |
| 573 | coroutineScope { |
| 574 | awaitPointerEventScope { |
| 575 | waitForUpOrCancellation()?.let { |
| 576 | onTap(it.position) |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | private fun Modifier.onClearSelectionRequested(block: () -> Unit): Modifier { |
| 584 | return if (hasFocus) pointerInput(Unit) { detectNonConsumingTap { block() } } else this |
| 585 | } |
| 586 | |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 587 | private fun convertToContainerCoordinates( |
| 588 | layoutCoordinates: LayoutCoordinates, |
| 589 | offset: Offset |
| 590 | ): Offset? { |
| 591 | val coordinates = containerLayoutCoordinates |
| 592 | if (coordinates == null || !coordinates.isAttached) return null |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 593 | return requireContainerCoordinates().localPositionOf(layoutCoordinates, offset) |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 596 | /** |
| 597 | * Cancel the previous selection and start a new selection at the given [position]. |
| 598 | * It's used for long-press, double-click, triple-click and so on to start selection. |
| 599 | * |
| 600 | * @param position initial position of the selection. Both start and end handle is considered |
| 601 | * at this position. |
| 602 | * @param isStartHandle whether it's considered as the start handle moving. This parameter |
| 603 | * will influence the [SelectionAdjustment]'s behavior. For example, |
| 604 | * [SelectionAdjustment.Character] only adjust the moving handle. |
| 605 | * @param adjustment the selection adjustment. |
| 606 | */ |
| 607 | private fun startSelection( |
| 608 | position: Offset, |
| 609 | isStartHandle: Boolean, |
| 610 | adjustment: SelectionAdjustment |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 611 | ) { |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 612 | updateSelection( |
| 613 | startHandlePosition = position, |
| 614 | endHandlePosition = position, |
| 615 | previousHandlePosition = null, |
| 616 | isStartHandle = isStartHandle, |
| 617 | adjustment = adjustment |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 618 | ) |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 619 | } |
Haoyu Zhang | a78608e | 2021-08-03 17:10:18 -0700 | [diff] [blame] | 620 | |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 621 | /** |
| 622 | * Updates the selection after one of the selection handle moved. |
| 623 | * |
| 624 | * @param newPosition the new position of the moving selection handle. |
| 625 | * @param previousPosition the previous position of the moving selection handle. |
| 626 | * @param isStartHandle whether the moving selection handle is the start handle. |
| 627 | * @param adjustment the [SelectionAdjustment] used to adjust the raw selection range and |
| 628 | * produce the final selection range. |
| 629 | * |
| 630 | * @return a boolean representing whether the movement is consumed. |
| 631 | * |
| 632 | * @see SelectionAdjustment |
| 633 | */ |
| 634 | internal fun updateSelection( |
| 635 | newPosition: Offset?, |
| 636 | previousPosition: Offset?, |
| 637 | isStartHandle: Boolean, |
| 638 | adjustment: SelectionAdjustment, |
| 639 | ): Boolean { |
| 640 | if (newPosition == null) return false |
| 641 | val otherHandlePosition = selection?.let { selection -> |
| 642 | val otherSelectableId = if (isStartHandle) { |
| 643 | selection.end.selectableId |
| 644 | } else { |
| 645 | selection.start.selectableId |
| 646 | } |
| 647 | val otherSelectable = |
| 648 | selectionRegistrar.selectableMap[otherSelectableId] ?: return@let null |
| 649 | convertToContainerCoordinates( |
| 650 | otherSelectable.getLayoutCoordinates()!!, |
| 651 | getAdjustedCoordinates( |
| 652 | otherSelectable.getHandlePosition(selection, !isStartHandle) |
| 653 | ) |
Haoyu Zhang | a78608e | 2021-08-03 17:10:18 -0700 | [diff] [blame] | 654 | ) |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 655 | } ?: return false |
| 656 | |
| 657 | val startHandlePosition = if (isStartHandle) newPosition else otherHandlePosition |
| 658 | val endHandlePosition = if (isStartHandle) otherHandlePosition else newPosition |
| 659 | |
| 660 | return updateSelection( |
| 661 | startHandlePosition = startHandlePosition, |
| 662 | endHandlePosition = endHandlePosition, |
| 663 | previousHandlePosition = previousPosition, |
| 664 | isStartHandle = isStartHandle, |
| 665 | adjustment = adjustment |
| 666 | ) |
| 667 | } |
| 668 | |
| 669 | /** |
| 670 | * Updates the selection after one of the selection handle moved. |
| 671 | * |
| 672 | * To make sure that [SelectionAdjustment] works correctly, it's expected that only one |
| 673 | * selection handle is updated each time. The only exception is that when a new selection is |
| 674 | * started. In this case, [previousHandlePosition] is always null. |
| 675 | * |
| 676 | * @param startHandlePosition the position of the start selection handle. |
| 677 | * @param endHandlePosition the position of the end selection handle. |
| 678 | * @param previousHandlePosition the position of the moving handle before the update. |
| 679 | * @param isStartHandle whether the moving selection handle is the start handle. |
| 680 | * @param adjustment the [SelectionAdjustment] used to adjust the raw selection range and |
| 681 | * produce the final selection range. |
| 682 | * |
| 683 | * @return a boolean representing whether the movement is consumed. It's useful for the case |
| 684 | * where a selection handle is updating consecutively. When the return value is true, it's |
| 685 | * expected that the caller will update the [startHandlePosition] to be the given |
| 686 | * [endHandlePosition] in following calls. |
| 687 | * |
| 688 | * @see SelectionAdjustment |
| 689 | */ |
| 690 | internal fun updateSelection( |
| 691 | startHandlePosition: Offset, |
| 692 | endHandlePosition: Offset, |
| 693 | previousHandlePosition: Offset?, |
| 694 | isStartHandle: Boolean, |
| 695 | adjustment: SelectionAdjustment, |
| 696 | ): Boolean { |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame^] | 697 | // TODO(b/206833278) This should be set as soon as down event happens, not wait for drag. |
| 698 | draggingHandle = if (isStartHandle) Handle.SelectionStart else Handle.SelectionEnd |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 699 | val newSubselections = mutableMapOf<Long, Selection>() |
| 700 | var moveConsumed = false |
| 701 | val newSelection = selectionRegistrar.sort(requireContainerCoordinates()) |
| 702 | .fastFold(null) { mergedSelection: Selection?, selectable: Selectable -> |
| 703 | val previousSubselection = |
| 704 | selectionRegistrar.subselections[selectable.selectableId] |
| 705 | val (selection, consumed) = selectable.updateSelection( |
| 706 | startHandlePosition = startHandlePosition, |
| 707 | endHandlePosition = endHandlePosition, |
| 708 | previousHandlePosition = previousHandlePosition, |
| 709 | isStartHandle = isStartHandle, |
| 710 | containerLayoutCoordinates = requireContainerCoordinates(), |
| 711 | adjustment = adjustment, |
| 712 | previousSelection = previousSubselection, |
| 713 | ) |
| 714 | |
| 715 | moveConsumed = moveConsumed || consumed |
| 716 | selection?.let { newSubselections[selectable.selectableId] = it } |
| 717 | merge(mergedSelection, selection) |
| 718 | } |
| 719 | if (newSelection != selection) { |
| 720 | hapticFeedBack?.performHapticFeedback(HapticFeedbackType.TextHandleMove) |
| 721 | selectionRegistrar.subselections = newSubselections |
Haoyu Zhang | a78608e | 2021-08-03 17:10:18 -0700 | [diff] [blame] | 722 | onSelectionChange(newSelection) |
| 723 | } |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 724 | return moveConsumed |
Haoyu Zhang | a78608e | 2021-08-03 17:10:18 -0700 | [diff] [blame] | 725 | } |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Andrey Rudenko | 5dc7aad | 2020-09-18 10:33:37 +0200 | [diff] [blame] | 728 | internal fun merge(lhs: Selection?, rhs: Selection?): Selection? { |
Siyamed Sinir | 700df845 | 2019-10-22 20:23:58 -0700 | [diff] [blame] | 729 | return lhs?.merge(rhs) ?: rhs |
| 730 | } |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 731 | |
Andrey Rudenko | 8b4981e | 2021-01-29 15:31:59 +0100 | [diff] [blame] | 732 | internal expect fun isCopyKeyEvent(keyEvent: KeyEvent): Boolean |
| 733 | |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame^] | 734 | internal expect fun Modifier.selectionMagnifier(manager: SelectionManager): Modifier |
| 735 | |
| 736 | internal fun calculateSelectionMagnifierCenterAndroid(manager: SelectionManager): Offset { |
| 737 | fun getMagnifierCenter(anchor: AnchorInfo, isStartHandle: Boolean): Offset { |
| 738 | // TODO(b/206833278) Clamp x to the end of the selectable area, hide after a threshold. |
| 739 | // TODO(b/206833278) Animate x when jumping lines. |
| 740 | // TODO(b/206833278) X should track drag exactly, not clamp to cursor positions. |
| 741 | |
| 742 | // Let the selectable determine the vertical position of the magnifier, since it should be |
| 743 | // clamped to the center of text lines. |
| 744 | val selectable = manager.getAnchorSelectable(anchor) ?: return Offset.Unspecified |
| 745 | // The end offset is exclusive. |
| 746 | val selectionOffset = if (isStartHandle) anchor.offset else anchor.offset - 1 |
| 747 | val bounds = selectable.getBoundingBox(selectionOffset) |
| 748 | // Don't need to account for Rtl here because the bounds will already be adjusted for that. |
| 749 | // (i.e. in Rtl, left > right) |
| 750 | val localCenter = if (isStartHandle) bounds.centerLeft else bounds.centerRight |
| 751 | val containerCoordinates = manager.containerLayoutCoordinates ?: return Offset.Unspecified |
| 752 | return containerCoordinates.localPositionOf( |
| 753 | sourceCoordinates = selectable.getLayoutCoordinates() ?: return Offset.Unspecified, |
| 754 | relativeToSource = localCenter |
| 755 | ) |
| 756 | } |
| 757 | |
| 758 | val selection = manager.selection ?: return Offset.Unspecified |
| 759 | return when (manager.draggingHandle) { |
| 760 | null -> return Offset.Unspecified |
| 761 | Handle.SelectionStart -> getMagnifierCenter(selection.start, isStartHandle = true) |
| 762 | Handle.SelectionEnd -> getMagnifierCenter(selection.end, isStartHandle = false) |
| 763 | Handle.Cursor -> error("SelectionContainer does not support cursor") |
| 764 | } |
| 765 | } |
| 766 | |
Andrey Rudenko | 5dc7aad | 2020-09-18 10:33:37 +0200 | [diff] [blame] | 767 | internal fun getCurrentSelectedText( |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 768 | selectable: Selectable, |
| 769 | selection: Selection |
| 770 | ): AnnotatedString { |
| 771 | val currentText = selectable.getText() |
| 772 | |
| 773 | return if ( |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 774 | selectable.selectableId != selection.start.selectableId && |
| 775 | selectable.selectableId != selection.end.selectableId |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 776 | ) { |
| 777 | // Select the full text content if the current selectable is between the |
| 778 | // start and the end selectables. |
| 779 | currentText |
| 780 | } else if ( |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 781 | selectable.selectableId == selection.start.selectableId && |
| 782 | selectable.selectableId == selection.end.selectableId |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 783 | ) { |
| 784 | // Select partial text content if the current selectable is the start and |
| 785 | // the end selectable. |
| 786 | if (selection.handlesCrossed) { |
| 787 | currentText.subSequence(selection.end.offset, selection.start.offset) |
| 788 | } else { |
| 789 | currentText.subSequence(selection.start.offset, selection.end.offset) |
| 790 | } |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 791 | } else if (selectable.selectableId == selection.start.selectableId) { |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 792 | // Select partial text content if the current selectable is the start |
| 793 | // selectable. |
| 794 | if (selection.handlesCrossed) { |
| 795 | currentText.subSequence(0, selection.start.offset) |
| 796 | } else { |
| 797 | currentText.subSequence(selection.start.offset, currentText.length) |
| 798 | } |
| 799 | } else { |
| 800 | // Selectable partial text content if the current selectable is the end |
| 801 | // selectable. |
| 802 | if (selection.handlesCrossed) { |
| 803 | currentText.subSequence(selection.end.offset, currentText.length) |
| 804 | } else { |
| 805 | currentText.subSequence(0, selection.end.offset) |
| 806 | } |
| 807 | } |
| 808 | } |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 809 | |
| 810 | /** Returns the boundary of the visible area in this [LayoutCoordinates]. */ |
haoyu | ac341f0 | 2021-01-22 22:01:56 -0800 | [diff] [blame] | 811 | internal fun LayoutCoordinates.visibleBounds(): Rect { |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 812 | // globalBounds is the global boundaries of this LayoutCoordinates after it's clipped by |
| 813 | // parents. We can think it as the global visible bounds of this Layout. Here globalBounds |
| 814 | // is convert to local, which is the boundary of the visible area within the LayoutCoordinates. |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 815 | val boundsInWindow = boundsInWindow() |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 816 | return Rect( |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 817 | windowToLocal(boundsInWindow.topLeft), |
| 818 | windowToLocal(boundsInWindow.bottomRight) |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 819 | ) |
| 820 | } |
| 821 | |
haoyu | ac341f0 | 2021-01-22 22:01:56 -0800 | [diff] [blame] | 822 | internal fun Rect.containsInclusive(offset: Offset): Boolean = |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 823 | offset.x in left..right && offset.y in top..bottom |