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