From 7682db2fb22a1d49debaa6a562b408f9fe61762b Mon Sep 17 00:00:00 2001 From: eshifri <46511024+eshifri@users.noreply.github.com> Date: Mon, 19 Sep 2022 20:29:55 -0700 Subject: [PATCH] fix: ChoiceEx long press support re-added LVGL style (#65) --- .../src/thirdparty/libopenui/src/choiceex.cpp | 93 +++---------------- radio/src/thirdparty/libopenui/src/choiceex.h | 53 +++++------ 2 files changed, 40 insertions(+), 106 deletions(-) diff --git a/radio/src/thirdparty/libopenui/src/choiceex.cpp b/radio/src/thirdparty/libopenui/src/choiceex.cpp index f1986f904..bd7d7c113 100644 --- a/radio/src/thirdparty/libopenui/src/choiceex.cpp +++ b/radio/src/thirdparty/libopenui/src/choiceex.cpp @@ -20,18 +20,24 @@ */ #include "choiceex.h" -constexpr int LONG_PRESS_10MS = 40; - -extern inline tmr10ms_t getTicks() +static void localLongPressHandler(lv_event_t* e) { - return g_tmr10ms; + lv_eventData_t* ld = (lv_eventData_t*)lv_event_get_user_data(e); + ld->isLongPressed = true; + ld->lv_LongPressHandler(ld->userData); } - - -void ChoiceEx::setLongPressHandler(std::function handler) +void ChoiceEx::set_lv_LongPressHandler(lvHandler_t longPressHandler, void* data) { - longPressHandler = handler; + TRACE("longPressHandler=%p", longPressHandler); + + if (longPressHandler) { + longPressData.userData = data; + longPressData.lv_LongPressHandler = longPressHandler; + lv_obj_add_event_cb(lvobj, localLongPressHandler, LV_EVENT_LONG_PRESSED, + &longPressData); + lv_obj_add_event_cb(lvobj, ClickHandler, LV_EVENT_CLICKED, this); + } } ChoiceEx::ChoiceEx(Window* parent, const rect_t& rect, int16_t vmin, @@ -40,74 +46,5 @@ ChoiceEx::ChoiceEx(Window* parent, const rect_t& rect, int16_t vmin, WindowFlags windowFlags) : Choice(parent, rect, vmin, vmax, getValue, setValue, windowFlags) { -#if defined(HARDWARE_TOUCH) - duration10ms = 0; -#endif + longPressData.isLongPressed = false; } - -#if defined(HARDWARE_KEYS) -void ChoiceEx::onEvent(event_t event) -{ - if (event == EVT_KEY_LONG(KEY_ENTER)) { - if (longPressHandler) { - killEvents(event); - longPressHandler(event); - return; - } - } - - Choice::onEvent(event); -} -#endif - -#if defined(HARDWARE_TOUCH) -bool ChoiceEx::isLongPress() -{ - unsigned int curTimer = getTicks(); - return (!longPressed && duration10ms != 0 && curTimer - duration10ms > LONG_PRESS_10MS); -} - -void ChoiceEx::checkEvents(void) -{ - event_t event = getEvent(); - - if (isLongPress()) { - if (!longPressed && longPressHandler) { - longPressHandler(event); - killEvents(event); - duration10ms = 0; - longPressed = true; - return; - } - } - - if (hasFocus()) - onEvent(event); - else - pushEvent(event); -} - -bool ChoiceEx::onTouchStart(coord_t x, coord_t y) -{ - if (!longPressed && duration10ms == 0) { - duration10ms = getTicks(); - } - - return Choice::onTouchStart(x, y); -} - - -bool ChoiceEx::onTouchEnd(coord_t x, coord_t y) -{ - if (longPressed) { - longPressed = false; - return false; - } - - duration10ms = 0; - return Choice::onTouchEnd(x,y); -} -#endif - - - diff --git a/radio/src/thirdparty/libopenui/src/choiceex.h b/radio/src/thirdparty/libopenui/src/choiceex.h index 4d1348153..dcd21b750 100644 --- a/radio/src/thirdparty/libopenui/src/choiceex.h +++ b/radio/src/thirdparty/libopenui/src/choiceex.h @@ -22,41 +22,38 @@ #define _CHOICEEX_H_ #include "choice.h" +typedef void (*lvHandler_t)(void*); +typedef struct { + void* userData; + bool isLongPressed; + lvHandler_t lv_LongPressHandler; +} lv_eventData_t; + class ChoiceEx : public Choice { - public: - ChoiceEx(Window* parent, const rect_t& rect, int16_t vmin, int16_t vmax, - std::function getValue, - std::function setValue = nullptr, - WindowFlags windowFlags = 0); + public: + ChoiceEx(Window* parent, const rect_t& rect, int16_t vmin, int16_t vmax, + std::function getValue, + std::function setValue = nullptr, + WindowFlags windowFlags = 0); - void setLongPressHandler(std::function handler); + void set_lv_LongPressHandler(lvHandler_t longPressHandler, void* data); -#if defined(HARDWARE_KEYS) - void onEvent(event_t event) override; -#endif + lv_eventData_t longPressData; -#if defined(HARDWARE_TOUCH) - bool onTouchEnd(coord_t x, coord_t y) override; - bool onTouchStart(coord_t x, coord_t y) override; - void checkEvents(void) override; - bool isLongPress(); -#endif + void onClicked() override + { + if (!longPressData.isLongPressed) Choice::onClicked(); + } -#if defined(DEBUG_WINDOWS) - std::string getName() const override - { - return "ChoceEx"; - } -#endif + protected: + std::function longPressHandler = nullptr; -protected: - std::function longPressHandler = nullptr; - -#if defined(HARDWARE_TOUCH) - uint32_t duration10ms; - bool longPressed = false; -#endif + static void ClickHandler(lv_event_t* e) + { + ChoiceEx* ch = (ChoiceEx*)lv_event_get_user_data(e); + if (ch) ch->longPressData.isLongPressed = false; + } }; #endif