Skip to content

Commit

Permalink
Can scroll action bar on repeat items
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed Jun 16, 2024
1 parent 7862ce1 commit b95d2c2
Showing 1 changed file with 34 additions and 44 deletions.
78 changes: 34 additions & 44 deletions app/src/main/java/net/gsantner/markor/format/ActionButtonBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -307,45 +307,34 @@ public void recreateActionButtons(ViewGroup barLayout, ActionItem.DisplayMode di
}
}

@RequiresApi(api = Build.VERSION_CODES.P)
private void setupRepeat(final View btn, final ActionItem action) {
@SuppressLint("ClickableViewAccessibility")
private void setupRepeat(final View btn) {
// Velocity and acceleration parameters
final int initialDelay = 400, deltaDelay = 50, minDelay = 100;
final Integer token = action.keyId;

btn.setOnTouchListener(new View.OnTouchListener() {
Handler handler = null;
int delay = initialDelay;
final int INITIAL_DELAY = 400, DELTA_DELAY = 50, MIN_DELAY = 100;
final Handler handler = new Handler();

@SuppressLint("ClickableViewAccessibility")
final Runnable repeater = new Runnable() {
int delay = INITIAL_DELAY;
@Override
public boolean onTouch(final View v, final MotionEvent event) {
if (handler == null) {
handler = v.getHandler();
}

final int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
delay = initialDelay;
onClick(v);
return true;
} else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
handler.removeCallbacksAndMessages(token);
return true;
}
return false;
public void run() {
btn.callOnClick();
delay = Math.max(MIN_DELAY, delay - DELTA_DELAY);
handler.postDelayed(this, delay);
}
};

private void onClick(final View v) {
try {
v.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
onActionClick(action.keyId);
handler.postDelayed(() -> onClick(v), token, delay);
delay = Math.max(minDelay, delay - deltaDelay);
} catch (Exception ex) {
ex.printStackTrace();
}
btn.setOnLongClickListener(v -> {
btn.callOnClick(); // Trigger immediately
handler.postDelayed(repeater, INITIAL_DELAY);
return true;
});

btn.setOnTouchListener((view, event) -> {
final int eac = event.getAction();
if (eac == MotionEvent.ACTION_UP || eac == MotionEvent.ACTION_CANCEL) {
handler.removeCallbacksAndMessages(null);
}
return false;
});
}

Expand All @@ -357,18 +346,19 @@ protected void appendActionButtonToBar(final ViewGroup barLayout, final @NonNull
btn.setContentDescription(desc);
TooltipCompat.setTooltipText(btn, desc);

if (action.isRepeatable && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
setupRepeat(btn, action);
btn.setOnClickListener(v -> {
try {
// run action
v.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
onActionClick(action.keyId);
} catch (Exception ex) {
ex.printStackTrace();
}
});

if (action.isRepeatable) {
setupRepeat(btn);
} else {
btn.setOnClickListener(v -> {
try {
// run action
v.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
onActionClick(action.keyId);
} catch (Exception ex) {
ex.printStackTrace();
}
});
btn.setOnLongClickListener(v -> {
try {
v.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
Expand Down

0 comments on commit b95d2c2

Please sign in to comment.