Status Update
Comments
as...@google.com <as...@google.com>
jo...@google.com <jo...@google.com> #2
This is probably closely related to
sh...@google.com <sh...@google.com> #3
I tried wrapping the text in a Box with verticalScroll Modifier, and then using the results from onTextLayout in addition to current selection (through TextFieldValue) to determine current line offset and scroll to it on every change (tracked using onSizeChanged), but I guess I messed up something in there, it didn't work very well.
(btw, onTextLayout returns Float values for offsets, while the scroll state's scrollBy() function accepts Integers. I only round these Floats to Integers, could that be the problem?)
Anyways I honestly got tired of trying to hack around it. I'd love to know if there's any workaround for this in the moment, and I hope you fix this soon.
jo...@google.com <jo...@google.com> #4
It's kind of a crazy convoluted workaround, but this seems to work very well for me:
@Composable
fun AppScreen() {
val coroutineScope = rememberCoroutineScope()
var value by remember { mutableStateOf(TextFieldValue()) }
val scrollState = rememberScrollState()
var height by remember { mutableStateOf(0) }
var layoutResult: TextLayoutResult? by remember { mutableStateOf(null) }
BasicTextField(
value = value,
onValueChange = { value = it },
decorationBox = {
Box(modifier = Modifier.verticalScroll(scrollState)) {
it()
}
},
onTextLayout = { layoutResult = it },
textStyle = LocalTextStyle.current.copy(color = MaterialTheme.colors.onBackground),
cursorBrush = SolidColor(MaterialTheme.colors.secondary),
modifier = Modifier
.fillMaxSize()
.onSizeChanged {
coroutineScope.launch {
val cursorInView = value.isCursorInView(
layoutResult = layoutResult!!,
height = it.height.toFloat(),
scrollValue = scrollState.value.toFloat()
)
if (!cursorInView && height > it.height) {
scrollState.scrollBy(
value.calculateRequiredSizeScroll(
layoutResult = layoutResult!!,
oldHeight = height.toFloat(),
newHeight = it.height.toFloat(),
scrollValue = scrollState.value.toFloat()
)
)
}
height = it.height
}
}
)
LaunchedEffect(value.selection) {
val cursorInView = value.isCursorInView(
layoutResult = layoutResult!!,
height = height.toFloat(),
scrollValue = scrollState.value.toFloat()
)
if (!cursorInView) {
scrollState.scrollBy(
value.calculateRequiredSelectionScroll(
layoutResult = layoutResult!!,
height = height.toFloat(),
scrollValue = scrollState.value.toFloat()
)
)
}
}
}
fun TextFieldValue.isCursorInView(
layoutResult: TextLayoutResult,
height: Float,
scrollValue: Float
) = with(layoutResult) {
val currentLine = try {
getLineForOffset(selection.min)
} catch (ex: IllegalArgumentException) {
System.err.println("Corrected Wrong Offset!")
getLineForOffset(selection.min - 1)
}
val lineBottom = getLineBottom(currentLine)
val lineTop = getLineTop(currentLine)
lineBottom <= height + scrollValue && lineTop >= scrollValue
}
fun TextFieldValue.calculateRequiredSelectionScroll(
layoutResult: TextLayoutResult,
height: Float,
scrollValue: Float
) = with(layoutResult) {
val currentLine = try {
getLineForOffset(selection.min)
} catch (ex: IllegalArgumentException) {
System.err.println("Corrected Wrong Offset!")
getLineForOffset(selection.min - 1)
}
val lineTop = getLineTop(currentLine)
val lineBottom = getLineBottom(currentLine)
if (lineTop < scrollValue) -(scrollValue - lineTop)
else if (lineBottom > height + scrollValue) lineBottom - (height + scrollValue)
else 0f
}
fun TextFieldValue.calculateRequiredSizeScroll(
layoutResult: TextLayoutResult,
oldHeight: Float,
newHeight: Float,
scrollValue: Float
) = with(layoutResult) {
val currentLine = try {
getLineForOffset(selection.min)
} catch (ex: IllegalArgumentException) {
System.err.println("Corrected Wrong Offset!")
getLineForOffset(selection.min - 1)
}
val sizeDifference = oldHeight - newHeight
val lineBottom = getLineBottom(currentLine)
if (lineBottom in (newHeight + scrollValue)..(oldHeight + scrollValue))
sizeDifference - (oldHeight - (lineBottom - scrollValue))
else 0f
}
I basically wrapped the inner text field Composable in a Box with the verticalScroll modifier, and then using these parameters: TextLayoutResult (to get the current line position), scroll value, and height; all to determine if the cursor is currently visible on the screen, and if it's not, calculate the minimal amount of scroll needed to get it back on the screen, both when the TextField size changes (which is the goal), and also when navigating around the text or editing it (this works fine in Compose by default, but using a scroll modifier breaks it, so I have to manually scroll to follow the cursor).
It works perfectly well for me, with a small annoyance being that sometimes (when typing really fast) the selection I get from the TextFieldValue is higher than what the TextLayoutResult expects, causing a crash. I worked around this by wrapping it in a try/catch, and decrementing the selection value by 1 to get it working. Pretty hacky and lazy, but it works flawlessly now, and those catched exceptions don't seem to affect performance in any noticeable way.
Yet having something like this implemented properly would still be far better than these hacky and convoluted workarounds.
jo...@google.com <jo...@google.com>
ap...@google.com <ap...@google.com> #5
Branch: androidx-main
commit d31135ea17e9ba4a5e117ebbac5e45b924a1f0a3
Author: Zach Klippenstein <klippenstein@google.com>
Date: Wed Jul 06 11:05:41 2022
Add demo for full-screen text field and factor out lorem ipsum generator.
Bug:
Test: n/a, demo change only
Change-Id: I6fbe6fdd71d7debd38384228a66bd5ef77eedf25
M compose/foundation/foundation/integration-tests/foundation-demos/src/main/java/androidx/compose/foundation/demos/text/TextFieldWIthScroller.kt
M compose/foundation/foundation/integration-tests/foundation-demos/src/main/java/androidx/compose/foundation/demos/text/ComposeMultiParagraph.kt
A compose/foundation/foundation/integration-tests/foundation-demos/src/main/java/androidx/compose/foundation/demos/text/LoremIpsum.kt
M compose/foundation/foundation/integration-tests/foundation-demos/src/main/java/androidx/compose/foundation/demos/text/BrushDemo.kt
M compose/foundation/foundation/integration-tests/foundation-demos/src/main/java/androidx/compose/foundation/demos/text/TextDemos.kt
M compose/foundation/foundation/integration-tests/foundation-demos/src/main/java/androidx/compose/foundation/demos/text/DrawTextDemo.kt
A compose/foundation/foundation/integration-tests/foundation-demos/src/main/java/androidx/compose/foundation/demos/text/FullScreenTextFieldDemo.kt
ap...@google.com <ap...@google.com> #6
Branch: androidx-main
commit 7e77bb541331ce0d62cabad732e43eca41ac978a
Author: Zach Klippenstein <klippenstein@google.com>
Date: Wed Jul 06 11:42:21 2022
Move BringIntoView queueing into the scrollable implementation.
This change removes the over-complicated coroutine-based queuing of
bringIntoView requests in the BringIntoViewResponder, which now is only
responsible for dispatching the request to the actual responder
interface and propagating it up the modifier local chain.
This gives the actual responder implementation (i.e. in
Modifier.scrollable) all the information about conflicting
requests and manage the queue of ongoing requests with an actual,
explicit queue that is much more straightforward than the previous
coroutine job chaining mess. Each request is stored in an actual list
along with the continuation from the request, and requests are
explicitly resumed or cancelled when needed. This also makes debugging
easier, because there's an actual queue data structure we can inspect,
which contains all the actual information about each request.
It also completely changes how the scroll animation is performed.
On every frame, the scroll target is recomputed to account for
changes in the viewport size, the request size, or the set of active
requests since the last frame, while still keeping the animation
smooth. The animation logic is in its own class, which allows a single
suspend animation call to animate to a target value that can be updated
while the animation is running.
While this change is a valuable cleanup on its own, it also fixes
focused child tracking with an explicit API for keeping a thing in view.
Fixes:
Bug:
Bug:
Bug:
Bug:
Test: ScrollableFocusableInteractionTest
Test: androidx.compose.foundation.relocation.*
Test: RebasableAnimationStateTest
Test: BringIntoViewRequestPriorityQueueTest
Relnote: "Rewrote the way scrollables respond to
`bringIntoViewRequesters` and focusables to better model the
complexity of those operations and handle more edge cases."
Change-Id: I2e5fec8c8582a8fe1f191e37fd0f4f9165678664
M compose/foundation/foundation/api/current.txt
M compose/foundation/foundation/api/public_plus_experimental_current.txt
M compose/foundation/foundation/api/restricted_current.txt
M compose/foundation/foundation/integration-tests/foundation-demos/src/main/java/androidx/compose/foundation/demos/FoundationDemos.kt
M compose/foundation/foundation/integration-tests/foundation-demos/src/main/java/androidx/compose/foundation/demos/ScrollableFocusedChildDemo.kt
A compose/foundation/foundation/integration-tests/foundation-demos/src/main/java/androidx/compose/foundation/demos/relocation/BringIntoViewResponderDemo.kt
M compose/foundation/foundation/integration-tests/foundation-demos/src/main/java/androidx/compose/foundation/demos/relocation/BringRectangleIntoViewDemo.kt
M compose/foundation/foundation/samples/src/main/java/androidx/compose/foundation/samples/BringIntoViewSamples.kt
M compose/foundation/foundation/src/androidAndroidTest/kotlin/androidx/compose/foundation/ScrollableFocusableInteractionTest.kt
M compose/foundation/foundation/src/androidAndroidTest/kotlin/androidx/compose/foundation/lazy/list/LazyListFocusMoveTest.kt
M compose/foundation/foundation/src/androidAndroidTest/kotlin/androidx/compose/foundation/relocation/BringIntoViewResponderTest.kt
M compose/foundation/foundation/src/androidAndroidTest/kotlin/androidx/compose/foundation/relocation/BringIntoViewScrollableInteractionTest.kt
A compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/gestures/BringIntoViewRequestPriorityQueue.kt
M compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/gestures/ContentInViewModifier.kt
A compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/gestures/UpdatableAnimationState.kt
M compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/relocation/BringIntoViewResponder.kt
A compose/foundation/foundation/src/test/kotlin/androidx/compose/foundation/gestures/BringIntoViewRequestPriorityQueueTest.kt
A compose/foundation/foundation/src/test/kotlin/androidx/compose/foundation/gestures/UpdatableAnimationStateTest.kt
Description
Jetpack Compose version: 1.6.0-beta03 Jetpack Compose component used: AnchoredDraggable
Steps to Reproduce:
{ it * 0.8f }
and space the anchors evenly, or set it to{ it * 0.5f }
and space the first two anchors relatively closeIf this is intended, I personally find it unintuitive and there should at least be an option to change the behavior.