question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scheduler breaks keyboard behavior for Compose UI consumers

See original GitHub issue

Compose UI’s CoreTextField is very picky about getting immediate updates to its values. The common (but useless) example seen in the docs, where the text state is held in a by remember { mutableStateOf("") }, has no problems updating the text field when typing on the keyboard quickly. However, if you provide the text value from something that doesn’t provide updates synchronously from onValueChange, the text field starts behaving erratically; characters randomly disappear, the cursor moves around in its own, and other weird stuff.

This issue made us realize that mapping UI state on a background thread will never work. So we’ve been running our presenter composition on the main thread, using an immediate monotonic frame clock:

private object ImmediateMonotonicFrameClock : MonotonicFrameClock {
  override suspend fun <R> withFrameNanos(onFrame: (frameTimeNanos: Long) -> R): R {
    return onFrame(System.nanoTime())
  }
}

This completely solved the keyboard glitchiness.

We’re now looking to migrate to Molecule. But in doing so, we’ve seen the keyboard glitchiness return! I somewhat narrowed it down to behaviors that depend on the combination of the Molecule’s Dispatcher and MonotonicFrameClock:

Dispatcher MonotonicFrameClock Behavior
AndroidUiDispatcher Choreographer Bad
AndroidUiDispatcher ImmediateMonotonicFrameClock Less bad (but still bad)
Dispatchers.Main Choreographer Bad
Dispatchers.Main ImmediateMonotonicFrameClock Good

More specifics on the glitchiness:

Try spamming a character quickly. Occasionally, some will be dropped. When that happens, this appears in logcat:

getSurroundingText on inactive InputConnection
beginBatchEdit on inactive InputConnection
getTextBeforeCursor on inactive InputConnection
getTextAfterCursor on inactive InputConnection
getSelectedText on inactive InputConnection
endBatchEdit on inactive InputConnection

An easy way to check if the issue is happening is to hold the backspace button when there’s some text present. With the issue, the backspace will randomly “stop working”; characters will stop being deleted even though you’re still pressing backspace.

Honestly, I’m not positive as to whether this is Molecule’s or CoreTextField’s fault. Input appreciated.

Repro project with those dispatcher/frameclock modes

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
chris-hornercommented, Aug 1, 2022

I put together a sample that allows for easy demoing of the issue and switching between the two clock types. RecompositionClock.Immediate does seem to fix the weird behaviour of input fields.

https://user-images.githubusercontent.com/1245751/182053296-14df5da3-e45b-4747-aa10-fd49e74f3a54.mp4

3reactions
JakeWhartoncommented, Apr 9, 2022

I have no had a chance to look, no. I can try to look when I’m back working on this library as part of the Kotlin 1.6.20 upgrade to Compose.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Compose UI - Android Developers
Improved behavior with physical keyboard or TV remote. New APIs for: Window insets; Core primitives for gesture-driven, infinite and layout animations ...
Read more >
Keyboard handling in Jetpack Compose - DEV Community ‍ ‍
This article takes a closer look at how Jetpack Compose apps interact with the keyboard. Let's start with a simple Compose hierarchy:.
Read more >
360388 - Ignores all keyboard input - chromium - Monorail
I'm using a french keyboard on a lubuntu 14.04 freshly installed on my eepc 1005 ha. The trick explained in post #49 work...
Read more >
Animations and Effects broken on Keyboard written in Jetpack ...
I downloaded your code and saw your comment in the Compose's bug tracker. Looks like that if you do the following in your...
Read more >
Tokyo Patch 3 - ServiceNow Docs
This would be a performance improvement, not a functional behavior change. UI Builder. PRB1592426. Users are unable to save a page due to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found