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