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