Skip to content

Commit

Permalink
Improvements to show selection after search / heading etc, by @harshad1
Browse files Browse the repository at this point in the history
…, closes #1325 (PR #1653)
  • Loading branch information
harshad1 committed Apr 8, 2022
1 parent 40f39e9 commit efb9f6a
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 28 deletions.
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
android:exported="true"
android:label="@string/app_name"
android:launchMode="singleTop"
android:windowSoftInputMode="stateUnchanged|adjustResize"
android:windowSoftInputMode="stateAlwaysHidden|adjustResize"
android:taskAffinity=".activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down Expand Up @@ -171,7 +171,7 @@
android:parentActivityName=".activity.MainActivity"
android:taskAffinity=".activity.DocumentActivity"
android:theme="@style/AppTheme.Unified"
android:windowSoftInputMode="stateUnchanged|adjustResize"
android:windowSoftInputMode="stateAlwaysHidden|adjustResize"
tools:targetApi="lollipop">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.Menu;
Expand Down Expand Up @@ -202,6 +201,8 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
_hlEditor.setBackgroundColor(_appSettings.getEditorBackgroundColor());
_hlEditor.setTextColor(_appSettings.getEditorForegroundColor());

_hlEditor.setGravity(_appSettings.isEditorStartEditingInCenter() ? Gravity.CENTER : Gravity.NO_GRAVITY);

// Do not need to send contents to accessibility
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
_hlEditor.setImportantForAccessibility(View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS);
Expand All @@ -225,14 +226,14 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
if (isDisplayedAtMainActivity()) {
startPos = _hlEditor.length();
} else if (args.getInt(Document.EXTRA_FILE_LINE_NUMBER, -1) >= 0) {
startPos = StringUtils.getIndexFromLineOffset(_hlEditor.getText(), new int[]{args.getInt(Document.EXTRA_FILE_LINE_NUMBER), 0});
startPos = StringUtils.getIndexFromLineOffset(_hlEditor.getText(), args.getInt(Document.EXTRA_FILE_LINE_NUMBER), 0);
} else if (_appSettings.isEditorStartOnBotttom()) {
startPos = _hlEditor.length();
}
}

if (_hlEditor.indexesValid(startPos)) {
_hlEditor.smoothMoveCursor(startPos);
_hlEditor.setCursor(startPos);
}
}

Expand All @@ -242,8 +243,6 @@ public void onResume() {

loadDocument();

_hlEditor.setGravity(_appSettings.isEditorStartEditingInCenter() ? Gravity.CENTER : Gravity.NO_GRAVITY);

if (_document != null) {
_document.testCreateParent();
boolean permok = _shareUtil.canWriteFile(_document.getFile(), false);
Expand Down Expand Up @@ -631,8 +630,6 @@ private void setHorizontalScrollMode(final boolean wrap) {
final boolean isCurrentlyWrap = _hsView == null || (_hlEditor.getParent() == _primaryScrollView);
if (context != null && _hlEditor != null && isCurrentlyWrap != wrap) {

final int posn = _hlEditor.getSelectionStart();

_primaryScrollView.removeAllViews();
if (_hsView != null) {
_hsView.removeAllViews();
Expand All @@ -650,7 +647,7 @@ private void setHorizontalScrollMode(final boolean wrap) {
_primaryScrollView.addView(_hlEditor);
}

_hlEditor.smoothMoveCursor(posn);
StringUtils.showSelection(_hlEditor);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#########################################################*/
package net.gsantner.markor.ui.hleditor;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
Expand All @@ -33,7 +32,6 @@
import java.util.HashSet;
import java.util.Set;


@SuppressWarnings("UnusedReturnValue")
public class HighlightingEditor extends AppCompatEditText {

Expand Down Expand Up @@ -332,21 +330,12 @@ protected void onSelectionChanged(int selStart, int selEnd) {
}
}

public void smoothMoveCursor(final int index, final int... endIndex) {
post(() -> {
if (!hasFocus()) {
requestFocus();
}

final ObjectAnimator anim;
if (endIndex != null && endIndex.length > 0) {
anim = ObjectAnimator.ofInt(this, "selection", index, endIndex[0]);
} else {
anim = ObjectAnimator.ofInt(this, "selection", index);
}
anim.setDuration(300);
anim.start();
});
public void setCursor(final int index) {
if (!hasFocus()) {
requestFocus();
}
post(() -> StringUtils.showSelection(this, index, index));
post(() ->setSelection(index));
}

public void setAccessibilityEnabled(final boolean enabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#########################################################*/
package net.gsantner.markor.ui.hleditor;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
Expand Down Expand Up @@ -668,6 +669,7 @@ protected final boolean runCommonTextAction(final @StringRes int action) {

// Some long-press actions common to multiple file types
// Can be called _explicitly_ by a derived class
@SuppressLint("NonConstantResourceId")
protected final boolean runCommonLongPressTextActions(@StringRes int action) {
switch (action) {
case R.string.tmaid_common_deindent:
Expand Down Expand Up @@ -696,6 +698,11 @@ protected final boolean runCommonLongPressTextActions(@StringRes int action) {
runRenumberOrderedListIfRequired(true);
return true;
}
case R.string.tmaid_common_move_text_one_line_up:
case R.string.tmaid_common_move_text_one_line_down: {
StringUtils.showSelection(_hlEditor);
return true;
}
case R.string.tmaid_common_insert_snippet: {
if (!TextUtils.isEmpty(_lastSnip)) {
_hlEditor.insertOrReplaceTextOnCursor(StringUtils.interpolateEscapedDateTime(_lastSnip));
Expand Down
72 changes: 71 additions & 1 deletion app/src/main/java/net/gsantner/opoc/util/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#########################################################*/
package net.gsantner.opoc.util;

import android.graphics.Rect;
import android.text.Layout;
import android.text.TextUtils;
import android.util.Base64;
import android.widget.EditText;
Expand Down Expand Up @@ -323,7 +325,9 @@ public static void selectLines(final EditText edit, final List<Integer> position
}
final CharSequence text = edit.getText();
if (positions.size() == 1) { // Case 1 index
edit.setSelection(StringUtils.getIndexFromLineOffset(text, positions.get(0), 0));
final int posn = StringUtils.getIndexFromLineOffset(text, positions.get(0), 0);
showSelection(edit, posn, posn);
edit.setSelection(posn);
} else if (positions.size() > 1) {
final TreeSet<Integer> pSet = new TreeSet<>(positions);
final int selStart, selEnd;
Expand All @@ -343,10 +347,76 @@ public static void selectLines(final EditText edit, final List<Integer> position
selStart = 0;
selEnd = StringUtils.getIndexFromLineOffset(newText, positions.size() - 1, 0);
}
showSelection(edit, selStart, selEnd);
edit.setSelection(selStart, selEnd);
}
}

public static void showSelection(final TextView text) {
showSelection(text, text.getSelectionStart(), text.getSelectionEnd());
}

public static void showSelection(final TextView text, final int start, final int end) {

// Get view info
// ------------------------------------------------------------
final Layout layout = text.getLayout();
if (layout == null) {
return;
}

final int _start = Math.min(start, end);
final int _end = Math.max(start, end);
if (start < 0 || end > text.length()) {
return;
}
final int lineStart = StringUtils.getLineStart(text.getText(), _start);

final Rect viewSize = new Rect();
if (!text.getLocalVisibleRect(viewSize)) {
return;
}

// Region in Y
// ------------------------------------------------------------
final int selStartLine = layout.getLineForOffset(_start);
final int lineStartLine = layout.getLineForOffset(lineStart);
final int selStartLineTop = layout.getLineTop(selStartLine);
final int lineStartLineTop = layout.getLineTop(lineStartLine);

final Rect region = new Rect();

if ((selStartLine - lineStartLine) <= 3) {
// good to see the start of the line if close enough
region.top = lineStartLineTop;
} else {
region.top = selStartLineTop;
}

// Push the top to the top
region.bottom = region.top + viewSize.height();

// Region in X - as handling RTL, text alignment, and centred text etc is
// a huge pain (see TextView.bringPointIntoView), we use a very simple solution.
// ------------------------------------------------------------

final int endLeft = (int) layout.getPrimaryHorizontal(_end);
final int startLeft = (int) layout.getPrimaryHorizontal(_start);
region.left = Math.min(startLeft, endLeft);
region.right = Math.max(startLeft, endLeft);

if (region.left == region.right) {
// make sure rect width > 0
region.right += 1;
} else if (region.width() > viewSize.width()) {
// Make sure selStart is in rect if possible
region.left = startLeft;
region.right = region.left + viewSize.width();
}

text.post(() -> text.requestRectangleOnScreen(region));
}

// Search for matching pairs of backticks
// interpolate contents of backtick pair as SimpleDateFormat
public static String interpolateEscapedDateTime(final String snip) {
Expand Down

0 comments on commit efb9f6a

Please sign in to comment.