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