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 |
Louis Pullen-Freilich | 1f10a59 | 2020-07-24 16:35:14 +0100 | [diff] [blame] | 23 | import androidx.compose.runtime.State |
| 24 | import androidx.compose.runtime.getValue |
| 25 | import androidx.compose.runtime.mutableStateOf |
| 26 | import androidx.compose.runtime.setValue |
| 27 | import androidx.compose.runtime.structuralEqualityPolicy |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 28 | import androidx.compose.ui.Modifier |
| 29 | import androidx.compose.ui.focus.FocusRequester |
| 30 | import androidx.compose.ui.focus.focusRequester |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 31 | import androidx.compose.ui.focus.onFocusChanged |
Louis Pullen-Freilich | 534385a | 2020-08-14 14:10:12 +0100 | [diff] [blame] | 32 | import androidx.compose.ui.geometry.Offset |
| 33 | import androidx.compose.ui.geometry.Rect |
Matvei Malkov | f770a91 | 2021-03-24 18:12:41 +0000 | [diff] [blame] | 34 | import androidx.compose.foundation.text.TextDragObserver |
Louis Pullen-Freilich | a03fd6c | 2020-07-24 23:26:29 +0100 | [diff] [blame] | 35 | import androidx.compose.ui.hapticfeedback.HapticFeedback |
| 36 | import androidx.compose.ui.hapticfeedback.HapticFeedbackType |
Andrey Rudenko | 8b4981e | 2021-01-29 15:31:59 +0100 | [diff] [blame] | 37 | import androidx.compose.ui.input.key.KeyEvent |
| 38 | import androidx.compose.ui.input.key.onKeyEvent |
Louis Pullen-Freilich | 534385a | 2020-08-14 14:10:12 +0100 | [diff] [blame] | 39 | import androidx.compose.ui.layout.LayoutCoordinates |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 40 | import androidx.compose.ui.layout.boundsInWindow |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 41 | import androidx.compose.ui.layout.onGloballyPositioned |
haoyu | 7ad5ea3 | 2021-03-22 10:36:35 -0700 | [diff] [blame] | 42 | import androidx.compose.ui.layout.positionInWindow |
Louis Pullen-Freilich | 534385a | 2020-08-14 14:10:12 +0100 | [diff] [blame] | 43 | import androidx.compose.ui.platform.ClipboardManager |
Louis Pullen-Freilich | a03fd6c | 2020-07-24 23:26:29 +0100 | [diff] [blame] | 44 | import androidx.compose.ui.platform.TextToolbar |
| 45 | import androidx.compose.ui.platform.TextToolbarStatus |
Louis Pullen-Freilich | ab19475 | 2020-07-21 22:21:22 +0100 | [diff] [blame] | 46 | import androidx.compose.ui.text.AnnotatedString |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 47 | import kotlin.math.max |
| 48 | import kotlin.math.min |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 49 | |
Qingqing Deng | 35f97ea | 2019-09-18 19:24:37 -0700 | [diff] [blame] | 50 | /** |
Louis Pullen-Freilich | 5da28bd | 2019-10-15 17:05:07 +0100 | [diff] [blame] | 51 | * 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] | 52 | */ |
Siyamed Sinir | 700df845 | 2019-10-22 20:23:58 -0700 | [diff] [blame] | 53 | internal class SelectionManager(private val selectionRegistrar: SelectionRegistrarImpl) { |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 54 | /** |
| 55 | * The current selection. |
| 56 | */ |
| 57 | var selection: Selection? = null |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 58 | set(value) { |
| 59 | field = value |
haoyu | 9085c88 | 2020-12-08 12:01:06 -0800 | [diff] [blame] | 60 | if (value != null) { |
| 61 | updateHandleOffsets() |
| 62 | } |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 63 | } |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 64 | |
| 65 | /** |
Andrey Rudenko | 8b4981e | 2021-01-29 15:31:59 +0100 | [diff] [blame] | 66 | * Is touch mode active |
| 67 | */ |
| 68 | var touchMode: Boolean = true |
| 69 | |
| 70 | /** |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 71 | * The manager will invoke this every time it comes to the conclusion that the selection should |
| 72 | * change. The expectation is that this callback will end up causing `setSelection` to get |
| 73 | * called. This is what makes this a "controlled component". |
| 74 | */ |
| 75 | var onSelectionChange: (Selection?) -> Unit = {} |
| 76 | |
| 77 | /** |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 78 | * [HapticFeedback] handle to perform haptic feedback. |
| 79 | */ |
| 80 | var hapticFeedBack: HapticFeedback? = null |
| 81 | |
| 82 | /** |
Qingqing Deng | f2d0a2d | 2020-03-12 14:19:50 -0700 | [diff] [blame] | 83 | * [ClipboardManager] to perform clipboard features. |
| 84 | */ |
| 85 | var clipboardManager: ClipboardManager? = null |
| 86 | |
| 87 | /** |
Qingqing Deng | a89450d | 2020-04-03 19:11:49 -0700 | [diff] [blame] | 88 | * [TextToolbar] to show floating toolbar(post-M) or primary toolbar(pre-M). |
| 89 | */ |
| 90 | var textToolbar: TextToolbar? = null |
| 91 | |
| 92 | /** |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 93 | * Focus requester used to request focus when selection becomes active. |
| 94 | */ |
| 95 | var focusRequester: FocusRequester = FocusRequester() |
| 96 | |
| 97 | /** |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 98 | * Return true if the corresponding SelectionContainer is focused. |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 99 | */ |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 100 | var hasFocus: Boolean by mutableStateOf(false) |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 101 | |
| 102 | /** |
| 103 | * Modifier for selection container. |
| 104 | */ |
| 105 | val modifier get() = Modifier |
| 106 | .onGloballyPositioned { containerLayoutCoordinates = it } |
| 107 | .focusRequester(focusRequester) |
| 108 | .onFocusChanged { focusState -> |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 109 | if (!focusState.isFocused && hasFocus) { |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 110 | onRelease() |
| 111 | } |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 112 | hasFocus = focusState.isFocused |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 113 | } |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 114 | .focusable() |
Andrey Rudenko | 8b4981e | 2021-01-29 15:31:59 +0100 | [diff] [blame] | 115 | .onKeyEvent { |
| 116 | if (isCopyKeyEvent(it)) { |
| 117 | copy() |
| 118 | true |
| 119 | } else { |
| 120 | false |
| 121 | } |
| 122 | } |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 123 | |
haoyu | 7ad5ea3 | 2021-03-22 10:36:35 -0700 | [diff] [blame] | 124 | private var previousPosition: Offset? = null |
haoyu | c40d0275 | 2021-01-25 17:32:47 -0800 | [diff] [blame] | 125 | /** |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 126 | * Layout Coordinates of the selection container. |
| 127 | */ |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 128 | var containerLayoutCoordinates: LayoutCoordinates? = null |
| 129 | set(value) { |
| 130 | field = value |
haoyu | 2c6e984 | 2021-03-30 17:39:04 -0700 | [diff] [blame] | 131 | if (hasFocus && selection != null) { |
| 132 | val positionInWindow = value?.positionInWindow() |
| 133 | if (previousPosition != positionInWindow) { |
| 134 | previousPosition = positionInWindow |
| 135 | updateHandleOffsets() |
| 136 | updateSelectionToolbarPosition() |
| 137 | } |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 138 | } |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 139 | } |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 140 | |
| 141 | /** |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 142 | * The beginning position of the drag gesture. Every time a new drag gesture starts, it wil be |
| 143 | * recalculated. |
| 144 | */ |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 145 | private var dragBeginPosition = Offset.Zero |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 146 | |
| 147 | /** |
| 148 | * The total distance being dragged of the drag gesture. Every time a new drag gesture starts, |
| 149 | * it will be zeroed out. |
| 150 | */ |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 151 | private var dragTotalDistance = Offset.Zero |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 152 | |
| 153 | /** |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 154 | * The calculated position of the start handle in the [SelectionContainer] coordinates. It |
| 155 | * is null when handle shouldn't be displayed. |
| 156 | * It is a [State] so reading it during the composition will cause recomposition every time |
| 157 | * the position has been changed. |
| 158 | */ |
Chuck Jazdzewski | 0a90de9 | 2020-05-21 10:03:47 -0700 | [diff] [blame] | 159 | var startHandlePosition by mutableStateOf<Offset?>( |
| 160 | null, |
| 161 | policy = structuralEqualityPolicy() |
| 162 | ) |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 163 | private set |
| 164 | |
| 165 | /** |
| 166 | * The calculated position of the end handle in the [SelectionContainer] coordinates. It |
| 167 | * is null when handle shouldn't be displayed. |
| 168 | * It is a [State] so reading it during the composition will cause recomposition every time |
| 169 | * the position has been changed. |
| 170 | */ |
Chuck Jazdzewski | 0a90de9 | 2020-05-21 10:03:47 -0700 | [diff] [blame] | 171 | var endHandlePosition by mutableStateOf<Offset?>( |
| 172 | null, |
| 173 | policy = structuralEqualityPolicy() |
| 174 | ) |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 175 | private set |
| 176 | |
| 177 | init { |
haoyu | 7ad5ea3 | 2021-03-22 10:36:35 -0700 | [diff] [blame] | 178 | selectionRegistrar.onPositionChangeCallback = { selectableId -> |
| 179 | if ( |
| 180 | selectableId == selection?.start?.selectableId || |
| 181 | selectableId == selection?.end?.selectableId |
| 182 | ) { |
| 183 | updateHandleOffsets() |
| 184 | updateSelectionToolbarPosition() |
| 185 | } |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 188 | selectionRegistrar.onSelectionUpdateStartCallback = |
| 189 | { layoutCoordinates, startPosition, selectionMode -> |
| 190 | val startPositionInContainer = convertToContainerCoordinates( |
| 191 | layoutCoordinates, |
| 192 | startPosition |
| 193 | ) |
| 194 | |
| 195 | updateSelection( |
| 196 | startPosition = startPositionInContainer, |
| 197 | endPosition = startPositionInContainer, |
| 198 | isStartHandle = true, |
| 199 | adjustment = selectionMode |
| 200 | ) |
| 201 | |
| 202 | focusRequester.requestFocus() |
| 203 | hideSelectionToolbar() |
| 204 | } |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 205 | |
Qingqing Deng | 6bc981c | 2021-05-11 22:43:51 -0700 | [diff] [blame^] | 206 | selectionRegistrar.onSelectionUpdateSelectAll = |
| 207 | { selectableId -> |
| 208 | val (newSelection, newSubselection) = mergeSelections( |
| 209 | selectableId = selectableId, |
| 210 | previousSelection = selection, |
| 211 | ) |
| 212 | if (newSelection != selection) { |
| 213 | selectionRegistrar.subselections = newSubselection |
| 214 | onSelectionChange(newSelection) |
| 215 | } |
| 216 | |
| 217 | focusRequester.requestFocus() |
| 218 | hideSelectionToolbar() |
| 219 | } |
| 220 | |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 221 | selectionRegistrar.onSelectionUpdateCallback = |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 222 | { layoutCoordinates, startPosition, endPosition, selectionMode -> |
| 223 | val startPositionOrCurrent = if (startPosition == null) { |
| 224 | currentSelectionStartPosition() |
| 225 | } else { |
| 226 | convertToContainerCoordinates(layoutCoordinates, startPosition) |
| 227 | } |
| 228 | |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 229 | updateSelection( |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 230 | startPosition = startPositionOrCurrent, |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 231 | endPosition = convertToContainerCoordinates(layoutCoordinates, endPosition), |
haoyu | 70aed73 | 2020-12-15 07:10:56 -0800 | [diff] [blame] | 232 | isStartHandle = false, |
Haoyu Zhang | 4c6c637 | 2021-04-20 10:25:21 -0700 | [diff] [blame] | 233 | adjustment = selectionMode |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 234 | ) |
| 235 | } |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 236 | |
| 237 | selectionRegistrar.onSelectionUpdateEndCallback = { |
| 238 | showSelectionToolbar() |
| 239 | } |
haoyu | 9085c88 | 2020-12-08 12:01:06 -0800 | [diff] [blame] | 240 | |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 241 | selectionRegistrar.onSelectableChangeCallback = { selectableKey -> |
| 242 | if (selectableKey in selectionRegistrar.subselections) { |
haoyu | 9085c88 | 2020-12-08 12:01:06 -0800 | [diff] [blame] | 243 | // clear the selection range of each Selectable. |
| 244 | onRelease() |
| 245 | selection = null |
| 246 | } |
| 247 | } |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 248 | |
| 249 | selectionRegistrar.afterSelectableUnsubscribe = { selectableKey -> |
| 250 | if ( |
| 251 | selectableKey == selection?.start?.selectableId || |
| 252 | selectableKey == selection?.end?.selectableId |
| 253 | ) { |
| 254 | // The selectable that contains a selection handle just unsubscribed. |
| 255 | // Hide selection handles for now |
| 256 | startHandlePosition = null |
| 257 | endHandlePosition = null |
| 258 | } |
| 259 | } |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 260 | } |
| 261 | |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 262 | private fun currentSelectionStartPosition(): Offset? { |
| 263 | return selection?.let { selection -> |
| 264 | val startSelectable = |
| 265 | selectionRegistrar.selectableMap[selection.start.selectableId] |
| 266 | |
| 267 | requireContainerCoordinates().localPositionOf( |
| 268 | startSelectable?.getLayoutCoordinates()!!, |
| 269 | getAdjustedCoordinates( |
| 270 | startSelectable.getHandlePosition( |
| 271 | selection = selection, |
| 272 | isStartHandle = true |
| 273 | ) |
| 274 | ) |
| 275 | ) |
| 276 | } |
| 277 | } |
| 278 | |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 279 | private fun updateHandleOffsets() { |
| 280 | val selection = selection |
| 281 | val containerCoordinates = containerLayoutCoordinates |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 282 | val startSelectable = selection?.start?.selectableId?.let { |
| 283 | selectionRegistrar.selectableMap[it] |
| 284 | } |
| 285 | val endSelectable = selection?.end?.selectableId?.let { |
| 286 | selectionRegistrar.selectableMap[it] |
| 287 | } |
| 288 | val startLayoutCoordinates = startSelectable?.getLayoutCoordinates() |
| 289 | val endLayoutCoordinates = endSelectable?.getLayoutCoordinates() |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 290 | if ( |
| 291 | selection == null || |
| 292 | containerCoordinates == null || |
| 293 | !containerCoordinates.isAttached || |
| 294 | startLayoutCoordinates == null || |
| 295 | endLayoutCoordinates == null |
| 296 | ) { |
| 297 | this.startHandlePosition = null |
| 298 | this.endHandlePosition = null |
| 299 | return |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 300 | } |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 301 | |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 302 | val startHandlePosition = containerCoordinates.localPositionOf( |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 303 | startLayoutCoordinates, |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 304 | startSelectable.getHandlePosition( |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 305 | selection = selection, |
| 306 | isStartHandle = true |
| 307 | ) |
| 308 | ) |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 309 | val endHandlePosition = containerCoordinates.localPositionOf( |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 310 | endLayoutCoordinates, |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 311 | endSelectable.getHandlePosition( |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 312 | selection = selection, |
| 313 | isStartHandle = false |
| 314 | ) |
| 315 | ) |
| 316 | |
| 317 | val visibleBounds = containerCoordinates.visibleBounds() |
| 318 | this.startHandlePosition = |
| 319 | if (visibleBounds.containsInclusive(startHandlePosition)) startHandlePosition else null |
| 320 | this.endHandlePosition = |
| 321 | if (visibleBounds.containsInclusive(endHandlePosition)) endHandlePosition else null |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Returns non-nullable [containerLayoutCoordinates]. |
| 326 | */ |
| 327 | internal fun requireContainerCoordinates(): LayoutCoordinates { |
| 328 | val coordinates = containerLayoutCoordinates |
| 329 | require(coordinates != null) |
| 330 | require(coordinates.isAttached) |
| 331 | return coordinates |
| 332 | } |
| 333 | |
| 334 | /** |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 335 | * Iterates over the handlers, gets the selection for each Composable, and merges all the |
| 336 | * returned [Selection]s. |
| 337 | * |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 338 | * @param startPosition [Offset] for the start of the selection |
| 339 | * @param endPosition [Offset] for the end of the selection |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 340 | * @param previousSelection previous selection |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 341 | * |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 342 | * @return a [Pair] of a [Selection] object which is constructed by combining all |
| 343 | * composables that are selected and a [Map] from selectable key to [Selection]s on the |
| 344 | * [Selectable] corresponding to the that key. |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 345 | */ |
Qingqing Deng | 5819ff6 | 2019-11-18 15:26:23 -0800 | [diff] [blame] | 346 | // This function is internal for testing purposes. |
| 347 | internal fun mergeSelections( |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 348 | startPosition: Offset, |
| 349 | endPosition: Offset, |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 350 | adjustment: SelectionAdjustment = SelectionAdjustment.NONE, |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 351 | previousSelection: Selection? = null, |
Qingqing Deng | ff65ed6 | 2020-01-06 17:55:48 -0800 | [diff] [blame] | 352 | isStartHandle: Boolean = true |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 353 | ): Pair<Selection?, Map<Long, Selection>> { |
| 354 | val subselections = mutableMapOf<Long, Selection>() |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 355 | val newSelection = selectionRegistrar.sort(requireContainerCoordinates()) |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 356 | .fastFold(null) { mergedSelection: Selection?, selectable: Selectable -> |
| 357 | val selection = selectable.getSelection( |
| 358 | startPosition = startPosition, |
| 359 | endPosition = endPosition, |
| 360 | containerLayoutCoordinates = requireContainerCoordinates(), |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 361 | previousSelection = previousSelection, |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 362 | isStartHandle = isStartHandle, |
Haoyu Zhang | 4c6c637 | 2021-04-20 10:25:21 -0700 | [diff] [blame] | 363 | adjustment = adjustment |
Siyamed Sinir | 700df845 | 2019-10-22 20:23:58 -0700 | [diff] [blame] | 364 | ) |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 365 | selection?.let { subselections[selectable.selectableId] = it } |
| 366 | merge(mergedSelection, selection) |
Qingqing Deng | 247f2b4 | 2019-12-12 19:48:37 -0800 | [diff] [blame] | 367 | } |
Qingqing Deng | 25b8f8d | 2020-01-17 16:36:19 -0800 | [diff] [blame] | 368 | if (previousSelection != newSelection) hapticFeedBack?.performHapticFeedback( |
| 369 | HapticFeedbackType.TextHandleMove |
| 370 | ) |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 371 | return Pair(newSelection, subselections) |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Qingqing Deng | 6bc981c | 2021-05-11 22:43:51 -0700 | [diff] [blame^] | 374 | internal fun mergeSelections( |
| 375 | previousSelection: Selection? = null, |
| 376 | selectableId: Long |
| 377 | ): Pair<Selection?, Map<Long, Selection>> { |
| 378 | val subselections = mutableMapOf<Long, Selection>() |
| 379 | val newSelection = selectionRegistrar.sort(requireContainerCoordinates()) |
| 380 | .fastFold(null) { mergedSelection: Selection?, selectable: Selectable -> |
| 381 | val selection = if (selectable.selectableId == selectableId) |
| 382 | selectable.getSelectAllSelection() else null |
| 383 | selection?.let { subselections[selectable.selectableId] = it } |
| 384 | merge(mergedSelection, selection) |
| 385 | } |
| 386 | if (previousSelection != newSelection) hapticFeedBack?.performHapticFeedback( |
| 387 | HapticFeedbackType.TextHandleMove |
| 388 | ) |
| 389 | return Pair(newSelection, subselections) |
| 390 | } |
| 391 | |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 392 | internal fun getSelectedText(): AnnotatedString? { |
Andrey Kulikov | b8c7e05 | 2020-04-15 19:20:09 +0100 | [diff] [blame] | 393 | val selectables = selectionRegistrar.sort(requireContainerCoordinates()) |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 394 | var selectedText: AnnotatedString? = null |
| 395 | |
| 396 | selection?.let { |
Louis Pullen-Freilich | c6e92aa | 2021-03-08 00:42:16 +0000 | [diff] [blame] | 397 | for (i in selectables.indices) { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 398 | val selectable = selectables[i] |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 399 | // Continue if the current selectable is before the selection starts. |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 400 | if (selectable.selectableId != it.start.selectableId && |
| 401 | selectable.selectableId != it.end.selectableId && |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 402 | selectedText == null |
| 403 | ) continue |
| 404 | |
| 405 | val currentSelectedText = getCurrentSelectedText( |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 406 | selectable = selectable, |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 407 | selection = it |
| 408 | ) |
| 409 | selectedText = selectedText?.plus(currentSelectedText) ?: currentSelectedText |
| 410 | |
| 411 | // Break if the current selectable is the last selected selectable. |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 412 | if (selectable.selectableId == it.end.selectableId && !it.handlesCrossed || |
| 413 | selectable.selectableId == it.start.selectableId && it.handlesCrossed |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 414 | ) break |
| 415 | } |
| 416 | } |
| 417 | return selectedText |
| 418 | } |
| 419 | |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 420 | internal fun copy() { |
Qingqing Deng | f2d0a2d | 2020-03-12 14:19:50 -0700 | [diff] [blame] | 421 | val selectedText = getSelectedText() |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 422 | selectedText?.let { clipboardManager?.setText(it) } |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * This function get the selected region as a Rectangle region, and pass it to [TextToolbar] |
| 427 | * to make the FloatingToolbar show up in the proper place. In addition, this function passes |
| 428 | * the copy method as a callback when "copy" is clicked. |
| 429 | */ |
| 430 | internal fun showSelectionToolbar() { |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 431 | if (hasFocus) { |
| 432 | selection?.let { |
| 433 | textToolbar?.showMenu( |
| 434 | getContentRect(), |
| 435 | onCopyRequested = { |
| 436 | copy() |
| 437 | onRelease() |
| 438 | } |
| 439 | ) |
| 440 | } |
Qingqing Deng | a89450d | 2020-04-03 19:11:49 -0700 | [diff] [blame] | 441 | } |
Qingqing Deng | f2d0a2d | 2020-03-12 14:19:50 -0700 | [diff] [blame] | 442 | } |
| 443 | |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 444 | internal fun hideSelectionToolbar() { |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 445 | if (hasFocus && textToolbar?.status == TextToolbarStatus.Shown) { |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 446 | textToolbar?.hide() |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
| 450 | private fun updateSelectionToolbarPosition() { |
haoyu | 3c3fb45 | 2021-02-18 01:01:14 -0800 | [diff] [blame] | 451 | if (hasFocus && textToolbar?.status == TextToolbarStatus.Shown) { |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 452 | showSelectionToolbar() |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * Calculate selected region as [Rect]. The top is the top of the first selected |
| 458 | * line, and the bottom is the bottom of the last selected line. The left is the leftmost |
| 459 | * handle's horizontal coordinates, and the right is the rightmost handle's coordinates. |
| 460 | */ |
| 461 | private fun getContentRect(): Rect { |
Nader Jawad | 8432b8d | 2020-07-27 14:51:23 -0700 | [diff] [blame] | 462 | val selection = selection ?: return Rect.Zero |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 463 | val startSelectable = selectionRegistrar.selectableMap[selection.start.selectableId] |
| 464 | val endSelectable = selectionRegistrar.selectableMap[selection.start.selectableId] |
| 465 | val startLayoutCoordinates = startSelectable?.getLayoutCoordinates() ?: return Rect.Zero |
| 466 | val endLayoutCoordinates = endSelectable?.getLayoutCoordinates() ?: return Rect.Zero |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 467 | |
| 468 | val localLayoutCoordinates = containerLayoutCoordinates |
| 469 | if (localLayoutCoordinates != null && localLayoutCoordinates.isAttached) { |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 470 | var startOffset = localLayoutCoordinates.localPositionOf( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 471 | startLayoutCoordinates, |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 472 | startSelectable.getHandlePosition( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 473 | selection = selection, |
| 474 | isStartHandle = true |
| 475 | ) |
| 476 | ) |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 477 | var endOffset = localLayoutCoordinates.localPositionOf( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 478 | endLayoutCoordinates, |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 479 | endSelectable.getHandlePosition( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 480 | selection = selection, |
| 481 | isStartHandle = false |
| 482 | ) |
| 483 | ) |
| 484 | |
| 485 | startOffset = localLayoutCoordinates.localToRoot(startOffset) |
| 486 | endOffset = localLayoutCoordinates.localToRoot(endOffset) |
| 487 | |
| 488 | val left = min(startOffset.x, endOffset.x) |
| 489 | val right = max(startOffset.x, endOffset.x) |
| 490 | |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 491 | var startTop = localLayoutCoordinates.localPositionOf( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 492 | startLayoutCoordinates, |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 493 | Offset( |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 494 | 0f, |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 495 | startSelectable.getBoundingBox(selection.start.offset).top |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 496 | ) |
| 497 | ) |
| 498 | |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 499 | var endTop = localLayoutCoordinates.localPositionOf( |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 500 | endLayoutCoordinates, |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 501 | Offset( |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 502 | 0.0f, |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 503 | endSelectable.getBoundingBox(selection.end.offset).top |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 504 | ) |
| 505 | ) |
| 506 | |
| 507 | startTop = localLayoutCoordinates.localToRoot(startTop) |
| 508 | endTop = localLayoutCoordinates.localToRoot(endTop) |
| 509 | |
| 510 | val top = min(startTop.y, endTop.y) |
Nader Jawad | 16e330a | 2020-05-21 21:21:01 -0700 | [diff] [blame] | 511 | val bottom = max(startOffset.y, endOffset.y) + (HANDLE_HEIGHT.value * 4.0).toFloat() |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 512 | |
| 513 | return Rect( |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 514 | left, |
| 515 | top, |
| 516 | right, |
| 517 | bottom |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 518 | ) |
| 519 | } |
Nader Jawad | 8432b8d | 2020-07-27 14:51:23 -0700 | [diff] [blame] | 520 | return Rect.Zero |
Qingqing Deng | de023cc | 2020-04-24 14:23:41 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Qingqing Deng | b6f5d8a | 2019-11-11 18:19:22 -0800 | [diff] [blame] | 523 | // This is for PressGestureDetector to cancel the selection. |
| 524 | fun onRelease() { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 525 | selectionRegistrar.subselections = emptyMap() |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 526 | hideSelectionToolbar() |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 527 | if (selection != null) { |
| 528 | onSelectionChange(null) |
| 529 | hapticFeedBack?.performHapticFeedback(HapticFeedbackType.TextHandleMove) |
| 530 | } |
Qingqing Deng | b6f5d8a | 2019-11-11 18:19:22 -0800 | [diff] [blame] | 531 | } |
| 532 | |
Matvei Malkov | f770a91 | 2021-03-24 18:12:41 +0000 | [diff] [blame] | 533 | fun handleDragObserver(isStartHandle: Boolean): TextDragObserver { |
| 534 | return object : TextDragObserver { |
| 535 | override fun onStart(startPoint: Offset) { |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 536 | hideSelectionToolbar() |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 537 | val selection = selection!! |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 538 | val startSelectable = |
| 539 | selectionRegistrar.selectableMap[selection.start.selectableId] |
| 540 | val endSelectable = |
| 541 | selectionRegistrar.selectableMap[selection.end.selectableId] |
Louis Pullen-Freilich | 5da28bd | 2019-10-15 17:05:07 +0100 | [diff] [blame] | 542 | // The LayoutCoordinates of the composable where the drag gesture should begin. This |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 543 | // is used to convert the position of the beginning of the drag gesture from the |
Louis Pullen-Freilich | 5da28bd | 2019-10-15 17:05:07 +0100 | [diff] [blame] | 544 | // composable coordinates to selection container coordinates. |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 545 | val beginLayoutCoordinates = if (isStartHandle) { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 546 | startSelectable?.getLayoutCoordinates()!! |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 547 | } else { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 548 | endSelectable?.getLayoutCoordinates()!! |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 551 | // The position of the character where the drag gesture should begin. This is in |
Louis Pullen-Freilich | 5da28bd | 2019-10-15 17:05:07 +0100 | [diff] [blame] | 552 | // the composable coordinates. |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 553 | val beginCoordinates = getAdjustedCoordinates( |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 554 | if (isStartHandle) { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 555 | startSelectable!!.getHandlePosition( |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 556 | selection = selection, isStartHandle = true |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 557 | ) |
| 558 | } else { |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 559 | endSelectable!!.getHandlePosition( |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 560 | selection = selection, isStartHandle = false |
| 561 | ) |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 562 | } |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 563 | ) |
| 564 | |
Louis Pullen-Freilich | 5da28bd | 2019-10-15 17:05:07 +0100 | [diff] [blame] | 565 | // Convert the position where drag gesture begins from composable coordinates to |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 566 | // selection container coordinates. |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 567 | dragBeginPosition = requireContainerCoordinates().localPositionOf( |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 568 | beginLayoutCoordinates, |
| 569 | beginCoordinates |
| 570 | ) |
| 571 | |
| 572 | // Zero out the total distance that being dragged. |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 573 | dragTotalDistance = Offset.Zero |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 574 | } |
| 575 | |
Matvei Malkov | f770a91 | 2021-03-24 18:12:41 +0000 | [diff] [blame] | 576 | override fun onDrag(delta: Offset) { |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 577 | val selection = selection!! |
Matvei Malkov | f770a91 | 2021-03-24 18:12:41 +0000 | [diff] [blame] | 578 | dragTotalDistance += delta |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 579 | val startSelectable = |
| 580 | selectionRegistrar.selectableMap[selection.start.selectableId] |
| 581 | val endSelectable = |
| 582 | selectionRegistrar.selectableMap[selection.end.selectableId] |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 583 | val currentStart = if (isStartHandle) { |
| 584 | dragBeginPosition + dragTotalDistance |
| 585 | } else { |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 586 | requireContainerCoordinates().localPositionOf( |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 587 | startSelectable?.getLayoutCoordinates()!!, |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 588 | getAdjustedCoordinates( |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 589 | startSelectable.getHandlePosition( |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 590 | selection = selection, |
| 591 | isStartHandle = true |
| 592 | ) |
| 593 | ) |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 594 | ) |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 595 | } |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 596 | |
| 597 | val currentEnd = if (isStartHandle) { |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 598 | requireContainerCoordinates().localPositionOf( |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 599 | endSelectable?.getLayoutCoordinates()!!, |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 600 | getAdjustedCoordinates( |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 601 | endSelectable.getHandlePosition( |
Qingqing Deng | 6d1b7d2 | 2019-12-27 17:48:08 -0800 | [diff] [blame] | 602 | selection = selection, |
| 603 | isStartHandle = false |
| 604 | ) |
| 605 | ) |
Siyamed Sinir | 472c316 | 2019-10-21 23:41:00 -0700 | [diff] [blame] | 606 | ) |
| 607 | } else { |
| 608 | dragBeginPosition + dragTotalDistance |
| 609 | } |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 610 | updateSelection( |
Qingqing Deng | a5d8095 | 2019-10-11 16:46:52 -0700 | [diff] [blame] | 611 | startPosition = currentStart, |
| 612 | endPosition = currentEnd, |
Qingqing Deng | ff65ed6 | 2020-01-06 17:55:48 -0800 | [diff] [blame] | 613 | isStartHandle = isStartHandle |
Qingqing Deng | a5d8095 | 2019-10-11 16:46:52 -0700 | [diff] [blame] | 614 | ) |
Matvei Malkov | f770a91 | 2021-03-24 18:12:41 +0000 | [diff] [blame] | 615 | return |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 616 | } |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 617 | |
Matvei Malkov | f770a91 | 2021-03-24 18:12:41 +0000 | [diff] [blame] | 618 | override fun onStop() { |
haoyu | e6d80a1 | 2020-12-02 16:04:52 -0800 | [diff] [blame] | 619 | showSelectionToolbar() |
| 620 | } |
| 621 | |
| 622 | override fun onCancel() { |
| 623 | showSelectionToolbar() |
| 624 | } |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 625 | } |
| 626 | } |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 627 | |
| 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 | |
| 637 | private fun updateSelection( |
| 638 | startPosition: Offset?, |
| 639 | endPosition: Offset?, |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 640 | adjustment: SelectionAdjustment = SelectionAdjustment.NONE, |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 641 | isStartHandle: Boolean = true |
| 642 | ) { |
| 643 | if (startPosition == null || endPosition == null) return |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 644 | val (newSelection, newSubselection) = mergeSelections( |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 645 | startPosition = startPosition, |
| 646 | endPosition = endPosition, |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 647 | adjustment = adjustment, |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 648 | isStartHandle = isStartHandle, |
Andrey Rudenko | d6c91be | 2021-03-18 15:45:20 +0100 | [diff] [blame] | 649 | previousSelection = selection, |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 650 | ) |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 651 | if (newSelection != selection) { |
| 652 | selectionRegistrar.subselections = newSubselection |
| 653 | onSelectionChange(newSelection) |
| 654 | } |
Qingqing Deng | 739ec3a | 2020-09-09 15:40:30 -0700 | [diff] [blame] | 655 | } |
Qingqing Deng | 6f56a91 | 2019-05-13 10:10:37 -0700 | [diff] [blame] | 656 | } |
| 657 | |
Andrey Rudenko | 5dc7aad | 2020-09-18 10:33:37 +0200 | [diff] [blame] | 658 | internal fun merge(lhs: Selection?, rhs: Selection?): Selection? { |
Siyamed Sinir | 700df845 | 2019-10-22 20:23:58 -0700 | [diff] [blame] | 659 | return lhs?.merge(rhs) ?: rhs |
| 660 | } |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 661 | |
Andrey Rudenko | 8b4981e | 2021-01-29 15:31:59 +0100 | [diff] [blame] | 662 | internal expect fun isCopyKeyEvent(keyEvent: KeyEvent): Boolean |
| 663 | |
Andrey Rudenko | 5dc7aad | 2020-09-18 10:33:37 +0200 | [diff] [blame] | 664 | internal fun getCurrentSelectedText( |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 665 | selectable: Selectable, |
| 666 | selection: Selection |
| 667 | ): AnnotatedString { |
| 668 | val currentText = selectable.getText() |
| 669 | |
| 670 | return if ( |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 671 | selectable.selectableId != selection.start.selectableId && |
| 672 | selectable.selectableId != selection.end.selectableId |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 673 | ) { |
| 674 | // Select the full text content if the current selectable is between the |
| 675 | // start and the end selectables. |
| 676 | currentText |
| 677 | } else if ( |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 678 | selectable.selectableId == selection.start.selectableId && |
| 679 | selectable.selectableId == selection.end.selectableId |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 680 | ) { |
| 681 | // Select partial text content if the current selectable is the start and |
| 682 | // the end selectable. |
| 683 | if (selection.handlesCrossed) { |
| 684 | currentText.subSequence(selection.end.offset, selection.start.offset) |
| 685 | } else { |
| 686 | currentText.subSequence(selection.start.offset, selection.end.offset) |
| 687 | } |
haoyu | e04245e | 2021-03-08 14:52:56 -0800 | [diff] [blame] | 688 | } else if (selectable.selectableId == selection.start.selectableId) { |
Qingqing Deng | 648e1bf | 2019-12-30 11:49:48 -0800 | [diff] [blame] | 689 | // Select partial text content if the current selectable is the start |
| 690 | // selectable. |
| 691 | if (selection.handlesCrossed) { |
| 692 | currentText.subSequence(0, selection.start.offset) |
| 693 | } else { |
| 694 | currentText.subSequence(selection.start.offset, currentText.length) |
| 695 | } |
| 696 | } else { |
| 697 | // Selectable partial text content if the current selectable is the end |
| 698 | // selectable. |
| 699 | if (selection.handlesCrossed) { |
| 700 | currentText.subSequence(selection.end.offset, currentText.length) |
| 701 | } else { |
| 702 | currentText.subSequence(0, selection.end.offset) |
| 703 | } |
| 704 | } |
| 705 | } |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 706 | |
| 707 | /** Returns the boundary of the visible area in this [LayoutCoordinates]. */ |
haoyu | ac341f0 | 2021-01-22 22:01:56 -0800 | [diff] [blame] | 708 | internal fun LayoutCoordinates.visibleBounds(): Rect { |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 709 | // globalBounds is the global boundaries of this LayoutCoordinates after it's clipped by |
| 710 | // parents. We can think it as the global visible bounds of this Layout. Here globalBounds |
| 711 | // 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] | 712 | val boundsInWindow = boundsInWindow() |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 713 | return Rect( |
George Mount | 77ca2a2 | 2020-12-11 17:46:19 +0000 | [diff] [blame] | 714 | windowToLocal(boundsInWindow.topLeft), |
| 715 | windowToLocal(boundsInWindow.bottomRight) |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 716 | ) |
| 717 | } |
| 718 | |
haoyu | ac341f0 | 2021-01-22 22:01:56 -0800 | [diff] [blame] | 719 | internal fun Rect.containsInclusive(offset: Offset): Boolean = |
haoyu | e2678c6 | 2020-12-09 08:39:12 -0800 | [diff] [blame] | 720 | offset.x in left..right && offset.y in top..bottom |