Refactoring

This commit is contained in:
M66B
2022-10-07 17:19:30 +02:00
parent 77dbd3dbc7
commit ae960092bd
5 changed files with 56 additions and 45 deletions

View File

@@ -106,6 +106,58 @@ public class EditTextCompose extends FixedEditText {
boolean undo_manager = prefs.getBoolean("undo_manager", false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
setCustomSelectionActionModeCallback(new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
try {
int order = 1000;
menu.add(Menu.CATEGORY_SECONDARY, R.string.title_insert_parenthesis, order++, context.getString(R.string.title_insert_parenthesis));
menu.add(Menu.CATEGORY_SECONDARY, R.string.title_insert_quotes, order++, context.getString(R.string.title_insert_quotes));
} catch (Throwable ex) {
Log.e(ex);
}
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
int start = getSelectionStart();
int end = getSelectionEnd();
boolean selection = (BuildConfig.DEBUG && start >= 0 && start < end);
menu.findItem(R.string.title_insert_parenthesis).setVisible(selection);
menu.findItem(R.string.title_insert_quotes).setVisible(selection);
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
if (item.getGroupId() == Menu.CATEGORY_SECONDARY) {
int id = item.getItemId();
if (id == R.string.title_insert_parenthesis)
return surround("(", ")");
else if (id == R.string.title_insert_quotes)
return surround("\"", "\"");
}
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// Do nothing
}
private boolean surround(String before, String after) {
int start = getSelectionStart();
int end = getSelectionEnd();
boolean selection = (start >= 0 && start < end);
if (selection) {
getText().insert(end, after);
getText().insert(start, before);
}
return selection;
}
});
setCustomInsertionActionModeCallback(new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {