Created
May 9, 2023 17:50
-
-
Save MaxMyalkin/b63f4d4050e4ff882cf3a30abcab448f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun keyboardAsState(): State<KeyboardState> { | |
val keyboardState = remember { mutableStateOf(KeyboardState.Closed) } | |
val view = LocalView.current | |
val viewTreeObserver = view.viewTreeObserver | |
DisposableEffect(viewTreeObserver) { | |
val onGlobalListener = ViewTreeObserver.OnGlobalLayoutListener { | |
val rect = Rect() | |
view.getWindowVisibleDisplayFrame(rect) | |
val screenHeight = view.rootView.height | |
val keypadHeight = screenHeight - rect.bottom | |
keyboardState.value = if (keypadHeight > screenHeight * 0.15) { | |
KeyboardState.Opened | |
} else { | |
KeyboardState.Closed | |
} | |
} | |
viewTreeObserver.addOnGlobalLayoutListener(onGlobalListener) | |
onDispose { | |
viewTreeObserver.removeOnGlobalLayoutListener(onGlobalListener) | |
} | |
} | |
return keyboardState | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment