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