Skip to content

Commit 7a0bd86

Browse files
committed
Improve LineNumbersView
1 parent 353d771 commit 7a0bd86

File tree

3 files changed

+45
-33
lines changed

3 files changed

+45
-33
lines changed

app/src/main/java/net/gsantner/markor/activity/DocumentEditAndViewFragment.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
import net.gsantner.markor.frontend.MarkorDialogFactory;
5454
import net.gsantner.markor.frontend.filebrowser.MarkorFileBrowserFactory;
5555
import net.gsantner.markor.frontend.textview.HighlightingEditor;
56-
import net.gsantner.markor.frontend.textview.LineNumbersTextView;
56+
import net.gsantner.markor.frontend.textview.LineNumbersView;
5757
import net.gsantner.markor.frontend.textview.TextViewUtils;
5858
import net.gsantner.markor.model.AppSettings;
5959
import net.gsantner.markor.model.Document;
@@ -102,7 +102,7 @@ public static DocumentEditAndViewFragment newInstance(final @NonNull Document do
102102

103103
private DraggableScrollbarScrollView _verticalScrollView;
104104
private HorizontalScrollView _horizontalScrollView;
105-
private LineNumbersTextView _lineNumbersView;
105+
private LineNumbersView _lineNumbersView;
106106
private SearchView _menuSearchViewForViewMode;
107107
private Document _document;
108108
private FormatRegistry _format;
@@ -598,7 +598,7 @@ public void onFsViewerConfig(GsFileBrowserOptions.Options dopt) {
598598
final boolean newState = !isWrapped();
599599
_appSettings.setDocumentWrapState(_document.path, newState);
600600
setWrapState(newState);
601-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
601+
if (_isPreviewVisible && _webView != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
602602
_webView.evaluateJavascript("setWrapWords('" + newState + "');", null);
603603
}
604604
updateMenuToggleStates(0);
@@ -608,7 +608,7 @@ public void onFsViewerConfig(GsFileBrowserOptions.Options dopt) {
608608
final boolean newState = !_lineNumbersView.isLineNumbersEnabled();
609609
_appSettings.setDocumentLineNumbersEnabled(_document.path, newState);
610610
_lineNumbersView.setLineNumbersEnabled(newState);
611-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
611+
if (_isPreviewVisible && _webView != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
612612
_webView.evaluateJavascript("setLineNumbers('" + newState + "');", null);
613613
}
614614
updateMenuToggleStates(0);
@@ -662,6 +662,7 @@ public void onFsViewerConfig(GsFileBrowserOptions.Options dopt) {
662662
}
663663
}
664664
}
665+
665666
public void checkTextChangeState() {
666667
final boolean isTextChanged = !_document.isContentSame(_hlEditor.getText());
667668
Drawable d;

app/src/main/java/net/gsantner/markor/frontend/textview/LineNumbersTextView.java renamed to app/src/main/java/net/gsantner/markor/frontend/textview/LineNumbersView.java

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,52 @@
1313
import android.widget.EditText;
1414

1515
import androidx.annotation.NonNull;
16-
import androidx.appcompat.widget.AppCompatTextView;
1716

1817
@SuppressWarnings("UnusedReturnValue")
19-
public class LineNumbersTextView extends AppCompatTextView {
18+
public class LineNumbersView extends View {
2019
private EditText editText;
2120
private LineNumbersDrawer lineNumbersDrawer;
2221
private boolean lineNumbersEnabled;
2322

24-
public LineNumbersTextView(Context context) {
23+
public LineNumbersView(Context context) {
2524
super(context);
2625
}
2726

28-
public LineNumbersTextView(Context context, AttributeSet attrs) {
27+
public LineNumbersView(Context context, AttributeSet attrs) {
2928
super(context, attrs);
3029
}
3130

32-
public LineNumbersTextView(Context context, AttributeSet attrs, int defStyleAttr) {
31+
public LineNumbersView(Context context, AttributeSet attrs, int defStyleAttr) {
3332
super(context, attrs, defStyleAttr);
3433
}
3534

3635
@Override
37-
protected void onVisibilityChanged(View changedView, int visibility) {
36+
protected void onAttachedToWindow() {
37+
super.onAttachedToWindow();
38+
setWidth(0); // Initial width
39+
}
40+
41+
@Override
42+
protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
3843
super.onVisibilityChanged(changedView, visibility);
3944
if (isLineNumbersEnabled() && visibility == View.VISIBLE) {
4045
refresh();
4146
}
4247
}
4348

4449
@Override
45-
protected void onDraw(Canvas canvas) {
50+
protected void onDraw(@NonNull Canvas canvas) {
4651
super.onDraw(canvas);
4752
if (lineNumbersEnabled) {
4853
lineNumbersDrawer.draw(canvas);
4954
}
5055
}
5156

57+
/**
58+
* Refresh LineNumbersView.
59+
*/
5260
public void refresh() {
53-
setText(""); // To activate LineNumbersTextView refresh
61+
invalidate();
5462
if (getWidth() == 0) {
5563
lineNumbersDrawer.getEditText().postInvalidate();
5664
}
@@ -64,6 +72,11 @@ public void setup(final @NonNull EditText editText) {
6472
this.lineNumbersDrawer = null;
6573
}
6674

75+
public void setWidth(int width) {
76+
getLayoutParams().width = width;
77+
requestLayout();
78+
}
79+
6780
public void setLineNumbersEnabled(final boolean enabled) {
6881
lineNumbersEnabled = enabled;
6982

@@ -76,7 +89,7 @@ public void setLineNumbersEnabled(final boolean enabled) {
7689
if (lineNumbersDrawer == null) {
7790
return;
7891
}
79-
lineNumbersDrawer.done();
92+
lineNumbersDrawer.cleanup();
8093
}
8194
refresh();
8295
}
@@ -87,7 +100,7 @@ public boolean isLineNumbersEnabled() {
87100

88101
static class LineNumbersDrawer {
89102
private final EditText editText;
90-
private final LineNumbersTextView textView;
103+
private final LineNumbersView lineNumbersView;
91104

92105
private final Paint paint = new Paint();
93106

@@ -123,7 +136,7 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
123136
@Override
124137
public void afterTextChanged(Editable editable) {
125138
if (isLayoutLineCountChanged() || isMaxNumberChanged()) {
126-
textView.refresh();
139+
lineNumbersView.refresh();
127140
}
128141
}
129142
};
@@ -136,14 +149,14 @@ public void onScrollChanged() {
136149
final long time = System.currentTimeMillis();
137150
if (time - lastTime > 125) {
138151
lastTime = time;
139-
textView.refresh();
152+
lineNumbersView.refresh();
140153
}
141154
}
142155
};
143156

144-
public LineNumbersDrawer(final @NonNull EditText editText, final @NonNull LineNumbersTextView textView) {
157+
public LineNumbersDrawer(final @NonNull EditText editText, final @NonNull LineNumbersView lineNumbersView) {
145158
this.editText = editText;
146-
this.textView = textView;
159+
this.lineNumbersView = lineNumbersView;
147160
ORIGINAL_PADDING_LEFT = editText.getPaddingLeft();
148161
paint.setColor(0xFF999999);
149162
paint.setTextAlign(Paint.Align.RIGHT);
@@ -262,7 +275,8 @@ private void setRefreshOnScrollChanged(final boolean enabled) {
262275
public void prepare() {
263276
setLineTracking(true);
264277
setRefreshOnScrollChanged(true);
265-
textView.setVisibility(VISIBLE);
278+
lineNumbersView.setWidth(0);
279+
lineNumbersView.setVisibility(VISIBLE);
266280
editText.setPadding(EDITOR_PADDING_LEFT, editText.getPaddingTop(), editText.getPaddingRight(), editText.getPaddingBottom());
267281
}
268282

@@ -286,7 +300,7 @@ public void draw(final Canvas canvas) {
286300
if (isTextSizeChanged() || isMaxNumberDigitsChanged()) {
287301
numberX = NUMBER_PADDING_LEFT + (int) paint.measureText(String.valueOf(maxNumber));
288302
fenceX = numberX + NUMBER_PADDING_RIGHT;
289-
textView.setWidth(fenceX + 1);
303+
lineNumbersView.setWidth(fenceX + 1);
290304
}
291305

292306
// If current visible area is out of current line numbers area,
@@ -341,12 +355,11 @@ public void draw(final Canvas canvas) {
341355
/**
342356
* Reset some states related line numbers.
343357
*/
344-
public void done() {
358+
public void cleanup() {
345359
setLineTracking(false);
346360
setRefreshOnScrollChanged(false);
347361
maxNumberDigits = 0;
348-
textView.setWidth(0);
349-
textView.setVisibility(GONE);
362+
lineNumbersView.setVisibility(GONE);
350363
editText.setPadding(ORIGINAL_PADDING_LEFT, editText.getPaddingTop(), editText.getPaddingRight(), editText.getPaddingBottom());
351364
}
352365
}

app/src/main/res/layout/document__fragment__edit.xml

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,30 @@
3030
android:layout_height="wrap_content"
3131
android:orientation="horizontal">
3232

33-
<net.gsantner.markor.frontend.textview.LineNumbersTextView
33+
<net.gsantner.markor.frontend.textview.LineNumbersView
3434
android:id="@+id/document__fragment__edit__line_numbers_view"
3535
android:layout_width="wrap_content"
36-
android:layout_height="match_parent"
37-
android:layout_weight="0"
38-
android:text="" />
36+
android:layout_height="match_parent" />
3937

4038
<net.gsantner.markor.frontend.textview.HighlightingEditor
4139
android:id="@+id/document__fragment__edit__highlighting_editor"
4240
android:layout_width="0dp"
4341
android:layout_height="wrap_content"
4442
android:layout_weight="1"
45-
android:background="@color/background"
4643
android:alpha="0"
44+
android:background="@color/background"
4745
android:gravity="top"
4846
android:imeOptions="flagNoExtractUi"
4947
android:inputType="textMultiLine|textCapSentences"
48+
android:isScrollContainer="false"
5049
android:minHeight="1dp"
50+
android:nestedScrollingEnabled="false"
51+
android:overScrollMode="never"
5152
android:paddingLeft="@dimen/activity_horizontal_margin"
5253
android:paddingTop="@dimen/activity_vertical_margin"
5354
android:paddingRight="@dimen/activity_horizontal_margin"
5455
android:paddingBottom="@dimen/editor_bottom_margin"
55-
android:nestedScrollingEnabled="false"
56-
android:isScrollContainer="false"
5756
android:scrollbars="none"
58-
android:overScrollMode="never"
5957
android:scrollHorizontally="false"
6058
android:textCursorDrawable="@drawable/cursor_accent" />
6159
</LinearLayout>
@@ -79,10 +77,10 @@
7977

8078
<ViewStub
8179
android:id="@+id/document__fragment_webview_stub"
82-
android:layout="@layout/document__fragment_view"
8380
android:layout_width="match_parent"
8481
android:layout_height="match_parent"
85-
android:layout_marginBottom="@dimen/textactions_bar_height" />
82+
android:layout_marginBottom="@dimen/textactions_bar_height"
83+
android:layout="@layout/document__fragment_view" />
8684

8785
<FrameLayout
8886
android:id="@+id/document__fragment_fullscreen_overlay"

0 commit comments

Comments
 (0)