From 6b1bc6c137844171f87d808b1b95b7371c41d814 Mon Sep 17 00:00:00 2001 From: Harshad Vedartham Date: Tue, 27 Aug 2024 11:38:05 -0700 Subject: [PATCH] Yet more massaging spans for performance --- .../format/todotxt/TodoTxtSyntaxHighlighter.java | 3 +-- .../frontend/textview/HighlightingEditor.java | 15 ++++++++++++--- .../frontend/textview/SyntaxHighlighterBase.java | 16 +++++++++++----- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/net/gsantner/markor/format/todotxt/TodoTxtSyntaxHighlighter.java b/app/src/main/java/net/gsantner/markor/format/todotxt/TodoTxtSyntaxHighlighter.java index 8301e9b82..37bfd4fb1 100644 --- a/app/src/main/java/net/gsantner/markor/format/todotxt/TodoTxtSyntaxHighlighter.java +++ b/app/src/main/java/net/gsantner/markor/format/todotxt/TodoTxtSyntaxHighlighter.java @@ -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; @@ -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; diff --git a/app/src/main/java/net/gsantner/markor/frontend/textview/HighlightingEditor.java b/app/src/main/java/net/gsantner/markor/frontend/textview/HighlightingEditor.java index 618335808..6c3c2d7ce 100644 --- a/app/src/main/java/net/gsantner/markor/frontend/textview/HighlightingEditor.java +++ b/app/src/main/java/net/gsantner/markor/frontend/textview/HighlightingEditor.java @@ -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; @@ -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())); } } @@ -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())); } }); } diff --git a/app/src/main/java/net/gsantner/markor/frontend/textview/SyntaxHighlighterBase.java b/app/src/main/java/net/gsantner/markor/frontend/textview/SyntaxHighlighterBase.java index a30b3ce7c..6eddc7ff7 100644 --- a/app/src/main/java/net/gsantner/markor/frontend/textview/SyntaxHighlighterBase.java +++ b/app/src/main/java/net/gsantner/markor/frontend/textview/SyntaxHighlighterBase.java @@ -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 { int start, end; @@ -114,7 +114,7 @@ public static class SpanGroup implements Comparable { span = o; start = s; end = e; - isStatic = o instanceof UpdateLayout; + isStatic = (span instanceof UpdateLayout || span instanceof StaticSpan); } @Override @@ -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 _groups, _groupBuffer; @@ -189,7 +192,7 @@ public SyntaxHighlighterBase clearStatic() { _staticApplied = false; - return this; + return reflow(); } /** @@ -262,6 +265,7 @@ private SyntaxHighlighterBase applyFixup() { break; } else { group.start += _fixupDelta; + group.end += _fixupDelta; } } clearFixup(); @@ -325,6 +329,8 @@ public SyntaxHighlighterBase applyDynamic(final int[] range) { return this; } + + public SyntaxHighlighterBase applyStatic() { if (_spannable == null || _staticApplied) { return this; @@ -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() {