menu: some clean
This commit is contained in:
committed by
Raphael Coeffic
parent
12eccdf685
commit
bd3eb7a9ba
+48
-38
@@ -23,35 +23,35 @@
|
|||||||
|
|
||||||
#include <lvgl/lvgl.h>
|
#include <lvgl/lvgl.h>
|
||||||
|
|
||||||
void menuBodyEventCallback(lv_event_t *e)
|
static void menu_clicked(lv_event_t *e)
|
||||||
{
|
{
|
||||||
auto code = lv_event_get_code(e);
|
auto code = lv_event_get_code(e);
|
||||||
if (code == LV_EVENT_CLICKED) {
|
if (code == LV_EVENT_CLICKED) {
|
||||||
lv_obj_t *target = lv_event_get_target(e);
|
|
||||||
if (target != nullptr) {
|
lv_obj_t* target = lv_event_get_target(e);
|
||||||
long index = (long)lv_event_get_user_data(e);
|
if (!target) return;
|
||||||
MenuBody *mb = (MenuBody *) lv_obj_get_user_data(lv_obj_get_parent(target));
|
|
||||||
if (mb) {
|
lv_obj_t* parent = lv_obj_get_parent(target);
|
||||||
Menu *menu = mb->getParentMenu();
|
if (!parent) return;
|
||||||
if (index < (int)mb->lines.size()) {
|
|
||||||
onKeyPress();
|
MenuBody* mb = (MenuBody*)lv_obj_get_user_data(parent);
|
||||||
if (menu->multiple) {
|
if (!mb) return;
|
||||||
if (mb->selectedIndex == index)
|
|
||||||
mb->lines[index].onPress();
|
size_t idx = (size_t)lv_event_get_user_data(e);
|
||||||
else
|
mb->onPress(idx);
|
||||||
mb->setIndex(index);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
mb->setIndex(index);
|
|
||||||
mb->lines[index].onPress();
|
|
||||||
menu->deleteLater();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void add_list_button(lv_obj_t* list, const char* icon, const char* txt, size_t idx)
|
||||||
|
{
|
||||||
|
lv_obj_t* btn = lv_list_add_btn(list, icon, txt);
|
||||||
|
lv_obj_add_flag(btn, LV_OBJ_FLAG_CHECKABLE);
|
||||||
|
|
||||||
|
void* user_data = (void *)idx;
|
||||||
|
lv_obj_add_event_cb(btn, menu_clicked, LV_EVENT_CLICKED, user_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
MenuBody::MenuBody(Window * parent, const rect_t & rect):
|
MenuBody::MenuBody(Window * parent, const rect_t & rect):
|
||||||
Window(parent, rect, OPAQUE, 0, lv_list_create)
|
Window(parent, rect, OPAQUE, 0, lv_list_create)
|
||||||
{
|
{
|
||||||
@@ -62,10 +62,7 @@ void MenuBody::addLine(const std::string &text, std::function<void()> onPress,
|
|||||||
std::function<bool()> isChecked)
|
std::function<bool()> isChecked)
|
||||||
{
|
{
|
||||||
lines.emplace_back(std::move(onPress), std::move(isChecked), nullptr);
|
lines.emplace_back(std::move(onPress), std::move(isChecked), nullptr);
|
||||||
lv_obj_t *btn = lv_list_add_btn(lvobj, nullptr, text.c_str());
|
add_list_button(lvobj, nullptr, text.c_str(), lines.size() - 1);
|
||||||
lv_obj_add_flag(btn, LV_OBJ_FLAG_CHECKABLE);
|
|
||||||
lv_obj_add_event_cb(btn, menuBodyEventCallback, LV_EVENT_CLICKED,
|
|
||||||
(void *)(lines.size() - 1));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MenuBody::addLine(const uint8_t *icon_mask, const std::string &text,
|
void MenuBody::addLine(const uint8_t *icon_mask, const std::string &text,
|
||||||
@@ -80,12 +77,8 @@ void MenuBody::addLine(const uint8_t *icon_mask, const std::string &text,
|
|||||||
void* buf = (void*)(icon_mask + 4);
|
void* buf = (void*)(icon_mask + 4);
|
||||||
lv_canvas_set_buffer(canvas, buf, w, h, LV_IMG_CF_ALPHA_8BIT);
|
lv_canvas_set_buffer(canvas, buf, w, h, LV_IMG_CF_ALPHA_8BIT);
|
||||||
|
|
||||||
lv_obj_t *btn = lv_list_add_btn(
|
const char* icon = (const char *)lv_canvas_get_img(canvas);
|
||||||
lvobj, (const char *)lv_canvas_get_img(canvas), text.c_str());
|
add_list_button(lvobj, icon, text.c_str(), lines.size() - 1);
|
||||||
|
|
||||||
lv_obj_add_flag(btn, LV_OBJ_FLAG_CHECKABLE);
|
|
||||||
lv_obj_add_event_cb(btn, menuBodyEventCallback, LV_EVENT_CLICKED,
|
|
||||||
(void *)(lines.size() - 1));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MenuBody::removeLines()
|
void MenuBody::removeLines()
|
||||||
@@ -106,16 +99,33 @@ coord_t MenuBody::getContentHeight()
|
|||||||
return h;
|
return h;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MenuBody::onPress(size_t index)
|
||||||
|
{
|
||||||
|
Menu *menu = getParentMenu();
|
||||||
|
if (index < lines.size()) {
|
||||||
|
onKeyPress();
|
||||||
|
if (menu->multiple) {
|
||||||
|
if (selectedIndex == index)
|
||||||
|
lines[index].onPress();
|
||||||
|
else
|
||||||
|
setIndex(index);
|
||||||
|
} else {
|
||||||
|
setIndex(index);
|
||||||
|
lines[index].onPress();
|
||||||
|
menu->deleteLater();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ensure index is in range and also handle wrapping index
|
// ensure index is in range and also handle wrapping index
|
||||||
int MenuBody::rangeCheck(int index)
|
int MenuBody::rangeCheck(int index)
|
||||||
{
|
{
|
||||||
if (index < 0)
|
if (index < 0)
|
||||||
index = lines.size() - 1;
|
index = lines.size() - 1;
|
||||||
else if (index > (signed)lines.size() - 1)
|
else if (index > (signed)lines.size() - 1)
|
||||||
index = 0;
|
index = 0;
|
||||||
|
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MenuBody::setIndex(int index)
|
void MenuBody::setIndex(int index)
|
||||||
|
|||||||
+2
-1
@@ -62,7 +62,6 @@ class MenuBody: public Window
|
|||||||
MenuLine(MenuLine &&) = default;
|
MenuLine(MenuLine &&) = default;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
friend void menuBodyEventCallback(lv_event_t *e);
|
|
||||||
bool isSeparator = false;
|
bool isSeparator = false;
|
||||||
std::function<void()> onPress;
|
std::function<void()> onPress;
|
||||||
std::function<bool()> isChecked;
|
std::function<bool()> isChecked;
|
||||||
@@ -121,6 +120,8 @@ class MenuBody: public Window
|
|||||||
|
|
||||||
coord_t getContentHeight();
|
coord_t getContentHeight();
|
||||||
|
|
||||||
|
void onPress(size_t index);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
friend void menuBodyEventCallback(lv_event_t *);
|
friend void menuBodyEventCallback(lv_event_t *);
|
||||||
void selectNext(MENU_DIRECTION direction);
|
void selectNext(MENU_DIRECTION direction);
|
||||||
|
|||||||
Reference in New Issue
Block a user