fix(ui): CodeEditText rendering
This commit is contained in:
@@ -60,7 +60,7 @@ public class CodeEditText extends AppCompatEditText {
|
|||||||
protected HVScrollView mParentScrollView;
|
protected HVScrollView mParentScrollView;
|
||||||
|
|
||||||
private final CopyOnWriteArrayList<CodeEditor.CursorChangeCallback> mCursorChangeCallbacks = new CopyOnWriteArrayList<>();
|
private final CopyOnWriteArrayList<CodeEditor.CursorChangeCallback> mCursorChangeCallbacks = new CopyOnWriteArrayList<>();
|
||||||
private JavaScriptHighlighter.HighlightTokens mHighlightTokens;
|
private volatile JavaScriptHighlighter.HighlightTokens mHighlightTokens;
|
||||||
private Theme mTheme;
|
private Theme mTheme;
|
||||||
private TimingLogger mLogger = new TimingLogger(LOG_TAG, "draw");
|
private TimingLogger mLogger = new TimingLogger(LOG_TAG, "draw");
|
||||||
private Paint mLineHighlightPaint = new Paint();
|
private Paint mLineHighlightPaint = new Paint();
|
||||||
@@ -192,10 +192,11 @@ public class CodeEditText extends AppCompatEditText {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
JavaScriptHighlighter.HighlightTokens highlightTokens = mHighlightTokens;
|
JavaScriptHighlighter.HighlightTokens highlightTokens = mHighlightTokens;
|
||||||
|
Log.d(LOG_TAG, "drawText: tokens = " + highlightTokens);
|
||||||
Layout layout = getLayout();
|
Layout layout = getLayout();
|
||||||
int lineCount = getLineCount();
|
int lineCount = getLineCount();
|
||||||
int textLength = highlightTokens == null ? 0 : highlightTokens.getText().length();
|
int textLength = highlightTokens == null ? 0 : highlightTokens.getText().length();
|
||||||
String text = highlightTokens == null ? "" : highlightTokens.getText();
|
Editable text = getText();
|
||||||
int paddingLeft = getPaddingLeft();
|
int paddingLeft = getPaddingLeft();
|
||||||
int scrollX = Math.max(getRealScrollX() - paddingLeft, 0);
|
int scrollX = Math.max(getRealScrollX() - paddingLeft, 0);
|
||||||
Paint paint = getPaint();
|
Paint paint = getPaint();
|
||||||
@@ -260,10 +261,10 @@ public class CodeEditText extends AppCompatEditText {
|
|||||||
}
|
}
|
||||||
paint.setColor(previousColor);
|
paint.setColor(previousColor);
|
||||||
float offsetX = paint.measureText(text, lineStart, previousColorPos);
|
float offsetX = paint.measureText(text, lineStart, previousColorPos);
|
||||||
if(previousColorPos < 0 || visibleCharEnd > textLength || previousColorPos >= visibleCharEnd){
|
if (previousColorPos < 0 || visibleCharEnd > textLength || previousColorPos >= visibleCharEnd) {
|
||||||
Log.e(LOG_TAG, "IndexOutOfBounds: previousColorPos = " + previousColorPos + ", visibleCharEnd = "
|
Log.e(LOG_TAG, "IndexOutOfBounds: previousColorPos = " + previousColorPos + ", visibleCharEnd = "
|
||||||
+visibleCharEnd + ", textLength = " + textLength);
|
+ visibleCharEnd + ", textLength = " + textLength);
|
||||||
postInvalidate();
|
//postInvalidate();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
canvas.drawText(text, previousColorPos, visibleCharEnd, paddingLeft + offsetX, lineBaseline, paint);
|
canvas.drawText(text, previousColorPos, visibleCharEnd, paddingLeft + offsetX, lineBaseline, paint);
|
||||||
@@ -413,10 +414,12 @@ public class CodeEditText extends AppCompatEditText {
|
|||||||
|
|
||||||
|
|
||||||
public void updateHighlightTokens(JavaScriptHighlighter.HighlightTokens highlightTokens) {
|
public void updateHighlightTokens(JavaScriptHighlighter.HighlightTokens highlightTokens) {
|
||||||
post(() -> {
|
if (mHighlightTokens != null && mHighlightTokens.getId() >= highlightTokens.getId()) {
|
||||||
mHighlightTokens = highlightTokens;
|
return;
|
||||||
invalidate();
|
}
|
||||||
});
|
mHighlightTokens = highlightTokens;
|
||||||
|
Log.d(LOG_TAG, "updateHighlightTokens: tokens = " + highlightTokens);
|
||||||
|
postInvalidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -12,9 +12,9 @@ import org.autojs.autojs.ui.widget.SimpleTextWatcher;
|
|||||||
import org.mozilla.javascript.Token;
|
import org.mozilla.javascript.Token;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
public class JavaScriptHighlighter implements SimpleTextWatcher.AfterTextChangedListener {
|
public class JavaScriptHighlighter implements SimpleTextWatcher.AfterTextChangedListener {
|
||||||
@@ -25,12 +25,17 @@ public class JavaScriptHighlighter implements SimpleTextWatcher.AfterTextChanged
|
|||||||
public final int[] colors;
|
public final int[] colors;
|
||||||
private String mText;
|
private String mText;
|
||||||
private int mCount;
|
private int mCount;
|
||||||
|
private final int mId;
|
||||||
|
|
||||||
public HighlightTokens(String text) {
|
public HighlightTokens(String text, int id) {
|
||||||
colors = new int[text.length()];
|
colors = new int[text.length()];
|
||||||
mText = text;
|
mText = text;
|
||||||
|
mId = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return mId;
|
||||||
|
}
|
||||||
|
|
||||||
public void addToken(int tokenStart, int tokenEnd, int color) {
|
public void addToken(int tokenStart, int tokenEnd, int color) {
|
||||||
for (int i = tokenStart; i < tokenEnd; i++) {
|
for (int i = tokenStart; i < tokenEnd; i++) {
|
||||||
@@ -41,9 +46,7 @@ public class JavaScriptHighlighter implements SimpleTextWatcher.AfterTextChanged
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "HighlightTokens{" +
|
return super.toString() + "{count = " + mCount + ", length = " + mText.length() + "}";
|
||||||
"colors=" + Arrays.toString(colors) +
|
|
||||||
'}';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getCharCount() {
|
public int getCharCount() {
|
||||||
@@ -57,11 +60,13 @@ public class JavaScriptHighlighter implements SimpleTextWatcher.AfterTextChanged
|
|||||||
|
|
||||||
private Theme mTheme;
|
private Theme mTheme;
|
||||||
private CodeEditText mCodeEditText;
|
private CodeEditText mCodeEditText;
|
||||||
private ExecutorService mExecutorService = Executors.newSingleThreadExecutor();
|
private ThreadPoolExecutor mExecutorService = new ThreadPoolExecutor(3, 6,
|
||||||
|
2L, TimeUnit.MINUTES, new LinkedBlockingQueue<>());
|
||||||
private AtomicInteger mRunningHighlighterId = new AtomicInteger();
|
private AtomicInteger mRunningHighlighterId = new AtomicInteger();
|
||||||
private TimingLogger mLogger = new TimingLogger(CodeEditText.LOG_TAG, "highlight");
|
private TimingLogger mLogger = new TimingLogger(CodeEditText.LOG_TAG, "highlight");
|
||||||
|
|
||||||
public JavaScriptHighlighter(Theme theme, CodeEditText codeEditText) {
|
public JavaScriptHighlighter(Theme theme, CodeEditText codeEditText) {
|
||||||
|
mExecutorService.allowCoreThreadTimeOut(true);
|
||||||
mTheme = theme;
|
mTheme = theme;
|
||||||
mCodeEditText = codeEditText;
|
mCodeEditText = codeEditText;
|
||||||
codeEditText.addTextChangedListener(new SimpleTextWatcher(this));
|
codeEditText.addTextChangedListener(new SimpleTextWatcher(this));
|
||||||
@@ -94,12 +99,10 @@ public class JavaScriptHighlighter implements SimpleTextWatcher.AfterTextChanged
|
|||||||
|
|
||||||
private void updateTokens(String sourceString, int id) throws IOException {
|
private void updateTokens(String sourceString, int id) throws IOException {
|
||||||
TokenStream ts = new TokenStream(null, sourceString, 0);
|
TokenStream ts = new TokenStream(null, sourceString, 0);
|
||||||
HighlightTokens highlightTokens = new HighlightTokens(sourceString);
|
HighlightTokens highlightTokens = new HighlightTokens(sourceString, id);
|
||||||
int token;
|
int token;
|
||||||
int color = mTheme.getColorForToken(Token.NAME);
|
int color = mTheme.getColorForToken(Token.NAME);
|
||||||
while ((token = ts.getToken()) != Token.EOF) {
|
while ((token = ts.getToken()) != Token.EOF) {
|
||||||
if (mRunningHighlighterId.get() != id)
|
|
||||||
return;
|
|
||||||
color = mTheme.getColorForToken(token);
|
color = mTheme.getColorForToken(token);
|
||||||
highlightTokens.addToken(ts.getTokenBeg(), ts.getTokenEnd(), color);
|
highlightTokens.addToken(ts.getTokenBeg(), ts.getTokenEnd(), color);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ public class Shell extends AbstractShell {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final boolean DEBUG = true;
|
private static final boolean DEBUG = false;
|
||||||
private static final String TAG = "Shell";
|
private static final String TAG = "Shell";
|
||||||
|
|
||||||
private volatile TermSession mTermSession;
|
private volatile TermSession mTermSession;
|
||||||
|
|||||||
Reference in New Issue
Block a user