fix: ChoiceEx long press support re-added LVGL style (#65)

This commit is contained in:
eshifri
2022-09-20 13:29:55 +10:00
committed by GitHub
parent 5b32c73690
commit 7682db2fb2
2 changed files with 40 additions and 106 deletions
+15 -78
View File
@@ -20,18 +20,24 @@
*/ */
#include "choiceex.h" #include "choiceex.h"
constexpr int LONG_PRESS_10MS = 40; static void localLongPressHandler(lv_event_t* e)
extern inline tmr10ms_t getTicks()
{ {
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::set_lv_LongPressHandler(lvHandler_t longPressHandler, void* data)
void ChoiceEx::setLongPressHandler(std::function<void(event_t)> handler)
{ {
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, 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) : WindowFlags windowFlags) :
Choice(parent, rect, vmin, vmax, getValue, setValue, windowFlags) Choice(parent, rect, vmin, vmax, getValue, setValue, windowFlags)
{ {
#if defined(HARDWARE_TOUCH) longPressData.isLongPressed = false;
duration10ms = 0;
#endif
} }
#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
+25 -28
View File
@@ -22,41 +22,38 @@
#define _CHOICEEX_H_ #define _CHOICEEX_H_
#include "choice.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 class ChoiceEx : public Choice
{ {
public: public:
ChoiceEx(Window* parent, const rect_t& rect, int16_t vmin, int16_t vmax, ChoiceEx(Window* parent, const rect_t& rect, int16_t vmin, int16_t vmax,
std::function<int16_t()> getValue, std::function<int16_t()> getValue,
std::function<void(int16_t)> setValue = nullptr, std::function<void(int16_t)> setValue = nullptr,
WindowFlags windowFlags = 0); WindowFlags windowFlags = 0);
void setLongPressHandler(std::function<void(event_t)> handler); void set_lv_LongPressHandler(lvHandler_t longPressHandler, void* data);
#if defined(HARDWARE_KEYS) lv_eventData_t longPressData;
void onEvent(event_t event) override;
#endif
#if defined(HARDWARE_TOUCH) void onClicked() override
bool onTouchEnd(coord_t x, coord_t y) override; {
bool onTouchStart(coord_t x, coord_t y) override; if (!longPressData.isLongPressed) Choice::onClicked();
void checkEvents(void) override; }
bool isLongPress();
#endif
#if defined(DEBUG_WINDOWS) protected:
std::string getName() const override std::function<void(event_t)> longPressHandler = nullptr;
{
return "ChoceEx";
}
#endif
protected: static void ClickHandler(lv_event_t* e)
std::function<void(event_t)> longPressHandler = nullptr; {
ChoiceEx* ch = (ChoiceEx*)lv_event_get_user_data(e);
#if defined(HARDWARE_TOUCH) if (ch) ch->longPressData.isLongPressed = false;
uint32_t duration10ms; }
bool longPressed = false;
#endif
}; };
#endif #endif