Skip to content

Commit

Permalink
Yet more massaging spans for performance
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed Aug 27, 2024
1 parent c620feb commit 6b1bc6c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import android.graphics.Paint;
import android.text.style.LineBackgroundSpan;
import android.text.style.LineHeightSpan;
import android.text.style.UpdateLayout;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
Expand Down Expand Up @@ -45,7 +44,7 @@ public void generateSpans() {
}

// Adds spacing and divider line between paragraphs
public static class ParagraphDividerSpan implements LineBackgroundSpan, LineHeightSpan, UpdateLayout {
public static class ParagraphDividerSpan implements LineBackgroundSpan, LineHeightSpan, StaticSpan {
private final int _lineColor;
private Integer _origAscent = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ protected void onDraw(Canvas canvas) {
// Highlighting
// ---------------------------------------------------------------------------------------------

private void batch(final Runnable runnable) {
try {
beginBatchEdit();
runnable.run();
} finally {
endBatchEdit();
}
}

private boolean isScrollSignificant() {
return Math.abs(_oldHlRect.top - _hlRect.top) > _hlShiftThreshold ||
Math.abs(_hlRect.bottom - _oldHlRect.bottom) > _hlShiftThreshold;
Expand All @@ -146,14 +155,14 @@ private boolean runHighlight(final boolean recompute) {

private void updateHighlighting() {
if (runHighlight(false)) {
_hl.clearDynamic().applyDynamic(hlRegion());
batch(() -> _hl.clearDynamic().applyDynamic(hlRegion()));
_oldHlRect.set(_hlRect);
}
}

public void recomputeHighlighting() {
if (runHighlight(true)) {
_hl.clearAll().recompute().applyStatic().applyDynamic(hlRegion());
batch(() -> _hl.clearAll().recompute().applyStatic().applyDynamic(hlRegion()));
}
}

Expand All @@ -174,7 +183,7 @@ private synchronized void _recomputeHighlightingWorker() {
_hl.compute();
post(() -> {
if (_textUnchangedWhileHighlighting.get()) {
_hl.clearAll().setComputed().applyStatic().applyDynamic(hlRegion());
batch(() -> _hl.clearAll().setComputed().applyStatic().applyDynamic(hlRegion()));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public SyntaxHighlighterBase configure(@Nullable final Paint paint) {
// ---------------------------------------------------------------------------------------------

/**
* A class representing any span
* A class holding any span
*/
public static class SpanGroup implements Comparable<SpanGroup> {
int start, end;
Expand All @@ -114,7 +114,7 @@ public static class SpanGroup implements Comparable<SpanGroup> {
span = o;
start = s;
end = e;
isStatic = o instanceof UpdateLayout;
isStatic = (span instanceof UpdateLayout || span instanceof StaticSpan);
}

@Override
Expand All @@ -127,6 +127,9 @@ private static class ForceUpdateLayout implements UpdateLayout {
// Empty class - just implements UpdateLayout
}

public interface StaticSpan extends UpdateAppearance {
}

private final ForceUpdateLayout _layoutUpdater;

private final List<SpanGroup> _groups, _groupBuffer;
Expand Down Expand Up @@ -189,7 +192,7 @@ public SyntaxHighlighterBase clearStatic() {

_staticApplied = false;

return this;
return reflow();
}

/**
Expand Down Expand Up @@ -262,6 +265,7 @@ private SyntaxHighlighterBase applyFixup() {
break;
} else {
group.start += _fixupDelta;
group.end += _fixupDelta;
}
}
clearFixup();
Expand Down Expand Up @@ -325,6 +329,8 @@ public SyntaxHighlighterBase applyDynamic(final int[] range) {
return this;
}



public SyntaxHighlighterBase applyStatic() {
if (_spannable == null || _staticApplied) {
return this;
Expand All @@ -334,13 +340,13 @@ public SyntaxHighlighterBase applyStatic() {

for (final SpanGroup group : _groups) {
if (group.isStatic) {
_spannable.setSpan(group.span, group.start, group.start + group.end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
_spannable.setSpan(group.span, group.start, group.end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}

_staticApplied = true;

return this;
return reflow();
}

public final SyntaxHighlighterBase reflow() {
Expand Down

0 comments on commit 6b1bc6c

Please sign in to comment.