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