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 = |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame^] | 195 | { layoutCoordinates, position, selectionMode -> |
| 196 | val positionInContainer = convertToContainerCoordinates( |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 197 | layoutCoordinates, |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame^] | 198 | position |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 199 | ) |
| 200 | |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame^] | 201 | if (positionInContainer != null) { |
| 202 | startSelection( |
| 203 | position = positionInContainer, |
| 204 | isStartHandle = false, |
| 205 | adjustment = selectionMode |
| 206 | ) |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 207 | |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame^] | 208 | focusRequester.requestFocus() |
| 209 | hideSelectionToolbar() |
| 210 | } |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 211 | } |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 212 | |
Qingqing Deng | 6bc981c | 2021-05-11 22:43:51 -0700 | [diff] [blame] | 213 | selectionRegistrar.onSelectionUpdateSelectAll = |
| 214 | { selectableId -> |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame^] | 215 | val (newSelection, newSubselection) = selectAll( |
Qingqing Deng | 6bc981c | 2021-05-11 22:43:51 -0700 | [diff] [blame] | 216 | selectableId = selectableId, |
| 217 | previousSelection = selection, |
| 218 | ) |
| 219 | if (newSelection != selection) { |
| 220 | selectionRegistrar.subselections = newSubselection |
| 221 | onSelectionChange(newSelection) |
| 222 | } |
| 223 | |
| 224 | focusRequester.requestFocus() |
| 225 | hideSelectionToolbar() |
| 226 | } |
| 227 | |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 228 | selectionRegistrar.onSelectionUpdateCallback = |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame^] | 229 | { layoutCoordinates, newPosition, previousPosition, isStartHandle, selectionMode -> |
| 230 | val newPositionInContainer = |
| 231 | convertToContainerCoordinates(layoutCoordinates, newPosition) |
| 232 | val previousPositionInContainer = |
| 233 | convertToContainerCoordinates(layoutCoordinates, previousPosition) |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 234 | |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 235 | updateSelection( |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame^] | 236 | newPosition = newPositionInContainer, |
| 237 | previousPosition = previousPositionInContainer, |
| 238 | isStartHandle = isStartHandle, |
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 | |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame^] | 340 | internal fun selectAll( |
| 341 | selectableId: Long, |
| 342 | previousSelection: Selection? |
Qingqing Deng | 6bc981c | 2021-05-11 22:43:51 -0700 | [diff] [blame] | 343 | ): Pair<Selection?, Map<Long, Selection>> { |
| 344 | val subselections = mutableMapOf<Long, Selection>() |
| 345 | val newSelection = selectionRegistrar.sort(requireContainerCoordinates()) |
| 346 | .fastFold(null) { mergedSelection: Selection?, selectable: Selectable -> |
| 347 | val selection = if (selectable.selectableId == selectableId) |
| 348 | selectable.getSelectAllSelection() else null |
| 349 | selection?.let { subselections[selectable.selectableId] = it } |
| 350 | merge(mergedSelection, selection) |
| 351 | } |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame^] | 352 | if (newSelection != previousSelection) { |
| 353 | hapticFeedBack?.performHapticFeedback(HapticFeedbackType.TextHandleMove) |
| 354 | } |
Qingqing Deng | 6bc981c | 2021-05-11 22:43:51 -0700 | [diff] [blame] | 355 | return Pair(newSelection, subselections) |
| 356 | } |
| 357 | |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 358 | internal fun getSelectedText(): AnnotatedString? { |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 359 | val selectables = selectionRegistrar.sort(requireContainerCoordinates()) |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 360 | var selectedText: AnnotatedString? = null |
| 361 | |
| 362 | selection?.let { |
Louis Pullen-Freilich | c6e92aa | 2021-03-08 00:42:16 +0000 | [diff] [blame] | 363 | for (i in selectables.indices) { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 364 | val selectable = selectables[i] |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 365 | // Continue if the current selectable is before the selection starts. |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 366 | if (selectable.selectableId != it.start.selectableId && |
| 367 | selectable.selectableId != it.end.selectableId && |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 368 | selectedText == null |
| 369 | ) continue |
| 370 | |
| 371 | val currentSelectedText = getCurrentSelectedText( |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 372 | selectable = selectable, |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 373 | selection = it |
| 374 | ) |
| 375 | selectedText = selectedText?.plus(currentSelectedText) ?: currentSelectedText |
| 376 | |
| 377 | // Break if the current selectable is the last selected selectable. |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 378 | if (selectable.selectableId == it.end.selectableId && !it.handlesCrossed || |
| 379 | selectable.selectableId == it.start.selectableId && it.handlesCrossed |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 380 | ) break |
| 381 | } |
| 382 | } |
| 383 | return selectedText |
| 384 | } |
| 385 | |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 386 | internal fun copy() { |
Qingqing Deng | f2d0a2d | 2020-03-12 14:19:50 -0700 | [diff] [blame] | 387 | val selectedText = getSelectedText() |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 388 | selectedText?.let { clipboardManager?.setText(it) } |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * This function get the selected region as a Rectangle region, and pass it to [TextToolbar] |
| 393 | * to make the FloatingToolbar show up in the proper place. In addition, this function passes |
| 394 | * the copy method as a callback when "copy" is clicked. |
| 395 | */ |
| 396 | internal fun showSelectionToolbar() { |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 397 | if (hasFocus) { |
| 398 | selection?.let { |
| 399 | textToolbar?.showMenu( |
| 400 | getContentRect(), |
| 401 | onCopyRequested = { |
| 402 | copy() |
| 403 | onRelease() |
| 404 | } |
| 405 | ) |
| 406 | } |
Qingqing Deng | a89450d | 2020-04-03 19:11:49 -0700 | [diff] [blame] | 407 | } |
Qingqing Deng | f2d0a2d | 2020-03-12 14:19:50 -0700 | [diff] [blame] | 408 | } |
| 409 | |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 410 | internal fun hideSelectionToolbar() { |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 411 | if (hasFocus && textToolbar?.status == TextToolbarStatus.Shown) { |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 412 | textToolbar?.hide() |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
| 416 | private fun updateSelectionToolbarPosition() { |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 417 | if (hasFocus && textToolbar?.status == TextToolbarStatus.Shown) { |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 418 | showSelectionToolbar() |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Calculate selected region as [Rect]. The top is the top of the first selected |
| 424 | * line, and the bottom is the bottom of the last selected line. The left is the leftmost |
| 425 | * handle's horizontal coordinates, and the right is the rightmost handle's coordinates. |
| 426 | */ |
| 427 | private fun getContentRect(): Rect { |
Nader Jawad | 8432b8d | 2020-07-27 14:51:23 -0700 | [diff] [blame] | 428 | val selection = selection ?: return Rect.Zero |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 429 | val startSelectable = selectionRegistrar.selectableMap[selection.start.selectableId] |
| 430 | val endSelectable = selectionRegistrar.selectableMap[selection.start.selectableId] |
| 431 | val startLayoutCoordinates = startSelectable?.getLayoutCoordinates() ?: return Rect.Zero |
| 432 | val endLayoutCoordinates = endSelectable?.getLayoutCoordinates() ?: return Rect.Zero |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 433 | |
| 434 | val localLayoutCoordinates = containerLayoutCoordinates |
| 435 | if (localLayoutCoordinates != null && localLayoutCoordinates.isAttached) { |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 436 | var startOffset = localLayoutCoordinates.localPositionOf( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 437 | startLayoutCoordinates, |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 438 | startSelectable.getHandlePosition( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 439 | selection = selection, |
| 440 | isStartHandle = true |
| 441 | ) |
| 442 | ) |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 443 | var endOffset = localLayoutCoordinates.localPositionOf( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 444 | endLayoutCoordinates, |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 445 | endSelectable.getHandlePosition( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 446 | selection = selection, |
| 447 | isStartHandle = false |
| 448 | ) |
| 449 | ) |
| 450 | |
| 451 | startOffset = localLayoutCoordinates.localToRoot(startOffset) |
| 452 | endOffset = localLayoutCoordinates.localToRoot(endOffset) |
| 453 | |
| 454 | val left = min(startOffset.x, endOffset.x) |
| 455 | val right = max(startOffset.x, endOffset.x) |
| 456 | |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 457 | var startTop = localLayoutCoordinates.localPositionOf( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 458 | startLayoutCoordinates, |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 459 | Offset( |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 460 | 0f, |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 461 | startSelectable.getBoundingBox(selection.start.offset).top |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 462 | ) |
| 463 | ) |
| 464 | |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 465 | var endTop = localLayoutCoordinates.localPositionOf( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 466 | endLayoutCoordinates, |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 467 | Offset( |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 468 | 0.0f, |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 469 | endSelectable.getBoundingBox(selection.end.offset).top |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 470 | ) |
| 471 | ) |
| 472 | |
| 473 | startTop = localLayoutCoordinates.localToRoot(startTop) |
| 474 | endTop = localLayoutCoordinates.localToRoot(endTop) |
| 475 | |
| 476 | val top = min(startTop.y, endTop.y) |
Haoyu Zhang | d47f1fe | 2021-07-13 03:10:55 -0700 | [diff] [blame] | 477 | val bottom = max(startOffset.y, endOffset.y) + (HandleHeight.value * 4.0).toFloat() |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 478 | |
| 479 | return Rect( |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 480 | left, |
| 481 | top, |
| 482 | right, |
| 483 | bottom |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 484 | ) |
| 485 | } |
Nader Jawad | 8432b8d | 2020-07-27 14:51:23 -0700 | [diff] [blame] | 486 | return Rect.Zero |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Qingqing Deng | b6f5d8a | 2019-11-11 18:19:22 -0800 | [diff] [blame] | 489 | // This is for PressGestureDetector to cancel the selection. |
| 490 | fun onRelease() { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 491 | selectionRegistrar.subselections = emptyMap() |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 492 | hideSelectionToolbar() |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 493 | if (selection != null) { |
| 494 | onSelectionChange(null) |
| 495 | hapticFeedBack?.performHapticFeedback(HapticFeedbackType.TextHandleMove) |
| 496 | } |
Qingqing Deng | b6f5d8a | 2019-11-11 18:19:22 -0800 | [diff] [blame] | 497 | } |
| 498 | |
Matvei Malkov | f770a91 | 2021-03-24 18:12:41 +0000 | [diff] [blame] | 499 | fun handleDragObserver(isStartHandle: Boolean): TextDragObserver { |
| 500 | return object : TextDragObserver { |
| 501 | override fun onStart(startPoint: Offset) { |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 502 | hideSelectionToolbar() |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 503 | val selection = selection!! |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 504 | val startSelectable = |
| 505 | selectionRegistrar.selectableMap[selection.start.selectableId] |
| 506 | val endSelectable = |
| 507 | selectionRegistrar.selectableMap[selection.end.selectableId] |
Louis Pullen-Freilich | 5da28bd | 2019-10-15 17:05:07 +0100 | [diff] [blame] | 508 | // The LayoutCoordinates of the composable where the drag gesture should begin. This |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 509 | // 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] | 510 | // composable coordinates to selection container coordinates. |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 511 | val beginLayoutCoordinates = if (isStartHandle) { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 512 | startSelectable?.getLayoutCoordinates()!! |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 513 | } else { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 514 | endSelectable?.getLayoutCoordinates()!! |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 517 | // 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] | 518 | // the composable coordinates. |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 519 | val beginCoordinates = getAdjustedCoordinates( |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 520 | if (isStartHandle) { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 521 | startSelectable!!.getHandlePosition( |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 522 | selection = selection, isStartHandle = true |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 523 | ) |
| 524 | } else { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 525 | endSelectable!!.getHandlePosition( |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 526 | selection = selection, isStartHandle = false |
| 527 | ) |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 528 | } |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 529 | ) |
| 530 | |
Louis Pullen-Freilich | 5da28bd | 2019-10-15 17:05:07 +0100 | [diff] [blame] | 531 | // Convert the position where drag gesture begins from composable coordinates to |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 532 | // selection container coordinates. |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 533 | dragBeginPosition = requireContainerCoordinates().localPositionOf( |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 534 | beginLayoutCoordinates, |
| 535 | beginCoordinates |
| 536 | ) |
| 537 | |
| 538 | // Zero out the total distance that being dragged. |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 539 | dragTotalDistance = Offset.Zero |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Matvei Malkov | f770a91 | 2021-03-24 18:12:41 +0000 | [diff] [blame] | 542 | override fun onDrag(delta: Offset) { |
Matvei Malkov | f770a91 | 2021-03-24 18:12:41 +0000 | [diff] [blame] | 543 | dragTotalDistance += delta |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame^] | 544 | val endPosition = dragBeginPosition + dragTotalDistance |
| 545 | val consumed = updateSelection( |
| 546 | newPosition = endPosition, |
| 547 | previousPosition = dragBeginPosition, |
Haoyu Zhang | 5740343b | 2021-07-15 11:29:27 -0700 | [diff] [blame] | 548 | isStartHandle = isStartHandle, |
Haoyu Zhang | a78608e | 2021-08-03 17:10:18 -0700 | [diff] [blame] | 549 | adjustment = SelectionAdjustment.CharacterWithWordAccelerate |
Qingqing Deng | a5d8095 | 2019-10-11 16:46:52 -0700 | [diff] [blame] | 550 | ) |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame^] | 551 | if (consumed) { |
| 552 | dragBeginPosition = endPosition |
| 553 | dragTotalDistance = Offset.Zero |
| 554 | } |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 555 | } |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 556 | |
Matvei Malkov | f770a91 | 2021-03-24 18:12:41 +0000 | [diff] [blame] | 557 | override fun onStop() { |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 558 | showSelectionToolbar() |
| 559 | } |
| 560 | |
| 561 | override fun onCancel() { |
| 562 | showSelectionToolbar() |
| 563 | } |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 564 | } |
| 565 | } |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 566 | |
Ralston Da Silva | de62bc6 | 2021-06-02 17:46:44 -0700 | [diff] [blame] | 567 | /** |
| 568 | * Detect tap without consuming the up event. |
| 569 | */ |
| 570 | private suspend fun PointerInputScope.detectNonConsumingTap(onTap: (Offset) -> Unit) { |
| 571 | forEachGesture { |
| 572 | coroutineScope { |
| 573 | awaitPointerEventScope { |
| 574 | waitForUpOrCancellation()?.let { |
| 575 | onTap(it.position) |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | private fun Modifier.onClearSelectionRequested(block: () -> Unit): Modifier { |
| 583 | return if (hasFocus) pointerInput(Unit) { detectNonConsumingTap { block() } } else this |
| 584 | } |
| 585 | |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 586 | private fun convertToContainerCoordinates( |
| 587 | layoutCoordinates: LayoutCoordinates, |
| 588 | offset: Offset |
| 589 | ): Offset? { |
| 590 | val coordinates = containerLayoutCoordinates |
| 591 | if (coordinates == null || !coordinates.isAttached) return null |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 592 | return requireContainerCoordinates().localPositionOf(layoutCoordinates, offset) |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame^] | 595 | /** |
| 596 | * Cancel the previous selection and start a new selection at the given [position]. |
| 597 | * It's used for long-press, double-click, triple-click and so on to start selection. |
| 598 | * |
| 599 | * @param position initial position of the selection. Both start and end handle is considered |
| 600 | * at this position. |
| 601 | * @param isStartHandle whether it's considered as the start handle moving. This parameter |
| 602 | * will influence the [SelectionAdjustment]'s behavior. For example, |
| 603 | * [SelectionAdjustment.Character] only adjust the moving handle. |
| 604 | * @param adjustment the selection adjustment. |
| 605 | */ |
| 606 | private fun startSelection( |
| 607 | position: Offset, |
| 608 | isStartHandle: Boolean, |
| 609 | adjustment: SelectionAdjustment |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 610 | ) { |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame^] | 611 | updateSelection( |
| 612 | startHandlePosition = position, |
| 613 | endHandlePosition = position, |
| 614 | previousHandlePosition = null, |
| 615 | isStartHandle = isStartHandle, |
| 616 | adjustment = adjustment |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 617 | ) |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 618 | } |
Haoyu Zhang | a78608e | 2021-08-03 17:10:18 -0700 | [diff] [blame] | 619 | |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame^] | 620 | /** |
| 621 | * Updates the selection after one of the selection handle moved. |
| 622 | * |
| 623 | * @param newPosition the new position of the moving selection handle. |
| 624 | * @param previousPosition the previous position of the moving selection handle. |
| 625 | * @param isStartHandle whether the moving selection handle is the start handle. |
| 626 | * @param adjustment the [SelectionAdjustment] used to adjust the raw selection range and |
| 627 | * produce the final selection range. |
| 628 | * |
| 629 | * @return a boolean representing whether the movement is consumed. |
| 630 | * |
| 631 | * @see SelectionAdjustment |
| 632 | */ |
| 633 | internal fun updateSelection( |
| 634 | newPosition: Offset?, |
| 635 | previousPosition: Offset?, |
| 636 | isStartHandle: Boolean, |
| 637 | adjustment: SelectionAdjustment, |
| 638 | ): Boolean { |
| 639 | if (newPosition == null) return false |
| 640 | val otherHandlePosition = selection?.let { selection -> |
| 641 | val otherSelectableId = if (isStartHandle) { |
| 642 | selection.end.selectableId |
| 643 | } else { |
| 644 | selection.start.selectableId |
| 645 | } |
| 646 | val otherSelectable = |
| 647 | selectionRegistrar.selectableMap[otherSelectableId] ?: return@let null |
| 648 | convertToContainerCoordinates( |
| 649 | otherSelectable.getLayoutCoordinates()!!, |
| 650 | getAdjustedCoordinates( |
| 651 | otherSelectable.getHandlePosition(selection, !isStartHandle) |
| 652 | ) |
Haoyu Zhang | a78608e | 2021-08-03 17:10:18 -0700 | [diff] [blame] | 653 | ) |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame^] | 654 | } ?: return false |
| 655 | |
| 656 | val startHandlePosition = if (isStartHandle) newPosition else otherHandlePosition |
| 657 | val endHandlePosition = if (isStartHandle) otherHandlePosition else newPosition |
| 658 | |
| 659 | return updateSelection( |
| 660 | startHandlePosition = startHandlePosition, |
| 661 | endHandlePosition = endHandlePosition, |
| 662 | previousHandlePosition = previousPosition, |
| 663 | isStartHandle = isStartHandle, |
| 664 | adjustment = adjustment |
| 665 | ) |
| 666 | } |
| 667 | |
| 668 | /** |
| 669 | * Updates the selection after one of the selection handle moved. |
| 670 | * |
| 671 | * To make sure that [SelectionAdjustment] works correctly, it's expected that only one |
| 672 | * selection handle is updated each time. The only exception is that when a new selection is |
| 673 | * started. In this case, [previousHandlePosition] is always null. |
| 674 | * |
| 675 | * @param startHandlePosition the position of the start selection handle. |
| 676 | * @param endHandlePosition the position of the end selection handle. |
| 677 | * @param previousHandlePosition the position of the moving handle before the update. |
| 678 | * @param isStartHandle whether the moving selection handle is the start handle. |
| 679 | * @param adjustment the [SelectionAdjustment] used to adjust the raw selection range and |
| 680 | * produce the final selection range. |
| 681 | * |
| 682 | * @return a boolean representing whether the movement is consumed. It's useful for the case |
| 683 | * where a selection handle is updating consecutively. When the return value is true, it's |
| 684 | * expected that the caller will update the [startHandlePosition] to be the given |
| 685 | * [endHandlePosition] in following calls. |
| 686 | * |
| 687 | * @see SelectionAdjustment |
| 688 | */ |
| 689 | internal fun updateSelection( |
| 690 | startHandlePosition: Offset, |
| 691 | endHandlePosition: Offset, |
| 692 | previousHandlePosition: Offset?, |
| 693 | isStartHandle: Boolean, |
| 694 | adjustment: SelectionAdjustment, |
| 695 | ): Boolean { |
| 696 | val newSubselections = mutableMapOf<Long, Selection>() |
| 697 | var moveConsumed = false |
| 698 | val newSelection = selectionRegistrar.sort(requireContainerCoordinates()) |
| 699 | .fastFold(null) { mergedSelection: Selection?, selectable: Selectable -> |
| 700 | val previousSubselection = |
| 701 | selectionRegistrar.subselections[selectable.selectableId] |
| 702 | val (selection, consumed) = selectable.updateSelection( |
| 703 | startHandlePosition = startHandlePosition, |
| 704 | endHandlePosition = endHandlePosition, |
| 705 | previousHandlePosition = previousHandlePosition, |
| 706 | isStartHandle = isStartHandle, |
| 707 | containerLayoutCoordinates = requireContainerCoordinates(), |
| 708 | adjustment = adjustment, |
| 709 | previousSelection = previousSubselection, |
| 710 | ) |
| 711 | |
| 712 | moveConsumed = moveConsumed || consumed |
| 713 | selection?.let { newSubselections[selectable.selectableId] = it } |
| 714 | merge(mergedSelection, selection) |
| 715 | } |
| 716 | if (newSelection != selection) { |
| 717 | hapticFeedBack?.performHapticFeedback(HapticFeedbackType.TextHandleMove) |
| 718 | selectionRegistrar.subselections = newSubselections |
Haoyu Zhang | a78608e | 2021-08-03 17:10:18 -0700 | [diff] [blame] | 719 | onSelectionChange(newSelection) |
| 720 | } |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame^] | 721 | return moveConsumed |
Haoyu Zhang | a78608e | 2021-08-03 17:10:18 -0700 | [diff] [blame] | 722 | } |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 723 | } |
| 724 | |
Andrey Rudenko | 5dc7aad | 2020-09-18 10:33:37 +0200 | [diff] [blame] | 725 | internal fun merge(lhs: Selection?, rhs: Selection?): Selection? { |
Siyamed Sinir | 700df845 | 2019-10-22 20:23:58 -0700 | [diff] [blame] | 726 | return lhs?.merge(rhs) ?: rhs |
| 727 | } |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 728 | |
Andrey Rudenko | 8b4981e | 2021-01-29 15:31:59 +0100 | [diff] [blame] | 729 | internal expect fun isCopyKeyEvent(keyEvent: KeyEvent): Boolean |
| 730 | |
Andrey Rudenko | 5dc7aad | 2020-09-18 10:33:37 +0200 | [diff] [blame] | 731 | internal fun getCurrentSelectedText( |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 732 | selectable: Selectable, |
| 733 | selection: Selection |
| 734 | ): AnnotatedString { |
| 735 | val currentText = selectable.getText() |
| 736 | |
| 737 | return if ( |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 738 | selectable.selectableId != selection.start.selectableId && |
| 739 | selectable.selectableId != selection.end.selectableId |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 740 | ) { |
| 741 | // Select the full text content if the current selectable is between the |
| 742 | // start and the end selectables. |
| 743 | currentText |
| 744 | } else if ( |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 745 | selectable.selectableId == selection.start.selectableId && |
| 746 | selectable.selectableId == selection.end.selectableId |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 747 | ) { |
| 748 | // Select partial text content if the current selectable is the start and |
| 749 | // the end selectable. |
| 750 | if (selection.handlesCrossed) { |
| 751 | currentText.subSequence(selection.end.offset, selection.start.offset) |
| 752 | } else { |
| 753 | currentText.subSequence(selection.start.offset, selection.end.offset) |
| 754 | } |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 755 | } else if (selectable.selectableId == selection.start.selectableId) { |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 756 | // Select partial text content if the current selectable is the start |
| 757 | // selectable. |
| 758 | if (selection.handlesCrossed) { |
| 759 | currentText.subSequence(0, selection.start.offset) |
| 760 | } else { |
| 761 | currentText.subSequence(selection.start.offset, currentText.length) |
| 762 | } |
| 763 | } else { |
| 764 | // Selectable partial text content if the current selectable is the end |
| 765 | // selectable. |
| 766 | if (selection.handlesCrossed) { |
| 767 | currentText.subSequence(selection.end.offset, currentText.length) |
| 768 | } else { |
| 769 | currentText.subSequence(0, selection.end.offset) |
| 770 | } |
| 771 | } |
| 772 | } |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 773 | |
| 774 | /** Returns the boundary of the visible area in this [LayoutCoordinates]. */ |
haoyu | ac341f0 | 2021-01-22 22:01:56 -0800 | [diff] [blame] | 775 | internal fun LayoutCoordinates.visibleBounds(): Rect { |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 776 | // globalBounds is the global boundaries of this LayoutCoordinates after it's clipped by |
| 777 | // parents. We can think it as the global visible bounds of this Layout. Here globalBounds |
| 778 | // 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] | 779 | val boundsInWindow = boundsInWindow() |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 780 | return Rect( |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 781 | windowToLocal(boundsInWindow.topLeft), |
| 782 | windowToLocal(boundsInWindow.bottomRight) |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 783 | ) |
| 784 | } |
| 785 | |
haoyu | ac341f0 | 2021-01-22 22:01:56 -0800 | [diff] [blame] | 786 | internal fun Rect.containsInclusive(offset: Offset): Boolean = |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 787 | offset.x in left..right && offset.y in top..bottom |