make button lvgl-Event aware

This commit is contained in:
gagarinlg
2022-06-11 15:13:11 +02:00
committed by Raphael Coeffic
parent f99c16c7aa
commit 9d45303531
3 changed files with 38 additions and 7 deletions
+35
View File
@@ -22,6 +22,26 @@
#include "font.h"
#include "theme.h"
static void lvglEvent(lv_event_t* e)
{
Button* btn = (Button*)lv_event_get_user_data(e);
if(btn->deleted())
return;
btn->onEvent(e);
}
Button::Button(FormGroup* parent, const rect_t& rect,
std::function<uint8_t(void)> pressHandler,
WindowFlags windowFlag, LcdFlags textFlags,
LvglCreate objConstruct) :
FormField(parent, rect, windowFlag, textFlags, objConstruct),
pressHandler(std::move(pressHandler))
{
lv_obj_set_style_bg_opa(lvobj, LV_OPA_TRANSP, LV_PART_MAIN);
lv_obj_add_event_cb(lvobj, lvglEvent, LV_EVENT_CLICKED, this);
}
void Button::onPress()
{
bool check = (pressHandler && pressHandler());
@@ -51,6 +71,21 @@ void Button::onEvent(event_t event)
}
#endif
void Button::onEvent(lv_event_t* event)
{
lv_event_code_t code = lv_event_get_code(event);
TRACE_WINDOWS("%s received lvgl event code 0x%X", getWindowDebugString("Button").c_str(), code);
if (enabled && code == LV_EVENT_CLICKED) {
onKeyPress();
onPress();
}
else {
// FormField::onEvent(event);
}
}
#if defined(HARDWARE_TOUCH)
bool Button::onTouchStart(coord_t x, coord_t y)
{
+2 -6
View File
@@ -31,12 +31,7 @@ class Button: public FormField
Button(FormGroup* parent, const rect_t& rect,
std::function<uint8_t(void)> pressHandler = nullptr,
WindowFlags windowFlags = 0, LcdFlags textFlags = 0,
LvglCreate objConstruct = nullptr) :
FormField(parent, rect, windowFlags, textFlags, objConstruct),
pressHandler(std::move(pressHandler))
{
lv_obj_set_style_bg_opa(lvobj, LV_OPA_TRANSP, LV_PART_MAIN);
}
LvglCreate objConstruct = nullptr);
#if defined(DEBUG_WINDOWS)
std::string getName() const override
@@ -74,6 +69,7 @@ class Button: public FormField
#if defined(HARDWARE_KEYS)
void onEvent(event_t event) override;
#endif
void onEvent(lv_event_t* e);// override;
#if defined(HARDWARE_TOUCH)
bool onTouchStart(coord_t x, coord_t y) override;
+1 -1
View File
@@ -290,7 +290,7 @@ void Window::setFocus(uint8_t flag, Window * from)
if (focusWindow != this) {
// synchronize lvgl focused state with libopenui
if (!lv_obj_has_state(lvobj, LV_STATE_FOCUSED)) {
if (lvobj != nullptr && !lv_obj_has_state(lvobj, LV_STATE_FOCUSED)) {
lv_obj_add_state(lvobj, LV_STATE_FOCUSED);
if (focusWindow != nullptr) {
lv_obj_clear_state(focusWindow->lvobj, LV_STATE_FOCUSED | LV_STATE_EDITED);