Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Android Open Source Project |
| 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 | |
Siyamed Sinir | fd8bc42 | 2019-11-21 18:23:58 -0800 | [diff] [blame] | 17 | package androidx.ui.core.selection |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 18 | |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 19 | import androidx.ui.core.LayoutCoordinates |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 20 | import androidx.ui.core.gesture.DragObserver |
Qingqing Deng | bf370e7 | 2019-10-17 11:14:39 -0700 | [diff] [blame] | 21 | import androidx.ui.core.gesture.LongPressDragObserver |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 22 | import androidx.ui.core.hapticfeedback.HapticFeedback |
| 23 | import androidx.ui.core.hapticfeedback.HapticFeedbackType |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame^] | 24 | import androidx.ui.text.AnnotatedString |
| 25 | import androidx.ui.text.length |
| 26 | import androidx.ui.text.subSequence |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 27 | import androidx.ui.unit.PxPosition |
| 28 | import androidx.ui.unit.px |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 29 | |
Qingqing Deng | 35f97ea | 2019-09-18 19:24:37 -0700 | [diff] [blame] | 30 | /** |
Louis Pullen-Freilich | 5da28bd | 2019-10-15 17:05:07 +0100 | [diff] [blame] | 31 | * 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] | 32 | */ |
Siyamed Sinir | 700df845 | 2019-10-22 20:23:58 -0700 | [diff] [blame] | 33 | internal class SelectionManager(private val selectionRegistrar: SelectionRegistrarImpl) { |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 34 | /** |
| 35 | * The current selection. |
| 36 | */ |
| 37 | var selection: Selection? = null |
| 38 | |
| 39 | /** |
| 40 | * The manager will invoke this every time it comes to the conclusion that the selection should |
| 41 | * change. The expectation is that this callback will end up causing `setSelection` to get |
| 42 | * called. This is what makes this a "controlled component". |
| 43 | */ |
| 44 | var onSelectionChange: (Selection?) -> Unit = {} |
| 45 | |
| 46 | /** |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 47 | * [HapticFeedback] handle to perform haptic feedback. |
| 48 | */ |
| 49 | var hapticFeedBack: HapticFeedback? = null |
| 50 | |
| 51 | /** |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 52 | * Layout Coordinates of the selection container. |
| 53 | */ |
| 54 | lateinit var containerLayoutCoordinates: LayoutCoordinates |
| 55 | |
| 56 | /** |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 57 | * The beginning position of the drag gesture. Every time a new drag gesture starts, it wil be |
| 58 | * recalculated. |
| 59 | */ |
| 60 | private var dragBeginPosition = PxPosition.Origin |
| 61 | |
| 62 | /** |
| 63 | * The total distance being dragged of the drag gesture. Every time a new drag gesture starts, |
| 64 | * it will be zeroed out. |
| 65 | */ |
| 66 | private var dragTotalDistance = PxPosition.Origin |
| 67 | |
| 68 | /** |
Qingqing Deng | 0cb86fe | 2019-07-16 15:36:27 -0700 | [diff] [blame] | 69 | * A flag to check if the selection start or end handle is being dragged. |
| 70 | * If this value is true, then onPress will not select any text. |
| 71 | * This value will be set to true when either handle is being dragged, and be reset to false |
| 72 | * when the dragging is stopped. |
| 73 | */ |
| 74 | private var draggingHandle = false |
| 75 | |
| 76 | /** |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 77 | * Iterates over the handlers, gets the selection for each Composable, and merges all the |
| 78 | * returned [Selection]s. |
| 79 | * |
| 80 | * @param startPosition [PxPosition] for the start of the selection |
| 81 | * @param endPosition [PxPosition] for the end of the selection |
Siyamed Sinir | 0100f12 | 2019-11-16 00:23:12 -0800 | [diff] [blame] | 82 | * @param longPress the selection is a result of long press |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 83 | * @param previousSelection previous selection |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 84 | * |
| 85 | * @return [Selection] object which is constructed by combining all Composables that are |
| 86 | * selected. |
| 87 | */ |
Qingqing Deng | 5819ff6 | 2019-11-18 15:26:23 -0800 | [diff] [blame] | 88 | // This function is internal for testing purposes. |
| 89 | internal fun mergeSelections( |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 90 | startPosition: PxPosition, |
| 91 | endPosition: PxPosition, |
Siyamed Sinir | 0100f12 | 2019-11-16 00:23:12 -0800 | [diff] [blame] | 92 | longPress: Boolean = false, |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 93 | previousSelection: Selection? = null, |
Qingqing Deng | ff65ed6 | 2020-01-06 17:55:48 -0800 | [diff] [blame] | 94 | isStartHandle: Boolean = true |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 95 | ): Selection? { |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 96 | |
Qingqing Deng | 247f2b4 | 2019-12-12 19:48:37 -0800 | [diff] [blame] | 97 | val newSelection = selectionRegistrar.sort(containerLayoutCoordinates) |
| 98 | .fold(null) { mergedSelection: Selection?, |
| 99 | handler: Selectable -> |
| 100 | merge( |
| 101 | mergedSelection, |
| 102 | handler.getSelection( |
| 103 | startPosition = startPosition, |
| 104 | endPosition = endPosition, |
| 105 | containerLayoutCoordinates = containerLayoutCoordinates, |
| 106 | longPress = longPress, |
| 107 | previousSelection = previousSelection, |
| 108 | isStartHandle = isStartHandle |
| 109 | ) |
Siyamed Sinir | 700df845 | 2019-10-22 20:23:58 -0700 | [diff] [blame] | 110 | ) |
Qingqing Deng | 247f2b4 | 2019-12-12 19:48:37 -0800 | [diff] [blame] | 111 | } |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 112 | if (previousSelection != newSelection) hapticFeedBack?.performHapticFeedback( |
| 113 | HapticFeedbackType.TextHandleMove |
| 114 | ) |
| 115 | return newSelection |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame^] | 118 | internal fun getSelectedText(): AnnotatedString? { |
| 119 | val selectables = selectionRegistrar.sort(containerLayoutCoordinates) |
| 120 | var selectedText: AnnotatedString? = null |
| 121 | |
| 122 | selection?.let { |
| 123 | for (handler in selectables) { |
| 124 | // Continue if the current selectable is before the selection starts. |
| 125 | if (handler != it.start.selectable && handler != it.end.selectable && |
| 126 | selectedText == null |
| 127 | ) continue |
| 128 | |
| 129 | val currentSelectedText = getCurrentSelectedText( |
| 130 | selectable = handler, |
| 131 | selection = it |
| 132 | ) |
| 133 | selectedText = selectedText?.plus(currentSelectedText) ?: currentSelectedText |
| 134 | |
| 135 | // Break if the current selectable is the last selected selectable. |
| 136 | if (handler == it.end.selectable && !it.handlesCrossed || |
| 137 | handler == it.start.selectable && it.handlesCrossed |
| 138 | ) break |
| 139 | } |
| 140 | } |
| 141 | return selectedText |
| 142 | } |
| 143 | |
Qingqing Deng | b6f5d8a | 2019-11-11 18:19:22 -0800 | [diff] [blame] | 144 | // This is for PressGestureDetector to cancel the selection. |
| 145 | fun onRelease() { |
| 146 | // Call mergeSelections with an out of boundary input to inform all text widgets to |
| 147 | // cancel their individual selection. |
Qingqing Deng | a5d8095 | 2019-10-11 16:46:52 -0700 | [diff] [blame] | 148 | mergeSelections( |
Siyamed Sinir | 0100f12 | 2019-11-16 00:23:12 -0800 | [diff] [blame] | 149 | startPosition = PxPosition((-1).px, (-1).px), |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 150 | endPosition = PxPosition((-1).px, (-1).px), |
| 151 | previousSelection = selection |
Siyamed Sinir | 0100f12 | 2019-11-16 00:23:12 -0800 | [diff] [blame] | 152 | ) |
Siyamed Sinir | e810eab | 2019-11-22 12:36:38 -0800 | [diff] [blame] | 153 | if (selection != null) onSelectionChange(null) |
Qingqing Deng | b6f5d8a | 2019-11-11 18:19:22 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Siyamed Sinir | e810eab | 2019-11-22 12:36:38 -0800 | [diff] [blame] | 156 | val longPressDragObserver = object : LongPressDragObserver { |
| 157 | override fun onLongPress(pxPosition: PxPosition) { |
| 158 | if (draggingHandle) return |
| 159 | val newSelection = mergeSelections( |
| 160 | startPosition = pxPosition, |
| 161 | endPosition = pxPosition, |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 162 | longPress = true, |
| 163 | previousSelection = selection |
Siyamed Sinir | e810eab | 2019-11-22 12:36:38 -0800 | [diff] [blame] | 164 | ) |
| 165 | if (newSelection != selection) onSelectionChange(newSelection) |
| 166 | dragBeginPosition = pxPosition |
Siyamed Sinir | 700df845 | 2019-10-22 20:23:58 -0700 | [diff] [blame] | 167 | } |
Siyamed Sinir | 700df845 | 2019-10-22 20:23:58 -0700 | [diff] [blame] | 168 | |
Siyamed Sinir | e810eab | 2019-11-22 12:36:38 -0800 | [diff] [blame] | 169 | override fun onDragStart() { |
| 170 | super.onDragStart() |
| 171 | // selection never started |
| 172 | if (selection == null) return |
| 173 | // Zero out the total distance that being dragged. |
| 174 | dragTotalDistance = PxPosition.Origin |
| 175 | } |
| 176 | |
| 177 | override fun onDrag(dragDistance: PxPosition): PxPosition { |
| 178 | // selection never started, did not consume any drag |
| 179 | if (selection == null) return PxPosition.Origin |
| 180 | |
| 181 | dragTotalDistance += dragDistance |
| 182 | val newSelection = mergeSelections( |
| 183 | startPosition = dragBeginPosition, |
| 184 | endPosition = dragBeginPosition + dragTotalDistance, |
Qingqing Deng | 74baaa2 | 2019-12-12 13:28:25 -0800 | [diff] [blame] | 185 | longPress = true, |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 186 | previousSelection = selection |
Siyamed Sinir | e810eab | 2019-11-22 12:36:38 -0800 | [diff] [blame] | 187 | ) |
| 188 | |
| 189 | if (newSelection != selection) onSelectionChange(newSelection) |
| 190 | return dragDistance |
| 191 | } |
| 192 | } |
| 193 | |
Qingqing Deng | 13743f7 | 2019-07-15 15:00:45 -0700 | [diff] [blame] | 194 | /** |
| 195 | * Adjust coordinates for given text offset. |
| 196 | * |
| 197 | * Currently [android.text.Layout.getLineBottom] returns y coordinates of the next |
| 198 | * line's top offset, which is not included in current line's hit area. To be able to |
| 199 | * hit current line, move up this y coordinates by 1 pixel. |
| 200 | */ |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 201 | private fun getAdjustedCoordinates(position: PxPosition): PxPosition { |
| 202 | return PxPosition(position.x, position.y - 1.px) |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 205 | fun handleDragObserver(isStartHandle: Boolean): DragObserver { |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 206 | return object : DragObserver { |
Andrey Kulikov | 0e6c40f | 2019-09-06 18:33:14 +0100 | [diff] [blame] | 207 | override fun onStart(downPosition: PxPosition) { |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 208 | val selection = selection!! |
Louis Pullen-Freilich | 5da28bd | 2019-10-15 17:05:07 +0100 | [diff] [blame] | 209 | // The LayoutCoordinates of the composable where the drag gesture should begin. This |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 210 | // 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] | 211 | // composable coordinates to selection container coordinates. |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 212 | val beginLayoutCoordinates = if (isStartHandle) { |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 213 | selection.start.selectable.getLayoutCoordinates()!! |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 214 | } else { |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 215 | selection.end.selectable.getLayoutCoordinates()!! |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 218 | // 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] | 219 | // the composable coordinates. |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 220 | val beginCoordinates = getAdjustedCoordinates( |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 221 | if (isStartHandle) |
| 222 | selection.start.selectable.getHandlePosition( |
| 223 | selection = selection, isStartHandle = true |
| 224 | ) else |
| 225 | selection.end.selectable.getHandlePosition( |
| 226 | selection = selection, isStartHandle = false |
| 227 | ) |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 228 | ) |
| 229 | |
Louis Pullen-Freilich | 5da28bd | 2019-10-15 17:05:07 +0100 | [diff] [blame] | 230 | // Convert the position where drag gesture begins from composable coordinates to |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 231 | // selection container coordinates. |
| 232 | dragBeginPosition = containerLayoutCoordinates.childToLocal( |
| 233 | beginLayoutCoordinates, |
| 234 | beginCoordinates |
| 235 | ) |
| 236 | |
| 237 | // Zero out the total distance that being dragged. |
| 238 | dragTotalDistance = PxPosition.Origin |
Qingqing Deng | 0cb86fe | 2019-07-16 15:36:27 -0700 | [diff] [blame] | 239 | draggingHandle = true |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | override fun onDrag(dragDistance: PxPosition): PxPosition { |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 243 | val selection = selection!! |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 244 | dragTotalDistance += dragDistance |
| 245 | |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 246 | val currentStart = if (isStartHandle) { |
| 247 | dragBeginPosition + dragTotalDistance |
| 248 | } else { |
| 249 | containerLayoutCoordinates.childToLocal( |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 250 | selection.start.selectable.getLayoutCoordinates()!!, |
| 251 | getAdjustedCoordinates( |
| 252 | selection.start.selectable.getHandlePosition( |
| 253 | selection = selection, |
| 254 | isStartHandle = true |
| 255 | ) |
| 256 | ) |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 257 | ) |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 258 | } |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 259 | |
| 260 | val currentEnd = if (isStartHandle) { |
| 261 | containerLayoutCoordinates.childToLocal( |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 262 | selection.end.selectable.getLayoutCoordinates()!!, |
| 263 | getAdjustedCoordinates( |
| 264 | selection.end.selectable.getHandlePosition( |
| 265 | selection = selection, |
| 266 | isStartHandle = false |
| 267 | ) |
| 268 | ) |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 269 | ) |
| 270 | } else { |
| 271 | dragBeginPosition + dragTotalDistance |
| 272 | } |
| 273 | |
Qingqing Deng | a5d8095 | 2019-10-11 16:46:52 -0700 | [diff] [blame] | 274 | val finalSelection = mergeSelections( |
| 275 | startPosition = currentStart, |
| 276 | endPosition = currentEnd, |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 277 | previousSelection = selection, |
Qingqing Deng | ff65ed6 | 2020-01-06 17:55:48 -0800 | [diff] [blame] | 278 | isStartHandle = isStartHandle |
Qingqing Deng | a5d8095 | 2019-10-11 16:46:52 -0700 | [diff] [blame] | 279 | ) |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 280 | onSelectionChange(finalSelection) |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 281 | return dragDistance |
| 282 | } |
Qingqing Deng | 0cb86fe | 2019-07-16 15:36:27 -0700 | [diff] [blame] | 283 | |
| 284 | override fun onStop(velocity: PxPosition) { |
| 285 | super.onStop(velocity) |
| 286 | draggingHandle = false |
| 287 | } |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
Siyamed Sinir | 700df845 | 2019-10-22 20:23:58 -0700 | [diff] [blame] | 292 | private fun merge(lhs: Selection?, rhs: Selection?): Selection? { |
| 293 | return lhs?.merge(rhs) ?: rhs |
| 294 | } |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame^] | 295 | |
| 296 | private fun getCurrentSelectedText( |
| 297 | selectable: Selectable, |
| 298 | selection: Selection |
| 299 | ): AnnotatedString { |
| 300 | val currentText = selectable.getText() |
| 301 | |
| 302 | return if ( |
| 303 | selectable != selection.start.selectable && |
| 304 | selectable != selection.end.selectable |
| 305 | ) { |
| 306 | // Select the full text content if the current selectable is between the |
| 307 | // start and the end selectables. |
| 308 | currentText |
| 309 | } else if ( |
| 310 | selectable == selection.start.selectable && |
| 311 | selectable == selection.end.selectable |
| 312 | ) { |
| 313 | // Select partial text content if the current selectable is the start and |
| 314 | // the end selectable. |
| 315 | if (selection.handlesCrossed) { |
| 316 | currentText.subSequence(selection.end.offset, selection.start.offset) |
| 317 | } else { |
| 318 | currentText.subSequence(selection.start.offset, selection.end.offset) |
| 319 | } |
| 320 | } else if (selectable == selection.start.selectable) { |
| 321 | // Select partial text content if the current selectable is the start |
| 322 | // selectable. |
| 323 | if (selection.handlesCrossed) { |
| 324 | currentText.subSequence(0, selection.start.offset) |
| 325 | } else { |
| 326 | currentText.subSequence(selection.start.offset, currentText.length) |
| 327 | } |
| 328 | } else { |
| 329 | // Selectable partial text content if the current selectable is the end |
| 330 | // selectable. |
| 331 | if (selection.handlesCrossed) { |
| 332 | currentText.subSequence(selection.end.offset, currentText.length) |
| 333 | } else { |
| 334 | currentText.subSequence(0, selection.end.offset) |
| 335 | } |
| 336 | } |
| 337 | } |