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 | |
haoyu | ac341f0 | 2021-01-22 22:01:56 -0800 | [diff] [blame] | 17 | package androidx.compose.foundation.text.selection |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 18 | |
Grant Toepfer | d681b5c | 2023-06-14 11:24:02 -0700 | [diff] [blame] | 19 | import androidx.annotation.VisibleForTesting |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 20 | import androidx.compose.foundation.focusable |
George Mount | 32de9dd | 2022-10-05 14:51:06 -0700 | [diff] [blame] | 21 | import androidx.compose.foundation.gestures.awaitEachGesture |
Ralston Da Silva | de62bc6 | 2021-06-02 17:46:44 -0700 | [diff] [blame] | 22 | import androidx.compose.foundation.gestures.waitForUpOrCancellation |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame] | 23 | import androidx.compose.foundation.text.Handle |
| 24 | import androidx.compose.foundation.text.TextDragObserver |
| 25 | import androidx.compose.foundation.text.selection.Selection.AnchorInfo |
Grant Toepfer | 427dbdf | 2023-09-05 18:38:42 -0700 | [diff] [blame] | 26 | import androidx.compose.foundation.text2.input.internal.coerceIn |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame] | 27 | import androidx.compose.runtime.MutableState |
Louis Pullen-Freilich | 1f10a59 | 2020-07-24 16:35:14 +0100 | [diff] [blame] | 28 | import androidx.compose.runtime.State |
| 29 | import androidx.compose.runtime.getValue |
| 30 | import androidx.compose.runtime.mutableStateOf |
| 31 | import androidx.compose.runtime.setValue |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 32 | import androidx.compose.ui.Modifier |
| 33 | import androidx.compose.ui.focus.FocusRequester |
| 34 | import androidx.compose.ui.focus.focusRequester |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 35 | import androidx.compose.ui.focus.onFocusChanged |
Louis Pullen-Freilich | 534385a | 2020-08-14 14:10:12 +0100 | [diff] [blame] | 36 | import androidx.compose.ui.geometry.Offset |
| 37 | import androidx.compose.ui.geometry.Rect |
Grant Toepfer | d681b5c | 2023-06-14 11:24:02 -0700 | [diff] [blame] | 38 | import androidx.compose.ui.geometry.isSpecified |
| 39 | import androidx.compose.ui.geometry.isUnspecified |
Louis Pullen-Freilich | a03fd6c | 2020-07-24 23:26:29 +0100 | [diff] [blame] | 40 | import androidx.compose.ui.hapticfeedback.HapticFeedback |
| 41 | import androidx.compose.ui.hapticfeedback.HapticFeedbackType |
Andrey Rudenko | 8b4981e | 2021-01-29 15:31:59 +0100 | [diff] [blame] | 42 | import androidx.compose.ui.input.key.KeyEvent |
| 43 | import androidx.compose.ui.input.key.onKeyEvent |
Ralston Da Silva | de62bc6 | 2021-06-02 17:46:44 -0700 | [diff] [blame] | 44 | import androidx.compose.ui.input.pointer.PointerInputScope |
| 45 | import androidx.compose.ui.input.pointer.pointerInput |
Louis Pullen-Freilich | 534385a | 2020-08-14 14:10:12 +0100 | [diff] [blame] | 46 | import androidx.compose.ui.layout.LayoutCoordinates |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 47 | import androidx.compose.ui.layout.boundsInWindow |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 48 | import androidx.compose.ui.layout.onGloballyPositioned |
haoyu | 7ad5ea3 | 2021-03-22 10:36:35 -0700 | [diff] [blame] | 49 | import androidx.compose.ui.layout.positionInWindow |
Louis Pullen-Freilich | 534385a | 2020-08-14 14:10:12 +0100 | [diff] [blame] | 50 | import androidx.compose.ui.platform.ClipboardManager |
Louis Pullen-Freilich | a03fd6c | 2020-07-24 23:26:29 +0100 | [diff] [blame] | 51 | import androidx.compose.ui.platform.TextToolbar |
| 52 | import androidx.compose.ui.platform.TextToolbarStatus |
Louis Pullen-Freilich | ab19475 | 2020-07-21 22:21:22 +0100 | [diff] [blame] | 53 | import androidx.compose.ui.text.AnnotatedString |
Grant Toepfer | 427dbdf | 2023-09-05 18:38:42 -0700 | [diff] [blame] | 54 | import androidx.compose.ui.text.buildAnnotatedString |
Zach Klippenstein | 9c32bdf | 2021-11-11 16:38:13 -0800 | [diff] [blame] | 55 | import androidx.compose.ui.unit.IntSize |
Grant Toepfer | d681b5c | 2023-06-14 11:24:02 -0700 | [diff] [blame] | 56 | import androidx.compose.ui.util.fastAny |
Grant Toepfer | 427dbdf | 2023-09-05 18:38:42 -0700 | [diff] [blame] | 57 | import androidx.compose.ui.util.fastFilter |
Louis Pullen-Freilich | 1ffdbfc | 2023-08-24 18:35:34 +0100 | [diff] [blame] | 58 | import androidx.compose.ui.util.fastFold |
Grant Toepfer | fefbd7a | 2023-03-03 15:02:55 -0800 | [diff] [blame] | 59 | import androidx.compose.ui.util.fastForEach |
Grant Toepfer | d681b5c | 2023-06-14 11:24:02 -0700 | [diff] [blame] | 60 | import androidx.compose.ui.util.fastForEachIndexed |
Zach Klippenstein | 9c32bdf | 2021-11-11 16:38:13 -0800 | [diff] [blame] | 61 | import kotlin.math.absoluteValue |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 62 | |
Qingqing Deng | 35f97ea | 2019-09-18 19:24:37 -0700 | [diff] [blame] | 63 | /** |
Louis Pullen-Freilich | 5da28bd | 2019-10-15 17:05:07 +0100 | [diff] [blame] | 64 | * 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] | 65 | */ |
Siyamed Sinir | 700df845 | 2019-10-22 20:23:58 -0700 | [diff] [blame] | 66 | internal class SelectionManager(private val selectionRegistrar: SelectionRegistrarImpl) { |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame] | 67 | |
| 68 | private val _selection: MutableState<Selection?> = mutableStateOf(null) |
| 69 | |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 70 | /** |
| 71 | * The current selection. |
| 72 | */ |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame] | 73 | var selection: Selection? |
| 74 | get() = _selection.value |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 75 | set(value) { |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame] | 76 | _selection.value = value |
haoyu | 9085c88 | 2020-12-08 12:01:06 -0800 | [diff] [blame] | 77 | if (value != null) { |
| 78 | updateHandleOffsets() |
| 79 | } |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 80 | } |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 81 | |
| 82 | /** |
Andrey Rudenko | 8b4981e | 2021-01-29 15:31:59 +0100 | [diff] [blame] | 83 | * Is touch mode active |
| 84 | */ |
Grant Toepfer | fefbd7a | 2023-03-03 15:02:55 -0800 | [diff] [blame] | 85 | private val _isInTouchMode = mutableStateOf(true) |
| 86 | var isInTouchMode: Boolean |
| 87 | get() = _isInTouchMode.value |
| 88 | set(value) { |
| 89 | if (_isInTouchMode.value != value) { |
| 90 | _isInTouchMode.value = value |
Grant Toepfer | 427dbdf | 2023-09-05 18:38:42 -0700 | [diff] [blame] | 91 | updateSelectionToolbar() |
Grant Toepfer | fefbd7a | 2023-03-03 15:02:55 -0800 | [diff] [blame] | 92 | } |
| 93 | } |
Andrey Rudenko | 8b4981e | 2021-01-29 15:31:59 +0100 | [diff] [blame] | 94 | |
| 95 | /** |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 96 | * The manager will invoke this every time it comes to the conclusion that the selection should |
| 97 | * change. The expectation is that this callback will end up causing `setSelection` to get |
| 98 | * called. This is what makes this a "controlled component". |
| 99 | */ |
| 100 | var onSelectionChange: (Selection?) -> Unit = {} |
| 101 | |
| 102 | /** |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 103 | * [HapticFeedback] handle to perform haptic feedback. |
| 104 | */ |
| 105 | var hapticFeedBack: HapticFeedback? = null |
| 106 | |
| 107 | /** |
Qingqing Deng | f2d0a2d | 2020-03-12 14:19:50 -0700 | [diff] [blame] | 108 | * [ClipboardManager] to perform clipboard features. |
| 109 | */ |
| 110 | var clipboardManager: ClipboardManager? = null |
| 111 | |
| 112 | /** |
Qingqing Deng | a89450d | 2020-04-03 19:11:49 -0700 | [diff] [blame] | 113 | * [TextToolbar] to show floating toolbar(post-M) or primary toolbar(pre-M). |
| 114 | */ |
| 115 | var textToolbar: TextToolbar? = null |
| 116 | |
| 117 | /** |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 118 | * Focus requester used to request focus when selection becomes active. |
| 119 | */ |
| 120 | var focusRequester: FocusRequester = FocusRequester() |
| 121 | |
| 122 | /** |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 123 | * Return true if the corresponding SelectionContainer is focused. |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 124 | */ |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 125 | var hasFocus: Boolean by mutableStateOf(false) |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 126 | |
| 127 | /** |
| 128 | * Modifier for selection container. |
| 129 | */ |
Zach Klippenstein | 72e3e51 | 2022-01-14 12:24:09 -0800 | [diff] [blame] | 130 | val modifier |
| 131 | get() = Modifier |
| 132 | .onClearSelectionRequested { onRelease() } |
| 133 | .onGloballyPositioned { containerLayoutCoordinates = it } |
| 134 | .focusRequester(focusRequester) |
| 135 | .onFocusChanged { focusState -> |
| 136 | if (!focusState.isFocused && hasFocus) { |
| 137 | onRelease() |
| 138 | } |
| 139 | hasFocus = focusState.isFocused |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 140 | } |
Zach Klippenstein | 72e3e51 | 2022-01-14 12:24:09 -0800 | [diff] [blame] | 141 | .focusable() |
Grant Toepfer | fefbd7a | 2023-03-03 15:02:55 -0800 | [diff] [blame] | 142 | .updateSelectionTouchMode { isInTouchMode = it } |
Zach Klippenstein | 72e3e51 | 2022-01-14 12:24:09 -0800 | [diff] [blame] | 143 | .onKeyEvent { |
| 144 | if (isCopyKeyEvent(it)) { |
| 145 | copy() |
| 146 | true |
| 147 | } else { |
| 148 | false |
| 149 | } |
Andrey Rudenko | 8b4981e | 2021-01-29 15:31:59 +0100 | [diff] [blame] | 150 | } |
Zach Klippenstein | 72e3e51 | 2022-01-14 12:24:09 -0800 | [diff] [blame] | 151 | .then(if (shouldShowMagnifier) Modifier.selectionMagnifier(this) else Modifier) |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 152 | |
haoyu | 7ad5ea3 | 2021-03-22 10:36:35 -0700 | [diff] [blame] | 153 | private var previousPosition: Offset? = null |
Zach Klippenstein | 72e3e51 | 2022-01-14 12:24:09 -0800 | [diff] [blame] | 154 | |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 155 | /** |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 156 | * Layout Coordinates of the selection container. |
| 157 | */ |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 158 | var containerLayoutCoordinates: LayoutCoordinates? = null |
| 159 | set(value) { |
| 160 | field = value |
haoyu | 2c6e984 | 2021-03-30 17:39:04 -0700 | [diff] [blame] | 161 | if (hasFocus && selection != null) { |
| 162 | val positionInWindow = value?.positionInWindow() |
| 163 | if (previousPosition != positionInWindow) { |
| 164 | previousPosition = positionInWindow |
| 165 | updateHandleOffsets() |
Grant Toepfer | 427dbdf | 2023-09-05 18:38:42 -0700 | [diff] [blame] | 166 | updateSelectionToolbar() |
haoyu | 2c6e984 | 2021-03-30 17:39:04 -0700 | [diff] [blame] | 167 | } |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 168 | } |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 169 | } |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 170 | |
| 171 | /** |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 172 | * The beginning position of the drag gesture. Every time a new drag gesture starts, it wil be |
| 173 | * recalculated. |
| 174 | */ |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame] | 175 | internal var dragBeginPosition by mutableStateOf(Offset.Zero) |
| 176 | private set |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 177 | |
| 178 | /** |
| 179 | * The total distance being dragged of the drag gesture. Every time a new drag gesture starts, |
| 180 | * it will be zeroed out. |
| 181 | */ |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame] | 182 | internal var dragTotalDistance by mutableStateOf(Offset.Zero) |
| 183 | private set |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 184 | |
| 185 | /** |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 186 | * The calculated position of the start handle in the [SelectionContainer] coordinates. It |
| 187 | * is null when handle shouldn't be displayed. |
| 188 | * It is a [State] so reading it during the composition will cause recomposition every time |
| 189 | * the position has been changed. |
| 190 | */ |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame] | 191 | var startHandlePosition: Offset? by mutableStateOf(null) |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 192 | private set |
| 193 | |
| 194 | /** |
| 195 | * The calculated position of the end handle in the [SelectionContainer] coordinates. It |
| 196 | * is null when handle shouldn't be displayed. |
| 197 | * It is a [State] so reading it during the composition will cause recomposition every time |
| 198 | * the position has been changed. |
| 199 | */ |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame] | 200 | var endHandlePosition: Offset? by mutableStateOf(null) |
| 201 | private set |
| 202 | |
| 203 | /** |
Zach Klippenstein | 6387089 | 2022-01-14 12:45:18 -0800 | [diff] [blame] | 204 | * The handle that is currently being dragged, or null when no handle is being dragged. To get |
| 205 | * the position of the last drag event, use [currentDragPosition]. |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame] | 206 | */ |
| 207 | var draggingHandle: Handle? by mutableStateOf(null) |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 208 | private set |
| 209 | |
Zach Klippenstein | 6387089 | 2022-01-14 12:45:18 -0800 | [diff] [blame] | 210 | /** |
| 211 | * When a handle is being dragged (i.e. [draggingHandle] is non-null), this is the last position |
| 212 | * of the actual drag event. It is not clamped to handle positions. Null when not being dragged. |
| 213 | */ |
| 214 | var currentDragPosition: Offset? by mutableStateOf(null) |
| 215 | private set |
| 216 | |
Grant Toepfer | fefbd7a | 2023-03-03 15:02:55 -0800 | [diff] [blame] | 217 | private val shouldShowMagnifier |
Grant Toepfer | d681b5c | 2023-06-14 11:24:02 -0700 | [diff] [blame] | 218 | get() = draggingHandle != null && isInTouchMode && !isTriviallyCollapsedSelection() |
| 219 | |
| 220 | @VisibleForTesting |
| 221 | internal var previousSelectionLayout: SelectionLayout? = null |
Zach Klippenstein | 4688a46 | 2021-12-08 08:28:07 -0800 | [diff] [blame] | 222 | |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 223 | init { |
haoyu | 7ad5ea3 | 2021-03-22 10:36:35 -0700 | [diff] [blame] | 224 | selectionRegistrar.onPositionChangeCallback = { selectableId -> |
Grant Toepfer | 427dbdf | 2023-09-05 18:38:42 -0700 | [diff] [blame] | 225 | if (selectableId in selectionRegistrar.subselections) { |
haoyu | 7ad5ea3 | 2021-03-22 10:36:35 -0700 | [diff] [blame] | 226 | updateHandleOffsets() |
Grant Toepfer | 427dbdf | 2023-09-05 18:38:42 -0700 | [diff] [blame] | 227 | updateSelectionToolbar() |
haoyu | 7ad5ea3 | 2021-03-22 10:36:35 -0700 | [diff] [blame] | 228 | } |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 231 | selectionRegistrar.onSelectionUpdateStartCallback = |
Grant Toepfer | c7d1a4e | 2023-09-18 23:06:37 +0000 | [diff] [blame^] | 232 | { isInTouchMode, layoutCoordinates, rawPosition, selectionMode -> |
| 233 | val textRect = with(layoutCoordinates.size) { |
| 234 | Rect(0f, 0f, width.toFloat(), height.toFloat()) |
| 235 | } |
| 236 | |
| 237 | val position = if (textRect.containsInclusive(rawPosition)) { |
| 238 | rawPosition |
| 239 | } else { |
| 240 | rawPosition.coerceIn(textRect) |
| 241 | } |
| 242 | |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 243 | val positionInContainer = convertToContainerCoordinates( |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 244 | layoutCoordinates, |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 245 | position |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 246 | ) |
| 247 | |
Grant Toepfer | d681b5c | 2023-06-14 11:24:02 -0700 | [diff] [blame] | 248 | if (positionInContainer.isSpecified) { |
Grant Toepfer | fefbd7a | 2023-03-03 15:02:55 -0800 | [diff] [blame] | 249 | this.isInTouchMode = isInTouchMode |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 250 | startSelection( |
| 251 | position = positionInContainer, |
| 252 | isStartHandle = false, |
| 253 | adjustment = selectionMode |
| 254 | ) |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 255 | |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 256 | focusRequester.requestFocus() |
Grant Toepfer | fefbd7a | 2023-03-03 15:02:55 -0800 | [diff] [blame] | 257 | showToolbar = false |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 258 | } |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 259 | } |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 260 | |
Qingqing Deng | 6bc981c | 2021-05-11 22:43:51 -0700 | [diff] [blame] | 261 | selectionRegistrar.onSelectionUpdateSelectAll = |
Grant Toepfer | fefbd7a | 2023-03-03 15:02:55 -0800 | [diff] [blame] | 262 | { isInTouchMode, selectableId -> |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 263 | val (newSelection, newSubselection) = selectAll( |
Qingqing Deng | 6bc981c | 2021-05-11 22:43:51 -0700 | [diff] [blame] | 264 | selectableId = selectableId, |
| 265 | previousSelection = selection, |
| 266 | ) |
| 267 | if (newSelection != selection) { |
| 268 | selectionRegistrar.subselections = newSubselection |
| 269 | onSelectionChange(newSelection) |
| 270 | } |
| 271 | |
Grant Toepfer | fefbd7a | 2023-03-03 15:02:55 -0800 | [diff] [blame] | 272 | this.isInTouchMode = isInTouchMode |
Qingqing Deng | 6bc981c | 2021-05-11 22:43:51 -0700 | [diff] [blame] | 273 | focusRequester.requestFocus() |
Grant Toepfer | fefbd7a | 2023-03-03 15:02:55 -0800 | [diff] [blame] | 274 | showToolbar = false |
Qingqing Deng | 6bc981c | 2021-05-11 22:43:51 -0700 | [diff] [blame] | 275 | } |
| 276 | |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 277 | selectionRegistrar.onSelectionUpdateCallback = |
Grant Toepfer | fefbd7a | 2023-03-03 15:02:55 -0800 | [diff] [blame] | 278 | { isInTouchMode, |
| 279 | layoutCoordinates, |
| 280 | newPosition, |
| 281 | previousPosition, |
| 282 | isStartHandle, |
| 283 | selectionMode -> |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 284 | val newPositionInContainer = |
| 285 | convertToContainerCoordinates(layoutCoordinates, newPosition) |
| 286 | val previousPositionInContainer = |
| 287 | convertToContainerCoordinates(layoutCoordinates, previousPosition) |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 288 | |
Grant Toepfer | fefbd7a | 2023-03-03 15:02:55 -0800 | [diff] [blame] | 289 | this.isInTouchMode = isInTouchMode |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 290 | updateSelection( |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 291 | newPosition = newPositionInContainer, |
| 292 | previousPosition = previousPositionInContainer, |
| 293 | isStartHandle = isStartHandle, |
Haoyu Zhang | 4c6c637 | 2021-04-20 10:25:21 -0700 | [diff] [blame] | 294 | adjustment = selectionMode |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 295 | ) |
| 296 | } |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 297 | |
| 298 | selectionRegistrar.onSelectionUpdateEndCallback = { |
Grant Toepfer | fefbd7a | 2023-03-03 15:02:55 -0800 | [diff] [blame] | 299 | showToolbar = true |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame] | 300 | // This property is set by updateSelection while dragging, so we need to clear it after |
| 301 | // the original selection drag. |
| 302 | draggingHandle = null |
Zach Klippenstein | 6387089 | 2022-01-14 12:45:18 -0800 | [diff] [blame] | 303 | currentDragPosition = null |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 304 | } |
haoyu | 9085c88 | 2020-12-08 12:01:06 -0800 | [diff] [blame] | 305 | |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 306 | selectionRegistrar.onSelectableChangeCallback = { selectableKey -> |
| 307 | if (selectableKey in selectionRegistrar.subselections) { |
Grant Toepfer | 427dbdf | 2023-09-05 18:38:42 -0700 | [diff] [blame] | 308 | // Clear the selection range of each Selectable. |
haoyu | 9085c88 | 2020-12-08 12:01:06 -0800 | [diff] [blame] | 309 | onRelease() |
| 310 | selection = null |
| 311 | } |
| 312 | } |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 313 | |
Grant Toepfer | 427dbdf | 2023-09-05 18:38:42 -0700 | [diff] [blame] | 314 | selectionRegistrar.afterSelectableUnsubscribe = { selectableId -> |
| 315 | if (selectableId == selection?.start?.selectableId) { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 316 | // The selectable that contains a selection handle just unsubscribed. |
Grant Toepfer | 427dbdf | 2023-09-05 18:38:42 -0700 | [diff] [blame] | 317 | // Hide the associated selection handle |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 318 | startHandlePosition = null |
Grant Toepfer | 427dbdf | 2023-09-05 18:38:42 -0700 | [diff] [blame] | 319 | } |
| 320 | if (selectableId == selection?.end?.selectableId) { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 321 | endHandlePosition = null |
| 322 | } |
Grant Toepfer | 427dbdf | 2023-09-05 18:38:42 -0700 | [diff] [blame] | 323 | |
| 324 | if (selectableId in selectionRegistrar.subselections) { |
| 325 | // Unsubscribing the selectable may make the selection empty, which would hide it. |
| 326 | updateSelectionToolbar() |
| 327 | } |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 328 | } |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 329 | } |
| 330 | |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame] | 331 | /** |
| 332 | * Returns the [Selectable] responsible for managing the given [AnchorInfo], or null if the |
| 333 | * anchor is not from a currently-registered [Selectable]. |
| 334 | */ |
| 335 | internal fun getAnchorSelectable(anchor: AnchorInfo): Selectable? { |
| 336 | return selectionRegistrar.selectableMap[anchor.selectableId] |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 337 | } |
| 338 | |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 339 | private fun updateHandleOffsets() { |
| 340 | val selection = selection |
| 341 | val containerCoordinates = containerLayoutCoordinates |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame] | 342 | val startSelectable = selection?.start?.let(::getAnchorSelectable) |
| 343 | val endSelectable = selection?.end?.let(::getAnchorSelectable) |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 344 | val startLayoutCoordinates = startSelectable?.getLayoutCoordinates() |
| 345 | val endLayoutCoordinates = endSelectable?.getLayoutCoordinates() |
Grant Toepfer | 427dbdf | 2023-09-05 18:38:42 -0700 | [diff] [blame] | 346 | |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 347 | if ( |
| 348 | selection == null || |
| 349 | containerCoordinates == null || |
| 350 | !containerCoordinates.isAttached || |
Grant Toepfer | 427dbdf | 2023-09-05 18:38:42 -0700 | [diff] [blame] | 351 | (startLayoutCoordinates == null && endLayoutCoordinates == null) |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 352 | ) { |
| 353 | this.startHandlePosition = null |
| 354 | this.endHandlePosition = null |
| 355 | return |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 356 | } |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 357 | |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 358 | val visibleBounds = containerCoordinates.visibleBounds() |
Grant Toepfer | 427dbdf | 2023-09-05 18:38:42 -0700 | [diff] [blame] | 359 | this.startHandlePosition = startLayoutCoordinates?.let { handleCoordinates -> |
| 360 | // Set the new handle position only if the handle is in visible bounds or |
| 361 | // the handle is still dragging. If handle goes out of visible bounds during drag, |
| 362 | // handle popup is also removed from composition, halting the drag gesture. This |
| 363 | // affects multiple text selection when selected text is configured with maxLines=1 |
| 364 | // and overflow=clip. |
| 365 | val handlePosition = startSelectable.getHandlePosition(selection, isStartHandle = true) |
| 366 | val position = containerCoordinates.localPositionOf(handleCoordinates, handlePosition) |
| 367 | position.takeIf { |
| 368 | draggingHandle == Handle.SelectionStart || visibleBounds.containsInclusive(it) |
| 369 | } |
Halil Ozercan | b2486f1 | 2023-01-09 07:40:22 +0000 | [diff] [blame] | 370 | } |
Grant Toepfer | 427dbdf | 2023-09-05 18:38:42 -0700 | [diff] [blame] | 371 | |
| 372 | this.endHandlePosition = endLayoutCoordinates?.let { handleCoordinates -> |
| 373 | val handlePosition = endSelectable.getHandlePosition(selection, isStartHandle = false) |
| 374 | val position = containerCoordinates.localPositionOf(handleCoordinates, handlePosition) |
| 375 | position.takeIf { |
| 376 | draggingHandle == Handle.SelectionEnd || visibleBounds.containsInclusive(it) |
| 377 | } |
Halil Ozercan | b2486f1 | 2023-01-09 07:40:22 +0000 | [diff] [blame] | 378 | } |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /** |
| 382 | * Returns non-nullable [containerLayoutCoordinates]. |
| 383 | */ |
| 384 | internal fun requireContainerCoordinates(): LayoutCoordinates { |
| 385 | val coordinates = containerLayoutCoordinates |
Ralston Da Silva | f24ae42 | 2023-05-30 12:59:24 -0700 | [diff] [blame] | 386 | requireNotNull(coordinates) { "null coordinates" } |
| 387 | require(coordinates.isAttached) { "unattached coordinates" } |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 388 | return coordinates |
| 389 | } |
| 390 | |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 391 | internal fun selectAll( |
| 392 | selectableId: Long, |
| 393 | previousSelection: Selection? |
Qingqing Deng | 6bc981c | 2021-05-11 22:43:51 -0700 | [diff] [blame] | 394 | ): Pair<Selection?, Map<Long, Selection>> { |
| 395 | val subselections = mutableMapOf<Long, Selection>() |
| 396 | val newSelection = selectionRegistrar.sort(requireContainerCoordinates()) |
| 397 | .fastFold(null) { mergedSelection: Selection?, selectable: Selectable -> |
| 398 | val selection = if (selectable.selectableId == selectableId) |
| 399 | selectable.getSelectAllSelection() else null |
| 400 | selection?.let { subselections[selectable.selectableId] = it } |
| 401 | merge(mergedSelection, selection) |
| 402 | } |
Grant Toepfer | fefbd7a | 2023-03-03 15:02:55 -0800 | [diff] [blame] | 403 | if (isInTouchMode && newSelection != previousSelection) { |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 404 | hapticFeedBack?.performHapticFeedback(HapticFeedbackType.TextHandleMove) |
| 405 | } |
Qingqing Deng | 6bc981c | 2021-05-11 22:43:51 -0700 | [diff] [blame] | 406 | return Pair(newSelection, subselections) |
| 407 | } |
| 408 | |
Grant Toepfer | d681b5c | 2023-06-14 11:24:02 -0700 | [diff] [blame] | 409 | /** |
| 410 | * Returns whether the start and end anchors are equal. |
| 411 | * |
| 412 | * It is possible that this returns true, but the selection is still empty because it has |
| 413 | * multiple collapsed selections across multiple selectables. To test for that case, use |
| 414 | * [isNonEmptySelection]. |
| 415 | */ |
| 416 | internal fun isTriviallyCollapsedSelection(): Boolean { |
| 417 | val selection = selection ?: return true |
| 418 | return selection.start == selection.end |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * Returns whether the selection selects zero characters. |
| 423 | * |
| 424 | * It is possible that the selection anchors are different but still result in a zero-width |
| 425 | * selection. In this case, you may want to still show the selection anchors, but not allow for |
| 426 | * a user to try and copy zero characters. To test for whether the anchors are equal, use |
| 427 | * [isTriviallyCollapsedSelection]. |
| 428 | */ |
Grant Toepfer | fefbd7a | 2023-03-03 15:02:55 -0800 | [diff] [blame] | 429 | internal fun isNonEmptySelection(): Boolean { |
| 430 | val selection = selection ?: return false |
Grant Toepfer | d681b5c | 2023-06-14 11:24:02 -0700 | [diff] [blame] | 431 | if (selection.start == selection.end) { |
| 432 | return false |
| 433 | } |
Grant Toepfer | fefbd7a | 2023-03-03 15:02:55 -0800 | [diff] [blame] | 434 | |
Grant Toepfer | 427dbdf | 2023-09-05 18:38:42 -0700 | [diff] [blame] | 435 | if (selection.start.selectableId == selection.end.selectableId) { |
| 436 | // Selection is in the same selectable, but not the same anchors, |
| 437 | // so there must be some selected text. |
| 438 | return true |
Grant Toepfer | fefbd7a | 2023-03-03 15:02:55 -0800 | [diff] [blame] | 439 | } |
Grant Toepfer | 427dbdf | 2023-09-05 18:38:42 -0700 | [diff] [blame] | 440 | |
| 441 | // All subselections associated with a selectable must be an empty selection. |
| 442 | return selectionRegistrar.sort(requireContainerCoordinates()).fastAny { selectable -> |
| 443 | selectionRegistrar.subselections[selectable.selectableId] |
| 444 | ?.run { start.offset != end.offset } |
| 445 | ?: false |
| 446 | } |
Grant Toepfer | fefbd7a | 2023-03-03 15:02:55 -0800 | [diff] [blame] | 447 | } |
| 448 | |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 449 | internal fun getSelectedText(): AnnotatedString? { |
Grant Toepfer | 427dbdf | 2023-09-05 18:38:42 -0700 | [diff] [blame] | 450 | if (selection == null || selectionRegistrar.subselections.isEmpty()) { |
| 451 | return null |
| 452 | } |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 453 | |
Grant Toepfer | 427dbdf | 2023-09-05 18:38:42 -0700 | [diff] [blame] | 454 | return buildAnnotatedString { |
| 455 | selectionRegistrar.sort(requireContainerCoordinates()).fastForEach { selectable -> |
| 456 | selectionRegistrar.subselections[selectable.selectableId]?.let { subSelection -> |
| 457 | val currentText = selectable.getText() |
| 458 | val currentSelectedText = if (subSelection.handlesCrossed) { |
| 459 | currentText.subSequence( |
| 460 | subSelection.end.offset, |
| 461 | subSelection.start.offset |
| 462 | ) |
| 463 | } else { |
| 464 | currentText.subSequence( |
| 465 | subSelection.start.offset, |
| 466 | subSelection.end.offset |
| 467 | ) |
| 468 | } |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 469 | |
Grant Toepfer | 427dbdf | 2023-09-05 18:38:42 -0700 | [diff] [blame] | 470 | append(currentSelectedText) |
| 471 | } |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 472 | } |
| 473 | } |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 474 | } |
| 475 | |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 476 | internal fun copy() { |
Grant Toepfer | 427dbdf | 2023-09-05 18:38:42 -0700 | [diff] [blame] | 477 | getSelectedText()?.takeIf { it.isNotEmpty() }?.let { clipboardManager?.setText(it) } |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | /** |
Grant Toepfer | fefbd7a | 2023-03-03 15:02:55 -0800 | [diff] [blame] | 481 | * Whether toolbar should be shown right now. |
| 482 | * Examples: Show toolbar after user finishes selection. |
| 483 | * Hide it during selection. |
| 484 | * Hide it when no selection exists. |
| 485 | */ |
| 486 | internal var showToolbar = false |
| 487 | internal set(value) { |
| 488 | field = value |
Grant Toepfer | 427dbdf | 2023-09-05 18:38:42 -0700 | [diff] [blame] | 489 | updateSelectionToolbar() |
Grant Toepfer | fefbd7a | 2023-03-03 15:02:55 -0800 | [diff] [blame] | 490 | } |
| 491 | |
Grant Toepfer | 427dbdf | 2023-09-05 18:38:42 -0700 | [diff] [blame] | 492 | private fun toolbarCopy() { |
| 493 | copy() |
| 494 | onRelease() |
| 495 | } |
| 496 | |
| 497 | private fun updateSelectionToolbar() { |
| 498 | if (!hasFocus) { |
| 499 | return |
| 500 | } |
| 501 | |
| 502 | val textToolbar = textToolbar ?: return |
| 503 | if (showToolbar && isInTouchMode && isNonEmptySelection()) { |
| 504 | val rect = getContentRect() ?: return |
| 505 | textToolbar.showMenu(rect = rect, onCopyRequested = ::toolbarCopy) |
| 506 | } else if (textToolbar.status == TextToolbarStatus.Shown) { |
| 507 | textToolbar.hide() |
| 508 | } |
| 509 | } |
| 510 | |
Grant Toepfer | fefbd7a | 2023-03-03 15:02:55 -0800 | [diff] [blame] | 511 | /** |
Grant Toepfer | 427dbdf | 2023-09-05 18:38:42 -0700 | [diff] [blame] | 512 | * Calculate selected region as [Rect]. |
| 513 | * The result is the smallest [Rect] that encapsulates the entire selection, |
| 514 | * coerced into visible bounds. |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 515 | */ |
Grant Toepfer | 427dbdf | 2023-09-05 18:38:42 -0700 | [diff] [blame] | 516 | private fun getContentRect(): Rect? { |
| 517 | selection ?: return null |
| 518 | val containerCoordinates = containerLayoutCoordinates ?: return null |
| 519 | if (!containerCoordinates.isAttached) return null |
| 520 | val visibleBounds = containerCoordinates.visibleBounds() |
| 521 | |
| 522 | var anyExists = false |
| 523 | var rootLeft = Float.POSITIVE_INFINITY |
| 524 | var rootTop = Float.POSITIVE_INFINITY |
| 525 | var rootRight = Float.NEGATIVE_INFINITY |
| 526 | var rootBottom = Float.NEGATIVE_INFINITY |
| 527 | |
| 528 | val sortedSelectables = selectionRegistrar.sort(requireContainerCoordinates()) |
| 529 | .fastFilter { |
| 530 | it.selectableId in selectionRegistrar.subselections |
| 531 | } |
| 532 | |
| 533 | if (sortedSelectables.isEmpty()) { |
| 534 | return null |
| 535 | } |
| 536 | |
| 537 | val selectedSelectables = if (sortedSelectables.size == 1) { |
| 538 | sortedSelectables |
| 539 | } else { |
| 540 | listOf(sortedSelectables.first(), sortedSelectables.last()) |
| 541 | } |
| 542 | |
| 543 | selectedSelectables.fastForEach { selectable -> |
| 544 | val subSelection = selectionRegistrar.subselections[selectable.selectableId] |
| 545 | ?: return@fastForEach |
| 546 | |
| 547 | val coordinates = selectable.getLayoutCoordinates() |
| 548 | ?: return@fastForEach |
| 549 | |
| 550 | with(subSelection) { |
| 551 | if (start.offset == end.offset) { |
| 552 | return@fastForEach |
Grant Toepfer | fefbd7a | 2023-03-03 15:02:55 -0800 | [diff] [blame] | 553 | } |
Grant Toepfer | 427dbdf | 2023-09-05 18:38:42 -0700 | [diff] [blame] | 554 | |
| 555 | val minOffset = minOf(start.offset, end.offset) |
| 556 | val maxOffset = maxOf(start.offset, end.offset) |
| 557 | |
| 558 | var left = Float.POSITIVE_INFINITY |
| 559 | var top = Float.POSITIVE_INFINITY |
| 560 | var right = Float.NEGATIVE_INFINITY |
| 561 | var bottom = Float.NEGATIVE_INFINITY |
| 562 | for (i in intArrayOf(minOffset, maxOffset)) { |
| 563 | val rect = selectable.getBoundingBox(i) |
| 564 | left = minOf(left, rect.left) |
| 565 | top = minOf(top, rect.top) |
| 566 | right = maxOf(right, rect.right) |
| 567 | bottom = maxOf(bottom, rect.bottom) |
| 568 | } |
| 569 | |
| 570 | val localTopLeft = Offset(left, top) |
| 571 | val localBottomRight = Offset(right, bottom) |
| 572 | |
| 573 | val containerTopLeft = |
| 574 | containerCoordinates.localPositionOf(coordinates, localTopLeft) |
| 575 | val containerBottomRight = |
| 576 | containerCoordinates.localPositionOf(coordinates, localBottomRight) |
| 577 | |
| 578 | val rootVisibleTopLeft = |
| 579 | containerCoordinates.localToRoot(containerTopLeft.coerceIn(visibleBounds)) |
| 580 | val rootVisibleBottomRight = |
| 581 | containerCoordinates.localToRoot(containerBottomRight.coerceIn(visibleBounds)) |
| 582 | |
| 583 | rootLeft = minOf(rootLeft, rootVisibleTopLeft.x) |
| 584 | rootTop = minOf(rootTop, rootVisibleTopLeft.y) |
| 585 | rootRight = maxOf(rootRight, rootVisibleBottomRight.x) |
| 586 | rootBottom = maxOf(rootBottom, rootVisibleBottomRight.y) |
| 587 | anyExists = true |
| 588 | } |
Qingqing Deng | a89450d | 2020-04-03 19:11:49 -0700 | [diff] [blame] | 589 | } |
Qingqing Deng | f2d0a2d | 2020-03-12 14:19:50 -0700 | [diff] [blame] | 590 | |
Grant Toepfer | 427dbdf | 2023-09-05 18:38:42 -0700 | [diff] [blame] | 591 | if (!anyExists) { |
| 592 | return null |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 593 | } |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 594 | |
Grant Toepfer | 427dbdf | 2023-09-05 18:38:42 -0700 | [diff] [blame] | 595 | rootBottom += HandleHeight.value * 4 |
| 596 | return Rect(rootLeft, rootTop, rootRight, rootBottom) |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Qingqing Deng | b6f5d8a | 2019-11-11 18:19:22 -0800 | [diff] [blame] | 599 | // This is for PressGestureDetector to cancel the selection. |
| 600 | fun onRelease() { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 601 | selectionRegistrar.subselections = emptyMap() |
Grant Toepfer | fefbd7a | 2023-03-03 15:02:55 -0800 | [diff] [blame] | 602 | showToolbar = false |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 603 | if (selection != null) { |
| 604 | onSelectionChange(null) |
Grant Toepfer | fefbd7a | 2023-03-03 15:02:55 -0800 | [diff] [blame] | 605 | if (isInTouchMode) { |
| 606 | hapticFeedBack?.performHapticFeedback(HapticFeedbackType.TextHandleMove) |
| 607 | } |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 608 | } |
Qingqing Deng | b6f5d8a | 2019-11-11 18:19:22 -0800 | [diff] [blame] | 609 | } |
| 610 | |
Zach Klippenstein | 72e3e51 | 2022-01-14 12:24:09 -0800 | [diff] [blame] | 611 | fun handleDragObserver(isStartHandle: Boolean): TextDragObserver = object : TextDragObserver { |
| 612 | override fun onDown(point: Offset) { |
Zach Klippenstein | 6387089 | 2022-01-14 12:45:18 -0800 | [diff] [blame] | 613 | val selection = selection ?: return |
| 614 | val anchor = if (isStartHandle) selection.start else selection.end |
| 615 | val selectable = getAnchorSelectable(anchor) ?: return |
| 616 | // The LayoutCoordinates of the composable where the drag gesture should begin. This |
| 617 | // is used to convert the position of the beginning of the drag gesture from the |
| 618 | // composable coordinates to selection container coordinates. |
| 619 | val beginLayoutCoordinates = selectable.getLayoutCoordinates() ?: return |
| 620 | |
| 621 | // The position of the character where the drag gesture should begin. This is in |
| 622 | // the composable coordinates. |
| 623 | val beginCoordinates = getAdjustedCoordinates( |
| 624 | selectable.getHandlePosition( |
| 625 | selection = selection, isStartHandle = isStartHandle |
| 626 | ) |
| 627 | ) |
| 628 | |
| 629 | // Convert the position where drag gesture begins from composable coordinates to |
| 630 | // selection container coordinates. |
| 631 | currentDragPosition = requireContainerCoordinates().localPositionOf( |
| 632 | beginLayoutCoordinates, |
| 633 | beginCoordinates |
| 634 | ) |
| 635 | draggingHandle = if (isStartHandle) Handle.SelectionStart else Handle.SelectionEnd |
Grant Toepfer | fefbd7a | 2023-03-03 15:02:55 -0800 | [diff] [blame] | 636 | showToolbar = false |
Zach Klippenstein | 72e3e51 | 2022-01-14 12:24:09 -0800 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | override fun onStart(startPoint: Offset) { |
Zach Klippenstein | 72e3e51 | 2022-01-14 12:24:09 -0800 | [diff] [blame] | 640 | val selection = selection!! |
| 641 | val startSelectable = |
| 642 | selectionRegistrar.selectableMap[selection.start.selectableId] |
| 643 | val endSelectable = |
| 644 | selectionRegistrar.selectableMap[selection.end.selectableId] |
| 645 | // The LayoutCoordinates of the composable where the drag gesture should begin. This |
| 646 | // is used to convert the position of the beginning of the drag gesture from the |
| 647 | // composable coordinates to selection container coordinates. |
| 648 | val beginLayoutCoordinates = if (isStartHandle) { |
| 649 | startSelectable?.getLayoutCoordinates()!! |
| 650 | } else { |
| 651 | endSelectable?.getLayoutCoordinates()!! |
| 652 | } |
| 653 | |
| 654 | // The position of the character where the drag gesture should begin. This is in |
| 655 | // the composable coordinates. |
| 656 | val beginCoordinates = getAdjustedCoordinates( |
| 657 | if (isStartHandle) { |
| 658 | startSelectable!!.getHandlePosition( |
| 659 | selection = selection, isStartHandle = true |
| 660 | ) |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 661 | } else { |
Zach Klippenstein | 72e3e51 | 2022-01-14 12:24:09 -0800 | [diff] [blame] | 662 | endSelectable!!.getHandlePosition( |
| 663 | selection = selection, isStartHandle = false |
| 664 | ) |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 665 | } |
Zach Klippenstein | 72e3e51 | 2022-01-14 12:24:09 -0800 | [diff] [blame] | 666 | ) |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 667 | |
Zach Klippenstein | 72e3e51 | 2022-01-14 12:24:09 -0800 | [diff] [blame] | 668 | // Convert the position where drag gesture begins from composable coordinates to |
| 669 | // selection container coordinates. |
| 670 | dragBeginPosition = requireContainerCoordinates().localPositionOf( |
| 671 | beginLayoutCoordinates, |
| 672 | beginCoordinates |
| 673 | ) |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 674 | |
Zach Klippenstein | 72e3e51 | 2022-01-14 12:24:09 -0800 | [diff] [blame] | 675 | // Zero out the total distance that being dragged. |
| 676 | dragTotalDistance = Offset.Zero |
| 677 | } |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 678 | |
Zach Klippenstein | 72e3e51 | 2022-01-14 12:24:09 -0800 | [diff] [blame] | 679 | override fun onDrag(delta: Offset) { |
| 680 | dragTotalDistance += delta |
| 681 | val endPosition = dragBeginPosition + dragTotalDistance |
| 682 | val consumed = updateSelection( |
| 683 | newPosition = endPosition, |
| 684 | previousPosition = dragBeginPosition, |
| 685 | isStartHandle = isStartHandle, |
| 686 | adjustment = SelectionAdjustment.CharacterWithWordAccelerate |
| 687 | ) |
| 688 | if (consumed) { |
| 689 | dragBeginPosition = endPosition |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 690 | dragTotalDistance = Offset.Zero |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 691 | } |
Zach Klippenstein | 72e3e51 | 2022-01-14 12:24:09 -0800 | [diff] [blame] | 692 | } |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 693 | |
Grant Toepfer | fefbd7a | 2023-03-03 15:02:55 -0800 | [diff] [blame] | 694 | private fun done() { |
| 695 | showToolbar = true |
Zach Klippenstein | 72e3e51 | 2022-01-14 12:24:09 -0800 | [diff] [blame] | 696 | draggingHandle = null |
Zach Klippenstein | 6387089 | 2022-01-14 12:45:18 -0800 | [diff] [blame] | 697 | currentDragPosition = null |
Zach Klippenstein | 72e3e51 | 2022-01-14 12:24:09 -0800 | [diff] [blame] | 698 | } |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 699 | |
Grant Toepfer | fefbd7a | 2023-03-03 15:02:55 -0800 | [diff] [blame] | 700 | override fun onUp() = done() |
| 701 | override fun onStop() = done() |
| 702 | override fun onCancel() = done() |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 703 | } |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 704 | |
Ralston Da Silva | de62bc6 | 2021-06-02 17:46:44 -0700 | [diff] [blame] | 705 | /** |
| 706 | * Detect tap without consuming the up event. |
| 707 | */ |
| 708 | private suspend fun PointerInputScope.detectNonConsumingTap(onTap: (Offset) -> Unit) { |
George Mount | 32de9dd | 2022-10-05 14:51:06 -0700 | [diff] [blame] | 709 | awaitEachGesture { |
| 710 | waitForUpOrCancellation()?.let { |
| 711 | onTap(it.position) |
Ralston Da Silva | de62bc6 | 2021-06-02 17:46:44 -0700 | [diff] [blame] | 712 | } |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | private fun Modifier.onClearSelectionRequested(block: () -> Unit): Modifier { |
| 717 | return if (hasFocus) pointerInput(Unit) { detectNonConsumingTap { block() } } else this |
| 718 | } |
| 719 | |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 720 | private fun convertToContainerCoordinates( |
| 721 | layoutCoordinates: LayoutCoordinates, |
| 722 | offset: Offset |
Grant Toepfer | d681b5c | 2023-06-14 11:24:02 -0700 | [diff] [blame] | 723 | ): Offset { |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 724 | val coordinates = containerLayoutCoordinates |
Grant Toepfer | d681b5c | 2023-06-14 11:24:02 -0700 | [diff] [blame] | 725 | if (coordinates == null || !coordinates.isAttached) return Offset.Unspecified |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 726 | return requireContainerCoordinates().localPositionOf(layoutCoordinates, offset) |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 727 | } |
| 728 | |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 729 | /** |
| 730 | * Cancel the previous selection and start a new selection at the given [position]. |
| 731 | * It's used for long-press, double-click, triple-click and so on to start selection. |
| 732 | * |
| 733 | * @param position initial position of the selection. Both start and end handle is considered |
| 734 | * at this position. |
| 735 | * @param isStartHandle whether it's considered as the start handle moving. This parameter |
| 736 | * will influence the [SelectionAdjustment]'s behavior. For example, |
| 737 | * [SelectionAdjustment.Character] only adjust the moving handle. |
| 738 | * @param adjustment the selection adjustment. |
| 739 | */ |
| 740 | private fun startSelection( |
| 741 | position: Offset, |
| 742 | isStartHandle: Boolean, |
| 743 | adjustment: SelectionAdjustment |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 744 | ) { |
Grant Toepfer | d681b5c | 2023-06-14 11:24:02 -0700 | [diff] [blame] | 745 | previousSelectionLayout = null |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 746 | updateSelection( |
| 747 | startHandlePosition = position, |
| 748 | endHandlePosition = position, |
Grant Toepfer | d681b5c | 2023-06-14 11:24:02 -0700 | [diff] [blame] | 749 | previousHandlePosition = Offset.Unspecified, |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 750 | isStartHandle = isStartHandle, |
| 751 | adjustment = adjustment |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 752 | ) |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 753 | } |
Haoyu Zhang | a78608e | 2021-08-03 17:10:18 -0700 | [diff] [blame] | 754 | |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 755 | /** |
| 756 | * Updates the selection after one of the selection handle moved. |
| 757 | * |
| 758 | * @param newPosition the new position of the moving selection handle. |
| 759 | * @param previousPosition the previous position of the moving selection handle. |
| 760 | * @param isStartHandle whether the moving selection handle is the start handle. |
| 761 | * @param adjustment the [SelectionAdjustment] used to adjust the raw selection range and |
| 762 | * produce the final selection range. |
| 763 | * |
| 764 | * @return a boolean representing whether the movement is consumed. |
| 765 | * |
| 766 | * @see SelectionAdjustment |
| 767 | */ |
| 768 | internal fun updateSelection( |
| 769 | newPosition: Offset?, |
Grant Toepfer | d681b5c | 2023-06-14 11:24:02 -0700 | [diff] [blame] | 770 | previousPosition: Offset, |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 771 | isStartHandle: Boolean, |
| 772 | adjustment: SelectionAdjustment, |
| 773 | ): Boolean { |
| 774 | if (newPosition == null) return false |
| 775 | val otherHandlePosition = selection?.let { selection -> |
| 776 | val otherSelectableId = if (isStartHandle) { |
| 777 | selection.end.selectableId |
| 778 | } else { |
| 779 | selection.start.selectableId |
| 780 | } |
| 781 | val otherSelectable = |
| 782 | selectionRegistrar.selectableMap[otherSelectableId] ?: return@let null |
| 783 | convertToContainerCoordinates( |
| 784 | otherSelectable.getLayoutCoordinates()!!, |
| 785 | getAdjustedCoordinates( |
| 786 | otherSelectable.getHandlePosition(selection, !isStartHandle) |
| 787 | ) |
Haoyu Zhang | a78608e | 2021-08-03 17:10:18 -0700 | [diff] [blame] | 788 | ) |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 789 | } ?: return false |
| 790 | |
| 791 | val startHandlePosition = if (isStartHandle) newPosition else otherHandlePosition |
| 792 | val endHandlePosition = if (isStartHandle) otherHandlePosition else newPosition |
| 793 | |
| 794 | return updateSelection( |
| 795 | startHandlePosition = startHandlePosition, |
| 796 | endHandlePosition = endHandlePosition, |
| 797 | previousHandlePosition = previousPosition, |
| 798 | isStartHandle = isStartHandle, |
| 799 | adjustment = adjustment |
| 800 | ) |
| 801 | } |
| 802 | |
| 803 | /** |
| 804 | * Updates the selection after one of the selection handle moved. |
| 805 | * |
| 806 | * To make sure that [SelectionAdjustment] works correctly, it's expected that only one |
| 807 | * selection handle is updated each time. The only exception is that when a new selection is |
| 808 | * started. In this case, [previousHandlePosition] is always null. |
| 809 | * |
| 810 | * @param startHandlePosition the position of the start selection handle. |
| 811 | * @param endHandlePosition the position of the end selection handle. |
| 812 | * @param previousHandlePosition the position of the moving handle before the update. |
| 813 | * @param isStartHandle whether the moving selection handle is the start handle. |
| 814 | * @param adjustment the [SelectionAdjustment] used to adjust the raw selection range and |
| 815 | * produce the final selection range. |
| 816 | * |
| 817 | * @return a boolean representing whether the movement is consumed. It's useful for the case |
| 818 | * where a selection handle is updating consecutively. When the return value is true, it's |
| 819 | * expected that the caller will update the [startHandlePosition] to be the given |
| 820 | * [endHandlePosition] in following calls. |
| 821 | * |
| 822 | * @see SelectionAdjustment |
| 823 | */ |
| 824 | internal fun updateSelection( |
| 825 | startHandlePosition: Offset, |
| 826 | endHandlePosition: Offset, |
Grant Toepfer | d681b5c | 2023-06-14 11:24:02 -0700 | [diff] [blame] | 827 | previousHandlePosition: Offset, |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 828 | isStartHandle: Boolean, |
| 829 | adjustment: SelectionAdjustment, |
| 830 | ): Boolean { |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame] | 831 | draggingHandle = if (isStartHandle) Handle.SelectionStart else Handle.SelectionEnd |
Zach Klippenstein | 6387089 | 2022-01-14 12:45:18 -0800 | [diff] [blame] | 832 | currentDragPosition = if (isStartHandle) startHandlePosition else endHandlePosition |
Haoyu Zhang | 0020dd7 | 2021-08-19 19:21:03 -0700 | [diff] [blame] | 833 | |
Grant Toepfer | d681b5c | 2023-06-14 11:24:02 -0700 | [diff] [blame] | 834 | val selectionLayout = getSelectionLayout( |
| 835 | startHandlePosition = startHandlePosition, |
| 836 | endHandlePosition = endHandlePosition, |
| 837 | previousHandlePosition = previousHandlePosition, |
| 838 | isStartHandle = isStartHandle, |
| 839 | ) |
Grant Toepfer | fefbd7a | 2023-03-03 15:02:55 -0800 | [diff] [blame] | 840 | |
Grant Toepfer | d681b5c | 2023-06-14 11:24:02 -0700 | [diff] [blame] | 841 | if (!selectionLayout.shouldRecomputeSelection(previousSelectionLayout)) { |
| 842 | return false |
Haoyu Zhang | a78608e | 2021-08-03 17:10:18 -0700 | [diff] [blame] | 843 | } |
Grant Toepfer | d681b5c | 2023-06-14 11:24:02 -0700 | [diff] [blame] | 844 | |
| 845 | val newSelection = adjustment.adjust(selectionLayout) |
| 846 | if (newSelection != selection) { |
| 847 | selectionChanged(selectionLayout, newSelection) |
| 848 | } |
| 849 | previousSelectionLayout = selectionLayout |
| 850 | return true |
Haoyu Zhang | a78608e | 2021-08-03 17:10:18 -0700 | [diff] [blame] | 851 | } |
Andrey Rudenko | f3906a0 | 2021-10-12 13:23:40 +0200 | [diff] [blame] | 852 | |
Grant Toepfer | d681b5c | 2023-06-14 11:24:02 -0700 | [diff] [blame] | 853 | private fun getSelectionLayout( |
| 854 | startHandlePosition: Offset, |
| 855 | endHandlePosition: Offset, |
| 856 | previousHandlePosition: Offset, |
| 857 | isStartHandle: Boolean, |
| 858 | ): SelectionLayout { |
| 859 | val containerCoordinates = requireContainerCoordinates() |
| 860 | val sortedSelectables = selectionRegistrar.sort(containerCoordinates) |
| 861 | |
| 862 | val idToIndexMap = mutableMapOf<Long, Int>() |
| 863 | sortedSelectables.fastForEachIndexed { index, selectable -> |
| 864 | idToIndexMap[selectable.selectableId] = index |
| 865 | } |
| 866 | |
| 867 | val selectableIdOrderingComparator = compareBy<Long> { idToIndexMap[it] } |
| 868 | |
| 869 | // if previous handle is null, then treat this as a new selection. |
| 870 | val previousSelection = if (previousHandlePosition.isUnspecified) null else selection |
| 871 | val builder = SelectionLayoutBuilder( |
| 872 | startHandlePosition = startHandlePosition, |
| 873 | endHandlePosition = endHandlePosition, |
| 874 | previousHandlePosition = previousHandlePosition, |
| 875 | containerCoordinates = containerCoordinates, |
| 876 | isStartHandle = isStartHandle, |
| 877 | previousSelection = previousSelection, |
| 878 | selectableIdOrderingComparator = selectableIdOrderingComparator, |
| 879 | ) |
| 880 | |
| 881 | sortedSelectables.fastForEach { |
| 882 | it.appendSelectableInfoToBuilder(builder) |
| 883 | } |
| 884 | |
| 885 | return builder.build() |
| 886 | } |
| 887 | |
| 888 | private fun selectionChanged(selectionLayout: SelectionLayout, newSelection: Selection) { |
| 889 | if (shouldPerformHaptics()) { |
| 890 | hapticFeedBack?.performHapticFeedback(HapticFeedbackType.TextHandleMove) |
| 891 | } |
| 892 | selectionRegistrar.subselections = selectionLayout.createSubSelections(newSelection) |
| 893 | onSelectionChange(newSelection) |
| 894 | } |
| 895 | |
| 896 | @VisibleForTesting |
| 897 | internal fun shouldPerformHaptics(): Boolean = |
| 898 | isInTouchMode && selectionRegistrar.selectables.fastAny { it.getText().isNotEmpty() } |
| 899 | |
Andrey Rudenko | f3906a0 | 2021-10-12 13:23:40 +0200 | [diff] [blame] | 900 | fun contextMenuOpenAdjustment(position: Offset) { |
| 901 | val isEmptySelection = selection?.toTextRange()?.collapsed ?: true |
| 902 | // TODO(b/209483184) the logic should be more complex here, it should check that current |
Grant Toepfer | fefbd7a | 2023-03-03 15:02:55 -0800 | [diff] [blame] | 903 | // selection doesn't include click position |
Andrey Rudenko | f3906a0 | 2021-10-12 13:23:40 +0200 | [diff] [blame] | 904 | if (isEmptySelection) { |
| 905 | startSelection( |
| 906 | position = position, |
| 907 | isStartHandle = true, |
| 908 | adjustment = SelectionAdjustment.Word |
| 909 | ) |
| 910 | } |
| 911 | } |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 912 | } |
| 913 | |
Andrey Rudenko | 5dc7aad | 2020-09-18 10:33:37 +0200 | [diff] [blame] | 914 | internal fun merge(lhs: Selection?, rhs: Selection?): Selection? { |
Siyamed Sinir | 700df845 | 2019-10-22 20:23:58 -0700 | [diff] [blame] | 915 | return lhs?.merge(rhs) ?: rhs |
| 916 | } |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 917 | |
Andrey Rudenko | 8b4981e | 2021-01-29 15:31:59 +0100 | [diff] [blame] | 918 | internal expect fun isCopyKeyEvent(keyEvent: KeyEvent): Boolean |
| 919 | |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame] | 920 | internal expect fun Modifier.selectionMagnifier(manager: SelectionManager): Modifier |
| 921 | |
Zach Klippenstein | 9c32bdf | 2021-11-11 16:38:13 -0800 | [diff] [blame] | 922 | internal fun calculateSelectionMagnifierCenterAndroid( |
| 923 | manager: SelectionManager, |
| 924 | magnifierSize: IntSize |
| 925 | ): Offset { |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame] | 926 | val selection = manager.selection ?: return Offset.Unspecified |
| 927 | return when (manager.draggingHandle) { |
| 928 | null -> return Offset.Unspecified |
Grant Toepfer | d681b5c | 2023-06-14 11:24:02 -0700 | [diff] [blame] | 929 | Handle.SelectionStart -> getMagnifierCenter(manager, magnifierSize, selection.start) |
| 930 | Handle.SelectionEnd -> getMagnifierCenter(manager, magnifierSize, selection.end) |
Zach Klippenstein | adabe34 | 2021-11-11 16:38:13 -0800 | [diff] [blame] | 931 | Handle.Cursor -> error("SelectionContainer does not support cursor") |
| 932 | } |
| 933 | } |
| 934 | |
Grant Toepfer | d681b5c | 2023-06-14 11:24:02 -0700 | [diff] [blame] | 935 | private fun getMagnifierCenter( |
| 936 | manager: SelectionManager, |
| 937 | magnifierSize: IntSize, |
| 938 | anchor: AnchorInfo |
| 939 | ): Offset { |
| 940 | val selectable = manager.getAnchorSelectable(anchor) ?: return Offset.Unspecified |
| 941 | val containerCoordinates = manager.containerLayoutCoordinates ?: return Offset.Unspecified |
| 942 | val selectableCoordinates = selectable.getLayoutCoordinates() ?: return Offset.Unspecified |
| 943 | val offset = anchor.offset |
| 944 | |
| 945 | if (offset > selectable.getLastVisibleOffset()) return Offset.Unspecified |
| 946 | |
| 947 | // The horizontal position doesn't snap to cursor positions but should directly track the |
| 948 | // actual drag. |
| 949 | val localDragPosition = selectableCoordinates.localPositionOf( |
| 950 | containerCoordinates, |
| 951 | manager.currentDragPosition!! |
| 952 | ) |
| 953 | val dragX = localDragPosition.x |
| 954 | |
| 955 | // But it is constrained by the horizontal bounds of the current line. |
| 956 | val lineRange = selectable.getRangeOfLineContaining(offset) |
| 957 | val textConstrainedX = if (lineRange.collapsed) { |
| 958 | // A collapsed range implies the text is empty. |
| 959 | // line left and right are equal for this offset, so use either |
| 960 | selectable.getLineLeft(offset) |
| 961 | } else { |
| 962 | val lineStartX = selectable.getLineLeft(lineRange.start) |
| 963 | val lineEndX = selectable.getLineRight(lineRange.end - 1) |
| 964 | // in RTL/BiDi, lineStartX may be larger than lineEndX |
| 965 | val minX = minOf(lineStartX, lineEndX) |
| 966 | val maxX = maxOf(lineStartX, lineEndX) |
| 967 | dragX.coerceIn(minX, maxX) |
| 968 | } |
| 969 | |
| 970 | // selectable couldn't determine horizontals |
| 971 | if (textConstrainedX == -1f) return Offset.Unspecified |
| 972 | |
| 973 | // Hide the magnifier when dragged too far (outside the horizontal bounds of how big the |
| 974 | // magnifier actually is). See |
| 975 | // https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/widget/Editor.java;l=5228-5231;drc=2fdb6bd709be078b72f011334362456bb758922c |
| 976 | if ((dragX - textConstrainedX).absoluteValue > magnifierSize.width / 2) { |
| 977 | return Offset.Unspecified |
| 978 | } |
| 979 | |
| 980 | val lineCenterY = selectable.getCenterYForOffset(offset) |
| 981 | |
| 982 | // selectable couldn't determine the line center |
| 983 | if (lineCenterY == -1f) return Offset.Unspecified |
| 984 | |
| 985 | return containerCoordinates.localPositionOf( |
| 986 | sourceCoordinates = selectableCoordinates, |
| 987 | relativeToSource = Offset(textConstrainedX, lineCenterY) |
| 988 | ) |
| 989 | } |
| 990 | |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 991 | /** Returns the boundary of the visible area in this [LayoutCoordinates]. */ |
haoyu | ac341f0 | 2021-01-22 22:01:56 -0800 | [diff] [blame] | 992 | internal fun LayoutCoordinates.visibleBounds(): Rect { |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 993 | // globalBounds is the global boundaries of this LayoutCoordinates after it's clipped by |
| 994 | // parents. We can think it as the global visible bounds of this Layout. Here globalBounds |
| 995 | // 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] | 996 | val boundsInWindow = boundsInWindow() |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 997 | return Rect( |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 998 | windowToLocal(boundsInWindow.topLeft), |
| 999 | windowToLocal(boundsInWindow.bottomRight) |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 1000 | ) |
| 1001 | } |
| 1002 | |
haoyu | ac341f0 | 2021-01-22 22:01:56 -0800 | [diff] [blame] | 1003 | internal fun Rect.containsInclusive(offset: Offset): Boolean = |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 1004 | offset.x in left..right && offset.y in top..bottom |