Skip to content
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

Select lines #2443

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,6 @@ private List<String> loadActionPreference(final String suffix) {
public List<String> getActionOrder() {
final Set<String> order = new LinkedHashSet<>(loadActionPreference(ORDER_SUFFIX));

// Handle the case where order was stored without suffix. i.e. before this release.
if (order.isEmpty()) {
order.addAll(loadActionPreference(""));
}

final Set<String> defined = new LinkedHashSet<>(getActiveActionKeys());
final Set<String> disabled = new LinkedHashSet<>(getDisabledActions());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ private AlertDialog makeDialog(final File basedir, final boolean allowCreateDir,
templateAdapter.addAll(GsCollectionUtils.map(templates, p -> p.first));
templateSpinner.setAdapter(templateAdapter);

templateSpinner.setOnItemSelectedListener(new GsAndroidSpinnerOnItemSelectedAdapter(pos -> {
final String template = templateAdapter.getItem(pos);
final String fmt = appSettings.getTemplateTitleFormat(template);
formatEdit.setText(fmt);
}));

// Setup type / format spinner and action
// -----------------------------------------------------------------------------------------
final ArrayAdapter<String> typeAdapter = new ArrayAdapter<>(activity, android.R.layout.simple_spinner_dropdown_item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
import android.text.Layout;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.ActionMode;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.accessibility.AccessibilityEvent;
Expand All @@ -28,6 +31,7 @@
import androidx.appcompat.widget.AppCompatEditText;

import net.gsantner.markor.ApplicationObject;
import net.gsantner.markor.R;
import net.gsantner.markor.activity.MainActivity;
import net.gsantner.markor.model.AppSettings;
import net.gsantner.opoc.format.GsTextUtils;
Expand Down Expand Up @@ -111,6 +115,9 @@ public void afterTextChanged(final Editable s) {

// Fix for Android 12 perf issues - https://github.com/gsantner/markor/discussions/1794
setEmojiCompatEnabled(false);

// Custom options
setupCustomOptions();
}

@Override
Expand Down Expand Up @@ -503,6 +510,11 @@ public void withAutoFormatDisabled(final GsCallback.a0 callback) {
// Utility functions for interaction
// ---------------------------------------------------------------------------------------------

public void selectLines() {
final int[] sel = TextViewUtils.getLineSelection(this);
setSelection(sel[0], sel[1]);
}

public void simulateKeyPress(int keyEvent_KEYCODE_SOMETHING) {
dispatchKeyEvent(new KeyEvent(0, 0, KeyEvent.ACTION_DOWN, keyEvent_KEYCODE_SOMETHING, 0));
dispatchKeyEvent(new KeyEvent(0, 0, KeyEvent.ACTION_UP, keyEvent_KEYCODE_SOMETHING, 0));
Expand Down Expand Up @@ -718,4 +730,37 @@ public void reset() {
_maxNumberDigits = 0;
}
}

private void setupCustomOptions() {
setCustomSelectionActionModeCallback(new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Add custom items programmatically
menu.add(0, R.string.option_select_lines, 0, "☰");
return true;
}

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// Modify menu items here if necessary
return true;
}

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.string.option_select_lines:
HighlightingEditor.this.selectLines();
return true;
default:
return false;
}
}

@Override
public void onDestroyActionMode(ActionMode mode) {
// Cleanup if needed
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ public void onViewCreated(final View root, final @Nullable Bundle savedInstanceS

_filesystemViewerAdapter = new GsFileBrowserListAdapter(_dopt, activity);
_recyclerList.setAdapter(_filesystemViewerAdapter);
_filesystemViewerAdapter.getFilter().filter("");
onFsViewerDoUiUpdate(_filesystemViewerAdapter);

// Setup callbacks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ public void onViewCreated(@NonNull View root, @Nullable Bundle savedInstanceStat

_filesystemViewerAdapter = new GsFileBrowserListAdapter(_dopt, context);
_recyclerList.setAdapter(_filesystemViewerAdapter);
_filesystemViewerAdapter.getFilter().filter("");
onFsViewerDoUiUpdate(_filesystemViewerAdapter);

_swipe.setOnRefreshListener(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ public void showFile(final File file) {
return;
}

if (getFilePosition(file) < 0) {
if (_adapterDataFiltered.contains(file)) {
final File dir = file.getParentFile();
if (dir != null) {
loadFolder(dir, file);
Expand All @@ -623,7 +623,7 @@ public void onLayoutChange(View v, int l, int t, int r, int b, int ol, int ot, i
* @param file File to blink
*/
public boolean scrollToAndFlash(final File file) {
final int pos = getFilePosition(file);
final int pos = _adapterDataFiltered.indexOf(file);
if (pos >= 0 && _layoutManager != null) {
_layoutManager.scrollToPosition(pos);
_recyclerView.post(() ->
Expand All @@ -638,19 +638,6 @@ public boolean scrollToAndFlash(final File file) {
return false;
}

// Get the position of a file in the current view
// -1 if file is not a child of the current directory
public int getFilePosition(final File file) {
if (file != null) {
for (int i = 0; i < _adapterDataFiltered.size(); i++) {
if (_adapterDataFiltered.get(i).equals(file)) {
return i;
}
}
}
return -1;
}

private static final ExecutorService executorService = new ThreadPoolExecutor(0, 3, 60, TimeUnit.SECONDS, new SynchronousQueue<>());

private void loadFolder(final File folder, final File show) {
Expand Down Expand Up @@ -751,12 +738,16 @@ private synchronized void _loadFolder(final @NonNull File folder, final @Nullabl
}

if (folderChanged || modSumChanged || !newData.equals(_adapterData)) {
final ArrayList<File> filteredData = new ArrayList<>();
_filter._filter(newData, filteredData);

_recyclerView.post(() -> {
// Modify all these values in the UI thread
_adapterData.clear();
_adapterData.addAll(newData);
_adapterDataFiltered.clear();
_adapterDataFiltered.addAll(filteredData);
_currentSelection.retainAll(_adapterData);
_filter.filter(_filter._lastFilter);
_currentFolder = folder;
_prevModSum = modSum;

Expand All @@ -775,15 +766,15 @@ private synchronized void _loadFolder(final @NonNull File folder, final @Nullabl

_recyclerView.post(() -> scrollToAndFlash(toShow));
});
} else if (toShow != null && _adapterDataFiltered.contains(toShow)) {
} else {
_recyclerView.post(() -> scrollToAndFlash(toShow));
}

if (_dopt.listener != null) {
_dopt.listener.onFsViewerDoUiUpdate(GsFileBrowserListAdapter.this);
}
});
} else if (toShow != null && _adapterDataFiltered.contains(toShow)) {
} else {
_recyclerView.post(() -> scrollToAndFlash(toShow));
}
}
Expand Down Expand Up @@ -828,7 +819,7 @@ private static class StringFilter extends Filter {
private final GsFileBrowserListAdapter _adapter;
private final List<File> _originalList;
private final List<File> _filteredList;
public CharSequence _lastFilter = "";
public String _lastFilter = "";

private StringFilter(GsFileBrowserListAdapter adapter, List<File> adapterData) {
super();
Expand All @@ -840,25 +831,28 @@ private StringFilter(GsFileBrowserListAdapter adapter, List<File> adapterData) {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults results = new FilterResults();
constraint = constraint.toString().toLowerCase(Locale.getDefault()).trim();
_filteredList.clear();

if (constraint.length() == 0) {
_filteredList.addAll(_originalList);
} else {
for (File file : _originalList) {
if (file.getName().toLowerCase(Locale.getDefault()).contains(constraint)) {
_filteredList.add(file);
}
}
}
_lastFilter = constraint.toString().toLowerCase().trim();
_filter(_originalList, _filteredList);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

we don't want to run an async filter inside load folder as it is already async. So we break the internal processing out into a stand-alone function


_lastFilter = constraint;
results.values = _filteredList;
results.count = _filteredList.size();
return results;
}

public void _filter(final List<File> all, final List<File> filtered) {
filtered.clear();
if (_lastFilter.isEmpty()) {
filtered.addAll(all);
} else {
for (final File file : all) {
if (file.getName().toLowerCase().contains(_lastFilter)) {
filtered.add(file);
}
}
}
}

@Override
@SuppressWarnings("unchecked")
protected void publishResults(CharSequence constraint, FilterResults results) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/new_file_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
android:text="@string/template"
android:textAppearance="@style/TextAppearance.AppCompat.Caption" />

<Spinner
<view class="net.gsantner.markor.frontend.NewFileDialog$ReselectSpinner"
android:id="@+id/new_file_dialog__template"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/string-not_translatable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -433,4 +433,5 @@ work. If not, see <https://creativecommons.org/publicdomain/zero/1.0/>.
<string name="squarebrackets" translatable="false">Square Brackets</string>
<string name="csv" translatable="false">CSV</string>
<string name="orgmode" translatable="false">OrgMode</string>
<string name="option_select_lines" translatable="false">option_select_lines</string>
</resources>