-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
701f5e4
commit 2cc02d0
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
...in/kotlin/ir/thatsmejavad/backgroundable/common/ui/ClearFocusOnKeyboardDismissModifier.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package ir.thatsmejavad.backgroundable.common.ui | ||
|
||
import android.graphics.Rect | ||
import android.view.View | ||
import android.view.ViewTreeObserver | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.State | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.produceState | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.composed | ||
import androidx.compose.ui.focus.onFocusEvent | ||
import androidx.compose.ui.platform.LocalFocusManager | ||
import androidx.compose.ui.platform.LocalView | ||
|
||
@Composable | ||
fun Modifier.clearFocusOnKeyboardDismiss(): Modifier = composed { | ||
var isFocused by remember { mutableStateOf(false) } | ||
var keyboardAppearedSinceLastFocused by remember { mutableStateOf(false) } | ||
|
||
if (isFocused) { | ||
val isKeyboardOpen by rememberIsKeyboardOpen() | ||
|
||
val focusManager = LocalFocusManager.current | ||
LaunchedEffect(isKeyboardOpen) { | ||
if (isKeyboardOpen) { | ||
keyboardAppearedSinceLastFocused = true | ||
} else if (keyboardAppearedSinceLastFocused) { | ||
focusManager.clearFocus() | ||
} | ||
} | ||
} | ||
onFocusEvent { | ||
if (isFocused != it.isFocused) { | ||
isFocused = it.isFocused | ||
if (isFocused) { | ||
keyboardAppearedSinceLastFocused = false | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun rememberIsKeyboardOpen(): State<Boolean> { | ||
val view = LocalView.current | ||
|
||
return produceState(initialValue = view.isKeyboardOpen()) { | ||
val viewTreeObserver = view.viewTreeObserver | ||
val listener = ViewTreeObserver.OnGlobalLayoutListener { value = view.isKeyboardOpen() } | ||
viewTreeObserver.addOnGlobalLayoutListener(listener) | ||
|
||
awaitDispose { viewTreeObserver.removeOnGlobalLayoutListener(listener) } | ||
} | ||
} | ||
|
||
fun View.isKeyboardOpen(): Boolean { | ||
val rect = Rect() | ||
getWindowVisibleDisplayFrame(rect) | ||
val screenHeight = rootView.height | ||
val keypadHeight = screenHeight - rect.bottom | ||
return keypadHeight > screenHeight * 0.15 | ||
} |
This file contains 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