Is there in Compose Multiplatform common code to d...
# compose
e
Is there in Compose Multiplatform common code to detect if the keyboard is open or not? This is my current approach:
Copy code
// common
@Composable
expect fun isKeyboardVisible(): Boolean

// android
@Composable
actual fun isKeyboardVisible(): Boolean {
    var isKeyboardOpen by remember { mutableStateOf(false) }
    val view = LocalView.current

    val viewTreeObserver = view.viewTreeObserver

    DisposableEffect(viewTreeObserver) {
        val onGlobalListener = ViewTreeObserver.OnGlobalLayoutListener {
            val rect = android.graphics.Rect()
            view.getWindowVisibleDisplayFrame(rect)
            val screenHeight = view.rootView.height
            val keypadHeight = screenHeight - rect.bottom
            isKeyboardOpen = keypadHeight > screenHeight * 0.15
        }
        viewTreeObserver.addOnGlobalLayoutListener(onGlobalListener)

        onDispose {
            viewTreeObserver.removeOnGlobalLayoutListener(onGlobalListener)
        }
    }

    return isKeyboardOpen
}

// ios
@Composable
actual fun isKeyboardVisible(): Boolean = WindowInsets.Companion.ime.getBottom(LocalDensity.current) > 0
h
Currently what I'm doing is checking the imePadding's values 🙈 That's the only reliable way that I found at least
e
Can you do this from common?
h
Yes I'm talking about common
e
Can you share your code? 🙂
h
I'm not in front of a computer now but essentially val bottomImePadding = WindowInsets.ime.asPaddingValues().calculateBottomPadding()
e
I'll give it a try thank you!
Would be awesome to have this build in in compose, but its not even in compose android
h
yeah, i know, i always check if the dp’s value is bigger than 0 and it works, just keep in mind that in between there are values as the keyboard is being animated
z
Why would compose android include apis for dealing with the IME? The IME is a system thing, there’s nothing Compose-specific about it.
e
Not compose per se, but it seems to create some frustration (e.g. see comments here https://stackoverflow.com/a/7530328) in the Android community that there is no "simple" solution to detect if the keyboard is open. Regardless this could also be a cool Compose Multiplatform Community Library.