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