From ff2e4337ef6dc432a6af06e9a17b71f29d339562 Mon Sep 17 00:00:00 2001 From: Li Guanglin Date: Sun, 17 Nov 2024 14:25:11 +0800 Subject: [PATCH] improve initialization --- .../activity/DocumentEditAndViewFragment.java | 2 +- .../markdown/MarkdownSyntaxHighlighter.java | 2 +- .../textview/LineNumbersTextView.java | 65 ++++++++++++------- 3 files changed, 42 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/net/gsantner/markor/activity/DocumentEditAndViewFragment.java b/app/src/main/java/net/gsantner/markor/activity/DocumentEditAndViewFragment.java index 8fab9ce72..09b59c72f 100644 --- a/app/src/main/java/net/gsantner/markor/activity/DocumentEditAndViewFragment.java +++ b/app/src/main/java/net/gsantner/markor/activity/DocumentEditAndViewFragment.java @@ -155,7 +155,7 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) { return; } - _lineNumbersView.setup(_hlEditor, _primaryScrollView); + _lineNumbersView.setup(_hlEditor); _lineNumbersView.setLineNumbersEnabled(_appSettings.getDocumentLineNumbersEnabled(_document.path)); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && _appSettings.getSetWebViewFulldrawing()) { diff --git a/app/src/main/java/net/gsantner/markor/format/markdown/MarkdownSyntaxHighlighter.java b/app/src/main/java/net/gsantner/markor/format/markdown/MarkdownSyntaxHighlighter.java index 4c42ef809..601b16f8c 100644 --- a/app/src/main/java/net/gsantner/markor/format/markdown/MarkdownSyntaxHighlighter.java +++ b/app/src/main/java/net/gsantner/markor/format/markdown/MarkdownSyntaxHighlighter.java @@ -36,7 +36,7 @@ public class MarkdownSyntaxHighlighter extends SyntaxHighlighterBase { private static final int MD_COLOR_LINK = 0xff1ea3fe; private static final int MD_COLOR_LIST = 0xffdaa521; private static final int MD_COLOR_QUOTE = 0xff88b04c; - private static final int MD_COLOR_CODEBLOCK = 0x448c8c8c; + private static final int MD_COLOR_CODEBLOCK = 0x2bafafaf; public MarkdownSyntaxHighlighter(AppSettings as) { super(as); diff --git a/app/src/main/java/net/gsantner/markor/frontend/textview/LineNumbersTextView.java b/app/src/main/java/net/gsantner/markor/frontend/textview/LineNumbersTextView.java index 18c9a36a5..47030b057 100644 --- a/app/src/main/java/net/gsantner/markor/frontend/textview/LineNumbersTextView.java +++ b/app/src/main/java/net/gsantner/markor/frontend/textview/LineNumbersTextView.java @@ -11,27 +11,15 @@ import android.view.View; import android.view.ViewTreeObserver; import android.widget.EditText; -import android.widget.ScrollView; +import androidx.annotation.NonNull; import androidx.appcompat.widget.AppCompatTextView; @SuppressWarnings("UnusedReturnValue") public class LineNumbersTextView extends AppCompatTextView { + private EditText editText; private LineNumbersDrawer lineNumbersDrawer; private boolean lineNumbersEnabled; - private ScrollView scrollView; - private final ViewTreeObserver.OnScrollChangedListener onScrollChangedListener = new ViewTreeObserver.OnScrollChangedListener() { - private long lastTime; - - @Override - public void onScrollChanged() { - final long time = System.currentTimeMillis(); - if (time - lastTime > 125) { - lastTime = time; - refresh(); - } - } - }; public LineNumbersTextView(Context context) { super(context); @@ -68,23 +56,27 @@ public void refresh() { } } - public void setup(final EditText editText, ScrollView scrollView) { - this.scrollView = scrollView; - lineNumbersDrawer = new LineNumbersDrawer(editText, this); + public void setup(final @NonNull EditText editText) { + if (lineNumbersEnabled) { + setLineNumbersEnabled(false); + } + this.editText = editText; + this.lineNumbersDrawer = null; } public void setLineNumbersEnabled(final boolean enabled) { lineNumbersEnabled = enabled; - if (lineNumbersEnabled) { - lineNumbersDrawer.prepare(); - if (scrollView != null) { - scrollView.getViewTreeObserver().addOnScrollChangedListener(onScrollChangedListener); + + if (enabled) { + if (lineNumbersDrawer == null) { + lineNumbersDrawer = new LineNumbersDrawer(editText, this); } + lineNumbersDrawer.prepare(); } else { - lineNumbersDrawer.done(); - if (scrollView != null) { - scrollView.getViewTreeObserver().removeOnScrollChangedListener(onScrollChangedListener); + if (lineNumbersDrawer == null) { + return; } + lineNumbersDrawer.done(); } refresh(); } @@ -136,7 +128,20 @@ public void afterTextChanged(Editable editable) { } }; - public LineNumbersDrawer(final EditText editText, final LineNumbersTextView textView) { + private final ViewTreeObserver.OnScrollChangedListener onScrollChangedListener = new ViewTreeObserver.OnScrollChangedListener() { + private long lastTime; + + @Override + public void onScrollChanged() { + final long time = System.currentTimeMillis(); + if (time - lastTime > 125) { + lastTime = time; + textView.refresh(); + } + } + }; + + public LineNumbersDrawer(final @NonNull EditText editText, final @NonNull LineNumbersTextView textView) { this.editText = editText; this.textView = textView; ORIGINAL_PADDING_LEFT = editText.getPaddingLeft(); @@ -243,11 +248,20 @@ private void setLineTracking(boolean enabled) { } } + private void setRefreshOnScrollChanged(final boolean enabled) { + if (enabled) { + editText.getViewTreeObserver().addOnScrollChangedListener(onScrollChangedListener); + } else { + editText.getViewTreeObserver().removeOnScrollChangedListener(onScrollChangedListener); + } + } + /** * Prepare for drawing line numbers. */ public void prepare() { setLineTracking(true); + setRefreshOnScrollChanged(true); textView.setVisibility(VISIBLE); editText.setPadding(EDITOR_PADDING_LEFT, editText.getPaddingTop(), editText.getPaddingRight(), editText.getPaddingBottom()); } @@ -329,6 +343,7 @@ public void draw(final Canvas canvas) { */ public void done() { setLineTracking(false); + setRefreshOnScrollChanged(false); maxNumberDigits = 0; textView.setWidth(0); textView.setVisibility(GONE);