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