Skip to content

Commit

Permalink
Fix that line background covers error marking lines
Browse files Browse the repository at this point in the history
Fix wrongly written value in calling CodeEditor#setHardwareAcceleratedDrawAllowed
  • Loading branch information
Rosemoe committed Oct 9, 2021
1 parent a2ce33a commit ce84efe
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 41 deletions.
12 changes: 0 additions & 12 deletions app/src/main/java/io/github/rosemoe/sora/utils/CrashHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,15 @@ public void uncaughtException(Thread thread, @NonNull Throwable ex) {
// Save the world, hopefully
if (Looper.myLooper() != null) {
Handler handler = new Handler(Looper.myLooper());
Throwable e = ex;
while (true) {
try {
/*Writer writer = new StringWriter();
PrintWriter pw = new PrintWriter(writer);
e.printStackTrace(pw);
Throwable cause = e.getCause();
while (cause != null) {
cause.printStackTrace(pw);
cause = cause.getCause();
}
pw.close();
final String result = writer.toString();*/
handler.post(() -> {
Toast.makeText(mContext, R.string.err_crash_loop, Toast.LENGTH_SHORT).show();

});
Looper.loop();
} catch (Throwable t) {
saveCrashInfo(thread.getName(), t);
e = t;
}
}
}
Expand Down
84 changes: 59 additions & 25 deletions editor/src/main/java/io/github/rosemoe/sora/widget/CodeEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ private void initialize() {
setAutoCompletionOnComposing(true);
setAllowFullscreen(false);
setLigatureEnabled(false);
setHardwareAcceleratedDrawAllowed(false);
setHardwareAcceleratedDrawAllowed(true);
setInterceptParentHorizontalScrollIfNeeded(false);
setTypefaceText(Typeface.DEFAULT);
mPaintOther.setStrokeWidth(getDpUnit() * 1.8f);
Expand Down Expand Up @@ -652,9 +652,10 @@ public boolean isFullscreenAllowed() {
* {@link CodeEditor#setFontFeatureSettings(String)}
* <p>
* For enabling JetBrainsMono font's ligature, Use like this:
* <p>
* <pre class="prettyprint">
* CodeEditor editor = ...;
* editor.setFontFeatureSettings(enabled ? null : "'liga' 0,'hlig' 0,'dlig' 0,'clig' 0");
* </pre>
*/
public void setLigatureEnabled(boolean enabled) {
this.mLigatureEnabled = enabled;
Expand Down Expand Up @@ -1316,28 +1317,16 @@ protected void drawRows(Canvas canvas, float offset, LongArrayList postDrawLineN
mRenderer.setExpectedCapacity(Math.max(getLastVisibleRow() - getFirstVisibleRow(), 30));
mRenderer.keepCurrentInDisplay(getFirstVisibleRow(), getLastVisibleRow());
}

// Step 1 - Draw background of rows
for (int row = getFirstVisibleRow(); row <= getLastVisibleRow() && rowIterator.hasNext(); row++) {
Row rowInf = rowIterator.next();
int line = rowInf.lineIndex;
ContentLine contentLine = mText.getLine(line);
int columnCount = contentLine.length();
if (row == getFirstVisibleRow() && requiredFirstLn != null) {
requiredFirstLn.value = line;
} else if (rowInf.isLeadingRow) {
postDrawLineNumbers.add(IntPair.pack(line, row));
}

// Prepare data
int columnCount = mText.getColumnCount(line);
if (lastPreparedLine != line) {
lastPreparedLine = line;
prepareLine(line);
computeMatchedPositions(line, matchedPositions);
spanOffset = 0;
if (shouldInitializeNonPrintable()) {
long positions = findLeadingAndTrailingWhitespacePos(line);
leadingWhitespaceEnd = IntPair.getFirst(positions);
trailingWhitespaceStart = IntPair.getSecond(positions);
}
prepareLine(line);
lastPreparedLine = line;
}

// Get visible region on line
Expand All @@ -1358,15 +1347,19 @@ protected void drawRows(Canvas canvas, float offset, LongArrayList postDrawLineN
}
lastVisibleChar = Math.min(lastVisibleChar, rowInf.endColumn);

// Draw current line background (or save)
if (line == currentLine) {
drawRowBackground(canvas, currentLineBgColor, row);
postDrawCurrentLines.add(row);
}

// Draw matched text background
if (!matchedPositions.isEmpty()) {
for (int position : matchedPositions) {
drawRowRegionBackground(canvas, paintingOffset, row, firstVisibleChar, lastVisibleChar, position, position + mSearcher.mSearchText.length(), mColors.getColor(EditorColorScheme.MATCHED_TEXT_BACKGROUND));
}
}

float backupOffset = paintingOffset;

// Draw selected text background
if (mCursor.isSelected() && line >= mCursor.getLeftLine() && line <= mCursor.getRightLine()) {
int selectionStart = 0;
Expand All @@ -1379,12 +1372,52 @@ protected void drawRows(Canvas canvas, float offset, LongArrayList postDrawLineN
}
drawRowRegionBackground(canvas, paintingOffset, row, firstVisibleChar, lastVisibleChar, selectionStart, selectionEnd, mColors.getColor(EditorColorScheme.SELECTED_TEXT_BACKGROUND));
}
}
rowIterator.reset();

// Draw current line background (or save)
if (line == currentLine) {
drawRowBackground(canvas, currentLineBgColor, row);
postDrawCurrentLines.add(row);
// Step 2 - Draw text and text decorations
for (int row = getFirstVisibleRow(); row <= getLastVisibleRow() && rowIterator.hasNext(); row++) {
Row rowInf = rowIterator.next();
int line = rowInf.lineIndex;
ContentLine contentLine = mText.getLine(line);
int columnCount = contentLine.length();
if (row == getFirstVisibleRow() && requiredFirstLn != null) {
requiredFirstLn.value = line;
} else if (rowInf.isLeadingRow) {
postDrawLineNumbers.add(IntPair.pack(line, row));
}

// Prepare data
if (lastPreparedLine != line) {
lastPreparedLine = line;
prepareLine(line);
spanOffset = 0;
if (shouldInitializeNonPrintable()) {
long positions = findLeadingAndTrailingWhitespacePos(line);
leadingWhitespaceEnd = IntPair.getFirst(positions);
trailingWhitespaceStart = IntPair.getSecond(positions);
}
}

// Get visible region on line
float[] charPos = findFirstVisibleChar(offset, rowInf.startColumn, rowInf.endColumn, mBuffer);
int firstVisibleChar = (int) charPos[0];
int lastVisibleChar = firstVisibleChar;
float paintingOffset = charPos[1];
float temporaryOffset = paintingOffset;
while (temporaryOffset < getWidth() && lastVisibleChar < columnCount) {
char ch = mBuffer[lastVisibleChar];
if (isEmoji(ch) && lastVisibleChar + 1 < columnCount) {
temporaryOffset += mFontCache.measureText(mBuffer, lastVisibleChar, lastVisibleChar + 2, mPaint);
lastVisibleChar++;
} else {
temporaryOffset += mFontCache.measureChar(mBuffer[lastVisibleChar], mPaint);
}
lastVisibleChar++;
}
lastVisibleChar = Math.min(lastVisibleChar, rowInf.endColumn);

float backupOffset = paintingOffset;

// Draw text here
if (!mHardwareAccAllowed || !canvas.isHardwareAccelerated() || isWordwrap() || Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
Expand Down Expand Up @@ -1605,6 +1638,7 @@ protected void drawRows(Canvas canvas, float offset, LongArrayList postDrawLineN
postDrawCursor.add(new CursorPaintAction(row, centerX, mEventHandler.shouldDrawInsertHandle() ? mInsertHandle : null, true));
}
}

mPaintOther.setStrokeWidth(circleRadius * 2);
mDrawPoints.commitPoints(canvas, mPaintOther);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,10 @@ class LineBreakLayoutRowItr implements RowIterator {

private final Row result;
private int currentRow;
private final int initRow;

LineBreakLayoutRowItr(int initialRow) {
currentRow = initialRow;
initRow = currentRow = initialRow;
result = new Row();
result.isLeadingRow = true;
result.startColumn = 0;
Expand All @@ -173,6 +174,10 @@ public boolean hasNext() {
return currentRow >= 0 && currentRow < text.getLineCount();
}

@Override
public void reset() {
currentRow = initRow;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,12 @@ interface RowIterator {
*/
boolean hasNext();

/**
* Reset the position to its original position.
*
* This can be useful when the elements should be iterated for
* several times.
*/
void reset();

}
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,12 @@ static class RowRegion {

class WordwrapLayoutRowItr implements RowIterator {

final Row result;
int currentRow;
private final Row result;
private int currentRow;
private final int initRow;

WordwrapLayoutRowItr(int initialRow) {
currentRow = initialRow;
initRow = currentRow = initialRow;
result = new Row();
}

Expand All @@ -297,6 +298,10 @@ public boolean hasNext() {
return currentRow >= 0 && currentRow < rowTable.size();
}

@Override
public void reset() {
currentRow = initRow;
}
}

}

0 comments on commit ce84efe

Please sign in to comment.