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 | |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 19 | import androidx.compose.State |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 20 | import androidx.compose.getValue |
| 21 | import androidx.compose.mutableStateOf |
| 22 | import androidx.compose.setValue |
Chuck Jazdzewski | 0a90de9 | 2020-05-21 10:03:47 -0700 | [diff] [blame] | 23 | import androidx.compose.structuralEqualityPolicy |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 24 | import androidx.ui.core.LayoutCoordinates |
Qingqing Deng | f2d0a2d | 2020-03-12 14:19:50 -0700 | [diff] [blame] | 25 | import androidx.ui.core.clipboard.ClipboardManager |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 26 | import androidx.ui.core.gesture.DragObserver |
Qingqing Deng | bf370e7 | 2019-10-17 11:14:39 -0700 | [diff] [blame] | 27 | import androidx.ui.core.gesture.LongPressDragObserver |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 28 | import androidx.ui.core.hapticfeedback.HapticFeedback |
| 29 | import androidx.ui.core.hapticfeedback.HapticFeedbackType |
Qingqing Deng | a89450d | 2020-04-03 19:11:49 -0700 | [diff] [blame] | 30 | import androidx.ui.core.texttoolbar.TextToolbar |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 31 | import androidx.ui.core.texttoolbar.TextToolbarStatus |
Qingqing Deng | a89450d | 2020-04-03 19:11:49 -0700 | [diff] [blame] | 32 | import androidx.ui.geometry.Rect |
Louis Pullen-Freilich | ab19475 | 2020-07-21 22:21:22 +0100 | [diff] [blame^] | 33 | import androidx.compose.ui.text.AnnotatedString |
| 34 | import androidx.compose.ui.text.length |
| 35 | import androidx.compose.ui.text.subSequence |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 36 | import androidx.ui.geometry.Offset |
Louis Pullen-Freilich | ab19475 | 2020-07-21 22:21:22 +0100 | [diff] [blame^] | 37 | import androidx.compose.ui.text.InternalTextApi |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 38 | import kotlin.math.max |
| 39 | import kotlin.math.min |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 40 | |
Qingqing Deng | 35f97ea | 2019-09-18 19:24:37 -0700 | [diff] [blame] | 41 | /** |
Louis Pullen-Freilich | 5da28bd | 2019-10-15 17:05:07 +0100 | [diff] [blame] | 42 | * 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] | 43 | */ |
Qingqing Deng | 48b32a0 | 2020-06-22 17:06:27 -0700 | [diff] [blame] | 44 | @OptIn(InternalTextApi::class) |
Siyamed Sinir | 700df845 | 2019-10-22 20:23:58 -0700 | [diff] [blame] | 45 | internal class SelectionManager(private val selectionRegistrar: SelectionRegistrarImpl) { |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 46 | /** |
| 47 | * The current selection. |
| 48 | */ |
| 49 | var selection: Selection? = null |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 50 | set(value) { |
| 51 | field = value |
| 52 | updateHandleOffsets() |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 53 | hideSelectionToolbar() |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 54 | } |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * The manager will invoke this every time it comes to the conclusion that the selection should |
| 58 | * change. The expectation is that this callback will end up causing `setSelection` to get |
| 59 | * called. This is what makes this a "controlled component". |
| 60 | */ |
| 61 | var onSelectionChange: (Selection?) -> Unit = {} |
| 62 | |
| 63 | /** |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 64 | * [HapticFeedback] handle to perform haptic feedback. |
| 65 | */ |
| 66 | var hapticFeedBack: HapticFeedback? = null |
| 67 | |
| 68 | /** |
Qingqing Deng | f2d0a2d | 2020-03-12 14:19:50 -0700 | [diff] [blame] | 69 | * [ClipboardManager] to perform clipboard features. |
| 70 | */ |
| 71 | var clipboardManager: ClipboardManager? = null |
| 72 | |
| 73 | /** |
Qingqing Deng | a89450d | 2020-04-03 19:11:49 -0700 | [diff] [blame] | 74 | * [TextToolbar] to show floating toolbar(post-M) or primary toolbar(pre-M). |
| 75 | */ |
| 76 | var textToolbar: TextToolbar? = null |
| 77 | |
| 78 | /** |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 79 | * Layout Coordinates of the selection container. |
| 80 | */ |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 81 | var containerLayoutCoordinates: LayoutCoordinates? = null |
| 82 | set(value) { |
| 83 | field = value |
| 84 | updateHandleOffsets() |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 85 | updateSelectionToolbarPosition() |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 86 | } |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 87 | |
| 88 | /** |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 89 | * The beginning position of the drag gesture. Every time a new drag gesture starts, it wil be |
| 90 | * recalculated. |
| 91 | */ |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 92 | private var dragBeginPosition = Offset.Zero |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * The total distance being dragged of the drag gesture. Every time a new drag gesture starts, |
| 96 | * it will be zeroed out. |
| 97 | */ |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 98 | private var dragTotalDistance = Offset.Zero |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 99 | |
| 100 | /** |
Qingqing Deng | 0cb86fe | 2019-07-16 15:36:27 -0700 | [diff] [blame] | 101 | * A flag to check if the selection start or end handle is being dragged. |
| 102 | * If this value is true, then onPress will not select any text. |
| 103 | * This value will be set to true when either handle is being dragged, and be reset to false |
| 104 | * when the dragging is stopped. |
| 105 | */ |
| 106 | private var draggingHandle = false |
| 107 | |
| 108 | /** |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 109 | * The calculated position of the start handle in the [SelectionContainer] coordinates. It |
| 110 | * is null when handle shouldn't be displayed. |
| 111 | * It is a [State] so reading it during the composition will cause recomposition every time |
| 112 | * the position has been changed. |
| 113 | */ |
Chuck Jazdzewski | 0a90de9 | 2020-05-21 10:03:47 -0700 | [diff] [blame] | 114 | var startHandlePosition by mutableStateOf<Offset?>( |
| 115 | null, |
| 116 | policy = structuralEqualityPolicy() |
| 117 | ) |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 118 | private set |
| 119 | |
| 120 | /** |
| 121 | * The calculated position of the end handle in the [SelectionContainer] coordinates. It |
| 122 | * is null when handle shouldn't be displayed. |
| 123 | * It is a [State] so reading it during the composition will cause recomposition every time |
| 124 | * the position has been changed. |
| 125 | */ |
Chuck Jazdzewski | 0a90de9 | 2020-05-21 10:03:47 -0700 | [diff] [blame] | 126 | var endHandlePosition by mutableStateOf<Offset?>( |
| 127 | null, |
| 128 | policy = structuralEqualityPolicy() |
| 129 | ) |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 130 | private set |
| 131 | |
| 132 | init { |
| 133 | selectionRegistrar.onPositionChangeCallback = { |
| 134 | updateHandleOffsets() |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 135 | hideSelectionToolbar() |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | private fun updateHandleOffsets() { |
| 140 | val selection = selection |
| 141 | val containerCoordinates = containerLayoutCoordinates |
| 142 | if (selection != null && containerCoordinates != null && containerCoordinates.isAttached) { |
| 143 | val startLayoutCoordinates = selection.start.selectable.getLayoutCoordinates() |
| 144 | val endLayoutCoordinates = selection.end.selectable.getLayoutCoordinates() |
| 145 | |
| 146 | if (startLayoutCoordinates != null && endLayoutCoordinates != null) { |
| 147 | startHandlePosition = containerCoordinates.childToLocal( |
| 148 | startLayoutCoordinates, |
| 149 | selection.start.selectable.getHandlePosition( |
| 150 | selection = selection, |
| 151 | isStartHandle = true |
| 152 | ) |
| 153 | ) |
| 154 | endHandlePosition = containerCoordinates.childToLocal( |
| 155 | endLayoutCoordinates, |
| 156 | selection.end.selectable.getHandlePosition( |
| 157 | selection = selection, |
| 158 | isStartHandle = false |
| 159 | ) |
| 160 | ) |
| 161 | return |
| 162 | } |
| 163 | } |
| 164 | startHandlePosition = null |
| 165 | endHandlePosition = null |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Returns non-nullable [containerLayoutCoordinates]. |
| 170 | */ |
| 171 | internal fun requireContainerCoordinates(): LayoutCoordinates { |
| 172 | val coordinates = containerLayoutCoordinates |
| 173 | require(coordinates != null) |
| 174 | require(coordinates.isAttached) |
| 175 | return coordinates |
| 176 | } |
| 177 | |
| 178 | /** |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 179 | * Iterates over the handlers, gets the selection for each Composable, and merges all the |
| 180 | * returned [Selection]s. |
| 181 | * |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 182 | * @param startPosition [Offset] for the start of the selection |
| 183 | * @param endPosition [Offset] for the end of the selection |
Siyamed Sinir | 0100f12 | 2019-11-16 00:23:12 -0800 | [diff] [blame] | 184 | * @param longPress the selection is a result of long press |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 185 | * @param previousSelection previous selection |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 186 | * |
| 187 | * @return [Selection] object which is constructed by combining all Composables that are |
| 188 | * selected. |
| 189 | */ |
Qingqing Deng | 5819ff6 | 2019-11-18 15:26:23 -0800 | [diff] [blame] | 190 | // This function is internal for testing purposes. |
| 191 | internal fun mergeSelections( |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 192 | startPosition: Offset, |
| 193 | endPosition: Offset, |
Siyamed Sinir | 0100f12 | 2019-11-16 00:23:12 -0800 | [diff] [blame] | 194 | longPress: Boolean = false, |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 195 | previousSelection: Selection? = null, |
Qingqing Deng | ff65ed6 | 2020-01-06 17:55:48 -0800 | [diff] [blame] | 196 | isStartHandle: Boolean = true |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 197 | ): Selection? { |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 198 | |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 199 | val newSelection = selectionRegistrar.sort(requireContainerCoordinates()) |
Qingqing Deng | 247f2b4 | 2019-12-12 19:48:37 -0800 | [diff] [blame] | 200 | .fold(null) { mergedSelection: Selection?, |
| 201 | handler: Selectable -> |
| 202 | merge( |
| 203 | mergedSelection, |
| 204 | handler.getSelection( |
| 205 | startPosition = startPosition, |
| 206 | endPosition = endPosition, |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 207 | containerLayoutCoordinates = requireContainerCoordinates(), |
Qingqing Deng | 247f2b4 | 2019-12-12 19:48:37 -0800 | [diff] [blame] | 208 | longPress = longPress, |
| 209 | previousSelection = previousSelection, |
| 210 | isStartHandle = isStartHandle |
| 211 | ) |
Siyamed Sinir | 700df845 | 2019-10-22 20:23:58 -0700 | [diff] [blame] | 212 | ) |
Qingqing Deng | 247f2b4 | 2019-12-12 19:48:37 -0800 | [diff] [blame] | 213 | } |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 214 | if (previousSelection != newSelection) hapticFeedBack?.performHapticFeedback( |
| 215 | HapticFeedbackType.TextHandleMove |
| 216 | ) |
| 217 | return newSelection |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 220 | internal fun getSelectedText(): AnnotatedString? { |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 221 | val selectables = selectionRegistrar.sort(requireContainerCoordinates()) |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 222 | var selectedText: AnnotatedString? = null |
| 223 | |
| 224 | selection?.let { |
| 225 | for (handler in selectables) { |
| 226 | // Continue if the current selectable is before the selection starts. |
| 227 | if (handler != it.start.selectable && handler != it.end.selectable && |
| 228 | selectedText == null |
| 229 | ) continue |
| 230 | |
| 231 | val currentSelectedText = getCurrentSelectedText( |
| 232 | selectable = handler, |
| 233 | selection = it |
| 234 | ) |
| 235 | selectedText = selectedText?.plus(currentSelectedText) ?: currentSelectedText |
| 236 | |
| 237 | // Break if the current selectable is the last selected selectable. |
| 238 | if (handler == it.end.selectable && !it.handlesCrossed || |
| 239 | handler == it.start.selectable && it.handlesCrossed |
| 240 | ) break |
| 241 | } |
| 242 | } |
| 243 | return selectedText |
| 244 | } |
| 245 | |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 246 | internal fun copy() { |
Qingqing Deng | f2d0a2d | 2020-03-12 14:19:50 -0700 | [diff] [blame] | 247 | val selectedText = getSelectedText() |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 248 | selectedText?.let { clipboardManager?.setText(it) } |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * This function get the selected region as a Rectangle region, and pass it to [TextToolbar] |
| 253 | * to make the FloatingToolbar show up in the proper place. In addition, this function passes |
| 254 | * the copy method as a callback when "copy" is clicked. |
| 255 | */ |
| 256 | internal fun showSelectionToolbar() { |
| 257 | selection?.let { |
Qingqing Deng | 4051957 | 2020-07-10 13:25:36 -0700 | [diff] [blame] | 258 | textToolbar?.showMenu( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 259 | getContentRect(), |
Qingqing Deng | 4051957 | 2020-07-10 13:25:36 -0700 | [diff] [blame] | 260 | onCopyRequested = { |
| 261 | copy() |
| 262 | onRelease() |
| 263 | } |
Qingqing Deng | ce14056 | 2020-04-24 14:46:22 -0700 | [diff] [blame] | 264 | ) |
Qingqing Deng | a89450d | 2020-04-03 19:11:49 -0700 | [diff] [blame] | 265 | } |
Qingqing Deng | f2d0a2d | 2020-03-12 14:19:50 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 268 | private fun hideSelectionToolbar() { |
| 269 | if (textToolbar?.status == TextToolbarStatus.Shown) { |
| 270 | val selection = selection |
| 271 | if (selection == null) { |
| 272 | textToolbar?.hide() |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | private fun updateSelectionToolbarPosition() { |
| 278 | if (textToolbar?.status == TextToolbarStatus.Shown) { |
| 279 | showSelectionToolbar() |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Calculate selected region as [Rect]. The top is the top of the first selected |
| 285 | * line, and the bottom is the bottom of the last selected line. The left is the leftmost |
| 286 | * handle's horizontal coordinates, and the right is the rightmost handle's coordinates. |
| 287 | */ |
| 288 | private fun getContentRect(): Rect { |
| 289 | val selection = selection ?: return Rect.zero |
| 290 | val startLayoutCoordinates = |
| 291 | selection.start.selectable.getLayoutCoordinates() ?: return Rect.zero |
| 292 | val endLayoutCoordinates = |
| 293 | selection.end.selectable.getLayoutCoordinates() ?: return Rect.zero |
| 294 | |
| 295 | val localLayoutCoordinates = containerLayoutCoordinates |
| 296 | if (localLayoutCoordinates != null && localLayoutCoordinates.isAttached) { |
| 297 | var startOffset = localLayoutCoordinates.childToLocal( |
| 298 | startLayoutCoordinates, |
| 299 | selection.start.selectable.getHandlePosition( |
| 300 | selection = selection, |
| 301 | isStartHandle = true |
| 302 | ) |
| 303 | ) |
| 304 | var endOffset = localLayoutCoordinates.childToLocal( |
| 305 | endLayoutCoordinates, |
| 306 | selection.end.selectable.getHandlePosition( |
| 307 | selection = selection, |
| 308 | isStartHandle = false |
| 309 | ) |
| 310 | ) |
| 311 | |
| 312 | startOffset = localLayoutCoordinates.localToRoot(startOffset) |
| 313 | endOffset = localLayoutCoordinates.localToRoot(endOffset) |
| 314 | |
| 315 | val left = min(startOffset.x, endOffset.x) |
| 316 | val right = max(startOffset.x, endOffset.x) |
| 317 | |
| 318 | var startTop = localLayoutCoordinates.childToLocal( |
| 319 | startLayoutCoordinates, |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 320 | Offset( |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 321 | 0f, |
| 322 | selection.start.selectable.getBoundingBox(selection.start.offset).top |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 323 | ) |
| 324 | ) |
| 325 | |
| 326 | var endTop = localLayoutCoordinates.childToLocal( |
| 327 | endLayoutCoordinates, |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 328 | Offset( |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 329 | 0.0f, |
| 330 | selection.end.selectable.getBoundingBox(selection.end.offset).top |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 331 | ) |
| 332 | ) |
| 333 | |
| 334 | startTop = localLayoutCoordinates.localToRoot(startTop) |
| 335 | endTop = localLayoutCoordinates.localToRoot(endTop) |
| 336 | |
| 337 | val top = min(startTop.y, endTop.y) |
Nader Jawad | 16e330a | 2020-05-21 21:21:01 -0700 | [diff] [blame] | 338 | val bottom = max(startOffset.y, endOffset.y) + (HANDLE_HEIGHT.value * 4.0).toFloat() |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 339 | |
| 340 | return Rect( |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 341 | left, |
| 342 | top, |
| 343 | right, |
| 344 | bottom |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 345 | ) |
| 346 | } |
| 347 | return Rect.zero |
| 348 | } |
| 349 | |
Qingqing Deng | b6f5d8a | 2019-11-11 18:19:22 -0800 | [diff] [blame] | 350 | // This is for PressGestureDetector to cancel the selection. |
| 351 | fun onRelease() { |
| 352 | // Call mergeSelections with an out of boundary input to inform all text widgets to |
| 353 | // cancel their individual selection. |
Qingqing Deng | a5d8095 | 2019-10-11 16:46:52 -0700 | [diff] [blame] | 354 | mergeSelections( |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 355 | startPosition = Offset(-1f, -1f), |
| 356 | endPosition = Offset(-1f, -1f), |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 357 | previousSelection = selection |
Siyamed Sinir | 0100f12 | 2019-11-16 00:23:12 -0800 | [diff] [blame] | 358 | ) |
Siyamed Sinir | e810eab | 2019-11-22 12:36:38 -0800 | [diff] [blame] | 359 | if (selection != null) onSelectionChange(null) |
Qingqing Deng | b6f5d8a | 2019-11-11 18:19:22 -0800 | [diff] [blame] | 360 | } |
| 361 | |
Siyamed Sinir | e810eab | 2019-11-22 12:36:38 -0800 | [diff] [blame] | 362 | val longPressDragObserver = object : LongPressDragObserver { |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 363 | override fun onLongPress(pxPosition: Offset) { |
Siyamed Sinir | e810eab | 2019-11-22 12:36:38 -0800 | [diff] [blame] | 364 | if (draggingHandle) return |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 365 | val coordinates = containerLayoutCoordinates |
| 366 | if (coordinates == null || !coordinates.isAttached) return |
Siyamed Sinir | e810eab | 2019-11-22 12:36:38 -0800 | [diff] [blame] | 367 | val newSelection = mergeSelections( |
| 368 | startPosition = pxPosition, |
| 369 | endPosition = pxPosition, |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 370 | longPress = true, |
| 371 | previousSelection = selection |
Siyamed Sinir | e810eab | 2019-11-22 12:36:38 -0800 | [diff] [blame] | 372 | ) |
| 373 | if (newSelection != selection) onSelectionChange(newSelection) |
| 374 | dragBeginPosition = pxPosition |
Siyamed Sinir | 700df845 | 2019-10-22 20:23:58 -0700 | [diff] [blame] | 375 | } |
Siyamed Sinir | 700df845 | 2019-10-22 20:23:58 -0700 | [diff] [blame] | 376 | |
Siyamed Sinir | e810eab | 2019-11-22 12:36:38 -0800 | [diff] [blame] | 377 | override fun onDragStart() { |
| 378 | super.onDragStart() |
| 379 | // selection never started |
| 380 | if (selection == null) return |
| 381 | // Zero out the total distance that being dragged. |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 382 | dragTotalDistance = Offset.Zero |
Siyamed Sinir | e810eab | 2019-11-22 12:36:38 -0800 | [diff] [blame] | 383 | } |
| 384 | |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 385 | override fun onDrag(dragDistance: Offset): Offset { |
Siyamed Sinir | e810eab | 2019-11-22 12:36:38 -0800 | [diff] [blame] | 386 | // selection never started, did not consume any drag |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 387 | if (selection == null) return Offset.Zero |
Siyamed Sinir | e810eab | 2019-11-22 12:36:38 -0800 | [diff] [blame] | 388 | |
| 389 | dragTotalDistance += dragDistance |
| 390 | val newSelection = mergeSelections( |
| 391 | startPosition = dragBeginPosition, |
| 392 | endPosition = dragBeginPosition + dragTotalDistance, |
Qingqing Deng | 74baaa2 | 2019-12-12 13:28:25 -0800 | [diff] [blame] | 393 | longPress = true, |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 394 | previousSelection = selection |
Siyamed Sinir | e810eab | 2019-11-22 12:36:38 -0800 | [diff] [blame] | 395 | ) |
| 396 | |
| 397 | if (newSelection != selection) onSelectionChange(newSelection) |
| 398 | return dragDistance |
| 399 | } |
| 400 | } |
| 401 | |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 402 | fun handleDragObserver(isStartHandle: Boolean): DragObserver { |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 403 | return object : DragObserver { |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 404 | override fun onStart(downPosition: Offset) { |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 405 | val selection = selection!! |
Louis Pullen-Freilich | 5da28bd | 2019-10-15 17:05:07 +0100 | [diff] [blame] | 406 | // The LayoutCoordinates of the composable where the drag gesture should begin. This |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 407 | // 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] | 408 | // composable coordinates to selection container coordinates. |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 409 | val beginLayoutCoordinates = if (isStartHandle) { |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 410 | selection.start.selectable.getLayoutCoordinates()!! |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 411 | } else { |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 412 | selection.end.selectable.getLayoutCoordinates()!! |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 415 | // 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] | 416 | // the composable coordinates. |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 417 | val beginCoordinates = getAdjustedCoordinates( |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 418 | if (isStartHandle) |
| 419 | selection.start.selectable.getHandlePosition( |
| 420 | selection = selection, isStartHandle = true |
| 421 | ) else |
| 422 | selection.end.selectable.getHandlePosition( |
| 423 | selection = selection, isStartHandle = false |
| 424 | ) |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 425 | ) |
| 426 | |
Louis Pullen-Freilich | 5da28bd | 2019-10-15 17:05:07 +0100 | [diff] [blame] | 427 | // Convert the position where drag gesture begins from composable coordinates to |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 428 | // selection container coordinates. |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 429 | dragBeginPosition = requireContainerCoordinates().childToLocal( |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 430 | beginLayoutCoordinates, |
| 431 | beginCoordinates |
| 432 | ) |
| 433 | |
| 434 | // Zero out the total distance that being dragged. |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 435 | dragTotalDistance = Offset.Zero |
Qingqing Deng | 0cb86fe | 2019-07-16 15:36:27 -0700 | [diff] [blame] | 436 | draggingHandle = true |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 439 | override fun onDrag(dragDistance: Offset): Offset { |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 440 | val selection = selection!! |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 441 | dragTotalDistance += dragDistance |
| 442 | |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 443 | val currentStart = if (isStartHandle) { |
| 444 | dragBeginPosition + dragTotalDistance |
| 445 | } else { |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 446 | requireContainerCoordinates().childToLocal( |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 447 | selection.start.selectable.getLayoutCoordinates()!!, |
| 448 | getAdjustedCoordinates( |
| 449 | selection.start.selectable.getHandlePosition( |
| 450 | selection = selection, |
| 451 | isStartHandle = true |
| 452 | ) |
| 453 | ) |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 454 | ) |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 455 | } |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 456 | |
| 457 | val currentEnd = if (isStartHandle) { |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 458 | requireContainerCoordinates().childToLocal( |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 459 | selection.end.selectable.getLayoutCoordinates()!!, |
| 460 | getAdjustedCoordinates( |
| 461 | selection.end.selectable.getHandlePosition( |
| 462 | selection = selection, |
| 463 | isStartHandle = false |
| 464 | ) |
| 465 | ) |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 466 | ) |
| 467 | } else { |
| 468 | dragBeginPosition + dragTotalDistance |
| 469 | } |
| 470 | |
Qingqing Deng | a5d8095 | 2019-10-11 16:46:52 -0700 | [diff] [blame] | 471 | val finalSelection = mergeSelections( |
| 472 | startPosition = currentStart, |
| 473 | endPosition = currentEnd, |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 474 | previousSelection = selection, |
Qingqing Deng | ff65ed6 | 2020-01-06 17:55:48 -0800 | [diff] [blame] | 475 | isStartHandle = isStartHandle |
Qingqing Deng | a5d8095 | 2019-10-11 16:46:52 -0700 | [diff] [blame] | 476 | ) |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 477 | onSelectionChange(finalSelection) |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 478 | return dragDistance |
| 479 | } |
Qingqing Deng | 0cb86fe | 2019-07-16 15:36:27 -0700 | [diff] [blame] | 480 | |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 481 | override fun onStop(velocity: Offset) { |
Qingqing Deng | 0cb86fe | 2019-07-16 15:36:27 -0700 | [diff] [blame] | 482 | super.onStop(velocity) |
| 483 | draggingHandle = false |
| 484 | } |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
Siyamed Sinir | 700df845 | 2019-10-22 20:23:58 -0700 | [diff] [blame] | 489 | private fun merge(lhs: Selection?, rhs: Selection?): Selection? { |
| 490 | return lhs?.merge(rhs) ?: rhs |
| 491 | } |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 492 | |
| 493 | private fun getCurrentSelectedText( |
| 494 | selectable: Selectable, |
| 495 | selection: Selection |
| 496 | ): AnnotatedString { |
| 497 | val currentText = selectable.getText() |
| 498 | |
| 499 | return if ( |
| 500 | selectable != selection.start.selectable && |
| 501 | selectable != selection.end.selectable |
| 502 | ) { |
| 503 | // Select the full text content if the current selectable is between the |
| 504 | // start and the end selectables. |
| 505 | currentText |
| 506 | } else if ( |
| 507 | selectable == selection.start.selectable && |
| 508 | selectable == selection.end.selectable |
| 509 | ) { |
| 510 | // Select partial text content if the current selectable is the start and |
| 511 | // the end selectable. |
| 512 | if (selection.handlesCrossed) { |
| 513 | currentText.subSequence(selection.end.offset, selection.start.offset) |
| 514 | } else { |
| 515 | currentText.subSequence(selection.start.offset, selection.end.offset) |
| 516 | } |
| 517 | } else if (selectable == selection.start.selectable) { |
| 518 | // Select partial text content if the current selectable is the start |
| 519 | // selectable. |
| 520 | if (selection.handlesCrossed) { |
| 521 | currentText.subSequence(0, selection.start.offset) |
| 522 | } else { |
| 523 | currentText.subSequence(selection.start.offset, currentText.length) |
| 524 | } |
| 525 | } else { |
| 526 | // Selectable partial text content if the current selectable is the end |
| 527 | // selectable. |
| 528 | if (selection.handlesCrossed) { |
| 529 | currentText.subSequence(selection.end.offset, currentText.length) |
| 530 | } else { |
| 531 | currentText.subSequence(0, selection.end.offset) |
| 532 | } |
| 533 | } |
| 534 | } |