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