Check if can undo/redo

pull/194/merge
M66B 3 years ago
parent 8d87cbbe2d
commit aeaff42614

@ -34,12 +34,12 @@ import android.text.Spanned;
import android.text.style.QuoteSpan; import android.text.style.QuoteSpan;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.ActionMode; import android.view.ActionMode;
import android.view.KeyEvent;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.inputmethod.EditorInfo; import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnection;
import android.widget.TextView;
import androidx.core.view.inputmethod.EditorInfoCompat; import androidx.core.view.inputmethod.EditorInfoCompat;
import androidx.core.view.inputmethod.InputConnectionCompat; import androidx.core.view.inputmethod.InputConnectionCompat;
@ -47,7 +47,6 @@ import androidx.core.view.inputmethod.InputContentInfoCompat;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import java.lang.reflect.Method;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
public class EditTextCompose extends FixedEditText { public class EditTextCompose extends FixedEditText {
@ -55,6 +54,10 @@ public class EditTextCompose extends FixedEditText {
private ISelection selectionListener = null; private ISelection selectionListener = null;
private IInputContentListener inputContentListener = null; private IInputContentListener inputContentListener = null;
private Boolean canUndo = null;
private Boolean canRedo = null;
private boolean checkKeyEvent = false;
private static final ExecutorService executor = private static final ExecutorService executor =
Helper.getBackgroundExecutor(1, "paste"); Helper.getBackgroundExecutor(1, "paste");
@ -81,8 +84,8 @@ public class EditTextCompose extends FixedEditText {
setCustomInsertionActionModeCallback(new ActionMode.Callback() { setCustomInsertionActionModeCallback(new ActionMode.Callback() {
@Override @Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) { public boolean onCreateActionMode(ActionMode mode, Menu menu) {
boolean undo = can("Undo"); boolean undo = can(android.R.id.undo);
boolean redo = can("Redo"); boolean redo = can(android.R.id.redo);
if (undo) if (undo)
menu.add(Menu.CATEGORY_ALTERNATIVE, R.string.title_undo, 1, R.string.title_undo); menu.add(Menu.CATEGORY_ALTERNATIVE, R.string.title_undo, 1, R.string.title_undo);
if (redo) if (redo)
@ -115,15 +118,24 @@ public class EditTextCompose extends FixedEditText {
} }
} }
private boolean can(String can) { private boolean can(int what) {
canUndo = null;
canRedo = null;
try { try {
Method mCan = TextView.class.getMethod("can" + can); checkKeyEvent = true;
mCan.setAccessible(true); int meta = KeyEvent.META_CTRL_ON;
return (boolean) mCan.invoke(this); if (what == android.R.id.redo)
} catch (ReflectiveOperationException ex) { meta = meta | KeyEvent.META_SHIFT_ON;
Log.i(ex); KeyEvent ke = new KeyEvent(0, 0, 0, 0, 0, meta);
return true; onKeyShortcut(KeyEvent.KEYCODE_Z, ke);
} catch (Throwable ex) {
Log.e(ex);
} finally {
checkKeyEvent = false;
} }
return Boolean.TRUE.equals(what == android.R.id.redo ? canRedo : canUndo);
} }
@Override @Override
@ -289,6 +301,12 @@ public class EditTextCompose extends FixedEditText {
} }
}); });
return true;
} else if (id == android.R.id.undo) {
canUndo = true;
return true;
} else if (id == android.R.id.redo) {
canRedo = true;
return true; return true;
} }

Loading…
Cancel
Save