Skip to content

Commit

Permalink
improve initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
guanglinn committed Nov 17, 2024
1 parent 346b88b commit ff2e433
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ff2e433

Please sign in to comment.