E52B Add separate longclick retrigger delay by eternicode · Pull Request #86 · LennartHennigs/Button2 · GitHub
[go: up one dir, main page]

Skip to content

Add separate longclick retrigger delay#86

Open
eternicode wants to merge 1 commit intoLennartHennigs:masterfrom
eternicode:longclick-retrigger-delay
Open

Add separate longclick retrigger delay#86
eternicode wants to merge 1 commit intoLennartHennigs:masterfrom
eternicode:longclick-retrigger-delay

Conversation

@eternicode
Copy link

Objective

I was trying to implement a standard interaction pattern with long press:

  • User presses button
  • Press action is executed immediately
  • User continues pressing through long press delay (eg, 500ms)
  • Long press action is executed
  • Much shorter delay (eg, 100ms) is applied while button remains pressed
  • Long press action is repeated at shorter interval while button is held

Classic example would be long-pressing to quickly navigate a long list of items.

I had attempted this with (simplified and slightly pseudocode):

btn.setPressedHandler(pressAction);
btn.setLongClickDetectedHandler(
    [this](Button2 &btn) {
        btn.setLongClickTime(100);
        pressAction(btn);
    }
);
btn.setLongClickHandler([this](Button2 &btn) {
    btn.setLongClickTime(500);
});
btn.setLongClickDetectedRetriggerable(true);
btn.setLongClickTime(500);

but this approach consistently caused the first 3-5 long click actions to execute with ~30ms delay. The exact timing is probably dependent on other factors, but the threshold was adjusted downward when setLongClickTime(100) was called, so these 3-5 initial repeats were effectively immediately executed while the timer caught up to the adjusted longclick_counter multiplier math.

The MR produces cleaner behavior with cleaner code:

btn.setPressedHandler(pressAction);
btn.setLongClickDetectedHandler(pressAction);
btn.setLongClickTime(500);
btn.setLongClickDetectedRetriggerable(true, 100);

Considerations

I tried to come up with a clean way to avoid the has_longclick_retrigger_ms variable. Cleanest way would be to have an invalid value for longclick_retrigger_ms, ie -1, but uint to accommodate milliseconds precludes that option. Could default it to longclick_time_ms's value at init and sync the two when they have the same value, but that felt like too much magic. Explicit flagging felt best.


void Button2::setLongClickDetectedRetriggerable(bool retriggerable) {
longclick_retriggerable = retriggerable;
has_longclick_retrigger_ms = false;

Choose a reason for hiding this comment

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

Hi, i like your idea, not sure if the "has_longclick_retrigger_ms" is really needed you could just check if "longclick_retrigger_ms" has a value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

0