Skip to content

Commit

Permalink
Select lines (PR gsantner#2443 by @harshad1)
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 authored Dec 6, 2024
1 parent 14d0620 commit 0e94057
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 55 deletions.
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 Expand Up @@ -302,7 +297,7 @@ public void recreateActionButtons(final ViewGroup barLayout, final ActionItem.Di
@SuppressLint("ClickableViewAccessibility")
private void setupRepeat(final View btn) {
// Velocity and acceleration parameters
final int INITIAL_DELAY = 400, DELTA_DELAY = 50, MIN_DELAY = 100;
final int INITIAL_DELAY = 300, DELTA_DELAY = 100, MIN_DELAY = 100;
final Handler handler = new Handler();

final Runnable repeater = new Runnable() {
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,8 +18,11 @@
import android.text.Layout;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.ActionMode;
import android.util.Log;
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 @@ -30,6 +33,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 @@ -114,6 +118,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 @@ -522,6 +529,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 @@ -742,4 +754,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 @@ -89,7 +89,7 @@ public class GsFileBrowserListAdapter extends RecyclerView.Adapter<GsFileBrowser
private File _fileToShowAfterNextLoad;
private File _currentFolder;
private final Context _context;
private StringFilter _filter;
private final StringFilter _filter;
private RecyclerView _recyclerView;
private LinearLayoutManager _layoutManager;
private final Map<File, File> _virtualMapping;
Expand Down Expand Up @@ -139,6 +139,7 @@ public GsFileBrowserListAdapter(GsFileBrowserOptions.Options options, Context co
_virtualMapping = Collections.unmodifiableMap(getVirtualFolders());
_reverseVirtualMapping = Collections.unmodifiableMap(GsCollectionUtils.reverse(_virtualMapping));
loadFolder(_dopt.startFolder != null ? _dopt.startFolder : _dopt.rootFolder, null);
_filter = new StringFilter(this);
}

public Map<File, File> getVirtualFolders() {
Expand Down Expand Up @@ -367,9 +368,6 @@ public int getItemCount() {

@Override
public Filter getFilter() {
if (_filter == null) {
_filter = new StringFilter(this, _adapterData);
}
return _filter;
}

Expand Down Expand Up @@ -600,7 +598,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 @@ -620,13 +618,19 @@ public void onLayoutChange(View v, int l, int t, int r, int b, int ol, int ot, i
});
}

private void postScrollToAndFlash(final File file) {
if (_recyclerView != null && file != null) {
_recyclerView.post(() -> scrollToAndFlash(file));
}
}

/**
* Scroll to a file in current folder and flash
*
* @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 @@ -641,19 +645,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 @@ -696,6 +687,11 @@ private void loadFolder(final File folder, final File show) {

// This function is not called on the main thread, so post to the UI thread
private synchronized void _loadFolder(final @NonNull File folder, final @Nullable File toShow) {

if (_recyclerView == null) {
return;
}

final boolean folderChanged = !folder.equals(_currentFolder);
final List<File> newData = new ArrayList<>();

Expand All @@ -717,7 +713,6 @@ private synchronized void _loadFolder(final @NonNull File folder, final @Nullabl
newData.add(new File(folder, "0"));
}


if (folder.equals(VIRTUAL_STORAGE_RECENTS)) {
newData.addAll(_dopt.recentFiles);
} else if (folder.equals(VIRTUAL_STORAGE_POPULAR)) {
Expand Down Expand Up @@ -756,16 +751,17 @@ private synchronized void _loadFolder(final @NonNull File folder, final @Nullabl
}
}

if (_recyclerView == null) {
//noinspection UnnecessaryReturnStatement
return;
} else if (folderChanged || modSumChanged || !newData.equals(_adapterData)) {
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 @@ -782,18 +778,18 @@ private synchronized void _loadFolder(final @NonNull File folder, final @Nullabl
_layoutManager.onRestoreInstanceState(_folderScrollMap.remove(_currentFolder));
}

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

if (_dopt.listener != null) {
_dopt.listener.onFsViewerDoUiUpdate(GsFileBrowserListAdapter.this);
}
});
} else if (toShow != null && _adapterDataFiltered.contains(toShow)) {
_recyclerView.post(() -> scrollToAndFlash(toShow));
} else {
postScrollToAndFlash(toShow);
}
}

Expand Down Expand Up @@ -835,39 +831,40 @@ public boolean isCurrentFolderHome() {
//########################
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) {
private StringFilter(final GsFileBrowserListAdapter adapter) {
super();
_adapter = adapter;
_originalList = adapterData;
_filteredList = new ArrayList<>();
}

@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(_adapter._adapterData, _filteredList);

_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>

0 comments on commit 0e94057

Please sign in to comment.