From c88a9dff3632be9b002163a1aeae14197b3dfab4 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Fri, 8 Sep 2023 09:17:04 +0300 Subject: [PATCH] Handle enter press in search view --- .../kotatsu/search/ui/widget/SearchEditText.kt | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/search/ui/widget/SearchEditText.kt b/app/src/main/kotlin/org/koitharu/kotatsu/search/ui/widget/SearchEditText.kt index 8b8feff81..e4722e717 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/search/ui/widget/SearchEditText.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/search/ui/widget/SearchEditText.kt @@ -32,7 +32,8 @@ class SearchEditText @JvmOverloads constructor( ) : AppCompatEditText(context, attrs, defStyleAttr) { var searchSuggestionListener: SearchSuggestionListener? = null - private val clearIcon = ContextCompat.getDrawable(context, materialR.drawable.abc_ic_clear_material) + private val clearIcon = + ContextCompat.getDrawable(context, materialR.drawable.abc_ic_clear_material) private var isEmpty = text.isNullOrEmpty() init { @@ -52,12 +53,21 @@ class SearchEditText @JvmOverloads constructor( if (keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_UP) { if (hasFocus()) { clearFocus() - // return true } } return super.onKeyPreIme(keyCode, event) } + override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean { + if (keyCode == KeyEvent.KEYCODE_ENTER && event.hasNoModifiers() && query.isNotEmpty()) { + cancelLongPress() + searchSuggestionListener?.onQueryClick(query, submit = true) + clearFocus() + return true + } + return super.onKeyUp(keyCode, event) + } + override fun onEditorAction(actionCode: Int) { super.onEditorAction(actionCode) if (actionCode == EditorInfo.IME_ACTION_SEARCH) { @@ -88,7 +98,8 @@ class SearchEditText @JvmOverloads constructor( @SuppressLint("ClickableViewAccessibility") override fun onTouchEvent(event: MotionEvent): Boolean { if (event.action == MotionEvent.ACTION_UP) { - val drawable = compoundDrawablesRelative[DRAWABLE_END] ?: return super.onTouchEvent(event) + val drawable = + compoundDrawablesRelative[DRAWABLE_END] ?: return super.onTouchEvent(event) val isOnDrawable = drawable.isVisible && if (layoutDirection == LAYOUT_DIRECTION_RTL) { event.x.toInt() in paddingLeft..(drawable.bounds.width() + paddingLeft) } else {