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