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