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