Skip to content

WIP: Improving view mode and rendering #2566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public class DocumentEditAndViewFragment extends MarkorBaseFragment implements F
public static final String SAVESTATE_DOCUMENT = "DOCUMENT";
public static final String START_PREVIEW = "START_PREVIEW";

public static float VIEW_FONT_SCALE = 100f / 15.7f;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Do you see that scale still fitting quite good?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. But I do all my testing just my own primary device - a pixel 7 pro


public static DocumentEditAndViewFragment newInstance(final @NonNull Document document, final Integer lineNumber, final Boolean preview) {
DocumentEditAndViewFragment f = new DocumentEditAndViewFragment();
Bundle args = new Bundle();
Expand Down Expand Up @@ -169,7 +171,7 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
WebSettings webSettings = _webView.getSettings();
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
webSettings.setTextZoom((int) (_appSettings.getViewFontSize() / 15.7f * 100f));
webSettings.setTextZoom((int) (_appSettings.getDocumentViewFontSize(_document.path) * VIEW_FONT_SCALE));
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webSettings.setDatabaseEnabled(true);
webSettings.setGeolocationEnabled(false);
Expand Down Expand Up @@ -641,9 +643,15 @@ public void onFsViewerConfig(GsFileBrowserOptions.Options dopt) {
return true;
}
case R.id.action_set_font_size: {
MarkorDialogFactory.showFontSizeDialog(activity, _appSettings.getDocumentFontSize(_document.path), (newSize) -> {
_hlEditor.setTextSize(TypedValue.COMPLEX_UNIT_SP, (float) newSize);
_appSettings.setDocumentFontSize(_document.path, newSize);
final int current = _isPreviewVisible ? _appSettings.getDocumentViewFontSize(_document.path) : _appSettings.getDocumentFontSize(_document.path);
MarkorDialogFactory.showFontSizeDialog(activity, current, (newSize) -> {
if (_isPreviewVisible) {
_webView.getSettings().setTextZoom((int) (newSize * VIEW_FONT_SCALE));
_appSettings.setDocumentViewFontSize(_document.path, newSize);
} else {
_hlEditor.setTextSize(TypedValue.COMPLEX_UNIT_SP, (float) newSize);
_appSettings.setDocumentFontSize(_document.path, newSize);
}
});
return true;
}
Expand All @@ -652,18 +660,6 @@ public void onFsViewerConfig(GsFileBrowserOptions.Options dopt) {
_hlEditor.postDelayed(() -> MainActivity.launch(activity, _document.file, false), 250);
return true;
}
case R.id.action_toggle_case:
TextViewUtils.toggleSelectionCase(_hlEditor.getText());
return true;
case R.id.action_switch_case:
TextViewUtils.switchSelectionCase(_hlEditor.getText());
return true;
case R.id.action_capitalize_words:
TextViewUtils.capitalizeSelectionWords(_hlEditor.getText());
return true;
case R.id.action_capitalize_sentences:
TextViewUtils.capitalizeSelectionSentences(_hlEditor.getText());
return true;
default: {
return super.onOptionsItemSelected(item);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,9 @@ private void attachOrCopyAndClose(final File dest, final boolean show) {
}

_appSettings.addRecentFile(dest);
if (attachment != null) {

// Only if not forced link due to attachment
if (attachment == null) {
_appSettings.setFormatShareAsLink(asLink);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import android.text.Editable;
import android.text.Selection;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.view.HapticFeedbackConstants;
Expand Down Expand Up @@ -169,6 +170,7 @@ private List<ActionItem> getActionList() {
new ActionItem(R.string.abid_common_special_key, R.drawable.ic_keyboard_black_24dp, R.string.special_key),
new ActionItem(R.string.abid_common_time, R.drawable.ic_access_time_black_24dp, R.string.date_and_time),
new ActionItem(R.string.abid_common_open_link_browser, R.drawable.ic_open_in_browser_black_24dp, R.string.open_link),
new ActionItem(R.string.abid_common_change_case, R.drawable.ic_format_text_case_black_24dp, R.string.switch_case),

new ActionItem(R.string.abid_common_web_jump_to_very_top_or_bottom, R.drawable.ic_vertical_align_center_black_24dp, R.string.jump_to_bottom).setDisplayMode(ActionItem.DisplayMode.VIEW),
new ActionItem(R.string.abid_common_view_file_in_other_app, R.drawable.ic_baseline_open_in_new_24, R.string.open_with).setDisplayMode(ActionItem.DisplayMode.VIEW),
Expand Down Expand Up @@ -765,6 +767,10 @@ protected final boolean runCommonAction(final @StringRes int action) {
_cu.nextScreenRotationSetting(_activity);
return true;
}
case R.string.abid_common_change_case: {
MarkorDialogFactory.showCaseDialog(_activity, _hlEditor.getText());
return true;
}
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ private boolean followLinkUnderCursor() {
return true;
} else {
final File f = GsFileUtils.makeAbsolute(link.link, _document.file.getParentFile());
if (GsFileUtils.canCreate(f)) {
if (GsFileUtils.isDirectory(f) || f.isFile() || GsFileUtils.canCreate(f)) {
DocumentActivity.launch(getActivity(), f, null, null);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.Spannable;
import android.text.style.LineBackgroundSpan;
import android.text.style.LineHeightSpan;

Expand All @@ -23,54 +24,71 @@
public class TodoTxtSyntaxHighlighter extends TodoTxtBasicSyntaxHighlighter {

private final static Pattern LINE_OF_TEXT = Pattern.compile("(?m)(.*)?");
private ParagraphDividerSpan _paragraphSpan;

public TodoTxtSyntaxHighlighter(final AppSettings as) {
super(as);
}

@Override
public SyntaxHighlighterBase configure(Paint paint) {
super.configure(paint);

_delay = _appSettings.getHighlightingDelayTodoTxt();
return super.configure(paint);
_paragraphSpan = new ParagraphDividerSpan(paint);

return this;
}

@Override
public void generateSpans() {

super.generateSpans();
// Single span for the whole text - highly performant
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Single span for the whole file.

Much faster! Much less highlighting jank!

addSpanGroup(_paragraphSpan, 0, _spannable.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

// Paragraph space and divider half way up the space
createSpanForMatches(LINE_OF_TEXT, matcher -> new ParagraphDividerSpan(_textColor));
super.generateSpans();
}

// Adds spacing and divider line between paragraphs
public static class ParagraphDividerSpan implements LineBackgroundSpan, LineHeightSpan, StaticSpan {
private final int _lineColor;
private int _origAscent = 0;
private int _hash = 0;

public ParagraphDividerSpan(@ColorInt int lineColor) {
_lineColor = lineColor;
private final @ColorInt int _lineColor;
private final int _top, _ascent, _descent, _bottom, _offset;

public ParagraphDividerSpan(final Paint paint) {
final Paint.FontMetricsInt fm = paint.getFontMetricsInt();
_offset = Math.abs(fm.ascent) / 2;
_top = fm.top;
_ascent = fm.ascent;
_descent = fm.descent;
_bottom = fm.bottom;
_lineColor = paint.getColor();
}

@Override
public void drawBackground(@NonNull Canvas canvas, @NonNull Paint paint, int left, int right, int top, int baseline, int bottom, @NonNull CharSequence text, int start, int end, int lineNumber) {
if (start > 0 && text.charAt(start - 1) == '\n') {
if (end > 0 && text.charAt(end - 1) == '\n') {
paint.setColor(_lineColor);
paint.setStrokeWidth(0);
final float spacing = paint.getTextSize();
canvas.drawLine(left, top + spacing / 2, right, top + spacing / 2, paint);
canvas.drawLine(left, bottom, right, bottom, paint);
}
}

@Override
public void chooseHeight(CharSequence text, int start, int end, int spanstartv, int v, Paint.FontMetricsInt fm) {
if (_hash != text.hashCode()) {
_origAscent = fm.ascent;
_hash = text.hashCode();
fm.top = _top;
fm.ascent = _ascent;
fm.descent = _descent;
fm.bottom = _bottom;

if (start > 0 && text.charAt(start - 1) == '\n') {
fm.top = _top - _offset;
fm.ascent = _ascent - _offset;
}

if (end > 0 && text.charAt(end - 1) == '\n') {
fm.descent = _descent + _offset;
fm.bottom = _bottom + _offset;
}
boolean isFirstLineInParagraph = start > 0 && text.charAt(start - 1) == '\n';
fm.ascent = (isFirstLineInParagraph) ? (2 * _origAscent) : _origAscent;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,8 @@ private static void fetchAndInsertItem(
case AUDIO_BROWSE: {
if (activity instanceof AppCompatActivity && nameEdit != null && pathEdit != null) {
final GsFileBrowserOptions.SelectionListener fsListener = new GsFileBrowserOptions.SelectionListenerAdapter() {
GsFileBrowserOptions.Options _dopt = null;

@Override
public void onFsViewerSelected(final String request, final File file, final Integer lineNumber) {
setFields.callback(file);
Expand All @@ -462,6 +464,20 @@ public void onFsViewerSelected(final String request, final File file, final Inte
public void onFsViewerConfig(GsFileBrowserOptions.Options dopt) {
dopt.startFolder = currentFile.getParentFile();
dopt.rootFolder = GsFileBrowserListAdapter.VIRTUAL_STORAGE_ROOT;

if (action == InsertType.LINK_BROWSE) {
dopt.neutralButtonText = R.string.folder;
}

_dopt = dopt;
}

@Override
public void onFsViewerNeutralButtonPressed(File currentFolder) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Select folder

setFields.callback(currentFolder);
if (_dopt != null) {
_dopt.dialogInterface.dismiss();;
}
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import net.gsantner.markor.format.todotxt.TodoTxtBasicSyntaxHighlighter;
import net.gsantner.markor.format.todotxt.TodoTxtFilter;
import net.gsantner.markor.format.todotxt.TodoTxtTask;
import net.gsantner.markor.frontend.filebrowser.MarkorFileBrowserFactory;
import net.gsantner.markor.frontend.filesearch.FileSearchDialog;
import net.gsantner.markor.frontend.filesearch.FileSearchEngine;
import net.gsantner.markor.frontend.filesearch.FileSearchResultSelectorDialog;
Expand Down Expand Up @@ -919,6 +920,44 @@ public static void showPriorityDialog(Activity activity, char selectedPriority,
GsSearchOrCustomTextDialog.showMultiChoiceDialogWithSearchFilterUI(activity, dopt);
}

public static void showCaseDialog(final Activity activity, final Editable edit) {
if (activity == null || edit == null) {
return;
}

final DialogOptions dopt = baseConf(activity);

dopt.titleText = R.string.text_case;
dopt.isSearchEnabled = false;

final List<String> options = new ArrayList<>();
options.add(activity.getString(R.string.toggle_case));
options.add(activity.getString(R.string.switch_case));
options.add(activity.getString(R.string.capitalize_words));
options.add(activity.getString(R.string.capitalize_sentences));
dopt.data = options;

dopt.positionCallback = (result) -> {
final int index = result.get(0);
switch (index) {
case 0:
TextViewUtils.toggleSelectionCase(edit);
break;
case 1:
TextViewUtils.switchSelectionCase(edit);
break;
case 2:
TextViewUtils.capitalizeSelectionWords(edit);
break;
case 3:
TextViewUtils.capitalizeSelectionSentences(edit);
break;
}
};

GsSearchOrCustomTextDialog.showMultiChoiceDialogWithSearchFilterUI(activity, dopt);
}

@SuppressLint("StringFormatMatches")
public static void showCopyMoveConflictDialog(final Activity activity, final String fileName, final String destName, final boolean multiple, final GsCallback.a1<Integer> callback) {
final DialogOptions dopt = baseConf(activity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
#########################################################*/
package net.gsantner.markor.frontend.filebrowser;

import android.app.Activity;
import android.content.Context;
import android.os.Environment;

import androidx.fragment.app.FragmentManager;

import net.gsantner.markor.ApplicationObject;
import net.gsantner.markor.R;
import net.gsantner.markor.frontend.filesearch.FileSearchEngine;
import net.gsantner.markor.model.AppSettings;
import net.gsantner.markor.util.MarkorContextUtils;
import net.gsantner.opoc.frontend.filebrowser.GsFileBrowserDialog;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,14 +421,15 @@ public boolean getAutoFormatEnabled() {

public void setAutoFormatEnabled(final boolean enable) {
if (enable && !_autoFormatEnabled) {
if (_autoFormatFilter != null) {
setFilters(new InputFilter[]{_autoFormatFilter});
}
TextViewUtils.addFilter(this, _autoFormatFilter);

if (_autoFormatModifier != null) {
addTextChangedListener(_autoFormatModifier);
}

} else if (!enable && _autoFormatEnabled) {
setFilters(new InputFilter[]{});
TextViewUtils.removeFilter(this, _autoFormatFilter);

if (_autoFormatModifier != null) {
removeTextChangedListener(_autoFormatModifier);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,14 @@ public static class SpanGroup implements Comparable<SpanGroup> {
final Object span;
final boolean isStatic;
final boolean needsReflow;
final int type;

SpanGroup(Object o, int s, int e) {
SpanGroup(Object o, int s, int e, int t) {
span = o;
start = s;
end = e;
type = t;

needsReflow = span instanceof StaticSpan;
isStatic = needsReflow || span instanceof UpdateLayout;
}
Expand Down Expand Up @@ -353,7 +356,7 @@ public SyntaxHighlighterBase applyDynamic(final int[] range) {

final boolean valid = group.start >= 0 && group.end > range[0] && group.end <= length;
if (valid && !_appliedDynamic.contains(i)) {
_spannable.setSpan(group.span, group.start, group.end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
_spannable.setSpan(group.span, group.start, group.end, group.type);
_appliedDynamic.add(i);
}
}
Expand All @@ -370,7 +373,7 @@ public SyntaxHighlighterBase applyStatic() {
for (final SpanGroup group : _groups) {
if (group != null && group.isStatic) {
needsReflow |= group.needsReflow;
_spannable.setSpan(group.span, group.start, group.end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
_spannable.setSpan(group.span, group.start, group.end, group.type);
}
}

Expand Down Expand Up @@ -448,8 +451,14 @@ public final SyntaxHighlighterBase compute() {
//

protected final void addSpanGroup(final Object span, final int start, final int end) {
if (end > start && span != null) {
_groupBuffer.add(new SpanGroup(span, start, end));
if (end >= start && span != null) {
_groupBuffer.add(new SpanGroup(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE));
}
}

protected final void addSpanGroup(final Object span, final int start, final int end, final int type) {
if (end >= start && span != null) {
_groupBuffer.add(new SpanGroup(span, start, end, type));
}
}

Expand Down
Loading