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 | |
Louis Pullen-Freilich | c6e92aa | 2021-03-08 00:42:16 +0000 | [diff] [blame] | 21 | import androidx.compose.foundation.fastFold |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 22 | import androidx.compose.foundation.focusable |
Ralston Da Silva | de62bc6 | 2021-06-02 17:46:44 -0700 | [diff] [blame] | 23 | import androidx.compose.foundation.gestures.forEachGesture |
| 24 | import androidx.compose.foundation.gestures.waitForUpOrCancellation |
Louis Pullen-Freilich | 1f10a59 | 2020-07-24 16:35:14 +0100 | [diff] [blame] | 25 | import androidx.compose.runtime.State |
| 26 | import androidx.compose.runtime.getValue |
| 27 | import androidx.compose.runtime.mutableStateOf |
| 28 | import androidx.compose.runtime.setValue |
| 29 | import androidx.compose.runtime.structuralEqualityPolicy |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 30 | import androidx.compose.ui.Modifier |
| 31 | import androidx.compose.ui.focus.FocusRequester |
| 32 | import androidx.compose.ui.focus.focusRequester |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 33 | import androidx.compose.ui.focus.onFocusChanged |
Louis Pullen-Freilich | 534385a | 2020-08-14 14:10:12 +0100 | [diff] [blame] | 34 | import androidx.compose.ui.geometry.Offset |
| 35 | import androidx.compose.ui.geometry.Rect |
Matvei Malkov | f770a91 | 2021-03-24 18:12:41 +0000 | [diff] [blame] | 36 | import androidx.compose.foundation.text.TextDragObserver |
Louis Pullen-Freilich | a03fd6c | 2020-07-24 23:26:29 +0100 | [diff] [blame] | 37 | import androidx.compose.ui.hapticfeedback.HapticFeedback |
| 38 | import androidx.compose.ui.hapticfeedback.HapticFeedbackType |
Andrey Rudenko | 8b4981e | 2021-01-29 15:31:59 +0100 | [diff] [blame] | 39 | import androidx.compose.ui.input.key.KeyEvent |
| 40 | import androidx.compose.ui.input.key.onKeyEvent |
Ralston Da Silva | de62bc6 | 2021-06-02 17:46:44 -0700 | [diff] [blame] | 41 | import androidx.compose.ui.input.pointer.PointerInputScope |
| 42 | import androidx.compose.ui.input.pointer.pointerInput |
Louis Pullen-Freilich | 534385a | 2020-08-14 14:10:12 +0100 | [diff] [blame] | 43 | import androidx.compose.ui.layout.LayoutCoordinates |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 44 | import androidx.compose.ui.layout.boundsInWindow |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 45 | import androidx.compose.ui.layout.onGloballyPositioned |
haoyu | 7ad5ea3 | 2021-03-22 10:36:35 -0700 | [diff] [blame] | 46 | import androidx.compose.ui.layout.positionInWindow |
Louis Pullen-Freilich | 534385a | 2020-08-14 14:10:12 +0100 | [diff] [blame] | 47 | import androidx.compose.ui.platform.ClipboardManager |
Louis Pullen-Freilich | a03fd6c | 2020-07-24 23:26:29 +0100 | [diff] [blame] | 48 | import androidx.compose.ui.platform.TextToolbar |
| 49 | import androidx.compose.ui.platform.TextToolbarStatus |
Louis Pullen-Freilich | ab19475 | 2020-07-21 22:21:22 +0100 | [diff] [blame] | 50 | import androidx.compose.ui.text.AnnotatedString |
Ralston Da Silva | de62bc6 | 2021-06-02 17:46:44 -0700 | [diff] [blame] | 51 | import kotlinx.coroutines.coroutineScope |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 52 | import kotlin.math.max |
| 53 | import kotlin.math.min |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 54 | |
Qingqing Deng | 35f97ea | 2019-09-18 19:24:37 -0700 | [diff] [blame] | 55 | /** |
Louis Pullen-Freilich | 5da28bd | 2019-10-15 17:05:07 +0100 | [diff] [blame] | 56 | * 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] | 57 | */ |
Siyamed Sinir | 700df845 | 2019-10-22 20:23:58 -0700 | [diff] [blame] | 58 | internal class SelectionManager(private val selectionRegistrar: SelectionRegistrarImpl) { |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 59 | /** |
| 60 | * The current selection. |
| 61 | */ |
| 62 | var selection: Selection? = null |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 63 | set(value) { |
| 64 | field = value |
haoyu | 9085c88 | 2020-12-08 12:01:06 -0800 | [diff] [blame] | 65 | if (value != null) { |
| 66 | updateHandleOffsets() |
| 67 | } |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 68 | } |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 69 | |
| 70 | /** |
Andrey Rudenko | 8b4981e | 2021-01-29 15:31:59 +0100 | [diff] [blame] | 71 | * Is touch mode active |
| 72 | */ |
| 73 | var touchMode: Boolean = true |
| 74 | |
| 75 | /** |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 76 | * The manager will invoke this every time it comes to the conclusion that the selection should |
| 77 | * change. The expectation is that this callback will end up causing `setSelection` to get |
| 78 | * called. This is what makes this a "controlled component". |
| 79 | */ |
| 80 | var onSelectionChange: (Selection?) -> Unit = {} |
| 81 | |
| 82 | /** |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 83 | * [HapticFeedback] handle to perform haptic feedback. |
| 84 | */ |
| 85 | var hapticFeedBack: HapticFeedback? = null |
| 86 | |
| 87 | /** |
Qingqing Deng | f2d0a2d | 2020-03-12 14:19:50 -0700 | [diff] [blame] | 88 | * [ClipboardManager] to perform clipboard features. |
| 89 | */ |
| 90 | var clipboardManager: ClipboardManager? = null |
| 91 | |
| 92 | /** |
Qingqing Deng | a89450d | 2020-04-03 19:11:49 -0700 | [diff] [blame] | 93 | * [TextToolbar] to show floating toolbar(post-M) or primary toolbar(pre-M). |
| 94 | */ |
| 95 | var textToolbar: TextToolbar? = null |
| 96 | |
| 97 | /** |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 98 | * Focus requester used to request focus when selection becomes active. |
| 99 | */ |
| 100 | var focusRequester: FocusRequester = FocusRequester() |
| 101 | |
| 102 | /** |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 103 | * Return true if the corresponding SelectionContainer is focused. |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 104 | */ |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 105 | var hasFocus: Boolean by mutableStateOf(false) |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 106 | |
| 107 | /** |
| 108 | * Modifier for selection container. |
| 109 | */ |
| 110 | val modifier get() = Modifier |
Ralston Da Silva | de62bc6 | 2021-06-02 17:46:44 -0700 | [diff] [blame] | 111 | .onClearSelectionRequested { onRelease() } |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 112 | .onGloballyPositioned { containerLayoutCoordinates = it } |
| 113 | .focusRequester(focusRequester) |
| 114 | .onFocusChanged { focusState -> |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 115 | if (!focusState.isFocused && hasFocus) { |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 116 | onRelease() |
| 117 | } |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 118 | hasFocus = focusState.isFocused |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 119 | } |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 120 | .focusable() |
Andrey Rudenko | 8b4981e | 2021-01-29 15:31:59 +0100 | [diff] [blame] | 121 | .onKeyEvent { |
| 122 | if (isCopyKeyEvent(it)) { |
| 123 | copy() |
| 124 | true |
| 125 | } else { |
| 126 | false |
| 127 | } |
| 128 | } |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 129 | |
haoyu | 7ad5ea3 | 2021-03-22 10:36:35 -0700 | [diff] [blame] | 130 | private var previousPosition: Offset? = null |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 131 | /** |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 132 | * Layout Coordinates of the selection container. |
| 133 | */ |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 134 | var containerLayoutCoordinates: LayoutCoordinates? = null |
| 135 | set(value) { |
| 136 | field = value |
haoyu | 2c6e984 | 2021-03-30 17:39:04 -0700 | [diff] [blame] | 137 | if (hasFocus && selection != null) { |
| 138 | val positionInWindow = value?.positionInWindow() |
| 139 | if (previousPosition != positionInWindow) { |
| 140 | previousPosition = positionInWindow |
| 141 | updateHandleOffsets() |
| 142 | updateSelectionToolbarPosition() |
| 143 | } |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 144 | } |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 145 | } |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 146 | |
| 147 | /** |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 148 | * The beginning position of the drag gesture. Every time a new drag gesture starts, it wil be |
| 149 | * recalculated. |
| 150 | */ |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 151 | private var dragBeginPosition = Offset.Zero |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 152 | |
| 153 | /** |
| 154 | * The total distance being dragged of the drag gesture. Every time a new drag gesture starts, |
| 155 | * it will be zeroed out. |
| 156 | */ |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 157 | private var dragTotalDistance = Offset.Zero |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 158 | |
| 159 | /** |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 160 | * The calculated position of the start handle in the [SelectionContainer] coordinates. It |
| 161 | * is null when handle shouldn't be displayed. |
| 162 | * It is a [State] so reading it during the composition will cause recomposition every time |
| 163 | * the position has been changed. |
| 164 | */ |
Chuck Jazdzewski | 0a90de9 | 2020-05-21 10:03:47 -0700 | [diff] [blame] | 165 | var startHandlePosition by mutableStateOf<Offset?>( |
| 166 | null, |
| 167 | policy = structuralEqualityPolicy() |
| 168 | ) |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 169 | private set |
| 170 | |
| 171 | /** |
| 172 | * The calculated position of the end handle in the [SelectionContainer] coordinates. It |
| 173 | * is null when handle shouldn't be displayed. |
| 174 | * It is a [State] so reading it during the composition will cause recomposition every time |
| 175 | * the position has been changed. |
| 176 | */ |
Chuck Jazdzewski | 0a90de9 | 2020-05-21 10:03:47 -0700 | [diff] [blame] | 177 | var endHandlePosition by mutableStateOf<Offset?>( |
| 178 | null, |
| 179 | policy = structuralEqualityPolicy() |
| 180 | ) |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 181 | private set |
| 182 | |
| 183 | init { |
haoyu | 7ad5ea3 | 2021-03-22 10:36:35 -0700 | [diff] [blame] | 184 | selectionRegistrar.onPositionChangeCallback = { selectableId -> |
| 185 | if ( |
| 186 | selectableId == selection?.start?.selectableId || |
| 187 | selectableId == selection?.end?.selectableId |
| 188 | ) { |
| 189 | updateHandleOffsets() |
| 190 | updateSelectionToolbarPosition() |
| 191 | } |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 194 | selectionRegistrar.onSelectionUpdateStartCallback = |
| 195 | { layoutCoordinates, startPosition, selectionMode -> |
| 196 | val startPositionInContainer = convertToContainerCoordinates( |
| 197 | layoutCoordinates, |
| 198 | startPosition |
| 199 | ) |
| 200 | |
| 201 | updateSelection( |
| 202 | startPosition = startPositionInContainer, |
| 203 | endPosition = startPositionInContainer, |
| 204 | isStartHandle = true, |
| 205 | adjustment = selectionMode |
| 206 | ) |
| 207 | |
| 208 | focusRequester.requestFocus() |
| 209 | hideSelectionToolbar() |
| 210 | } |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 211 | |
Qingqing Deng | 6bc981c | 2021-05-11 22:43:51 -0700 | [diff] [blame] | 212 | selectionRegistrar.onSelectionUpdateSelectAll = |
| 213 | { selectableId -> |
| 214 | val (newSelection, newSubselection) = mergeSelections( |
| 215 | selectableId = selectableId, |
| 216 | previousSelection = selection, |
| 217 | ) |
| 218 | if (newSelection != selection) { |
| 219 | selectionRegistrar.subselections = newSubselection |
| 220 | onSelectionChange(newSelection) |
| 221 | } |
| 222 | |
| 223 | focusRequester.requestFocus() |
| 224 | hideSelectionToolbar() |
| 225 | } |
| 226 | |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 227 | selectionRegistrar.onSelectionUpdateCallback = |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 228 | { layoutCoordinates, startPosition, endPosition, selectionMode -> |
| 229 | val startPositionOrCurrent = if (startPosition == null) { |
| 230 | currentSelectionStartPosition() |
| 231 | } else { |
| 232 | convertToContainerCoordinates(layoutCoordinates, startPosition) |
| 233 | } |
| 234 | |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 235 | updateSelection( |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 236 | startPosition = startPositionOrCurrent, |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 237 | endPosition = convertToContainerCoordinates(layoutCoordinates, endPosition), |
haoyu | 70aed73 | 2020-12-15 07:10:56 -0800 | [diff] [blame] | 238 | isStartHandle = false, |
Haoyu Zhang | 4c6c637 | 2021-04-20 10:25:21 -0700 | [diff] [blame] | 239 | adjustment = selectionMode |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 240 | ) |
| 241 | } |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 242 | |
| 243 | selectionRegistrar.onSelectionUpdateEndCallback = { |
| 244 | showSelectionToolbar() |
Haoyu Zhang | a78608e | 2021-08-03 17:10:18 -0700 | [diff] [blame^] | 245 | finishSelectionUpdate() |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 246 | } |
haoyu | 9085c88 | 2020-12-08 12:01:06 -0800 | [diff] [blame] | 247 | |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 248 | selectionRegistrar.onSelectableChangeCallback = { selectableKey -> |
| 249 | if (selectableKey in selectionRegistrar.subselections) { |
haoyu | 9085c88 | 2020-12-08 12:01:06 -0800 | [diff] [blame] | 250 | // clear the selection range of each Selectable. |
| 251 | onRelease() |
| 252 | selection = null |
| 253 | } |
| 254 | } |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 255 | |
| 256 | selectionRegistrar.afterSelectableUnsubscribe = { selectableKey -> |
| 257 | if ( |
| 258 | selectableKey == selection?.start?.selectableId || |
| 259 | selectableKey == selection?.end?.selectableId |
| 260 | ) { |
| 261 | // The selectable that contains a selection handle just unsubscribed. |
| 262 | // Hide selection handles for now |
| 263 | startHandlePosition = null |
| 264 | endHandlePosition = null |
| 265 | } |
| 266 | } |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 267 | } |
| 268 | |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 269 | private fun currentSelectionStartPosition(): Offset? { |
| 270 | return selection?.let { selection -> |
| 271 | val startSelectable = |
| 272 | selectionRegistrar.selectableMap[selection.start.selectableId] |
| 273 | |
| 274 | requireContainerCoordinates().localPositionOf( |
| 275 | startSelectable?.getLayoutCoordinates()!!, |
| 276 | getAdjustedCoordinates( |
| 277 | startSelectable.getHandlePosition( |
| 278 | selection = selection, |
| 279 | isStartHandle = true |
| 280 | ) |
| 281 | ) |
| 282 | ) |
| 283 | } |
| 284 | } |
| 285 | |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 286 | private fun updateHandleOffsets() { |
| 287 | val selection = selection |
| 288 | val containerCoordinates = containerLayoutCoordinates |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 289 | val startSelectable = selection?.start?.selectableId?.let { |
| 290 | selectionRegistrar.selectableMap[it] |
| 291 | } |
| 292 | val endSelectable = selection?.end?.selectableId?.let { |
| 293 | selectionRegistrar.selectableMap[it] |
| 294 | } |
| 295 | val startLayoutCoordinates = startSelectable?.getLayoutCoordinates() |
| 296 | val endLayoutCoordinates = endSelectable?.getLayoutCoordinates() |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 297 | if ( |
| 298 | selection == null || |
| 299 | containerCoordinates == null || |
| 300 | !containerCoordinates.isAttached || |
| 301 | startLayoutCoordinates == null || |
| 302 | endLayoutCoordinates == null |
| 303 | ) { |
| 304 | this.startHandlePosition = null |
| 305 | this.endHandlePosition = null |
| 306 | return |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 307 | } |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 308 | |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 309 | val startHandlePosition = containerCoordinates.localPositionOf( |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 310 | startLayoutCoordinates, |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 311 | startSelectable.getHandlePosition( |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 312 | selection = selection, |
| 313 | isStartHandle = true |
| 314 | ) |
| 315 | ) |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 316 | val endHandlePosition = containerCoordinates.localPositionOf( |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 317 | endLayoutCoordinates, |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 318 | endSelectable.getHandlePosition( |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 319 | selection = selection, |
| 320 | isStartHandle = false |
| 321 | ) |
| 322 | ) |
| 323 | |
| 324 | val visibleBounds = containerCoordinates.visibleBounds() |
| 325 | this.startHandlePosition = |
| 326 | if (visibleBounds.containsInclusive(startHandlePosition)) startHandlePosition else null |
| 327 | this.endHandlePosition = |
| 328 | if (visibleBounds.containsInclusive(endHandlePosition)) endHandlePosition else null |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Returns non-nullable [containerLayoutCoordinates]. |
| 333 | */ |
| 334 | internal fun requireContainerCoordinates(): LayoutCoordinates { |
| 335 | val coordinates = containerLayoutCoordinates |
| 336 | require(coordinates != null) |
| 337 | require(coordinates.isAttached) |
| 338 | return coordinates |
| 339 | } |
| 340 | |
| 341 | /** |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 342 | * Iterates over the handlers, gets the selection for each Composable, and merges all the |
| 343 | * returned [Selection]s. |
| 344 | * |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 345 | * @param startPosition [Offset] for the start of the selection |
| 346 | * @param endPosition [Offset] for the end of the selection |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 347 | * @param previousSelection previous selection |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 348 | * |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 349 | * @return a [Pair] of a [Selection] object which is constructed by combining all |
| 350 | * composables that are selected and a [Map] from selectable key to [Selection]s on the |
| 351 | * [Selectable] corresponding to the that key. |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 352 | */ |
Qingqing Deng | 5819ff6 | 2019-11-18 15:26:23 -0800 | [diff] [blame] | 353 | // This function is internal for testing purposes. |
| 354 | internal fun mergeSelections( |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 355 | startPosition: Offset, |
| 356 | endPosition: Offset, |
Haoyu Zhang | a78608e | 2021-08-03 17:10:18 -0700 | [diff] [blame^] | 357 | adjustment: SelectionAdjustment = SelectionAdjustment.None, |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 358 | previousSelection: Selection? = null, |
Qingqing Deng | ff65ed6 | 2020-01-06 17:55:48 -0800 | [diff] [blame] | 359 | isStartHandle: Boolean = true |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 360 | ): Pair<Selection?, Map<Long, Selection>> { |
| 361 | val subselections = mutableMapOf<Long, Selection>() |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 362 | val newSelection = selectionRegistrar.sort(requireContainerCoordinates()) |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 363 | .fastFold(null) { mergedSelection: Selection?, selectable: Selectable -> |
Haoyu Zhang | a78608e | 2021-08-03 17:10:18 -0700 | [diff] [blame^] | 364 | val subselection = |
| 365 | selectionRegistrar.subselections[selectable.selectableId] |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 366 | val selection = selectable.getSelection( |
| 367 | startPosition = startPosition, |
| 368 | endPosition = endPosition, |
| 369 | containerLayoutCoordinates = requireContainerCoordinates(), |
Haoyu Zhang | a78608e | 2021-08-03 17:10:18 -0700 | [diff] [blame^] | 370 | adjustment = adjustment, |
| 371 | previousSelection = subselection, |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 372 | isStartHandle = isStartHandle, |
Siyamed Sinir | 700df845 | 2019-10-22 20:23:58 -0700 | [diff] [blame] | 373 | ) |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 374 | selection?.let { subselections[selectable.selectableId] = it } |
| 375 | merge(mergedSelection, selection) |
Qingqing Deng | 247f2b4 | 2019-12-12 19:48:37 -0800 | [diff] [blame] | 376 | } |
Haoyu Zhang | a78608e | 2021-08-03 17:10:18 -0700 | [diff] [blame^] | 377 | performHapticFeedbackIfNeeded(newSelection, previousSelection, hapticFeedBack) |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 378 | return Pair(newSelection, subselections) |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Qingqing Deng | 6bc981c | 2021-05-11 22:43:51 -0700 | [diff] [blame] | 381 | internal fun mergeSelections( |
| 382 | previousSelection: Selection? = null, |
| 383 | selectableId: Long |
| 384 | ): Pair<Selection?, Map<Long, Selection>> { |
| 385 | val subselections = mutableMapOf<Long, Selection>() |
| 386 | val newSelection = selectionRegistrar.sort(requireContainerCoordinates()) |
| 387 | .fastFold(null) { mergedSelection: Selection?, selectable: Selectable -> |
| 388 | val selection = if (selectable.selectableId == selectableId) |
| 389 | selectable.getSelectAllSelection() else null |
| 390 | selection?.let { subselections[selectable.selectableId] = it } |
| 391 | merge(mergedSelection, selection) |
| 392 | } |
Haoyu Zhang | a78608e | 2021-08-03 17:10:18 -0700 | [diff] [blame^] | 393 | performHapticFeedbackIfNeeded(previousSelection, newSelection, hapticFeedBack) |
Qingqing Deng | 6bc981c | 2021-05-11 22:43:51 -0700 | [diff] [blame] | 394 | return Pair(newSelection, subselections) |
| 395 | } |
| 396 | |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 397 | internal fun getSelectedText(): AnnotatedString? { |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 398 | val selectables = selectionRegistrar.sort(requireContainerCoordinates()) |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 399 | var selectedText: AnnotatedString? = null |
| 400 | |
| 401 | selection?.let { |
Louis Pullen-Freilich | c6e92aa | 2021-03-08 00:42:16 +0000 | [diff] [blame] | 402 | for (i in selectables.indices) { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 403 | val selectable = selectables[i] |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 404 | // Continue if the current selectable is before the selection starts. |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 405 | if (selectable.selectableId != it.start.selectableId && |
| 406 | selectable.selectableId != it.end.selectableId && |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 407 | selectedText == null |
| 408 | ) continue |
| 409 | |
| 410 | val currentSelectedText = getCurrentSelectedText( |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 411 | selectable = selectable, |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 412 | selection = it |
| 413 | ) |
| 414 | selectedText = selectedText?.plus(currentSelectedText) ?: currentSelectedText |
| 415 | |
| 416 | // Break if the current selectable is the last selected selectable. |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 417 | if (selectable.selectableId == it.end.selectableId && !it.handlesCrossed || |
| 418 | selectable.selectableId == it.start.selectableId && it.handlesCrossed |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 419 | ) break |
| 420 | } |
| 421 | } |
| 422 | return selectedText |
| 423 | } |
| 424 | |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 425 | internal fun copy() { |
Qingqing Deng | f2d0a2d | 2020-03-12 14:19:50 -0700 | [diff] [blame] | 426 | val selectedText = getSelectedText() |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 427 | selectedText?.let { clipboardManager?.setText(it) } |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * This function get the selected region as a Rectangle region, and pass it to [TextToolbar] |
| 432 | * to make the FloatingToolbar show up in the proper place. In addition, this function passes |
| 433 | * the copy method as a callback when "copy" is clicked. |
| 434 | */ |
| 435 | internal fun showSelectionToolbar() { |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 436 | if (hasFocus) { |
| 437 | selection?.let { |
| 438 | textToolbar?.showMenu( |
| 439 | getContentRect(), |
| 440 | onCopyRequested = { |
| 441 | copy() |
| 442 | onRelease() |
| 443 | } |
| 444 | ) |
| 445 | } |
Qingqing Deng | a89450d | 2020-04-03 19:11:49 -0700 | [diff] [blame] | 446 | } |
Qingqing Deng | f2d0a2d | 2020-03-12 14:19:50 -0700 | [diff] [blame] | 447 | } |
| 448 | |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 449 | internal fun hideSelectionToolbar() { |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 450 | if (hasFocus && textToolbar?.status == TextToolbarStatus.Shown) { |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 451 | textToolbar?.hide() |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
| 455 | private fun updateSelectionToolbarPosition() { |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 456 | if (hasFocus && textToolbar?.status == TextToolbarStatus.Shown) { |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 457 | showSelectionToolbar() |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Calculate selected region as [Rect]. The top is the top of the first selected |
| 463 | * line, and the bottom is the bottom of the last selected line. The left is the leftmost |
| 464 | * handle's horizontal coordinates, and the right is the rightmost handle's coordinates. |
| 465 | */ |
| 466 | private fun getContentRect(): Rect { |
Nader Jawad | 8432b8d | 2020-07-27 14:51:23 -0700 | [diff] [blame] | 467 | val selection = selection ?: return Rect.Zero |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 468 | val startSelectable = selectionRegistrar.selectableMap[selection.start.selectableId] |
| 469 | val endSelectable = selectionRegistrar.selectableMap[selection.start.selectableId] |
| 470 | val startLayoutCoordinates = startSelectable?.getLayoutCoordinates() ?: return Rect.Zero |
| 471 | val endLayoutCoordinates = endSelectable?.getLayoutCoordinates() ?: return Rect.Zero |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 472 | |
| 473 | val localLayoutCoordinates = containerLayoutCoordinates |
| 474 | if (localLayoutCoordinates != null && localLayoutCoordinates.isAttached) { |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 475 | var startOffset = localLayoutCoordinates.localPositionOf( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 476 | startLayoutCoordinates, |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 477 | startSelectable.getHandlePosition( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 478 | selection = selection, |
| 479 | isStartHandle = true |
| 480 | ) |
| 481 | ) |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 482 | var endOffset = localLayoutCoordinates.localPositionOf( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 483 | endLayoutCoordinates, |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 484 | endSelectable.getHandlePosition( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 485 | selection = selection, |
| 486 | isStartHandle = false |
| 487 | ) |
| 488 | ) |
| 489 | |
| 490 | startOffset = localLayoutCoordinates.localToRoot(startOffset) |
| 491 | endOffset = localLayoutCoordinates.localToRoot(endOffset) |
| 492 | |
| 493 | val left = min(startOffset.x, endOffset.x) |
| 494 | val right = max(startOffset.x, endOffset.x) |
| 495 | |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 496 | var startTop = localLayoutCoordinates.localPositionOf( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 497 | startLayoutCoordinates, |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 498 | Offset( |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 499 | 0f, |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 500 | startSelectable.getBoundingBox(selection.start.offset).top |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 501 | ) |
| 502 | ) |
| 503 | |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 504 | var endTop = localLayoutCoordinates.localPositionOf( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 505 | endLayoutCoordinates, |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 506 | Offset( |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 507 | 0.0f, |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 508 | endSelectable.getBoundingBox(selection.end.offset).top |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 509 | ) |
| 510 | ) |
| 511 | |
| 512 | startTop = localLayoutCoordinates.localToRoot(startTop) |
| 513 | endTop = localLayoutCoordinates.localToRoot(endTop) |
| 514 | |
| 515 | val top = min(startTop.y, endTop.y) |
Haoyu Zhang | d47f1fe | 2021-07-13 03:10:55 -0700 | [diff] [blame] | 516 | val bottom = max(startOffset.y, endOffset.y) + (HandleHeight.value * 4.0).toFloat() |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 517 | |
| 518 | return Rect( |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 519 | left, |
| 520 | top, |
| 521 | right, |
| 522 | bottom |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 523 | ) |
| 524 | } |
Nader Jawad | 8432b8d | 2020-07-27 14:51:23 -0700 | [diff] [blame] | 525 | return Rect.Zero |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Qingqing Deng | b6f5d8a | 2019-11-11 18:19:22 -0800 | [diff] [blame] | 528 | // This is for PressGestureDetector to cancel the selection. |
| 529 | fun onRelease() { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 530 | selectionRegistrar.subselections = emptyMap() |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 531 | hideSelectionToolbar() |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 532 | if (selection != null) { |
| 533 | onSelectionChange(null) |
| 534 | hapticFeedBack?.performHapticFeedback(HapticFeedbackType.TextHandleMove) |
| 535 | } |
Qingqing Deng | b6f5d8a | 2019-11-11 18:19:22 -0800 | [diff] [blame] | 536 | } |
| 537 | |
Matvei Malkov | f770a91 | 2021-03-24 18:12:41 +0000 | [diff] [blame] | 538 | fun handleDragObserver(isStartHandle: Boolean): TextDragObserver { |
| 539 | return object : TextDragObserver { |
| 540 | override fun onStart(startPoint: Offset) { |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 541 | hideSelectionToolbar() |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 542 | val selection = selection!! |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 543 | val startSelectable = |
| 544 | selectionRegistrar.selectableMap[selection.start.selectableId] |
| 545 | val endSelectable = |
| 546 | selectionRegistrar.selectableMap[selection.end.selectableId] |
Louis Pullen-Freilich | 5da28bd | 2019-10-15 17:05:07 +0100 | [diff] [blame] | 547 | // The LayoutCoordinates of the composable where the drag gesture should begin. This |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 548 | // 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] | 549 | // composable coordinates to selection container coordinates. |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 550 | val beginLayoutCoordinates = if (isStartHandle) { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 551 | startSelectable?.getLayoutCoordinates()!! |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 552 | } else { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 553 | endSelectable?.getLayoutCoordinates()!! |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 556 | // 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] | 557 | // the composable coordinates. |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 558 | val beginCoordinates = getAdjustedCoordinates( |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 559 | if (isStartHandle) { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 560 | startSelectable!!.getHandlePosition( |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 561 | selection = selection, isStartHandle = true |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 562 | ) |
| 563 | } else { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 564 | endSelectable!!.getHandlePosition( |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 565 | selection = selection, isStartHandle = false |
| 566 | ) |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 567 | } |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 568 | ) |
| 569 | |
Louis Pullen-Freilich | 5da28bd | 2019-10-15 17:05:07 +0100 | [diff] [blame] | 570 | // Convert the position where drag gesture begins from composable coordinates to |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 571 | // selection container coordinates. |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 572 | dragBeginPosition = requireContainerCoordinates().localPositionOf( |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 573 | beginLayoutCoordinates, |
| 574 | beginCoordinates |
| 575 | ) |
| 576 | |
| 577 | // Zero out the total distance that being dragged. |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 578 | dragTotalDistance = Offset.Zero |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Matvei Malkov | f770a91 | 2021-03-24 18:12:41 +0000 | [diff] [blame] | 581 | override fun onDrag(delta: Offset) { |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 582 | val selection = selection!! |
Matvei Malkov | f770a91 | 2021-03-24 18:12:41 +0000 | [diff] [blame] | 583 | dragTotalDistance += delta |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 584 | val startSelectable = |
| 585 | selectionRegistrar.selectableMap[selection.start.selectableId] |
| 586 | val endSelectable = |
| 587 | selectionRegistrar.selectableMap[selection.end.selectableId] |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 588 | val currentStart = if (isStartHandle) { |
| 589 | dragBeginPosition + dragTotalDistance |
| 590 | } else { |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 591 | requireContainerCoordinates().localPositionOf( |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 592 | startSelectable?.getLayoutCoordinates()!!, |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 593 | getAdjustedCoordinates( |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 594 | startSelectable.getHandlePosition( |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 595 | selection = selection, |
| 596 | isStartHandle = true |
| 597 | ) |
| 598 | ) |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 599 | ) |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 600 | } |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 601 | |
| 602 | val currentEnd = if (isStartHandle) { |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 603 | requireContainerCoordinates().localPositionOf( |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 604 | endSelectable?.getLayoutCoordinates()!!, |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 605 | getAdjustedCoordinates( |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 606 | endSelectable.getHandlePosition( |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 607 | selection = selection, |
| 608 | isStartHandle = false |
| 609 | ) |
| 610 | ) |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 611 | ) |
| 612 | } else { |
| 613 | dragBeginPosition + dragTotalDistance |
| 614 | } |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 615 | updateSelection( |
Qingqing Deng | a5d8095 | 2019-10-11 16:46:52 -0700 | [diff] [blame] | 616 | startPosition = currentStart, |
| 617 | endPosition = currentEnd, |
Haoyu Zhang | 5740343b | 2021-07-15 11:29:27 -0700 | [diff] [blame] | 618 | isStartHandle = isStartHandle, |
Haoyu Zhang | a78608e | 2021-08-03 17:10:18 -0700 | [diff] [blame^] | 619 | adjustment = SelectionAdjustment.CharacterWithWordAccelerate |
Qingqing Deng | a5d8095 | 2019-10-11 16:46:52 -0700 | [diff] [blame] | 620 | ) |
Matvei Malkov | f770a91 | 2021-03-24 18:12:41 +0000 | [diff] [blame] | 621 | return |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 622 | } |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 623 | |
Matvei Malkov | f770a91 | 2021-03-24 18:12:41 +0000 | [diff] [blame] | 624 | override fun onStop() { |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 625 | showSelectionToolbar() |
Haoyu Zhang | a78608e | 2021-08-03 17:10:18 -0700 | [diff] [blame^] | 626 | finishSelectionUpdate() |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | override fun onCancel() { |
| 630 | showSelectionToolbar() |
| 631 | } |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 632 | } |
| 633 | } |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 634 | |
Ralston Da Silva | de62bc6 | 2021-06-02 17:46:44 -0700 | [diff] [blame] | 635 | /** |
| 636 | * Detect tap without consuming the up event. |
| 637 | */ |
| 638 | private suspend fun PointerInputScope.detectNonConsumingTap(onTap: (Offset) -> Unit) { |
| 639 | forEachGesture { |
| 640 | coroutineScope { |
| 641 | awaitPointerEventScope { |
| 642 | waitForUpOrCancellation()?.let { |
| 643 | onTap(it.position) |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | private fun Modifier.onClearSelectionRequested(block: () -> Unit): Modifier { |
| 651 | return if (hasFocus) pointerInput(Unit) { detectNonConsumingTap { block() } } else this |
| 652 | } |
| 653 | |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 654 | private fun convertToContainerCoordinates( |
| 655 | layoutCoordinates: LayoutCoordinates, |
| 656 | offset: Offset |
| 657 | ): Offset? { |
| 658 | val coordinates = containerLayoutCoordinates |
| 659 | if (coordinates == null || !coordinates.isAttached) return null |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 660 | return requireContainerCoordinates().localPositionOf(layoutCoordinates, offset) |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | private fun updateSelection( |
| 664 | startPosition: Offset?, |
| 665 | endPosition: Offset?, |
Haoyu Zhang | a78608e | 2021-08-03 17:10:18 -0700 | [diff] [blame^] | 666 | adjustment: SelectionAdjustment = SelectionAdjustment.None, |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 667 | isStartHandle: Boolean = true |
| 668 | ) { |
| 669 | if (startPosition == null || endPosition == null) return |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 670 | val (newSelection, newSubselection) = mergeSelections( |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 671 | startPosition = startPosition, |
| 672 | endPosition = endPosition, |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 673 | adjustment = adjustment, |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 674 | previousSelection = selection, |
Haoyu Zhang | a78608e | 2021-08-03 17:10:18 -0700 | [diff] [blame^] | 675 | isStartHandle = isStartHandle |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 676 | ) |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 677 | if (newSelection != selection) { |
| 678 | selectionRegistrar.subselections = newSubselection |
| 679 | onSelectionChange(newSelection) |
| 680 | } |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 681 | } |
Haoyu Zhang | a78608e | 2021-08-03 17:10:18 -0700 | [diff] [blame^] | 682 | |
| 683 | private fun finishSelectionUpdate() { |
| 684 | selection?.let { |
| 685 | val newSelection = Selection( |
| 686 | start = it.start.copy(rawOffset = it.start.offset), |
| 687 | end = it.end.copy(rawOffset = it.end.offset), |
| 688 | handlesCrossed = it.handlesCrossed |
| 689 | ) |
| 690 | onSelectionChange(newSelection) |
| 691 | } |
| 692 | } |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Andrey Rudenko | 5dc7aad | 2020-09-18 10:33:37 +0200 | [diff] [blame] | 695 | internal fun merge(lhs: Selection?, rhs: Selection?): Selection? { |
Siyamed Sinir | 700df845 | 2019-10-22 20:23:58 -0700 | [diff] [blame] | 696 | return lhs?.merge(rhs) ?: rhs |
| 697 | } |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 698 | |
Andrey Rudenko | 8b4981e | 2021-01-29 15:31:59 +0100 | [diff] [blame] | 699 | internal expect fun isCopyKeyEvent(keyEvent: KeyEvent): Boolean |
| 700 | |
Andrey Rudenko | 5dc7aad | 2020-09-18 10:33:37 +0200 | [diff] [blame] | 701 | internal fun getCurrentSelectedText( |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 702 | selectable: Selectable, |
| 703 | selection: Selection |
| 704 | ): AnnotatedString { |
| 705 | val currentText = selectable.getText() |
| 706 | |
| 707 | return if ( |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 708 | selectable.selectableId != selection.start.selectableId && |
| 709 | selectable.selectableId != selection.end.selectableId |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 710 | ) { |
| 711 | // Select the full text content if the current selectable is between the |
| 712 | // start and the end selectables. |
| 713 | currentText |
| 714 | } else if ( |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 715 | selectable.selectableId == selection.start.selectableId && |
| 716 | selectable.selectableId == selection.end.selectableId |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 717 | ) { |
| 718 | // Select partial text content if the current selectable is the start and |
| 719 | // the end selectable. |
| 720 | if (selection.handlesCrossed) { |
| 721 | currentText.subSequence(selection.end.offset, selection.start.offset) |
| 722 | } else { |
| 723 | currentText.subSequence(selection.start.offset, selection.end.offset) |
| 724 | } |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 725 | } else if (selectable.selectableId == selection.start.selectableId) { |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 726 | // Select partial text content if the current selectable is the start |
| 727 | // selectable. |
| 728 | if (selection.handlesCrossed) { |
| 729 | currentText.subSequence(0, selection.start.offset) |
| 730 | } else { |
| 731 | currentText.subSequence(selection.start.offset, currentText.length) |
| 732 | } |
| 733 | } else { |
| 734 | // Selectable partial text content if the current selectable is the end |
| 735 | // selectable. |
| 736 | if (selection.handlesCrossed) { |
| 737 | currentText.subSequence(selection.end.offset, currentText.length) |
| 738 | } else { |
| 739 | currentText.subSequence(0, selection.end.offset) |
| 740 | } |
| 741 | } |
| 742 | } |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 743 | |
| 744 | /** Returns the boundary of the visible area in this [LayoutCoordinates]. */ |
haoyu | ac341f0 | 2021-01-22 22:01:56 -0800 | [diff] [blame] | 745 | internal fun LayoutCoordinates.visibleBounds(): Rect { |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 746 | // globalBounds is the global boundaries of this LayoutCoordinates after it's clipped by |
| 747 | // parents. We can think it as the global visible bounds of this Layout. Here globalBounds |
| 748 | // 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] | 749 | val boundsInWindow = boundsInWindow() |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 750 | return Rect( |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 751 | windowToLocal(boundsInWindow.topLeft), |
| 752 | windowToLocal(boundsInWindow.bottomRight) |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 753 | ) |
| 754 | } |
| 755 | |
haoyu | ac341f0 | 2021-01-22 22:01:56 -0800 | [diff] [blame] | 756 | internal fun Rect.containsInclusive(offset: Offset): Boolean = |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 757 | offset.x in left..right && offset.y in top..bottom |
Haoyu Zhang | a78608e | 2021-08-03 17:10:18 -0700 | [diff] [blame^] | 758 | |
| 759 | private fun performHapticFeedbackIfNeeded( |
| 760 | previousSelection: Selection?, |
| 761 | newSelection: Selection?, |
| 762 | hapticFeedback: HapticFeedback?, |
| 763 | ) { |
| 764 | if (hapticFeedback == null) { |
| 765 | return |
| 766 | } |
| 767 | if (previousSelection == null) { |
| 768 | if (newSelection != null) { |
| 769 | hapticFeedback.performHapticFeedback(HapticFeedbackType.TextHandleMove) |
| 770 | } |
| 771 | return |
| 772 | } |
| 773 | if (newSelection == null) { |
| 774 | // We know previousSelection is not null, so selection is updated. |
| 775 | hapticFeedback.performHapticFeedback(HapticFeedbackType.TextHandleMove) |
| 776 | return |
| 777 | } |
| 778 | // We don't care if rawOffset of Selection.AnchorInfo is different, so we only compare the |
| 779 | // other fields. If any of them changes, we need to perform hapticFeedback. |
| 780 | if (previousSelection.start.offset != newSelection.start.offset || |
| 781 | previousSelection.start.selectableId != newSelection.start.selectableId || |
| 782 | previousSelection.end.offset != newSelection.end.offset || |
| 783 | previousSelection.end.selectableId != previousSelection.end.selectableId |
| 784 | ) { |
| 785 | hapticFeedback.performHapticFeedback(HapticFeedbackType.TextHandleMove) |
| 786 | } |
| 787 | } |