Skip to content

Commit

Permalink
Improve line numbers (PR #2470)
Browse files Browse the repository at this point in the history
  • Loading branch information
guanglinn authored Jan 18, 2025
1 parent e6421bd commit abdefb0
Show file tree
Hide file tree
Showing 7 changed files with 410 additions and 227 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.SearchView;
import android.widget.Toast;
Expand All @@ -51,6 +52,7 @@
import net.gsantner.markor.frontend.MarkorDialogFactory;
import net.gsantner.markor.frontend.filebrowser.MarkorFileBrowserFactory;
import net.gsantner.markor.frontend.textview.HighlightingEditor;
import net.gsantner.markor.frontend.textview.LineNumbersTextView;
import net.gsantner.markor.frontend.textview.TextViewUtils;
import net.gsantner.markor.model.AppSettings;
import net.gsantner.markor.model.Document;
Expand Down Expand Up @@ -91,10 +93,12 @@ public static DocumentEditAndViewFragment newInstance(final @NonNull Document do
private HighlightingEditor _hlEditor;
private WebView _webView;
private MarkorWebViewClient _webViewClient;
private FrameLayout _editorHolder;
private ViewGroup _textActionsBar;

private DraggableScrollbarScrollView _primaryScrollView;
private HorizontalScrollView _hsView;
private LineNumbersTextView _lineNumbersView;
private SearchView _menuSearchViewForViewMode;
private Document _document;
private FormatRegistry _format;
Expand Down Expand Up @@ -132,9 +136,12 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
final Activity activity = getActivity();

_hlEditor = view.findViewById(R.id.document__fragment__edit__highlighting_editor);
_editorHolder = view.findViewById(R.id.document__fragment__edit__editor_holder);
_textActionsBar = view.findViewById(R.id.document__fragment__edit__text_actions_bar);
_webView = view.findViewById(R.id.document__fragment_view_webview);
_primaryScrollView = view.findViewById(R.id.document__fragment__edit__content_editor__scrolling_parent);
_lineNumbersView = view.findViewById(R.id.document__fragment__edit__line_numbers_view);

_cu = new MarkorContextUtils(activity);

// Using `if (_document != null)` everywhere is dangerous
Expand All @@ -148,6 +155,9 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
return;
}

_lineNumbersView.setup(_hlEditor);
_lineNumbersView.setLineNumbersEnabled(_appSettings.getDocumentLineNumbersEnabled(_document.path));

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && _appSettings.getSetWebViewFulldrawing()) {
WebView.enableSlowWholeDocumentDraw();
}
Expand Down Expand Up @@ -202,7 +212,6 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
_hlEditor.setTextColor(_appSettings.getEditorForegroundColor());
_hlEditor.setGravity(_appSettings.isEditorStartEditingInCenter() ? Gravity.CENTER : Gravity.NO_GRAVITY);
_hlEditor.setHighlightingEnabled(_appSettings.getDocumentHighlightState(_document.path, _hlEditor.getText()));
_hlEditor.setLineNumbersEnabled(_appSettings.getDocumentLineNumbersEnabled(_document.path));
_hlEditor.setAutoFormatEnabled(_appSettings.getDocumentAutoFormatEnabled(_document.path));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Do not need to send contents to accessibility
Expand Down Expand Up @@ -526,7 +535,7 @@ public boolean onOptionsItemSelected(@NonNull final MenuItem item) {
if (saveDocument(false)) {
TextConverterBase converter = FormatRegistry.getFormat(_document.getFormat(), activity, _document).getConverter();
_cu.shareText(getActivity(),
converter.convertMarkup(getTextString(), getActivity(), false, _hlEditor.isLineNumbersEnabled(), _document.file),
converter.convertMarkup(getTextString(), getActivity(), false, _lineNumbersView.isLineNumbersEnabled(), _document.file),
"text/" + (item.getItemId() == R.id.action_share_html ? "html" : "plain")
);
}
Expand Down Expand Up @@ -615,9 +624,9 @@ public void onFsViewerConfig(GsFileBrowserOptions.Options dopt) {
return true;
}
case R.id.action_line_numbers: {
final boolean newState = !_hlEditor.isLineNumbersEnabled();
final boolean newState = !_lineNumbersView.isLineNumbersEnabled();
_appSettings.setDocumentLineNumbersEnabled(_document.path, newState);
_hlEditor.setLineNumbersEnabled(newState);
_lineNumbersView.setLineNumbersEnabled(newState);
updateMenuToggleStates(0);
return true;
}
Expand Down Expand Up @@ -738,7 +747,7 @@ private void updateMenuToggleStates(final int selectedFormatActionId) {
mi.setChecked(_hlEditor.getHighlightingEnabled());
}
if ((mi = _fragmentMenu.findItem(R.id.action_line_numbers)) != null) {
mi.setChecked(_hlEditor.isLineNumbersEnabled());
mi.setChecked(_lineNumbersView.isLineNumbersEnabled());
}
if ((mi = _fragmentMenu.findItem(R.id.action_enable_auto_format)) != null) {
mi.setChecked(_hlEditor.getAutoFormatEnabled());
Expand All @@ -756,19 +765,17 @@ private void updateMenuToggleStates(final int selectedFormatActionId) {
}

private boolean isWrapped() {
return _hsView == null || (_hlEditor.getParent() == _primaryScrollView);
return _hsView == null || (_hlEditor.getParent() == _editorHolder);
}

private void setHorizontalScrollMode(final boolean wrap) {
final Context context = getContext();
if (context != null && _hlEditor != null && isWrapped() != wrap) {

final int[] sel = TextViewUtils.getSelection(_hlEditor);

final boolean hlEnabled = _hlEditor.getHighlightingEnabled();
_hlEditor.setHighlightingEnabled(false);

_primaryScrollView.removeAllViews();
_editorHolder.removeAllViews();
if (_hsView != null) {
_hsView.removeAllViews();
}
Expand All @@ -778,15 +785,14 @@ private void setHorizontalScrollMode(final boolean wrap) {
_hsView.setFillViewport(true);
}
_hsView.addView(_hlEditor);
_primaryScrollView.addView(_hsView);
_editorHolder.addView(_hsView);
} else {
_primaryScrollView.addView(_hlEditor);
_editorHolder.addView(_hlEditor);
}

_hlEditor.setHighlightingEnabled(hlEnabled);

// Run after layout() of immediate parent completes
(wrap ? _primaryScrollView : _hsView).post(() -> TextViewUtils.setSelectionAndShow(_hlEditor, sel));
(wrap ? _editorHolder : _hsView).post(() -> TextViewUtils.setSelectionAndShow(_hlEditor, sel));
}
}

Expand Down Expand Up @@ -860,9 +866,9 @@ private boolean isDisplayedAtMainActivity() {
public void updateViewModeText() {
// Don't let text to view mode crash app
try {
_format.getConverter().convertMarkupShowInWebView(_document, getTextString(), getActivity(), _webView, _nextConvertToPrintMode, _hlEditor.isLineNumbersEnabled());
_format.getConverter().convertMarkupShowInWebView(_document, getTextString(), getActivity(), _webView, _nextConvertToPrintMode, _lineNumbersView.isLineNumbersEnabled());
} catch (OutOfMemoryError e) {
_format.getConverter().convertMarkupShowInWebView(_document, "updateViewModeText getTextString(): OutOfMemory " + e, getActivity(), _webView, _nextConvertToPrintMode, _hlEditor.isLineNumbersEnabled());
_format.getConverter().convertMarkupShowInWebView(_document, "updateViewModeText getTextString(): OutOfMemory " + e, getActivity(), _webView, _nextConvertToPrintMode, _lineNumbersView.isLineNumbersEnabled());
}
}

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
Loading

0 comments on commit abdefb0

Please sign in to comment.