Skip to content

Commit b7cd27a

Browse files
committed
fix(android): search crashes
1 parent 56593a0 commit b7cd27a

1 file changed

Lines changed: 31 additions & 5 deletions

File tree

android/app/src/main/java/com/logseq/app/LiquidTabsPlugin.kt

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import android.view.Gravity
77
import android.view.KeyEvent
88
import android.view.View
99
import android.view.ViewGroup
10+
import android.view.inputmethod.EditorInfo
1011
import android.widget.EditText
1112
import android.widget.FrameLayout
1213
import android.widget.LinearLayout
@@ -276,6 +277,7 @@ class LiquidTabsPlugin : Plugin() {
276277
val input = EditText(activity).apply {
277278
hint = "Search"
278279
setSingleLine(true)
280+
imeOptions = EditorInfo.IME_ACTION_SEARCH
279281
setTextColor(labelColor)
280282
setHintTextColor(secondaryLabelColor)
281283
// Remove EditText default background/border for a flat look
@@ -292,14 +294,38 @@ class LiquidTabsPlugin : Plugin() {
292294
// Layout params to make EditText take most of the horizontal space
293295
layoutParams = LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f)
294296

297+
setOnEditorActionListener { _, actionId, event ->
298+
val isEnter =
299+
actionId == EditorInfo.IME_ACTION_SEARCH ||
300+
actionId == EditorInfo.IME_ACTION_DONE ||
301+
actionId == EditorInfo.IME_ACTION_GO ||
302+
(event?.keyCode == KeyEvent.KEYCODE_ENTER)
303+
if (isEnter) {
304+
notifyListeners("keyboardHackKey", JSObject().put("key", "enter"))
305+
true
306+
} else {
307+
false
308+
}
309+
}
310+
295311
setOnKeyListener { _, keyCode, event ->
296-
if (event.action == KeyEvent.ACTION_DOWN) {
297-
when (keyCode) {
298-
KeyEvent.KEYCODE_DEL -> notifyListeners("keyboardHackKey", JSObject().put("key", "backspace"))
299-
KeyEvent.KEYCODE_ENTER -> notifyListeners("keyboardHackKey", JSObject().put("key", "enter"))
312+
when (keyCode) {
313+
KeyEvent.KEYCODE_ENTER -> {
314+
if (event.action == KeyEvent.ACTION_DOWN) {
315+
notifyListeners("keyboardHackKey", JSObject().put("key", "enter"))
316+
}
317+
true
318+
}
319+
KeyEvent.KEYCODE_DEL -> {
320+
if (text.isNullOrEmpty() && event.action == KeyEvent.ACTION_DOWN) {
321+
notifyListeners("keyboardHackKey", JSObject().put("key", "backspace"))
322+
true
323+
} else {
324+
false
325+
}
300326
}
327+
else -> false
301328
}
302-
false
303329
}
304330
addTextChangedListener(object : TextWatcher {
305331
override fun afterTextChanged(s: Editable?) {

0 commit comments

Comments
 (0)