diff --git a/src/touch/gestures.c b/src/touch/gestures.c index f23ca1e2e8..3450cd8766 100644 --- a/src/touch/gestures.c +++ b/src/touch/gestures.c @@ -144,18 +144,6 @@ static void _reset_state(void) memset(_state, 0, sizeof(_state)); } -/** - * Prepares the gestures data to be sent with an emitted event - */ -static void _collect_gestures_data( - gestures_detection_state_t* state, - gestures_slider_data_t* slider_data) -{ - slider_data->position = state->position_current; - slider_data->diff = state->position_current - state->position_start; - slider_data->velocity = state->velocity_sum; -} - /********************************** GESTURE DETECTION **********************************/ static bool _is_continuous_tap(uint8_t location) @@ -194,59 +182,63 @@ static void _gesture_emit_event(uint8_t id, slider_location_t location) if (!_released_since_new_screen) { return; } - gestures_slider_data_t slider_data; - _collect_gestures_data(&_state[location], &slider_data); - event_t event = {.data = &slider_data, .id = id}; + event_slider_data_t slider_data = { + .source = location, + .position = _state[location].position_current, + .diff = _state[location].position_current - _state[location].position_start, + .velocity = _state[location].velocity_sum, + }; + event_t event = {.id = id, .data = slider_data}; emit_event(&event); } static void _emit_continuous_slide_event(void) { if (_is_continuous_slide(top_slider)) { - _gesture_emit_event(EVENT_TOP_SLIDE, top_slider); + _gesture_emit_event(EVENT_SLIDE, top_slider); } if (_is_continuous_slide(bottom_slider)) { - _gesture_emit_event(EVENT_BOTTOM_SLIDE, bottom_slider); + _gesture_emit_event(EVENT_SLIDE, bottom_slider); } } static void _emit_slide_release_event(void) { if (_is_slide_released(top_slider)) { - _gesture_emit_event(EVENT_TOP_SLIDE_RELEASED, top_slider); + _gesture_emit_event(EVENT_SLIDE_RELEASED, top_slider); } if (_is_slide_released(bottom_slider)) { - _gesture_emit_event(EVENT_BOTTOM_SLIDE_RELEASED, bottom_slider); + _gesture_emit_event(EVENT_SLIDE_RELEASED, bottom_slider); } } static void _emit_long_tap_event(void) { if (_is_long_tap_release(top_slider)) { - _gesture_emit_event(EVENT_TOP_LONG_TAP, top_slider); + _gesture_emit_event(EVENT_LONG_TAP, top_slider); } if (_is_long_tap_release(bottom_slider)) { - _gesture_emit_event(EVENT_BOTTOM_LONG_TAP, bottom_slider); + _gesture_emit_event(EVENT_LONG_TAP, bottom_slider); } } static void _emit_short_tap_event(void) { if (_is_tap_release(top_slider)) { - _gesture_emit_event(EVENT_TOP_SHORT_TAP, top_slider); + _gesture_emit_event(EVENT_SHORT_TAP, top_slider); } if (_is_tap_release(bottom_slider)) { - _gesture_emit_event(EVENT_BOTTOM_SHORT_TAP, bottom_slider); + _gesture_emit_event(EVENT_SHORT_TAP, bottom_slider); } } static void _emit_continuous_tap_event(void) { if (_is_continuous_tap(top_slider)) { - _gesture_emit_event(EVENT_TOP_CONTINUOUS_TAP, top_slider); + _gesture_emit_event(EVENT_CONTINUOUS_TAP, top_slider); } if (_is_continuous_tap(bottom_slider)) { - _gesture_emit_event(EVENT_BOTTOM_CONTINUOUS_TAP, bottom_slider); + _gesture_emit_event(EVENT_CONTINUOUS_TAP, bottom_slider); } } diff --git a/src/touch/gestures.h b/src/touch/gestures.h index 47198104bc..c6bc771ce6 100644 --- a/src/touch/gestures.h +++ b/src/touch/gestures.h @@ -33,12 +33,6 @@ #define SLIDER_POSITION_ONE_THIRD (MAX_SLIDER_POS / 3) #define SLIDER_POSITION_TWO_THIRD (MAX_SLIDER_POS / 3 * 2) -typedef struct { - int16_t diff; - uint16_t position; - int32_t velocity; -} gestures_slider_data_t; - /** * Detects a gestures and calls the respective callback. * @param[in] reset The flag indicates whether the gesture history should be diff --git a/src/ui/components/button.c b/src/ui/components/button.c index 8564882982..307110dda7 100644 --- a/src/ui/components/button.c +++ b/src/ui/components/button.c @@ -59,18 +59,13 @@ static void _render(component_t* component) static void _on_event(const event_t* event, component_t* component) { button_data_t* data = (button_data_t*)component->data; - const gestures_slider_data_t* slider_data = (const gestures_slider_data_t*)event->data; - if (data->span_over_slider) { - if (event->id == - ((data->location == top_slider) ? EVENT_TOP_SHORT_TAP : EVENT_BOTTOM_SHORT_TAP)) { + if (event->id == EVENT_SHORT_TAP && event->data.source == data->location) { + if (data->span_over_slider) { data->callback(component); - } - } else { - if (event->id == - ((data->location == top_slider) ? EVENT_TOP_SHORT_TAP : EVENT_BOTTOM_SHORT_TAP) && - slider_data->position >= component->position.left * MAX_SLIDER_POS / SCREEN_WIDTH && - slider_data->position <= (component->position.left + component->dimension.width) * - MAX_SLIDER_POS / SCREEN_WIDTH) { + } else if ( + event->data.position >= component->position.left * MAX_SLIDER_POS / SCREEN_WIDTH && + event->data.position <= (component->position.left + component->dimension.width) * + MAX_SLIDER_POS / SCREEN_WIDTH) { data->callback(component); } } diff --git a/src/ui/components/confirm_gesture.c b/src/ui/components/confirm_gesture.c index cfd650a5a5..81fbbd8df3 100644 --- a/src/ui/components/confirm_gesture.c +++ b/src/ui/components/confirm_gesture.c @@ -100,41 +100,32 @@ static void _render(component_t* component) */ static void _on_event(const event_t* event, component_t* component) { - // Avoid type casting to unknown events - if (event->id != EVENT_TOP_SLIDE_RELEASED && event->id != EVENT_BOTTOM_SLIDE_RELEASED && - event->id != EVENT_TOP_CONTINUOUS_TAP && event->id != EVENT_BOTTOM_CONTINUOUS_TAP && - event->id != EVENT_TOP_SHORT_TAP && event->id != EVENT_BOTTOM_SHORT_TAP && - event->id != EVENT_TOP_LONG_TAP && event->id != EVENT_BOTTOM_LONG_TAP && - event->id != EVENT_TOP_SLIDE && event->id != EVENT_BOTTOM_SLIDE) { - return; - } confirm_data_t* data = (confirm_data_t*)component->data; - const gestures_slider_data_t* slider_data = (const gestures_slider_data_t*)event->data; switch (event->id) { - case EVENT_TOP_SLIDE_RELEASED: - data->active_top = false; - break; - case EVENT_TOP_CONTINUOUS_TAP: - if (slider_data->position > SLIDER_POSITION_TWO_THIRD && - slider_data->position <= MAX_SLIDER_POS) { - data->active_top = true; + case EVENT_SLIDE_RELEASED: + if (event->data.source == top_slider) { + data->active_top = false; + } else { + data->active_bottom = false; } break; - case EVENT_TOP_SHORT_TAP: - data->active_top = false; - break; - case EVENT_BOTTOM_SLIDE_RELEASED: - data->active_bottom = false; - break; - case EVENT_BOTTOM_CONTINUOUS_TAP: - if (slider_data->position > SLIDER_POSITION_TWO_THIRD && - slider_data->position <= MAX_SLIDER_POS) { - data->active_bottom = true; + case EVENT_CONTINUOUS_TAP: + if (event->data.position > SLIDER_POSITION_TWO_THIRD && + event->data.position <= MAX_SLIDER_POS) { + if (event->data.source == top_slider) { + data->active_top = true; + } else { + data->active_bottom = true; + } } break; - case EVENT_BOTTOM_SHORT_TAP: - data->active_bottom = false; + case EVENT_SHORT_TAP: + if (event->data.source == top_slider) { + data->active_top = false; + } else { + data->active_bottom = false; + } break; default: break; diff --git a/src/ui/components/icon_button.c b/src/ui/components/icon_button.c index f08a65c9af..9ea9fec3a7 100644 --- a/src/ui/components/icon_button.c +++ b/src/ui/components/icon_button.c @@ -106,16 +106,9 @@ static void _on_event(const event_t* event, component_t* component) // Return if the slider event is on the wrong slider switch (event->id) { - case EVENT_TOP_SHORT_TAP: - case EVENT_TOP_CONTINUOUS_TAP: - if (data->location != top_slider) { - data->active = false; - return; - } - break; - case EVENT_BOTTOM_SHORT_TAP: - case EVENT_BOTTOM_CONTINUOUS_TAP: - if (data->location != bottom_slider) { + case EVENT_SHORT_TAP: + case EVENT_CONTINUOUS_TAP: + if (event->data.source != data->location) { data->active = false; return; } @@ -127,17 +120,16 @@ static void _on_event(const event_t* event, component_t* component) // Return if the slider position is away from the button // Only slider events reach here, so ok to typescast event->data - const gestures_slider_data_t* slider_data = (const gestures_slider_data_t*)event->data; switch (data->type) { case ICON_BUTTON_CHECK: case ICON_BUTTON_NEXT: - if (slider_data->position < SLIDER_POSITION_TWO_THIRD) { + if (event->data.position < SLIDER_POSITION_TWO_THIRD) { data->active = false; return; } break; case ICON_BUTTON_CROSS: - if (slider_data->position >= SLIDER_POSITION_ONE_THIRD) { + if (event->data.position >= SLIDER_POSITION_ONE_THIRD) { data->active = false; return; } @@ -151,8 +143,7 @@ static void _on_event(const event_t* event, component_t* component) // Call the callback on short tap switch (event->id) { - case EVENT_TOP_SHORT_TAP: - case EVENT_BOTTOM_SHORT_TAP: + case EVENT_SHORT_TAP: if (data->callback) { data->callback(data->user_data); } diff --git a/src/ui/components/keyboard_switch.c b/src/ui/components/keyboard_switch.c index 4c6b56dcb1..b06b621597 100644 --- a/src/ui/components/keyboard_switch.c +++ b/src/ui/components/keyboard_switch.c @@ -30,7 +30,6 @@ */ typedef struct { keyboard_mode_t mode; - slider_location_t location; bool active; // Marker is 'active', i.e., touched // if true, the special chars keyboard mode is available. bool special_chars; @@ -90,18 +89,20 @@ static void _render(component_t* component) static void _on_event(const event_t* event, component_t* component) { keyboard_switch_data_t* ks_data = (keyboard_switch_data_t*)component->data; - const gestures_slider_data_t* slider_data = (const gestures_slider_data_t*)event->data; + if (event->data.source != top_slider) { + return; + } switch (event->id) { - case EVENT_TOP_CONTINUOUS_TAP: - if (ks_data->location == top_slider && slider_data->position > SLIDER_POSITION_ONE_THIRD && - slider_data->position <= SLIDER_POSITION_TWO_THIRD) { + case EVENT_CONTINUOUS_TAP: + if (event->data.position > SLIDER_POSITION_ONE_THIRD && + event->data.position <= SLIDER_POSITION_TWO_THIRD) { ks_data->active = true; break; } /* FALLTHROUGH */ - case EVENT_TOP_SHORT_TAP: - if (ks_data->location == top_slider && slider_data->position > SLIDER_POSITION_ONE_THIRD && - slider_data->position <= SLIDER_POSITION_TWO_THIRD) { + case EVENT_SHORT_TAP: + if (event->data.position > SLIDER_POSITION_ONE_THIRD && + event->data.position <= SLIDER_POSITION_TWO_THIRD) { ks_data->active = false; switch (ks_data->mode) { case LOWER_CASE: @@ -144,7 +145,6 @@ static component_functions_t _component_functions = { /********************************** Create Instance **********************************/ component_t* keyboard_switch_create( - slider_location_t location, bool special_chars, bool default_to_digits, component_t* parent, @@ -163,7 +163,6 @@ component_t* keyboard_switch_create( } memset(ks_data, 0, sizeof(keyboard_switch_data_t)); - ks_data->location = location; ks_data->mode = default_to_digits ? DIGITS : LOWER_CASE; ks_data->active = false; ks_data->special_chars = special_chars; diff --git a/src/ui/components/keyboard_switch.h b/src/ui/components/keyboard_switch.h index 0e3ddbf46b..24f40c2e36 100644 --- a/src/ui/components/keyboard_switch.h +++ b/src/ui/components/keyboard_switch.h @@ -30,7 +30,6 @@ typedef enum { LOWER_CASE, UPPER_CASE, DIGITS, SPECIAL_CHARS } keyboard_mode_t; * @param[in] parent The parent component. */ component_t* keyboard_switch_create( - slider_location_t location, bool special_chars, bool default_to_digits, component_t* parent, diff --git a/src/ui/components/label.c b/src/ui/components/label.c index 0af9f8e158..ae0bb23d85 100644 --- a/src/ui/components/label.c +++ b/src/ui/components/label.c @@ -138,36 +138,35 @@ static void _render(component_t* component) static void _on_event(const event_t* event, component_t* component) { data_t* data = (data_t*)component->data; + if (event->data.source != bottom_slider) { + return; + } if (data->scrollable) { switch (event->id) { - case EVENT_BOTTOM_SLIDE: { - const gestures_slider_data_t* slider_data = (const gestures_slider_data_t*)event->data; + case EVENT_SLIDE: { // Variable scroll speed int16_t margin = SCREEN_WIDTH / 5; - data->slider_position_diff += SIGMOID(slider_data->velocity); + data->slider_position_diff += SIGMOID(event->data.velocity); data->text_position = data->text_position_last + (int16_t)data->slider_position_diff; data->text_position = MIN( component->dimension.width / 2 + margin, MAX(-margin - component->dimension.width / 2 + SCREEN_WIDTH, data->text_position)); - data->slider_position = slider_data->position; + data->slider_position = event->data.position; data->slider_is_touched = true; data->slider_was_touched = true; break; } - - case EVENT_BOTTOM_SLIDE_RELEASED: + case EVENT_SLIDE_RELEASED: data->text_position_last = data->text_position; data->slider_position_diff = 0; data->slider_is_touched = false; break; - case EVENT_BOTTOM_CONTINUOUS_TAP: { - const gestures_slider_data_t* slider_data = (const gestures_slider_data_t*)event->data; - data->slider_position = slider_data->position; + case EVENT_CONTINUOUS_TAP: + data->slider_position = event->data.position; data->slider_is_touched = true; data->slider_was_touched = true; break; - } default: break; } diff --git a/src/ui/components/left_arrow.c b/src/ui/components/left_arrow.c index e9c9053f1b..80b670ea05 100644 --- a/src/ui/components/left_arrow.c +++ b/src/ui/components/left_arrow.c @@ -60,17 +60,12 @@ static void _render(component_t* component) static void _on_event(const event_t* event, component_t* component) { left_arrow_data_t* data = (left_arrow_data_t*)component->data; - const gestures_slider_data_t* slider_data = (const gestures_slider_data_t*)event->data; + if (event->data.source != data->location) { + return; + } switch (event->id) { - case EVENT_TOP_SHORT_TAP: - case EVENT_BOTTOM_SHORT_TAP: - if (data->location == top_slider && event->id == EVENT_BOTTOM_SHORT_TAP) { - break; - } - if (data->location == bottom_slider && event->id == EVENT_TOP_SHORT_TAP) { - break; - } - if (slider_data->position <= SLIDER_POSITION_ONE_THIRD) { + case EVENT_SHORT_TAP: + if (event->data.position <= SLIDER_POSITION_ONE_THIRD) { data->active = false; if (data->callback) { data->callback(data->user_data); @@ -78,15 +73,8 @@ static void _on_event(const event_t* event, component_t* component) break; } /* FALLTHROUGH */ - case EVENT_TOP_CONTINUOUS_TAP: - case EVENT_BOTTOM_CONTINUOUS_TAP: - if (data->location == top_slider && event->id == EVENT_BOTTOM_CONTINUOUS_TAP) { - break; - } - if (data->location == bottom_slider && event->id == EVENT_TOP_CONTINUOUS_TAP) { - break; - } - if (slider_data->position <= SLIDER_POSITION_ONE_THIRD) { + case EVENT_CONTINUOUS_TAP: + if (event->data.position <= SLIDER_POSITION_ONE_THIRD) { data->active = true; break; } diff --git a/src/ui/components/right_arrow.c b/src/ui/components/right_arrow.c index 15b637fafe..72ddaf6d57 100644 --- a/src/ui/components/right_arrow.c +++ b/src/ui/components/right_arrow.c @@ -60,18 +60,13 @@ static void _render(component_t* component) static void _on_event(const event_t* event, component_t* component) { right_arrow_data_t* data = (right_arrow_data_t*)component->data; - const gestures_slider_data_t* slider_data = (const gestures_slider_data_t*)event->data; + if (event->data.source != data->location) { + return; + } switch (event->id) { - case EVENT_TOP_SHORT_TAP: - case EVENT_BOTTOM_SHORT_TAP: - if (data->location == top_slider && event->id == EVENT_BOTTOM_SHORT_TAP) { - break; - } - if (data->location == bottom_slider && event->id == EVENT_TOP_SHORT_TAP) { - break; - } - if (slider_data->position > SLIDER_POSITION_TWO_THIRD && - slider_data->position <= MAX_SLIDER_POS) { + case EVENT_SHORT_TAP: + if (event->data.position > SLIDER_POSITION_TWO_THIRD && + event->data.position <= MAX_SLIDER_POS) { data->active = false; if (data->callback) { data->callback(data->user_data); @@ -79,17 +74,9 @@ static void _on_event(const event_t* event, component_t* component) break; } /* FALLTHROUGH */ - case EVENT_TOP_CONTINUOUS_TAP: - case EVENT_BOTTOM_CONTINUOUS_TAP: - if (data->location == top_slider && event->id == EVENT_BOTTOM_CONTINUOUS_TAP) { - break; - } - if (data->location == bottom_slider && event->id == EVENT_TOP_CONTINUOUS_TAP) { - break; - } - - if (slider_data->position > SLIDER_POSITION_TWO_THIRD && - slider_data->position <= MAX_SLIDER_POS) { + case EVENT_CONTINUOUS_TAP: + if (event->data.position > SLIDER_POSITION_TWO_THIRD && + event->data.position <= MAX_SLIDER_POS) { data->active = true; break; } diff --git a/src/ui/components/trinary_input_char.c b/src/ui/components/trinary_input_char.c index 305ffbe1f8..e20dd64e35 100644 --- a/src/ui/components/trinary_input_char.c +++ b/src/ui/components/trinary_input_char.c @@ -95,15 +95,14 @@ static void _alphabet_selected(component_t* component, const char* alphabet) static void _on_event(const event_t* event, component_t* component) { - if (event->id != EVENT_BOTTOM_SHORT_TAP) { + if (!(event->id == EVENT_SHORT_TAP && event->data.source == bottom_slider)) { return; } data_t* data = (data_t*)component->data; - const gestures_slider_data_t* slider_data = (const gestures_slider_data_t*)event->data; const char* selected_alphabet; - if (slider_data->position <= SLIDER_POSITION_ONE_THIRD) { + if (event->data.position <= SLIDER_POSITION_ONE_THIRD) { selected_alphabet = data->left_alphabet; - } else if (slider_data->position <= SLIDER_POSITION_TWO_THIRD) { + } else if (event->data.position <= SLIDER_POSITION_TWO_THIRD) { selected_alphabet = data->middle_alphabet; } else { selected_alphabet = data->right_alphabet; diff --git a/src/ui/components/trinary_input_string.c b/src/ui/components/trinary_input_string.c index 27269281ed..28af00d151 100644 --- a/src/ui/components/trinary_input_string.c +++ b/src/ui/components/trinary_input_string.c @@ -495,7 +495,6 @@ component_t* trinary_input_string_create( if (params->wordlist == NULL && !params->number_input) { data->keyboard_switch_component = keyboard_switch_create( - top_slider, params->special_chars, params->default_to_digits, component, diff --git a/src/ui/event.h b/src/ui/event.h index 1ea6f94cb2..cb1e3fd211 100644 --- a/src/ui/event.h +++ b/src/ui/event.h @@ -15,24 +15,27 @@ #ifndef _EVENT_H_ #define _EVENT_H_ +#include #include +typedef struct { + slider_location_t source; + int16_t diff; + uint16_t position; + int32_t velocity; +} event_slider_data_t; + enum { - EVENT_BOTTOM_SLIDE, - EVENT_TOP_SLIDE, - EVENT_BOTTOM_SLIDE_RELEASED, - EVENT_TOP_SLIDE_RELEASED, - EVENT_TOP_CONTINUOUS_TAP, - EVENT_BOTTOM_CONTINUOUS_TAP, - EVENT_TOP_LONG_TAP, - EVENT_BOTTOM_LONG_TAP, - EVENT_TOP_SHORT_TAP, - EVENT_BOTTOM_SHORT_TAP, + EVENT_SLIDE, + EVENT_SLIDE_RELEASED, + EVENT_CONTINUOUS_TAP, + EVENT_LONG_TAP, + EVENT_SHORT_TAP, }; typedef struct { - const void* data; uint8_t id; + event_slider_data_t data; } event_t; #endif diff --git a/src/ui/event_handler.c b/src/ui/event_handler.c index 1200463b8b..25039c2642 100644 --- a/src/ui/event_handler.c +++ b/src/ui/event_handler.c @@ -47,16 +47,12 @@ void emit_event(const event_t* event) // // If the screensaver is not active, we reset the timer with any touch interaction. switch (event->id) { - case EVENT_TOP_SHORT_TAP: - case EVENT_BOTTOM_SHORT_TAP: - case EVENT_BOTTOM_SLIDE_RELEASED: - case EVENT_TOP_SLIDE_RELEASED: + case EVENT_SHORT_TAP: + case EVENT_SLIDE_RELEASED: screen_saver_reset(); break; - case EVENT_TOP_SLIDE: - case EVENT_BOTTOM_SLIDE: - case EVENT_TOP_CONTINUOUS_TAP: - case EVENT_BOTTOM_CONTINUOUS_TAP: + case EVENT_SLIDE: + case EVENT_CONTINUOUS_TAP: if (screen_saver_get() == NULL) { screen_saver_reset(); } diff --git a/test/unit-test/test_ui_components.c b/test/unit-test/test_ui_components.c index 1cb9a1fafd..a3494f7062 100644 --- a/test/unit-test/test_ui_components.c +++ b/test/unit-test/test_ui_components.c @@ -142,7 +142,7 @@ static void test_ui_components_keyboard_switch(void** state) component_t* mock_component = fake_component_create(); component_t* keyboard_switch = - keyboard_switch_create(top_slider, true, false, mock_component, _ks_cb, NULL); + keyboard_switch_create(true, false, mock_component, _ks_cb, NULL); assert_non_null(keyboard_switch); assert_ui_component_functions(keyboard_switch); keyboard_switch->f->cleanup(keyboard_switch);