[Rich text editor] Ensure keyboard opens for reply and text formatting modes (#1337)
This commit is contained in:
@@ -17,8 +17,11 @@
|
||||
package io.element.android.libraries.androidutils.ui
|
||||
|
||||
import android.view.View
|
||||
import android.view.ViewTreeObserver
|
||||
import android.view.inputmethod.InputMethodManager
|
||||
import androidx.core.content.getSystemService
|
||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||
import kotlin.coroutines.resume
|
||||
|
||||
fun View.hideKeyboard() {
|
||||
val imm = context?.getSystemService<InputMethodManager>()
|
||||
@@ -41,3 +44,24 @@ fun View.setHorizontalPadding(padding: Int) {
|
||||
paddingBottom
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun View.awaitWindowFocus() = suspendCancellableCoroutine { continuation ->
|
||||
if (hasWindowFocus()) {
|
||||
continuation.resume(Unit)
|
||||
} else {
|
||||
val listener = object : ViewTreeObserver.OnWindowFocusChangeListener {
|
||||
override fun onWindowFocusChanged(hasFocus: Boolean) {
|
||||
if (hasFocus) {
|
||||
viewTreeObserver.removeOnWindowFocusChangeListener(this)
|
||||
continuation.resume(Unit)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
viewTreeObserver.addOnWindowFocusChangeListener(listener)
|
||||
|
||||
continuation.invokeOnCancellation {
|
||||
viewTreeObserver.removeOnWindowFocusChangeListener(listener)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user