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