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