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