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