Experiment: spell check from style toolbar

pull/210/head
M66B 2 years ago
parent 62130d23c6
commit a51f879994

@ -26,6 +26,7 @@ import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.Layout;
import android.text.NoCopySpan;
@ -107,6 +108,7 @@ public class StyleHelper {
R.id.menu_style_superscript,
R.id.menu_style_strikethrough,
R.id.menu_style_insert_line,
R.id.menu_style_spell_check,
R.id.menu_style_password,
R.id.menu_style_code,
R.id.menu_style_clear
@ -132,7 +134,11 @@ public class StyleHelper {
v.setOnClickListener(styleListener);
if (id == R.id.menu_style_password)
if (id == R.id.menu_style_spell_check)
v.setVisibility(
BuildConfig.DEBUG && LanguageTool.isEnabled(v.getContext())
? View.VISIBLE : View.GONE);
else if (id == R.id.menu_style_password)
v.setVisibility(
!BuildConfig.PLAY_STORE_RELEASE && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
? View.VISIBLE : View.GONE);
@ -376,6 +382,15 @@ public class StyleHelper {
end = tmp;
}
if (start == end &&
itemId == R.id.menu_style_spell_check) {
Pair<Integer, Integer> paragraph = getParagraph(etBody);
if (paragraph == null)
return false;
start = paragraph.first;
end = paragraph.second;
}
if (start == end &&
itemId != R.id.menu_link &&
itemId != R.id.menu_clear &&
@ -428,6 +443,8 @@ public class StyleHelper {
return setStrikeThrough(etBody, start, end, false);
else if (itemId == R.id.menu_style_insert_line)
return setLine(etBody, end);
else if (itemId == R.id.menu_style_spell_check)
return spellCheck(owner, etBody, start, end);
else if (itemId == R.id.menu_style_password)
return setPassword(owner, etBody, start, end);
else if (itemId == R.id.menu_style_code) {
@ -1226,6 +1243,53 @@ public class StyleHelper {
return true;
}
static boolean spellCheck(LifecycleOwner owner, EditText etBody, int start, int end) {
Log.breadcrumb("style", "action", "spell");
etBody.setSelection(end);
final Context context = etBody.getContext();
Bundle args = new Bundle();
args.putCharSequence("text", etBody.getText().subSequence(start, end));
new SimpleTask<List<LanguageTool.Suggestion>>() {
private BackgroundColorSpan highlightSpan = null;
@Override
protected void onPreExecute(Bundle args) {
int textColorHighlight = Helper.resolveColor(context, android.R.attr.textColorHighlight);
highlightSpan = new BackgroundColorSpan(textColorHighlight);
etBody.getText().setSpan(highlightSpan, start, end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING);
}
@Override
protected void onPostExecute(Bundle args) {
if (highlightSpan != null)
etBody.getText().removeSpan(highlightSpan);
}
@Override
protected List<LanguageTool.Suggestion> onExecute(Context context, Bundle args) throws Throwable {
CharSequence text = args.getCharSequence("text");
return LanguageTool.getSuggestions(context, text);
}
@Override
protected void onExecuted(Bundle args, List<LanguageTool.Suggestion> suggestions) {
LanguageTool.applySuggestions(etBody, start, end, suggestions);
}
@Override
protected void onException(Bundle args, Throwable ex) {
ToastEx.makeText(context, Log.formatThrowable(ex), Toast.LENGTH_LONG).show();
}
}.execute(context, owner, args, "spell");
return true;
}
static boolean setPassword(LifecycleOwner owner, EditText etBody, int start, int end) {
Log.breadcrumb("style", "action", "password");

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M12.45,16h2.09L9.43,3L7.57,3L2.46,16h2.09l1.12,-3h5.64l1.14,3zM6.43,11L8.5,5.48 10.57,11L6.43,11zM21.59,11.59l-8.09,8.09L9.83,16l-1.41,1.41 5.09,5.09L23,13l-1.41,-1.41z"/>
</vector>

@ -269,6 +269,21 @@
app:srcCompat="@drawable/twotone_horizontal_rule_24"
app:tint="@color/action_foreground" />
<ImageButton
android:id="@+id/menu_style_spell_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/title_advanced_lt"
android:paddingHorizontal="12dp"
android:paddingVertical="6dp"
android:scaleType="fitCenter"
android:tooltipText="@string/title_advanced_lt"
app:layout_constraintStart_toEndOf="@id/menu_style_insert_line"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/twotone_spellcheck_24"
app:tint="@color/action_foreground" />
<ImageButton
android:id="@+id/menu_style_password"
android:layout_width="wrap_content"
@ -279,7 +294,7 @@
android:paddingVertical="6dp"
android:scaleType="fitCenter"
android:tooltipText="@string/title_style_protect"
app:layout_constraintStart_toEndOf="@id/menu_style_insert_line"
app:layout_constraintStart_toEndOf="@id/menu_style_spell_check"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/twotone_lock_24"
app:tint="@color/action_foreground" />

Loading…
Cancel
Save