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() |
| 245 | } |
haoyu | 9085c88 | 2020-12-08 12:01:06 -0800 | [diff] [blame] | 246 | |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 247 | selectionRegistrar.onSelectableChangeCallback = { selectableKey -> |
| 248 | if (selectableKey in selectionRegistrar.subselections) { |
haoyu | 9085c88 | 2020-12-08 12:01:06 -0800 | [diff] [blame] | 249 | // clear the selection range of each Selectable. |
| 250 | onRelease() |
| 251 | selection = null |
| 252 | } |
| 253 | } |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 254 | |
| 255 | selectionRegistrar.afterSelectableUnsubscribe = { selectableKey -> |
| 256 | if ( |
| 257 | selectableKey == selection?.start?.selectableId || |
| 258 | selectableKey == selection?.end?.selectableId |
| 259 | ) { |
| 260 | // The selectable that contains a selection handle just unsubscribed. |
| 261 | // Hide selection handles for now |
| 262 | startHandlePosition = null |
| 263 | endHandlePosition = null |
| 264 | } |
| 265 | } |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 266 | } |
| 267 | |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 268 | private fun currentSelectionStartPosition(): Offset? { |
| 269 | return selection?.let { selection -> |
| 270 | val startSelectable = |
| 271 | selectionRegistrar.selectableMap[selection.start.selectableId] |
| 272 | |
| 273 | requireContainerCoordinates().localPositionOf( |
| 274 | startSelectable?.getLayoutCoordinates()!!, |
| 275 | getAdjustedCoordinates( |
| 276 | startSelectable.getHandlePosition( |
| 277 | selection = selection, |
| 278 | isStartHandle = true |
| 279 | ) |
| 280 | ) |
| 281 | ) |
| 282 | } |
| 283 | } |
| 284 | |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 285 | private fun updateHandleOffsets() { |
| 286 | val selection = selection |
| 287 | val containerCoordinates = containerLayoutCoordinates |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 288 | val startSelectable = selection?.start?.selectableId?.let { |
| 289 | selectionRegistrar.selectableMap[it] |
| 290 | } |
| 291 | val endSelectable = selection?.end?.selectableId?.let { |
| 292 | selectionRegistrar.selectableMap[it] |
| 293 | } |
| 294 | val startLayoutCoordinates = startSelectable?.getLayoutCoordinates() |
| 295 | val endLayoutCoordinates = endSelectable?.getLayoutCoordinates() |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 296 | if ( |
| 297 | selection == null || |
| 298 | containerCoordinates == null || |
| 299 | !containerCoordinates.isAttached || |
| 300 | startLayoutCoordinates == null || |
| 301 | endLayoutCoordinates == null |
| 302 | ) { |
| 303 | this.startHandlePosition = null |
| 304 | this.endHandlePosition = null |
| 305 | return |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 306 | } |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 307 | |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 308 | val startHandlePosition = containerCoordinates.localPositionOf( |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 309 | startLayoutCoordinates, |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 310 | startSelectable.getHandlePosition( |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 311 | selection = selection, |
| 312 | isStartHandle = true |
| 313 | ) |
| 314 | ) |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 315 | val endHandlePosition = containerCoordinates.localPositionOf( |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 316 | endLayoutCoordinates, |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 317 | endSelectable.getHandlePosition( |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 318 | selection = selection, |
| 319 | isStartHandle = false |
| 320 | ) |
| 321 | ) |
| 322 | |
| 323 | val visibleBounds = containerCoordinates.visibleBounds() |
| 324 | this.startHandlePosition = |
| 325 | if (visibleBounds.containsInclusive(startHandlePosition)) startHandlePosition else null |
| 326 | this.endHandlePosition = |
| 327 | if (visibleBounds.containsInclusive(endHandlePosition)) endHandlePosition else null |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Returns non-nullable [containerLayoutCoordinates]. |
| 332 | */ |
| 333 | internal fun requireContainerCoordinates(): LayoutCoordinates { |
| 334 | val coordinates = containerLayoutCoordinates |
| 335 | require(coordinates != null) |
| 336 | require(coordinates.isAttached) |
| 337 | return coordinates |
| 338 | } |
| 339 | |
| 340 | /** |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 341 | * Iterates over the handlers, gets the selection for each Composable, and merges all the |
| 342 | * returned [Selection]s. |
| 343 | * |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 344 | * @param startPosition [Offset] for the start of the selection |
| 345 | * @param endPosition [Offset] for the end of the selection |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 346 | * @param previousSelection previous selection |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 347 | * |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 348 | * @return a [Pair] of a [Selection] object which is constructed by combining all |
| 349 | * composables that are selected and a [Map] from selectable key to [Selection]s on the |
| 350 | * [Selectable] corresponding to the that key. |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 351 | */ |
Qingqing Deng | 5819ff6 | 2019-11-18 15:26:23 -0800 | [diff] [blame] | 352 | // This function is internal for testing purposes. |
| 353 | internal fun mergeSelections( |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 354 | startPosition: Offset, |
| 355 | endPosition: Offset, |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 356 | adjustment: SelectionAdjustment = SelectionAdjustment.NONE, |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 357 | previousSelection: Selection? = null, |
Qingqing Deng | ff65ed6 | 2020-01-06 17:55:48 -0800 | [diff] [blame] | 358 | isStartHandle: Boolean = true |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 359 | ): Pair<Selection?, Map<Long, Selection>> { |
| 360 | val subselections = mutableMapOf<Long, Selection>() |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 361 | val newSelection = selectionRegistrar.sort(requireContainerCoordinates()) |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 362 | .fastFold(null) { mergedSelection: Selection?, selectable: Selectable -> |
| 363 | val selection = selectable.getSelection( |
| 364 | startPosition = startPosition, |
| 365 | endPosition = endPosition, |
| 366 | containerLayoutCoordinates = requireContainerCoordinates(), |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 367 | previousSelection = previousSelection, |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 368 | isStartHandle = isStartHandle, |
Haoyu Zhang | 4c6c637 | 2021-04-20 10:25:21 -0700 | [diff] [blame] | 369 | adjustment = adjustment |
Siyamed Sinir | 700df845 | 2019-10-22 20:23:58 -0700 | [diff] [blame] | 370 | ) |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 371 | selection?.let { subselections[selectable.selectableId] = it } |
| 372 | merge(mergedSelection, selection) |
Qingqing Deng | 247f2b4 | 2019-12-12 19:48:37 -0800 | [diff] [blame] | 373 | } |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 374 | if (previousSelection != newSelection) hapticFeedBack?.performHapticFeedback( |
| 375 | HapticFeedbackType.TextHandleMove |
| 376 | ) |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 377 | return Pair(newSelection, subselections) |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Qingqing Deng | 6bc981c | 2021-05-11 22:43:51 -0700 | [diff] [blame] | 380 | internal fun mergeSelections( |
| 381 | previousSelection: Selection? = null, |
| 382 | selectableId: Long |
| 383 | ): Pair<Selection?, Map<Long, Selection>> { |
| 384 | val subselections = mutableMapOf<Long, Selection>() |
| 385 | val newSelection = selectionRegistrar.sort(requireContainerCoordinates()) |
| 386 | .fastFold(null) { mergedSelection: Selection?, selectable: Selectable -> |
| 387 | val selection = if (selectable.selectableId == selectableId) |
| 388 | selectable.getSelectAllSelection() else null |
| 389 | selection?.let { subselections[selectable.selectableId] = it } |
| 390 | merge(mergedSelection, selection) |
| 391 | } |
| 392 | if (previousSelection != newSelection) hapticFeedBack?.performHapticFeedback( |
| 393 | HapticFeedbackType.TextHandleMove |
| 394 | ) |
| 395 | return Pair(newSelection, subselections) |
| 396 | } |
| 397 | |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 398 | internal fun getSelectedText(): AnnotatedString? { |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 399 | val selectables = selectionRegistrar.sort(requireContainerCoordinates()) |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 400 | var selectedText: AnnotatedString? = null |
| 401 | |
| 402 | selection?.let { |
Louis Pullen-Freilich | c6e92aa | 2021-03-08 00:42:16 +0000 | [diff] [blame] | 403 | for (i in selectables.indices) { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 404 | val selectable = selectables[i] |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 405 | // Continue if the current selectable is before the selection starts. |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 406 | if (selectable.selectableId != it.start.selectableId && |
| 407 | selectable.selectableId != it.end.selectableId && |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 408 | selectedText == null |
| 409 | ) continue |
| 410 | |
| 411 | val currentSelectedText = getCurrentSelectedText( |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 412 | selectable = selectable, |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 413 | selection = it |
| 414 | ) |
| 415 | selectedText = selectedText?.plus(currentSelectedText) ?: currentSelectedText |
| 416 | |
| 417 | // Break if the current selectable is the last selected selectable. |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 418 | if (selectable.selectableId == it.end.selectableId && !it.handlesCrossed || |
| 419 | selectable.selectableId == it.start.selectableId && it.handlesCrossed |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 420 | ) break |
| 421 | } |
| 422 | } |
| 423 | return selectedText |
| 424 | } |
| 425 | |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 426 | internal fun copy() { |
Qingqing Deng | f2d0a2d | 2020-03-12 14:19:50 -0700 | [diff] [blame] | 427 | val selectedText = getSelectedText() |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 428 | selectedText?.let { clipboardManager?.setText(it) } |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * This function get the selected region as a Rectangle region, and pass it to [TextToolbar] |
| 433 | * to make the FloatingToolbar show up in the proper place. In addition, this function passes |
| 434 | * the copy method as a callback when "copy" is clicked. |
| 435 | */ |
| 436 | internal fun showSelectionToolbar() { |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 437 | if (hasFocus) { |
| 438 | selection?.let { |
| 439 | textToolbar?.showMenu( |
| 440 | getContentRect(), |
| 441 | onCopyRequested = { |
| 442 | copy() |
| 443 | onRelease() |
| 444 | } |
| 445 | ) |
| 446 | } |
Qingqing Deng | a89450d | 2020-04-03 19:11:49 -0700 | [diff] [blame] | 447 | } |
Qingqing Deng | f2d0a2d | 2020-03-12 14:19:50 -0700 | [diff] [blame] | 448 | } |
| 449 | |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 450 | internal fun hideSelectionToolbar() { |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 451 | if (hasFocus && textToolbar?.status == TextToolbarStatus.Shown) { |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 452 | textToolbar?.hide() |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
| 456 | private fun updateSelectionToolbarPosition() { |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 457 | if (hasFocus && textToolbar?.status == TextToolbarStatus.Shown) { |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 458 | showSelectionToolbar() |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * Calculate selected region as [Rect]. The top is the top of the first selected |
| 464 | * line, and the bottom is the bottom of the last selected line. The left is the leftmost |
| 465 | * handle's horizontal coordinates, and the right is the rightmost handle's coordinates. |
| 466 | */ |
| 467 | private fun getContentRect(): Rect { |
Nader Jawad | 8432b8d | 2020-07-27 14:51:23 -0700 | [diff] [blame] | 468 | val selection = selection ?: return Rect.Zero |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 469 | val startSelectable = selectionRegistrar.selectableMap[selection.start.selectableId] |
| 470 | val endSelectable = selectionRegistrar.selectableMap[selection.start.selectableId] |
| 471 | val startLayoutCoordinates = startSelectable?.getLayoutCoordinates() ?: return Rect.Zero |
| 472 | val endLayoutCoordinates = endSelectable?.getLayoutCoordinates() ?: return Rect.Zero |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 473 | |
| 474 | val localLayoutCoordinates = containerLayoutCoordinates |
| 475 | if (localLayoutCoordinates != null && localLayoutCoordinates.isAttached) { |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 476 | var startOffset = localLayoutCoordinates.localPositionOf( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 477 | startLayoutCoordinates, |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 478 | startSelectable.getHandlePosition( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 479 | selection = selection, |
| 480 | isStartHandle = true |
| 481 | ) |
| 482 | ) |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 483 | var endOffset = localLayoutCoordinates.localPositionOf( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 484 | endLayoutCoordinates, |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 485 | endSelectable.getHandlePosition( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 486 | selection = selection, |
| 487 | isStartHandle = false |
| 488 | ) |
| 489 | ) |
| 490 | |
| 491 | startOffset = localLayoutCoordinates.localToRoot(startOffset) |
| 492 | endOffset = localLayoutCoordinates.localToRoot(endOffset) |
| 493 | |
| 494 | val left = min(startOffset.x, endOffset.x) |
| 495 | val right = max(startOffset.x, endOffset.x) |
| 496 | |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 497 | var startTop = localLayoutCoordinates.localPositionOf( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 498 | startLayoutCoordinates, |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 499 | Offset( |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 500 | 0f, |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 501 | startSelectable.getBoundingBox(selection.start.offset).top |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 502 | ) |
| 503 | ) |
| 504 | |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 505 | var endTop = localLayoutCoordinates.localPositionOf( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 506 | endLayoutCoordinates, |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 507 | Offset( |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 508 | 0.0f, |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 509 | endSelectable.getBoundingBox(selection.end.offset).top |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 510 | ) |
| 511 | ) |
| 512 | |
| 513 | startTop = localLayoutCoordinates.localToRoot(startTop) |
| 514 | endTop = localLayoutCoordinates.localToRoot(endTop) |
| 515 | |
| 516 | val top = min(startTop.y, endTop.y) |
Nader Jawad | 16e330a | 2020-05-21 21:21:01 -0700 | [diff] [blame] | 517 | 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] | 518 | |
| 519 | return Rect( |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 520 | left, |
| 521 | top, |
| 522 | right, |
| 523 | bottom |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 524 | ) |
| 525 | } |
Nader Jawad | 8432b8d | 2020-07-27 14:51:23 -0700 | [diff] [blame] | 526 | return Rect.Zero |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Qingqing Deng | b6f5d8a | 2019-11-11 18:19:22 -0800 | [diff] [blame] | 529 | // This is for PressGestureDetector to cancel the selection. |
| 530 | fun onRelease() { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 531 | selectionRegistrar.subselections = emptyMap() |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 532 | hideSelectionToolbar() |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 533 | if (selection != null) { |
| 534 | onSelectionChange(null) |
| 535 | hapticFeedBack?.performHapticFeedback(HapticFeedbackType.TextHandleMove) |
| 536 | } |
Qingqing Deng | b6f5d8a | 2019-11-11 18:19:22 -0800 | [diff] [blame] | 537 | } |
| 538 | |
Matvei Malkov | f770a91 | 2021-03-24 18:12:41 +0000 | [diff] [blame] | 539 | fun handleDragObserver(isStartHandle: Boolean): TextDragObserver { |
| 540 | return object : TextDragObserver { |
| 541 | override fun onStart(startPoint: Offset) { |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 542 | hideSelectionToolbar() |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 543 | val selection = selection!! |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 544 | val startSelectable = |
| 545 | selectionRegistrar.selectableMap[selection.start.selectableId] |
| 546 | val endSelectable = |
| 547 | selectionRegistrar.selectableMap[selection.end.selectableId] |
Louis Pullen-Freilich | 5da28bd | 2019-10-15 17:05:07 +0100 | [diff] [blame] | 548 | // The LayoutCoordinates of the composable where the drag gesture should begin. This |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 549 | // 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] | 550 | // composable coordinates to selection container coordinates. |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 551 | val beginLayoutCoordinates = if (isStartHandle) { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 552 | startSelectable?.getLayoutCoordinates()!! |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 553 | } else { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 554 | endSelectable?.getLayoutCoordinates()!! |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 555 | } |
| 556 | |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 557 | // 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] | 558 | // the composable coordinates. |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 559 | val beginCoordinates = getAdjustedCoordinates( |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 560 | if (isStartHandle) { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 561 | startSelectable!!.getHandlePosition( |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 562 | selection = selection, isStartHandle = true |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 563 | ) |
| 564 | } else { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 565 | endSelectable!!.getHandlePosition( |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 566 | selection = selection, isStartHandle = false |
| 567 | ) |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 568 | } |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 569 | ) |
| 570 | |
Louis Pullen-Freilich | 5da28bd | 2019-10-15 17:05:07 +0100 | [diff] [blame] | 571 | // Convert the position where drag gesture begins from composable coordinates to |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 572 | // selection container coordinates. |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 573 | dragBeginPosition = requireContainerCoordinates().localPositionOf( |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 574 | beginLayoutCoordinates, |
| 575 | beginCoordinates |
| 576 | ) |
| 577 | |
| 578 | // Zero out the total distance that being dragged. |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 579 | dragTotalDistance = Offset.Zero |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Matvei Malkov | f770a91 | 2021-03-24 18:12:41 +0000 | [diff] [blame] | 582 | override fun onDrag(delta: Offset) { |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 583 | val selection = selection!! |
Matvei Malkov | f770a91 | 2021-03-24 18:12:41 +0000 | [diff] [blame] | 584 | dragTotalDistance += delta |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 585 | val startSelectable = |
| 586 | selectionRegistrar.selectableMap[selection.start.selectableId] |
| 587 | val endSelectable = |
| 588 | selectionRegistrar.selectableMap[selection.end.selectableId] |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 589 | val currentStart = if (isStartHandle) { |
| 590 | dragBeginPosition + dragTotalDistance |
| 591 | } else { |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 592 | requireContainerCoordinates().localPositionOf( |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 593 | startSelectable?.getLayoutCoordinates()!!, |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 594 | getAdjustedCoordinates( |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 595 | startSelectable.getHandlePosition( |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 596 | selection = selection, |
| 597 | isStartHandle = true |
| 598 | ) |
| 599 | ) |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 600 | ) |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 601 | } |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 602 | |
| 603 | val currentEnd = if (isStartHandle) { |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 604 | requireContainerCoordinates().localPositionOf( |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 605 | endSelectable?.getLayoutCoordinates()!!, |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 606 | getAdjustedCoordinates( |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 607 | endSelectable.getHandlePosition( |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 608 | selection = selection, |
| 609 | isStartHandle = false |
| 610 | ) |
| 611 | ) |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 612 | ) |
| 613 | } else { |
| 614 | dragBeginPosition + dragTotalDistance |
| 615 | } |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 616 | updateSelection( |
Qingqing Deng | a5d8095 | 2019-10-11 16:46:52 -0700 | [diff] [blame] | 617 | startPosition = currentStart, |
| 618 | endPosition = currentEnd, |
Qingqing Deng | ff65ed6 | 2020-01-06 17:55:48 -0800 | [diff] [blame] | 619 | isStartHandle = isStartHandle |
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() |
| 626 | } |
| 627 | |
| 628 | override fun onCancel() { |
| 629 | showSelectionToolbar() |
| 630 | } |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 631 | } |
| 632 | } |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 633 | |
Ralston Da Silva | de62bc6 | 2021-06-02 17:46:44 -0700 | [diff] [blame^] | 634 | /** |
| 635 | * Detect tap without consuming the up event. |
| 636 | */ |
| 637 | private suspend fun PointerInputScope.detectNonConsumingTap(onTap: (Offset) -> Unit) { |
| 638 | forEachGesture { |
| 639 | coroutineScope { |
| 640 | awaitPointerEventScope { |
| 641 | waitForUpOrCancellation()?.let { |
| 642 | onTap(it.position) |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | private fun Modifier.onClearSelectionRequested(block: () -> Unit): Modifier { |
| 650 | return if (hasFocus) pointerInput(Unit) { detectNonConsumingTap { block() } } else this |
| 651 | } |
| 652 | |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 653 | private fun convertToContainerCoordinates( |
| 654 | layoutCoordinates: LayoutCoordinates, |
| 655 | offset: Offset |
| 656 | ): Offset? { |
| 657 | val coordinates = containerLayoutCoordinates |
| 658 | if (coordinates == null || !coordinates.isAttached) return null |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 659 | return requireContainerCoordinates().localPositionOf(layoutCoordinates, offset) |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | private fun updateSelection( |
| 663 | startPosition: Offset?, |
| 664 | endPosition: Offset?, |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 665 | adjustment: SelectionAdjustment = SelectionAdjustment.NONE, |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 666 | isStartHandle: Boolean = true |
| 667 | ) { |
| 668 | if (startPosition == null || endPosition == null) return |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 669 | val (newSelection, newSubselection) = mergeSelections( |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 670 | startPosition = startPosition, |
| 671 | endPosition = endPosition, |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 672 | adjustment = adjustment, |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 673 | isStartHandle = isStartHandle, |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 674 | previousSelection = selection, |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 675 | ) |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 676 | if (newSelection != selection) { |
| 677 | selectionRegistrar.subselections = newSubselection |
| 678 | onSelectionChange(newSelection) |
| 679 | } |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 680 | } |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Andrey Rudenko | 5dc7aad | 2020-09-18 10:33:37 +0200 | [diff] [blame] | 683 | internal fun merge(lhs: Selection?, rhs: Selection?): Selection? { |
Siyamed Sinir | 700df845 | 2019-10-22 20:23:58 -0700 | [diff] [blame] | 684 | return lhs?.merge(rhs) ?: rhs |
| 685 | } |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 686 | |
Andrey Rudenko | 8b4981e | 2021-01-29 15:31:59 +0100 | [diff] [blame] | 687 | internal expect fun isCopyKeyEvent(keyEvent: KeyEvent): Boolean |
| 688 | |
Andrey Rudenko | 5dc7aad | 2020-09-18 10:33:37 +0200 | [diff] [blame] | 689 | internal fun getCurrentSelectedText( |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 690 | selectable: Selectable, |
| 691 | selection: Selection |
| 692 | ): AnnotatedString { |
| 693 | val currentText = selectable.getText() |
| 694 | |
| 695 | return if ( |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 696 | selectable.selectableId != selection.start.selectableId && |
| 697 | selectable.selectableId != selection.end.selectableId |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 698 | ) { |
| 699 | // Select the full text content if the current selectable is between the |
| 700 | // start and the end selectables. |
| 701 | currentText |
| 702 | } else if ( |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 703 | selectable.selectableId == selection.start.selectableId && |
| 704 | selectable.selectableId == selection.end.selectableId |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 705 | ) { |
| 706 | // Select partial text content if the current selectable is the start and |
| 707 | // the end selectable. |
| 708 | if (selection.handlesCrossed) { |
| 709 | currentText.subSequence(selection.end.offset, selection.start.offset) |
| 710 | } else { |
| 711 | currentText.subSequence(selection.start.offset, selection.end.offset) |
| 712 | } |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 713 | } else if (selectable.selectableId == selection.start.selectableId) { |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 714 | // Select partial text content if the current selectable is the start |
| 715 | // selectable. |
| 716 | if (selection.handlesCrossed) { |
| 717 | currentText.subSequence(0, selection.start.offset) |
| 718 | } else { |
| 719 | currentText.subSequence(selection.start.offset, currentText.length) |
| 720 | } |
| 721 | } else { |
| 722 | // Selectable partial text content if the current selectable is the end |
| 723 | // selectable. |
| 724 | if (selection.handlesCrossed) { |
| 725 | currentText.subSequence(selection.end.offset, currentText.length) |
| 726 | } else { |
| 727 | currentText.subSequence(0, selection.end.offset) |
| 728 | } |
| 729 | } |
| 730 | } |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 731 | |
| 732 | /** Returns the boundary of the visible area in this [LayoutCoordinates]. */ |
haoyu | ac341f0 | 2021-01-22 22:01:56 -0800 | [diff] [blame] | 733 | internal fun LayoutCoordinates.visibleBounds(): Rect { |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 734 | // globalBounds is the global boundaries of this LayoutCoordinates after it's clipped by |
| 735 | // parents. We can think it as the global visible bounds of this Layout. Here globalBounds |
| 736 | // 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] | 737 | val boundsInWindow = boundsInWindow() |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 738 | return Rect( |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 739 | windowToLocal(boundsInWindow.topLeft), |
| 740 | windowToLocal(boundsInWindow.bottomRight) |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 741 | ) |
| 742 | } |
| 743 | |
haoyu | ac341f0 | 2021-01-22 22:01:56 -0800 | [diff] [blame] | 744 | internal fun Rect.containsInclusive(offset: Offset): Boolean = |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 745 | offset.x in left..right && offset.y in top..bottom |