diff --git a/src/main/kotlin/nya/kitsunyan/foxydroid/screen/TabsFragment.kt b/src/main/kotlin/nya/kitsunyan/foxydroid/screen/TabsFragment.kt index 66013aa..351f98a 100644 --- a/src/main/kotlin/nya/kitsunyan/foxydroid/screen/TabsFragment.kt +++ b/src/main/kotlin/nya/kitsunyan/foxydroid/screen/TabsFragment.kt @@ -42,10 +42,12 @@ import nya.kitsunyan.foxydroid.utility.extension.android.* import nya.kitsunyan.foxydroid.utility.extension.resources.* import nya.kitsunyan.foxydroid.utility.extension.text.* import nya.kitsunyan.foxydroid.widget.EnumRecyclerAdapter +import nya.kitsunyan.foxydroid.widget.FocusSearchView import kotlin.math.* class TabsFragment: ScreenFragment() { companion object { + private const val STATE_SEARCH_FOCUSED = "searchFocused" private const val STATE_SEARCH_QUERY = "searchQuery" private const val STATE_SHOW_CATEGORIES = "showCategories" private const val STATE_CATEGORIES = "categories" @@ -115,8 +117,11 @@ class TabsFragment: ScreenFragment() { val toolbar = view.findViewById(R.id.toolbar)!! screenActivity.onToolbarCreated(toolbar) toolbar.setTitle(R.string.app_name) + // Move focus from SearchView to Toolbar + toolbar.isFocusableInTouchMode = true - val searchView = SearchView(toolbar.context) + val searchView = FocusSearchView(toolbar.context) + searchView.allowFocus = savedInstanceState?.getBoolean(STATE_SEARCH_FOCUSED) == true searchView.maxWidth = Int.MAX_VALUE searchView.setOnQueryTextListener(object: SearchView.OnQueryTextListener { override fun onQueryTextSubmit(query: String?): Boolean { @@ -307,6 +312,7 @@ class TabsFragment: ScreenFragment() { override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) + outState.putBoolean(STATE_SEARCH_FOCUSED, searchMenuItem?.actionView!!.hasFocus()) outState.putString(STATE_SEARCH_QUERY, searchQuery) outState.putByte(STATE_SHOW_CATEGORIES, if (showCategories) 1 else 0) outState.putStringArrayList(STATE_CATEGORIES, ArrayList(categories)) @@ -317,6 +323,7 @@ class TabsFragment: ScreenFragment() { override fun onViewStateRestored(savedInstanceState: Bundle?) { super.onViewStateRestored(savedInstanceState) + (searchMenuItem?.actionView as FocusSearchView).allowFocus = true if (needSelectUpdates) { needSelectUpdates = false selectUpdatesInternal(false) diff --git a/src/main/kotlin/nya/kitsunyan/foxydroid/widget/FocusSearchView.kt b/src/main/kotlin/nya/kitsunyan/foxydroid/widget/FocusSearchView.kt new file mode 100644 index 0000000..a71ae0e --- /dev/null +++ b/src/main/kotlin/nya/kitsunyan/foxydroid/widget/FocusSearchView.kt @@ -0,0 +1,35 @@ +package nya.kitsunyan.foxydroid.widget + +import android.content.Context +import android.util.AttributeSet +import android.view.KeyEvent +import android.widget.SearchView + +class FocusSearchView: SearchView { + constructor(context: Context): super(context) + constructor(context: Context, attrs: AttributeSet?): super(context, attrs) + constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int): super(context, attrs, defStyleAttr) + + var allowFocus = true + + override fun dispatchKeyEventPreIme(event: KeyEvent): Boolean { + // Always clear focus on back press + return if (hasFocus() && event.keyCode == KeyEvent.KEYCODE_BACK) { + if (event.action == KeyEvent.ACTION_UP) { + clearFocus() + } + true + } else { + super.dispatchKeyEventPreIme(event) + } + } + + override fun setIconified(iconify: Boolean) { + super.setIconified(iconify) + + // Don't focus view and raise keyboard unless allowed + if (!iconify && !allowFocus) { + clearFocus() + } + } +}