Skip to content

Commit 69964fc

Browse files
committed
Merge branch 'maint/simplify-gesture-event-emitting'
2 parents 028c10f + ffd238b commit 69964fc

File tree

2 files changed

+19
-55
lines changed

2 files changed

+19
-55
lines changed

src/touch/gestures.c

Lines changed: 17 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ static void _reset_state(void)
146146

147147
/********************************** GESTURE DETECTION **********************************/
148148

149+
typedef bool (*gesture_detect_fn)(uint8_t location);
150+
149151
static bool _is_continuous_tap(uint8_t location)
150152
{
151153
return _state[location].max_slide_travel < TAP_SLIDE_TOLERANCE &&
@@ -192,55 +194,13 @@ static void _gesture_emit_event(uint8_t id, slider_location_t location)
192194
emit_event(&event);
193195
}
194196

195-
static void _emit_continuous_slide_event(void)
196-
{
197-
if (_is_continuous_slide(top_slider)) {
198-
_gesture_emit_event(EVENT_SLIDE, top_slider);
199-
}
200-
if (_is_continuous_slide(bottom_slider)) {
201-
_gesture_emit_event(EVENT_SLIDE, bottom_slider);
202-
}
203-
}
204-
205-
static void _emit_slide_release_event(void)
206-
{
207-
if (_is_slide_released(top_slider)) {
208-
_gesture_emit_event(EVENT_SLIDE_RELEASED, top_slider);
209-
}
210-
if (_is_slide_released(bottom_slider)) {
211-
_gesture_emit_event(EVENT_SLIDE_RELEASED, bottom_slider);
212-
}
213-
}
214-
215-
static void _emit_long_tap_event(void)
216-
{
217-
if (_is_long_tap_release(top_slider)) {
218-
_gesture_emit_event(EVENT_LONG_TAP, top_slider);
219-
}
220-
if (_is_long_tap_release(bottom_slider)) {
221-
_gesture_emit_event(EVENT_LONG_TAP, bottom_slider);
222-
}
223-
}
224-
225-
static void _emit_short_tap_event(void)
226-
{
227-
if (_is_tap_release(top_slider)) {
228-
_gesture_emit_event(EVENT_SHORT_TAP, top_slider);
229-
}
230-
if (_is_tap_release(bottom_slider)) {
231-
_gesture_emit_event(EVENT_SHORT_TAP, bottom_slider);
232-
}
233-
}
234-
235-
static void _emit_continuous_tap_event(void)
236-
{
237-
if (_is_continuous_tap(top_slider)) {
238-
_gesture_emit_event(EVENT_CONTINUOUS_TAP, top_slider);
239-
}
240-
if (_is_continuous_tap(bottom_slider)) {
241-
_gesture_emit_event(EVENT_CONTINUOUS_TAP, bottom_slider);
242-
}
243-
}
197+
static gesture_detect_fn _emit_event_detect_fns[EVENT_ENUM_MAX] = {
198+
[EVENT_SLIDE] = _is_continuous_slide,
199+
[EVENT_SLIDE_RELEASED] = _is_slide_released,
200+
[EVENT_LONG_TAP] = _is_long_tap_release,
201+
[EVENT_SHORT_TAP] = _is_tap_release,
202+
[EVENT_CONTINUOUS_TAP] = _is_continuous_tap,
203+
};
244204

245205
/********************************** MEASURE, DETECT and CALLBACK **********************************/
246206

@@ -263,11 +223,14 @@ static void _measure_and_emit(void)
263223
}
264224

265225
if (gesture_detected) {
266-
_emit_continuous_slide_event();
267-
_emit_slide_release_event();
268-
_emit_long_tap_event();
269-
_emit_short_tap_event();
270-
_emit_continuous_tap_event();
226+
for (int event_idx = 0; event_idx < EVENT_ENUM_MAX; event_idx++) {
227+
if (_emit_event_detect_fns[event_idx](top_slider)) {
228+
_gesture_emit_event(event_idx, top_slider);
229+
}
230+
if (_emit_event_detect_fns[event_idx](bottom_slider)) {
231+
_gesture_emit_event(event_idx, bottom_slider);
232+
}
233+
}
271234
}
272235

273236
bool both_sliders_released_or_inactive = true;

src/ui/event.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ typedef struct {
2828
enum {
2929
EVENT_SLIDE,
3030
EVENT_SLIDE_RELEASED,
31-
EVENT_CONTINUOUS_TAP,
3231
EVENT_LONG_TAP,
3332
EVENT_SHORT_TAP,
33+
EVENT_CONTINUOUS_TAP,
34+
EVENT_ENUM_MAX, // MAX must always be last, indicates the number of items in the enumration
3435
};
3536

3637
typedef struct {

0 commit comments

Comments
 (0)