-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add WebViewSearchBar for your web view searching needs
Just a basic widget for replacing the broken implementation in the WebView itself. As a bonus it's themed to match the toolbar now! I've added the counter that shows how many results you have/which one is currently highlighted, and tried to style it like Chrome does it (on the desktop anyway) - so basically the count area can expand and push back on the typey bit, but the longer the part you're typing the less likely you are to get a lot of results, so it shouldn't be an issue Also when I came to use the carets for the prev/next buttons, I noticed the down one was bigger - different viewport settings for whatever reason, and I couldn't change those on the up caret without getting a weird offset. So I've switched out references to that big caret for the smaller one, and adjusted the scaling on those image views to compensate That's the subforum expander and the down button to show the original PM message - the former should look about the same, I can't expand the PM button image without the button growing with it, so I left it but it looks fine. I've also deleted an XML thing that wasn't being used (pretty sure it was abandoned in favour of the spinny expander arrows)
- Loading branch information
Showing
10 changed files
with
180 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
Awful.apk/src/main/java/com/ferg/awfulapp/widget/WebViewSearchBar.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package com.ferg.awfulapp.widget | ||
|
||
import android.content.Context | ||
import android.support.v7.view.CollapsibleActionView | ||
import android.text.Editable | ||
import android.text.TextWatcher | ||
import android.util.AttributeSet | ||
import android.view.LayoutInflater | ||
import android.view.inputmethod.InputMethodManager | ||
import android.webkit.WebView | ||
import android.widget.EditText | ||
import android.widget.ImageButton | ||
import android.widget.LinearLayout | ||
import android.widget.TextView | ||
import com.ferg.awfulapp.R | ||
import com.ferg.awfulapp.util.bind | ||
import kotlin.properties.Delegates | ||
|
||
|
||
/** | ||
* Created by baka kaba on 24/02/2020. | ||
* | ||
* A simple widget for searching a provided [webView]. | ||
* | ||
* Replaces the in-built find toolbar in the Webview class which is fully broken at this point. | ||
* Set [webView] and text entered into the search bar will be forwarded to its search system. This | ||
* implements [CollapsibleActionView] so it can be used as an expanding menu item that appears in | ||
* the toolbar, and cleans up after itself once closed. | ||
* | ||
* Implemented as a LinearLayout because ConstraintLayout wasn't playing nice with the textbox filling | ||
* the available area. | ||
*/ | ||
|
||
class WebViewSearchBar @JvmOverloads constructor( | ||
context: Context, | ||
attrs: AttributeSet? = null, | ||
defStyleAttr: Int = 0 | ||
) : LinearLayout(context, attrs, defStyleAttr), WebView.FindListener, CollapsibleActionView { | ||
|
||
private val nextButton: ImageButton by bind(R.id.web_view_search_next_result) | ||
private val prevButton: ImageButton by bind(R.id.web_view_search_prev_result) | ||
private val searchBox: EditText by bind(R.id.web_view_search_text) | ||
private val resultCount: TextView by bind(R.id.webview_search_result_count) | ||
|
||
/** The webview this widget will attempt to search in */ | ||
var webView by Delegates.observable<WebView?>(null) { _, _, new -> new?.setFindListener(this) } | ||
|
||
init { | ||
LayoutInflater.from(context) | ||
.inflate(R.layout.webview_search_bar, this, true) | ||
searchBox.addTextChangedListener(object : TextWatcher { | ||
override fun afterTextChanged(s: Editable?) { | ||
onTextChanged() | ||
} | ||
|
||
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {} | ||
|
||
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {} | ||
|
||
}) | ||
nextButton.setOnClickListener { webView?.findNext(true) } | ||
prevButton.setOnClickListener { webView?.findNext(false) } | ||
} | ||
|
||
/** Called when the text in the search box changes */ | ||
private fun onTextChanged() { | ||
// don't pass null to #findAllAsync no matter what it claims, those NPEs are why I had to write this | ||
webView?.findAllAsync(searchBox.text?.toString() ?: "") | ||
} | ||
|
||
override fun onFindResultReceived(activeMatchOrdinal: Int, numberOfMatches: Int, isDoneCounting: Boolean) { | ||
resultCount.text = when { | ||
searchBox.text.isEmpty() -> "" | ||
numberOfMatches == 0 -> "0/0" | ||
else -> "${activeMatchOrdinal + 1}/$numberOfMatches" | ||
} | ||
} | ||
|
||
override fun onActionViewExpanded() { | ||
searchBox.requestFocus() | ||
// need to delay this call for... reasons? this makes the keyboard pop up reliably though | ||
searchBox.postDelayed({ | ||
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager? | ||
imm?.showSoftInput(searchBox, InputMethodManager.SHOW_IMPLICIT) | ||
}, 100) | ||
} | ||
|
||
override fun onActionViewCollapsed() { | ||
// clean up because we're only getting hidden | ||
searchBox.text.clear() | ||
searchBox.clearFocus() | ||
webView?.clearMatches() | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="18.0" | ||
android:viewportHeight="18.0"> | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="24.0" | ||
android:viewportHeight="24.0"> | ||
<path | ||
android:fillColor="?attr/iconColor" | ||
android:pathData="M13.59,5.59L9,10.17 4.41,5.59 3,7l6,6 6,-6z"/> | ||
android:pathData="M16.59,8.59L12,13.17 7.41,8.59 6,10l6,6 6,-6z" /> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
tools:layout_height="?android:attr/actionBarSize"> | ||
|
||
<EditText | ||
android:id="@+id/web_view_search_text" | ||
android:layout_width="0dp" | ||
android:layout_height="match_parent" | ||
android:layout_weight="1" | ||
android:ems="10" | ||
android:background="@android:color/transparent" | ||
android:hint="@string/web_view_search_box_hint" | ||
android:inputType="textPersonName" | ||
android:singleLine="true" /> | ||
|
||
<TextView | ||
android:id="@+id/webview_search_result_count" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:paddingStart="8dp" | ||
android:paddingEnd="16dp" | ||
android:textAlignment="textEnd" | ||
android:textAllCaps="false" | ||
android:textAppearance="@style/TextAppearance.MaterialComponents.Caption" | ||
android:textColor="?attr/actionBarFontColor" | ||
android:textSize="12sp" | ||
android:typeface="normal" | ||
tools:text="1000/1000" /> | ||
|
||
<View | ||
android:layout_width="1dp" | ||
android:layout_height="match_parent" | ||
android:layout_marginTop="8dp" | ||
android:layout_marginBottom="8dp" | ||
android:background="?attr/actionBarDivider" /> | ||
|
||
<ImageButton | ||
android:id="@+id/web_view_search_prev_result" | ||
style="@style/MaterialImageButton.Borderless" | ||
android:layout_width="@dimen/material_touch_target_size" | ||
android:layout_height="@dimen/material_touch_target_size" | ||
android:layout_gravity="center_vertical" | ||
android:src="@drawable/ic_expand_less" | ||
android:scaleType="centerInside" | ||
android:contentDescription="@string/web_view_previous_result_description" /> | ||
|
||
<ImageButton | ||
android:id="@+id/web_view_search_next_result" | ||
style="@style/MaterialImageButton.Borderless" | ||
android:layout_width="@dimen/material_touch_target_size" | ||
android:layout_height="@dimen/material_touch_target_size" | ||
android:layout_gravity="center_vertical" | ||
android:src="@drawable/ic_expand_more" | ||
android:scaleType="centerInside" | ||
android:contentDescription="@string/web_view_next_result_description" /> | ||
|
||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters