Added select text block

pull/209/head
M66B 2 years ago
parent ea84f158aa
commit 37a36dddbf

@ -71,6 +71,7 @@ import android.text.TextUtils;
import android.text.format.DateUtils; import android.text.format.DateUtils;
import android.text.format.Time; import android.text.format.Time;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.util.Pair;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.ActionMode; import android.view.ActionMode;
import android.view.KeyEvent; import android.view.KeyEvent;
@ -2093,11 +2094,28 @@ public class Helper {
return new ActionMode.Callback() { return new ActionMode.Callback() {
@Override @Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) { public boolean onCreateActionMode(ActionMode mode, Menu menu) {
try {
int order = 1000;
menu.add(Menu.CATEGORY_SECONDARY, R.string.title_select_block, order++,
view.getContext().getString(R.string.title_select_block));
} catch (Throwable ex) {
Log.e(ex);
}
return true; return true;
} }
@Override @Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) { public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
try {
Pair<Integer, Integer> block = StyleHelper.getParagraph(view, true);
boolean ablock = (block != null &&
block.first == view.getSelectionStart() &&
block.second == view.getSelectionEnd());
menu.findItem(R.string.title_select_block).setVisible(!ablock);
} catch (Throwable ex) {
Log.e(ex);
}
for (int i = 0; i < menu.size(); i++) { for (int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i); MenuItem item = menu.getItem(i);
Intent intent = item.getIntent(); Intent intent = item.getIntent();
@ -2158,6 +2176,18 @@ public class Helper {
@Override @Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) { public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
if (item.getGroupId() == Menu.CATEGORY_SECONDARY)
try {
int id = item.getItemId();
if (id == R.string.title_select_block) {
Pair<Integer, Integer> block = StyleHelper.getParagraph(view, true);
if (block != null)
android.text.Selection.setSelection((Spannable) view.getText(), block.first, block.second);
return true;
}
} catch (Throwable ex) {
Log.e(ex);
}
return false; return false;
} }

@ -52,6 +52,7 @@ import android.view.MenuItem;
import android.view.SubMenu; import android.view.SubMenu;
import android.view.View; import android.view.View;
import android.widget.EditText; import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.widget.PopupMenu; import androidx.appcompat.widget.PopupMenu;
@ -889,14 +890,14 @@ public class StyleHelper {
return new Pair<>(start, end); return new Pair<>(start, end);
} }
public static Pair<Integer, Integer> getParagraph(EditText etBody) { public static Pair<Integer, Integer> getParagraph(TextView tvBody) {
return getParagraph(etBody, false); return getParagraph(tvBody, false);
} }
public static Pair<Integer, Integer> getParagraph(EditText etBody, boolean block) { public static Pair<Integer, Integer> getParagraph(TextView tvBody, boolean block) {
int start = etBody.getSelectionStart(); int start = tvBody.getSelectionStart();
int end = etBody.getSelectionEnd(); int end = tvBody.getSelectionEnd();
Editable edit = etBody.getText(); Spannable edit = (Spannable) tvBody.getText();
if (start < 0 || end < 0) if (start < 0 || end < 0)
return null; return null;

@ -1946,6 +1946,7 @@
<string name="title_undo">Undo</string> <string name="title_undo">Undo</string>
<string name="title_redo">Redo</string> <string name="title_redo">Redo</string>
<string name="title_insert_line">Insert line</string> <string name="title_insert_line">Insert line</string>
<string name="title_select_block">Select block</string>
<string name="title_add">Add</string> <string name="title_add">Add</string>
<string name="title_browse">Open with</string> <string name="title_browse">Open with</string>
<string name="title_info">Info</string> <string name="title_info">Info</string>

Loading…
Cancel
Save