From b7cd27a92a60495392adfdba16a754ec13e7a6ad Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Sat, 13 Dec 2025 00:59:55 +0800 Subject: [PATCH] fix(android): search crashes --- .../java/com/logseq/app/LiquidTabsPlugin.kt | 40 +++++++++++++++---- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/android/app/src/main/java/com/logseq/app/LiquidTabsPlugin.kt b/android/app/src/main/java/com/logseq/app/LiquidTabsPlugin.kt index 48900cd57e..cfbb420aab 100644 --- a/android/app/src/main/java/com/logseq/app/LiquidTabsPlugin.kt +++ b/android/app/src/main/java/com/logseq/app/LiquidTabsPlugin.kt @@ -7,6 +7,7 @@ import android.view.Gravity import android.view.KeyEvent import android.view.View import android.view.ViewGroup +import android.view.inputmethod.EditorInfo import android.widget.EditText import android.widget.FrameLayout import android.widget.LinearLayout @@ -276,6 +277,7 @@ class LiquidTabsPlugin : Plugin() { val input = EditText(activity).apply { hint = "Search" setSingleLine(true) + imeOptions = EditorInfo.IME_ACTION_SEARCH setTextColor(labelColor) setHintTextColor(secondaryLabelColor) // Remove EditText default background/border for a flat look @@ -292,14 +294,38 @@ class LiquidTabsPlugin : Plugin() { // Layout params to make EditText take most of the horizontal space layoutParams = LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f) - setOnKeyListener { _, keyCode, event -> - if (event.action == KeyEvent.ACTION_DOWN) { - when (keyCode) { - KeyEvent.KEYCODE_DEL -> notifyListeners("keyboardHackKey", JSObject().put("key", "backspace")) - KeyEvent.KEYCODE_ENTER -> notifyListeners("keyboardHackKey", JSObject().put("key", "enter")) - } + setOnEditorActionListener { _, actionId, event -> + val isEnter = + actionId == EditorInfo.IME_ACTION_SEARCH || + actionId == EditorInfo.IME_ACTION_DONE || + actionId == EditorInfo.IME_ACTION_GO || + (event?.keyCode == KeyEvent.KEYCODE_ENTER) + if (isEnter) { + notifyListeners("keyboardHackKey", JSObject().put("key", "enter")) + true + } else { + false + } + } + + setOnKeyListener { _, keyCode, event -> + when (keyCode) { + KeyEvent.KEYCODE_ENTER -> { + if (event.action == KeyEvent.ACTION_DOWN) { + notifyListeners("keyboardHackKey", JSObject().put("key", "enter")) + } + true + } + KeyEvent.KEYCODE_DEL -> { + if (text.isNullOrEmpty() && event.action == KeyEvent.ACTION_DOWN) { + notifyListeners("keyboardHackKey", JSObject().put("key", "backspace")) + true + } else { + false + } + } + else -> false } - false } addTextChangedListener(object : TextWatcher { override fun afterTextChanged(s: Editable?) {