FloatingSearchBar
This commit is contained in:
67
app/src/main/java/xyz/quaver/pupil/util/KeyboardManager.kt
Normal file
67
app/src/main/java/xyz/quaver/pupil/util/KeyboardManager.kt
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Pupil, Hitomi.la viewer for Android
|
||||
* Copyright (C) 2021 tom5079
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package xyz.quaver.pupil.util
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.graphics.Rect
|
||||
import android.view.View
|
||||
import android.view.ViewTreeObserver
|
||||
|
||||
//https://stackoverflow.com/questions/68389802/how-to-clear-textfield-focus-when-closing-the-keyboard-and-prevent-two-back-pres
|
||||
class KeyboardManager(context: Context) {
|
||||
private val activity = context as Activity
|
||||
private var keyboardDismissListener: KeyboardDismissListener? = null
|
||||
|
||||
private abstract class KeyboardDismissListener(
|
||||
private val rootView: View,
|
||||
private val onKeyboardDismiss: () -> Unit
|
||||
) : ViewTreeObserver.OnGlobalLayoutListener {
|
||||
private var isKeyboardClosed: Boolean = false
|
||||
override fun onGlobalLayout() {
|
||||
val r = Rect()
|
||||
rootView.getWindowVisibleDisplayFrame(r)
|
||||
val screenHeight = rootView.rootView.height
|
||||
val keypadHeight = screenHeight - r.bottom
|
||||
if (keypadHeight > screenHeight * 0.15) {
|
||||
// 0.15 ratio is right enough to determine keypad height.
|
||||
isKeyboardClosed = false
|
||||
} else if (!isKeyboardClosed) {
|
||||
isKeyboardClosed = true
|
||||
onKeyboardDismiss.invoke()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun attachKeyboardDismissListener(onKeyboardDismiss: () -> Unit) {
|
||||
val rootView = activity.findViewById<View>(android.R.id.content)
|
||||
keyboardDismissListener = object : KeyboardDismissListener(rootView, onKeyboardDismiss) {}
|
||||
keyboardDismissListener?.let {
|
||||
rootView.viewTreeObserver.addOnGlobalLayoutListener(it)
|
||||
}
|
||||
}
|
||||
|
||||
fun release() {
|
||||
val rootView = activity.findViewById<View>(android.R.id.content)
|
||||
keyboardDismissListener?.let {
|
||||
rootView.viewTreeObserver.removeOnGlobalLayoutListener(it)
|
||||
}
|
||||
keyboardDismissListener = null
|
||||
}
|
||||
}
|
||||
@@ -25,14 +25,18 @@ import android.graphics.BitmapFactory
|
||||
import android.graphics.BitmapRegionDecoder
|
||||
import android.view.MenuItem
|
||||
import android.view.View
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.composed
|
||||
import androidx.compose.ui.focus.onFocusEvent
|
||||
import androidx.compose.ui.geometry.Rect
|
||||
import androidx.compose.ui.geometry.Size
|
||||
import androidx.compose.ui.graphics.ImageBitmap
|
||||
import androidx.compose.ui.graphics.asImageBitmap
|
||||
import androidx.compose.ui.graphics.toAndroidRect
|
||||
import androidx.compose.ui.platform.LocalFocusManager
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import com.google.accompanist.insets.LocalWindowInsets
|
||||
import kotlinx.serialization.json.*
|
||||
import org.kodein.di.DIAware
|
||||
import org.kodein.di.DirectDIAware
|
||||
|
||||
Reference in New Issue
Block a user