Skip to content

Commit

Permalink
Refine WebView search UI to be closer to Chrome's.
Browse files Browse the repository at this point in the history
  • Loading branch information
maniac103 committed Jun 16, 2020
1 parent cc25a57 commit d4fa448
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ private void doSearch() {
return;
}
FindActionModeCallback findAction = new FindActionModeCallback(mWebView.getContext());
mWebView.startActionMode(findAction);
startSupportActionMode(findAction);
findAction.setWebView(mWebView);
findAction.showSoftInput();
}
Expand Down
46 changes: 33 additions & 13 deletions app/src/main/java/com/gh4a/widget/FindActionModeCallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
package com.gh4a.widget;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.text.Editable;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextWatcher;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
Expand All @@ -31,6 +32,8 @@
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.view.ActionMode;
import androidx.core.view.MenuItemCompat;

import com.gh4a.R;
import com.gh4a.utils.UiUtils;
Expand All @@ -40,6 +43,8 @@ public class FindActionModeCallback implements ActionMode.Callback, TextWatcher,
private EditText mEditText;
private TextView mMatches;
private WebView mWebView;
private MenuItem mPrevItem;
private MenuItem mNextItem;
private boolean mHasStartedSearch;
private int mNumberOfMatches;

Expand Down Expand Up @@ -88,14 +93,12 @@ public void setWebView(@NonNull WebView webView) {
@Override
public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches, boolean isDoneCounting) {
if (isDoneCounting) {
int activeMatch = numberOfMatches > 0 ? activeMatchOrdinal + 1 : 0;
mNumberOfMatches = numberOfMatches;
if (numberOfMatches > 0) {
mMatches.setText(mMatches.getResources().getQuantityString(
R.plurals.matches_found, mNumberOfMatches, activeMatchOrdinal + 1, mNumberOfMatches));
mMatches.setVisibility(View.VISIBLE);
} else {
mMatches.setVisibility(View.GONE);
}
mMatches.setSelected(numberOfMatches == 0);
mMatches.setText(String.format("%d/%d", activeMatch, numberOfMatches));
mMatches.setVisibility(View.VISIBLE);
updatePrevNextItemState();
}
}

Expand Down Expand Up @@ -127,13 +130,16 @@ public void findAll() {
"No WebView for FindActionModeCallback::findAll");
}
String find = mEditText.getText().toString();
mNumberOfMatches = 0;
if (find.isEmpty()) {
mWebView.clearMatches();
mMatches.setVisibility(View.GONE);
} else {
mWebView.findAllAsync(find);
mMatches.setVisibility(View.INVISIBLE);
mHasStartedSearch = true;
}
mNumberOfMatches = 0;
mMatches.setVisibility(find.isEmpty() ? View.GONE : View.INVISIBLE);
mWebView.findAllAsync(find);
mHasStartedSearch = true;
updatePrevNextItemState();
}

public void showSoftInput() {
Expand All @@ -148,9 +154,18 @@ public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.webview_find, menu);
Editable edit = mEditText.getText();
Selection.setSelection(edit, edit.length());

final Resources res = mCustomView.getContext().getResources();
final ColorStateList iconTintList = res.getColorStateList(R.color.webview_find_icon);

mPrevItem = menu.findItem(R.id.find_prev);
MenuItemCompat.setIconTintList(mPrevItem, iconTintList);
mNextItem = menu.findItem(R.id.find_next);
MenuItemCompat.setIconTintList(mNextItem, iconTintList);

mMatches.setVisibility(View.GONE);
updatePrevNextItemState();
mHasStartedSearch = false;
mMatches.setText("0");
mEditText.requestFocus();
return true;
}
Expand Down Expand Up @@ -200,4 +215,9 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
public void afterTextChanged(Editable s) {
findAll();
}

private void updatePrevNextItemState() {
mPrevItem.setEnabled(mNumberOfMatches > 0);
mNextItem.setEnabled(mNumberOfMatches > 0);
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/color/webview_find_icon.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="#61ffffff" />
<item android:color="#fff" />
</selector>
5 changes: 5 additions & 0 deletions app/src/main/res/color/webview_find_matches.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#ef5350" /> <!-- red 400 -->
<item android:color="#fff" />
</selector>
7 changes: 4 additions & 3 deletions app/src/main/res/layout/webview_find.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@
android:inputType="text"
android:hint="@string/abc_search_hint"
android:imeOptions="actionDone|flagNoExtractUi|flagNoFullscreen"
android:background="@drawable/abc_textfield_search_material"
android:layout_marginEnd="8dp" />
android:background="@drawable/abc_textfield_search_material" />

<TextView
android:id="@+id/matches"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="16dp"
android:layout_marginEnd="8dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="?android:attr/textColorPrimary" />
android:textColor="@color/webview_find_matches" />

</LinearLayout>
5 changes: 0 additions & 5 deletions app/src/main/res/values/plurals.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,4 @@
<item quantity="one">%1$d contribution</item>
<item quantity="other">%1$d contributions</item>
</plurals>

<plurals name="matches_found">
<item quantity="one">1 match</item>
<item quantity="other">%1$d of %2$d</item>
</plurals>
</resources>

0 comments on commit d4fa448

Please # to comment.