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"
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<void(event_t)> 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
+25 -28
View File
@@ -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<int16_t()> getValue,
std::function<void(int16_t)> setValue = nullptr,
WindowFlags windowFlags = 0);
public:
ChoiceEx(Window* parent, const rect_t& rect, int16_t vmin, int16_t vmax,
std::function<int16_t()> getValue,
std::function<void(int16_t)> setValue = nullptr,
WindowFlags windowFlags = 0);
void setLongPressHandler(std::function<void(event_t)> 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<void(event_t)> longPressHandler = nullptr;
protected:
std::function<void(event_t)> 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