feat(color): enhancements and fixes to the LVGL Lua API's (#6351)

This commit is contained in:
philmoz
2025-06-13 11:10:50 +10:00
committed by GitHub
parent 5c04801162
commit 3475978dd8
12 changed files with 194 additions and 77 deletions
+1 -1
View File
@@ -228,7 +228,7 @@ void Choice::openMenu()
{ {
setEditMode(true); // this needs to be done first before menu is created. setEditMode(true); // this needs to be done first before menu is created.
auto menu = new Menu(); auto menu = new Menu(false, popupWidth);
if (menuTitle) menu->setTitle(menuTitle); if (menuTitle) menu->setTitle(menuTitle);
fillMenu(menu); fillMenu(menu);
+3
View File
@@ -54,10 +54,13 @@ class ChoiceBase : public FormField
const char* getTitle() const { return menuTitle; } const char* getTitle() const { return menuTitle; }
void setPopupWidth(coord_t w) { popupWidth = w; }
static LAYOUT_VAL_SCALED(ICON_W, 18) static LAYOUT_VAL_SCALED(ICON_W, 18)
protected: protected:
lv_obj_t *label; lv_obj_t *label;
coord_t popupWidth = 0;
int vmin = 0; int vmin = 0;
int vmax = 0; int vmax = 0;
const char *menuTitle = nullptr; const char *menuTitle = nullptr;
+7 -5
View File
@@ -305,13 +305,15 @@ static lv_obj_t* menu_content_create(lv_obj_t* parent)
class MenuWindowContent : public Window class MenuWindowContent : public Window
{ {
public: public:
explicit MenuWindowContent(Menu* parent) : explicit MenuWindowContent(Menu* parent, coord_t popupWidth) :
Window(parent, rect_t{}, menu_content_create) Window(parent, rect_t{}, menu_content_create)
{ {
setWindowFlag(OPAQUE); setWindowFlag(OPAQUE);
coord_t w = (popupWidth > MENUS_WIDTH) ? popupWidth : MENUS_WIDTH;
lv_obj_center(lvobj); lv_obj_center(lvobj);
setFlexLayout(LV_FLEX_FLOW_COLUMN, PAD_ZERO, MENUS_WIDTH, LV_SIZE_CONTENT); setFlexLayout(LV_FLEX_FLOW_COLUMN, PAD_ZERO, w, LV_SIZE_CONTENT);
header = new StaticText(this, {0, 0, LV_PCT(100), 0}, "", header = new StaticText(this, {0, 0, LV_PCT(100), 0}, "",
COLOR_THEME_PRIMARY2_INDEX); COLOR_THEME_PRIMARY2_INDEX);
@@ -319,7 +321,7 @@ class MenuWindowContent : public Window
header->padAll(PAD_SMALL); header->padAll(PAD_SMALL);
header->hide(); header->hide();
body = new MenuBody(this, rect_t{0, 0, MENUS_WIDTH, LV_SIZE_CONTENT}); body = new MenuBody(this, rect_t{0, 0, w, LV_SIZE_CONTENT});
lv_obj_set_style_max_height(body->getLvObj(), LCD_H * 0.8, LV_PART_MAIN); lv_obj_set_style_max_height(body->getLvObj(), LCD_H * 0.8, LV_PART_MAIN);
} }
@@ -357,10 +359,10 @@ class MenuWindowContent : public Window
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
Menu::Menu(bool multiple) : Menu::Menu(bool multiple, coord_t popupWidth) :
ModalWindow(true), ModalWindow(true),
multiple(multiple), multiple(multiple),
content(new MenuWindowContent(this)) content(new MenuWindowContent(this, popupWidth))
{ {
} }
+1 -1
View File
@@ -29,7 +29,7 @@ class Menu : public ModalWindow
friend class MenuBody; friend class MenuBody;
public: public:
explicit Menu(bool multiple = false); explicit Menu(bool multiple = false, coord_t popupWidth = 0);
#if defined(DEBUG_WINDOWS) #if defined(DEBUG_WINDOWS)
std::string getName() const override { return "Menu"; } std::string getName() const override { return "Menu"; }
+7 -2
View File
@@ -59,15 +59,20 @@ void Window::eventHandler(lv_event_t *e)
switch (code) { switch (code) {
case LV_EVENT_SCROLL: { case LV_EVENT_SCROLL: {
lv_coord_t scroll_x = lv_obj_get_scroll_x(target);
lv_coord_t scroll_y = lv_obj_get_scroll_y(target);
if (scrollHandler) scrollHandler(scroll_x, scroll_y);
// exclude pointer based scrolling (only focus scrolling) // exclude pointer based scrolling (only focus scrolling)
if (!lv_obj_is_scrolling(target)) { if (!lv_obj_is_scrolling(target) && !noForcedScroll) {
lv_point_t *p = (lv_point_t *)lv_event_get_param(e); lv_point_t *p = (lv_point_t *)lv_event_get_param(e);
lv_coord_t scroll_bottom = lv_obj_get_scroll_bottom(target); lv_coord_t scroll_bottom = lv_obj_get_scroll_bottom(target);
lv_coord_t scroll_y = lv_obj_get_scroll_y(target);
TRACE("SCROLL[x=%d;y=%d;top=%d;bottom=%d]", p->x, p->y, scroll_y, TRACE("SCROLL[x=%d;y=%d;top=%d;bottom=%d]", p->x, p->y, scroll_y,
scroll_bottom); scroll_bottom);
// Force scroll to top or bottom when near either edge.
// Only applies when using rotary encoder or keys.
if (scroll_y <= 45 && p->y > 0) { if (scroll_y <= 45 && p->y > 0) {
lv_obj_scroll_by(target, 0, scroll_y, LV_ANIM_OFF); lv_obj_scroll_by(target, 0, scroll_y, LV_ANIM_OFF);
} else if (scroll_bottom <= 16 && p->y < 0) { } else if (scroll_bottom <= 16 && p->y < 0) {
+9 -2
View File
@@ -75,6 +75,9 @@ class Window
typedef std::function<void(bool)> FocusHandler; typedef std::function<void(bool)> FocusHandler;
void setFocusHandler(FocusHandler h) { focusHandler = std::move(h); } void setFocusHandler(FocusHandler h) { focusHandler = std::move(h); }
typedef std::function<void(coord_t, coord_t)> ScrollHandler;
void setScrollHandler(ScrollHandler h) { scrollHandler = std::move(h); }
virtual void clear(); virtual void clear();
virtual void deleteLater(bool detach = true, bool trash = true); virtual void deleteLater(bool detach = true, bool trash = true);
@@ -182,6 +185,8 @@ class Window
virtual void enable(bool enabled = true); virtual void enable(bool enabled = true);
void disable() { enable(false); } void disable() { enable(false); }
void disableForcedScroll() { noForcedScroll = true; }
protected: protected:
static std::list<Window *> trash; static std::list<Window *> trash;
@@ -194,12 +199,14 @@ class Window
WindowFlags windowFlags = 0; WindowFlags windowFlags = 0;
LcdFlags textFlags = 0; LcdFlags textFlags = 0;
bool noForcedScroll = false;
bool _deleted = false; bool _deleted = false;
static bool _longPressed; static bool _longPressed;
std::function<void()> closeHandler; CloseHandler closeHandler;
std::function<void(bool)> focusHandler; FocusHandler focusHandler;
std::function<void(coord_t, coord_t)> scrollHandler;
void deleteChildren(); void deleteChildren();
+3 -3
View File
@@ -143,9 +143,9 @@ class WidgetFactory
Widget::PersistentData* persistentData); Widget::PersistentData* persistentData);
protected: protected:
const char* name; const char* name = nullptr;
const char* displayName; const char* displayName = nullptr;
const ZoneOption* options; const ZoneOption* options = nullptr;
}; };
template <class T> template <class T>
-1
View File
@@ -433,7 +433,6 @@ LROT_BEGIN(lvgl_base_mt, NULL, LROT_MASK_GC_INDEX)
LROT_FUNCENTRY(set, luaLvglSet) LROT_FUNCENTRY(set, luaLvglSet)
LROT_FUNCENTRY(show, luaLvglShow) LROT_FUNCENTRY(show, luaLvglShow)
LROT_FUNCENTRY(hide, luaLvglHide) LROT_FUNCENTRY(hide, luaLvglHide)
LROT_FUNCENTRY(getScrollPos, luaLvglGetScrollPos)
LROT_END(lvgl_base_mt, NULL, LROT_MASK_GC_INDEX) LROT_END(lvgl_base_mt, NULL, LROT_MASK_GC_INDEX)
// Metatable for complex objects // Metatable for complex objects
+112 -38
View File
@@ -41,15 +41,12 @@ static void clearRef(lua_State *L, int& ref)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void LvglParamFuncOrValue::parse(lua_State *L, ParamType typ) void LvglParamFuncOrValue::parse(lua_State *L)
{ {
if (lua_isfunction(L, -1)) { if (lua_isfunction(L, -1)) {
function = luaL_ref(L, LUA_REGISTRYINDEX); function = luaL_ref(L, LUA_REGISTRYINDEX);
} else { } else {
if (typ == PTXT) value = luaL_checkunsigned(L, -1);
txt = luaL_checkstring(L, -1);
else
value = luaL_checkunsigned(L, -1);
} }
} }
@@ -63,17 +60,6 @@ bool LvglParamFuncOrValue::changedColor(LcdFlags newFlags)
return false; return false;
} }
bool LvglParamFuncOrValue::changedText(const char* s)
{
txt = s;
uint32_t h = hash(txt, strlen(txt));
if (h != currVal) {
currVal = h;
return true;
}
return false;
}
bool LvglParamFuncOrValue::changedValue(uint32_t v) bool LvglParamFuncOrValue::changedValue(uint32_t v)
{ {
value = v; value = v;
@@ -89,6 +75,31 @@ void LvglParamFuncOrValue::clearRef(lua_State *L)
::clearRef(L, function); ::clearRef(L, function);
} }
void LvglParamFuncOrString::parse(lua_State *L)
{
if (lua_isfunction(L, -1)) {
function = luaL_ref(L, LUA_REGISTRYINDEX);
} else {
txt = luaL_checkstring(L, -1);
}
}
bool LvglParamFuncOrString::changedText(const char* s)
{
txt = s;
uint32_t h = hash(chars(), txt.size());
if (h != txtHash) {
txtHash = h;
return true;
}
return false;
}
void LvglParamFuncOrString::clearRef(lua_State *L)
{
::clearRef(L, function);
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
bool LvglGetSetParams::parseGetSetParam(lua_State *L, const char *key) bool LvglGetSetParams::parseGetSetParam(lua_State *L, const char *key)
@@ -113,7 +124,7 @@ void LvglGetSetParams::clearGetSetRefs(lua_State *L)
bool LvglTextParams::parseTextParam(lua_State *L, const char *key) bool LvglTextParams::parseTextParam(lua_State *L, const char *key)
{ {
if (!strcmp(key, "text")) { if (!strcmp(key, "text")) {
txt.parse(L, LvglParamFuncOrValue::PTXT); txt.parse(L);
return true; return true;
} }
if (!strcmp(key, "font")) { if (!strcmp(key, "font")) {
@@ -189,6 +200,27 @@ bool LvglValuesParam::parseValuesParam(lua_State *L, const char *key)
return false; return false;
} }
bool LvglScrollableParams::parseScrollableParam(lua_State *L, const char *key)
{
if (!strcmp(key, "scrollBar")) {
showScrollBar = lua_toboolean(L, -1);
return true;
}
if (!strcmp(key, "scrollDir")) {
scrollDir = (lv_dir_t)luaL_checkunsigned(L, -1);
return true;
}
if (!strcmp(key, "scrollTo")) {
scrollToFunction = luaL_ref(L, LUA_REGISTRYINDEX);
return true;
}
if (!strcmp(key, "scrolled")) {
scrolledFunction = luaL_ref(L, LUA_REGISTRYINDEX);
return true;
}
return false;
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static int pcallcont (lua_State *L, int status, lua_KContext extra) { static int pcallcont (lua_State *L, int status, lua_KContext extra) {
@@ -214,6 +246,17 @@ static bool pcallFuncWithInt(lua_State *L, int funcRef, int nretval, int val)
return false; return false;
} }
static bool pcallFuncWith2Int(lua_State *L, int funcRef, int nretval, int val1, int val2)
{
if (funcRef != LUA_REFNIL) {
lua_rawgeti(L, LUA_REGISTRYINDEX, funcRef);
lua_pushinteger(L, val1);
lua_pushinteger(L, val2);
return lua_pcallk(L, 2, nretval, 0, 0, pcallcont) == LUA_OK;
}
return false;
}
static bool pcallFuncWithString(lua_State *L, int funcRef, int nretval, const char* val) static bool pcallFuncWithString(lua_State *L, int funcRef, int nretval, const char* val)
{ {
if (funcRef != LUA_REFNIL) { if (funcRef != LUA_REFNIL) {
@@ -696,7 +739,7 @@ void LvglWidgetLabel::clearRefs(lua_State *L)
void LvglWidgetLabel::setText(const char *s) void LvglWidgetLabel::setText(const char *s)
{ {
if (lvobj && txt.changedText(s)) if (lvobj && txt.changedText(s))
lv_label_set_text(lvobj, txt.txt); lv_label_set_text(lvobj, txt.chars());
} }
void LvglWidgetLabel::setColor(LcdFlags newColor) void LvglWidgetLabel::setColor(LcdFlags newColor)
@@ -731,7 +774,7 @@ void LvglWidgetLabel::build(lua_State *L)
lvobj = lv_label_create(lvglManager->getCurrentParent()->getLvObj()); lvobj = lv_label_create(lvglManager->getCurrentParent()->getLvObj());
setPos(x, y); setPos(x, y);
setSize(w, h); setSize(w, h);
setText(txt.txt); setText(txt.chars());
setColor(color.flags); setColor(color.flags);
setOpacity(opacity.value); setOpacity(opacity.value);
setFont(font.flags); setFont(font.flags);
@@ -869,7 +912,7 @@ uint32_t LvglWidgetLine::getPts(lua_State* L)
} }
for (size_t i = 0; i < ptCnt; i += 1) for (size_t i = 0; i < ptCnt; i += 1)
getPt(L, i); getPt(L, i);
return hash(pts, sizeof(ptCnt * sizeof(lv_point_t))); return hash(pts, ptCnt * sizeof(lv_point_t));
} else { } else {
ptCnt = 0; ptCnt = 0;
return -1; return -1;
@@ -1332,11 +1375,8 @@ void LvglWidgetObject::clearRefs(lua_State *L)
void LvglWidgetBox::parseParam(lua_State *L, const char *key) void LvglWidgetBox::parseParam(lua_State *L, const char *key)
{ {
if (!strcmp(key, "scrollDir")) { if (parseScrollableParam(L, key)) return;
scrollDir = (lv_dir_t)luaL_checkunsigned(L, -1); LvglWidgetObject::parseParam(L, key);
} else {
LvglWidgetObject::parseParam(L, key);
}
} }
coord_t LvglWidgetBox::getScrollX() coord_t LvglWidgetBox::getScrollX()
@@ -1349,16 +1389,34 @@ coord_t LvglWidgetBox::getScrollY()
return lv_obj_get_scroll_y(window->getLvObj()); return lv_obj_get_scroll_y(window->getLvObj());
} }
bool LvglWidgetBox::callRefs(lua_State *L)
{
if (!pcallUpdate2Int(L, scrollToFunction,
[=](int x, int y) { if (x != getScrollX() || y != getScrollY()) lv_obj_scroll_to(window->getLvObj(), x, y, LV_ANIM_OFF); })) {
return false;
}
return LvglWidgetObject::callRefs(L);
}
void LvglWidgetBox::clearRefs(lua_State *L)
{
clearRef(L, scrollToFunction);
LvglWidgetObject::clearRefs(L);
}
void LvglWidgetBox::build(lua_State *L) void LvglWidgetBox::build(lua_State *L)
{ {
window = window =
new Window(lvglManager->getCurrentParent(), {x, y, w, h}, lv_obj_create); new Window(lvglManager->getCurrentParent(), {x, y, w, h}, lv_obj_create);
window->disableForcedScroll();
window->setScrollHandler([=](coord_t x, coord_t y) { pcallFuncWith2Int(L, scrolledFunction, 0, x, y); });
lv_obj_add_flag(window->getLvObj(), LV_OBJ_FLAG_EVENT_BUBBLE); lv_obj_add_flag(window->getLvObj(), LV_OBJ_FLAG_EVENT_BUBBLE);
if (luaScriptManager->isWidget()) { if (luaScriptManager->isWidget()) {
lv_obj_clear_flag(window->getLvObj(), LV_OBJ_FLAG_CLICKABLE); lv_obj_clear_flag(window->getLvObj(), LV_OBJ_FLAG_CLICKABLE);
} else { } else {
lv_obj_set_scroll_dir(window->getLvObj(), scrollDir); lv_obj_set_scroll_dir(window->getLvObj(), scrollDir);
etx_scrollbar(window->getLvObj()); if (showScrollBar)
etx_scrollbar(window->getLvObj());
} }
if (setFlex()) if (setFlex())
lv_obj_set_flex_align(window->getLvObj(), LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_SPACE_AROUND); lv_obj_set_flex_align(window->getLvObj(), LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_SPACE_AROUND);
@@ -1637,7 +1695,7 @@ void LvglWidgetArc::build(lua_State *L)
void LvglWidgetImage::parseParam(lua_State *L, const char *key) void LvglWidgetImage::parseParam(lua_State *L, const char *key)
{ {
if (!strcmp(key, "file")) { if (!strcmp(key, "file")) {
filename.parse(L, LvglParamFuncOrValue::PTXT); filename.parse(L);
} else if (!strcmp(key, "fill")) { } else if (!strcmp(key, "fill")) {
fillFrame = lua_toboolean(L, -1); fillFrame = lua_toboolean(L, -1);
} else { } else {
@@ -1652,7 +1710,7 @@ bool LvglWidgetImage::callRefs(lua_State *L)
if (pcallFunc(L, filename.function, 1)) { if (pcallFunc(L, filename.function, 1)) {
const char *s = luaL_checkstring(L, -1); const char *s = luaL_checkstring(L, -1);
if (filename.changedText(s)) { if (filename.changedText(s)) {
((StaticImage*)window)->setSource(filename.txt); ((StaticImage*)window)->setSource(filename.chars());
} }
lua_settop(L, t); lua_settop(L, t);
} else { } else {
@@ -1671,7 +1729,7 @@ void LvglWidgetImage::clearRefs(lua_State *L)
void LvglWidgetImage::build(lua_State *L) void LvglWidgetImage::build(lua_State *L)
{ {
window = new StaticImage(lvglManager->getCurrentParent(), {x, y, w, h}, window = new StaticImage(lvglManager->getCurrentParent(), {x, y, w, h},
filename.txt, fillFrame); filename.chars(), fillFrame);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@@ -1805,7 +1863,7 @@ void LvglWidgetTextButton::build(lua_State *L)
{ {
if (h == LV_SIZE_CONTENT) h = 0; if (h == LV_SIZE_CONTENT) h = 0;
auto btn = auto btn =
new TextButton(lvglManager->getCurrentParent(), {x, y, w, h}, txt.txt, [=]() { new TextButton(lvglManager->getCurrentParent(), {x, y, w, h}, txt.chars(), [=]() {
return pcallGetOptIntVal(L, pressFunction, 0); return pcallGetOptIntVal(L, pressFunction, 0);
}); });
if (longPressFunction != LUA_REFNIL) { if (longPressFunction != LUA_REFNIL) {
@@ -1842,7 +1900,7 @@ void LvglWidgetMomentaryButton::build(lua_State *L)
{ {
if (h == LV_SIZE_CONTENT) h = 0; if (h == LV_SIZE_CONTENT) h = 0;
window = window =
new MomentaryButton(lvglManager->getCurrentParent(), {x, y, w, h}, txt.txt, new MomentaryButton(lvglManager->getCurrentParent(), {x, y, w, h}, txt.chars(),
[=]() { [=]() {
pcallSimpleFunc(L, pressFunction); pcallSimpleFunc(L, pressFunction);
}, },
@@ -2028,7 +2086,8 @@ class WidgetPage : public NavWindow, public LuaEventHandler
{ {
public: public:
WidgetPage(Window *parent, std::function<void()> backAction, WidgetPage(Window *parent, std::function<void()> backAction,
std::string title, std::string subtitle, std::string iconFile, lv_dir_t scrollDir) : std::string title, std::string subtitle, std::string iconFile,
lv_dir_t scrollDir, bool showScrollBar) :
NavWindow(parent, {0, 0, LCD_W, LCD_H}), backAction(std::move(backAction)) NavWindow(parent, {0, 0, LCD_W, LCD_H}), backAction(std::move(backAction))
{ {
if (iconFile.empty()) if (iconFile.empty())
@@ -2047,7 +2106,8 @@ class WidgetPage : public NavWindow, public LuaEventHandler
lv_obj_set_style_max_height(body->getLvObj(), LCD_H - EdgeTxStyles::MENU_HEADER_HEIGHT, lv_obj_set_style_max_height(body->getLvObj(), LCD_H - EdgeTxStyles::MENU_HEADER_HEIGHT,
LV_PART_MAIN); LV_PART_MAIN);
lv_obj_set_scroll_dir(body->getLvObj(), scrollDir); lv_obj_set_scroll_dir(body->getLvObj(), scrollDir);
etx_scrollbar(body->getLvObj()); if (showScrollBar)
etx_scrollbar(body->getLvObj());
#if defined(HARDWARE_TOUCH) #if defined(HARDWARE_TOUCH)
addBackButton(); addBackButton();
@@ -2079,14 +2139,13 @@ class WidgetPage : public NavWindow, public LuaEventHandler
void LvglWidgetPage::parseParam(lua_State *L, const char *key) void LvglWidgetPage::parseParam(lua_State *L, const char *key)
{ {
if (parseTitleParam(L, key)) return; if (parseTitleParam(L, key)) return;
if (parseScrollableParam(L, key)) return;
if (!strcmp(key, "back")) { if (!strcmp(key, "back")) {
backActionFunction = luaL_ref(L, LUA_REGISTRYINDEX); backActionFunction = luaL_ref(L, LUA_REGISTRYINDEX);
} else if (!strcmp(key, "subtitle")) { } else if (!strcmp(key, "subtitle")) {
subtitle = luaL_checkstring(L, -1); subtitle = luaL_checkstring(L, -1);
} else if (!strcmp(key, "icon")) { } else if (!strcmp(key, "icon")) {
iconFile = luaL_checkstring(L, -1); iconFile = luaL_checkstring(L, -1);
} else if (!strcmp(key, "scrollDir")) {
scrollDir = (lv_dir_t)luaL_checkunsigned(L, -1);
} else { } else {
LvglWidgetObject::parseParam(L, key); LvglWidgetObject::parseParam(L, key);
} }
@@ -2102,6 +2161,15 @@ coord_t LvglWidgetPage::getScrollY()
return lv_obj_get_scroll_y(window->getLvObj()); return lv_obj_get_scroll_y(window->getLvObj());
} }
bool LvglWidgetPage::callRefs(lua_State *L)
{
if (!pcallUpdate2Int(L, scrollToFunction,
[=](int x, int y) { if (x != getScrollX() || y != getScrollY()) lv_obj_scroll_to(window->getLvObj(), x, y, LV_ANIM_OFF); })) {
return false;
}
return LvglWidgetObject::callRefs(L);
}
void LvglWidgetPage::clearRefs(lua_State *L) void LvglWidgetPage::clearRefs(lua_State *L)
{ {
clearRef(L, backActionFunction); clearRef(L, backActionFunction);
@@ -2112,9 +2180,11 @@ void LvglWidgetPage::build(lua_State *L)
{ {
auto page = new WidgetPage( auto page = new WidgetPage(
lvglManager->getCurrentParent(), lvglManager->getCurrentParent(),
[=]() { pcallSimpleFunc(L, backActionFunction); }, title, subtitle, iconFile, scrollDir); [=]() { pcallSimpleFunc(L, backActionFunction); }, title, subtitle, iconFile, scrollDir, showScrollBar);
window = page->getBody(); window = page->getBody();
window->disableForcedScroll();
window->setScrollHandler([=](coord_t x, coord_t y) { pcallFuncWith2Int(L, scrolledFunction, 0, x, y); });
if (setFlex()) if (setFlex())
lv_obj_set_flex_align(window->getLvObj(), LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_SPACE_AROUND); lv_obj_set_flex_align(window->getLvObj(), LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_SPACE_AROUND);
} }
@@ -2221,7 +2291,7 @@ void LvglWidgetMessageDialog::parseParam(lua_State *L, const char *key)
void LvglWidgetMessageDialog::build(lua_State *L) void LvglWidgetMessageDialog::build(lua_State *L)
{ {
window = new MessageDialog(title.c_str(), message.c_str(), details); window = new MessageDialog(title.c_str(), message.c_str(), details.c_str());
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@@ -2247,6 +2317,8 @@ void LvglWidgetChoice::parseParam(lua_State *L, const char *key)
if (parseValuesParam(L, key)) return; if (parseValuesParam(L, key)) return;
if (!strcmp(key, "filter")) { if (!strcmp(key, "filter")) {
filterFunction = luaL_ref(L, LUA_REGISTRYINDEX); filterFunction = luaL_ref(L, LUA_REGISTRYINDEX);
} else if (!strcmp(key, "popupWidth")) {
popupWidth = luaL_checkinteger(L, -1);
} else { } else {
LvglWidgetPicker::parseParam(L, key); LvglWidgetPicker::parseParam(L, key);
} }
@@ -2266,6 +2338,8 @@ void LvglWidgetChoice::build(lua_State *L)
[=]() { return pcallGetIntVal(L, getFunction) - 1; }, [=]() { return pcallGetIntVal(L, getFunction) - 1; },
[=](int val) { pcallSetIntVal(L, setFunction, val + 1); }, title.c_str()); [=](int val) { pcallSetIntVal(L, setFunction, val + 1); }, title.c_str());
c->setPopupWidth(popupWidth);
if (filterFunction != LUA_REFNIL) if (filterFunction != LUA_REFNIL)
c->setAvailableHandler([=](int n) { c->setAvailableHandler([=](int n) {
bool rv = true; bool rv = true;
@@ -2451,7 +2525,7 @@ void LvglWidgetFilePicker::build(lua_State *L)
if (h == LV_SIZE_CONTENT) h = 0; if (h == LV_SIZE_CONTENT) h = 0;
auto c = new FileChoice( auto c = new FileChoice(
lvglManager->getCurrentParent(), {x, y, w, h}, lvglManager->getCurrentParent(), {x, y, w, h},
folder, extension, maxLen, folder.c_str(), extension.c_str(), maxLen,
[=]() { return pcallGetStringVal(L, getFunction); }, [=]() { return pcallGetStringVal(L, getFunction); },
[=](std::string val) { pcallSetStringVal(L, setFunction, val.c_str()); }, [=](std::string val) { pcallSetStringVal(L, setFunction, val.c_str()); },
hideExtension, title.c_str()); hideExtension, title.c_str());
+45 -20
View File
@@ -32,30 +32,39 @@ class LvglDialog;
struct LvglParamFuncOrValue struct LvglParamFuncOrValue
{ {
public: public:
enum ParamType {
PVALUE,
PTXT,
};
int function; int function;
union { union {
LcdFlags flags; LcdFlags flags;
coord_t coord; coord_t coord;
const char* txt;
uint32_t value; uint32_t value;
}; };
uint32_t currVal = -1; uint32_t currVal = -1;
void parse(lua_State *L, ParamType typ = PVALUE); void parse(lua_State *L);
void forceUpdate() { currVal = -1; } void forceUpdate() { currVal = -1; }
bool changedColor(LcdFlags newFlags); bool changedColor(LcdFlags newFlags);
bool changedFont(LcdFlags newFlags) { return changedColor(newFlags);} bool changedFont(LcdFlags newFlags) { return changedColor(newFlags);}
bool changedText(const char* s);
bool changedValue(uint32_t v); bool changedValue(uint32_t v);
void clearRef(lua_State *L); void clearRef(lua_State *L);
}; };
struct LvglParamFuncOrString
{
public:
int function;
std::string txt;
uint32_t txtHash = -1;
void parse(lua_State *L);
void forceUpdate() { txtHash = -1; }
bool changedText(const char* s);
const char* chars() const { return txt.c_str(); }
void clearRef(lua_State *L);
private:
};
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
class LvglGetSetParams class LvglGetSetParams
@@ -77,7 +86,7 @@ class LvglTextParams
LvglTextParams() {} LvglTextParams() {}
protected: protected:
LvglParamFuncOrValue txt = { .function = LUA_REFNIL, .txt = ""}; LvglParamFuncOrString txt = { .function = LUA_REFNIL, .txt = ""};
LvglParamFuncOrValue font = { .function = LUA_REFNIL, .flags = FONT(STD)}; LvglParamFuncOrValue font = { .function = LUA_REFNIL, .flags = FONT(STD)};
bool parseTextParam(lua_State *L, const char *key); bool parseTextParam(lua_State *L, const char *key);
@@ -151,6 +160,20 @@ class LvglValuesParam
bool parseValuesParam(lua_State *L, const char *key); bool parseValuesParam(lua_State *L, const char *key);
}; };
class LvglScrollableParams
{
public:
LvglScrollableParams() {}
protected:
bool showScrollBar = true;
lv_dir_t scrollDir = LV_DIR_ALL;
int scrollToFunction = LUA_REFNIL;
int scrolledFunction = LUA_REFNIL;
bool parseScrollableParam(lua_State *L, const char *key);
};
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
class LvglWidgetObjectBase class LvglWidgetObjectBase
@@ -265,7 +288,7 @@ class LvglWidgetLabel : public LvglSimpleWidgetObject, public LvglTextParams
void parseParam(lua_State *L, const char *key) override; void parseParam(lua_State *L, const char *key) override;
void refresh() override void refresh() override
{ {
setText(txt.txt); setText(txt.chars());
setFont(font.flags); setFont(font.flags);
setAlign(align.flags); setAlign(align.flags);
LvglSimpleWidgetObject::refresh(); LvglSimpleWidgetObject::refresh();
@@ -414,7 +437,7 @@ class LvglWidgetObject : public LvglWidgetObjectBase
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
class LvglWidgetBox : public LvglWidgetObject class LvglWidgetBox : public LvglWidgetObject, public LvglScrollableParams
{ {
public: public:
LvglWidgetBox() : LvglWidgetObject() {} LvglWidgetBox() : LvglWidgetObject() {}
@@ -422,9 +445,10 @@ class LvglWidgetBox : public LvglWidgetObject
coord_t getScrollX() override; coord_t getScrollX() override;
coord_t getScrollY() override; coord_t getScrollY() override;
protected: bool callRefs(lua_State *L) override;
lv_dir_t scrollDir = LV_DIR_ALL; void clearRefs(lua_State *L) override;
protected:
void build(lua_State *L) override; void build(lua_State *L) override;
void parseParam(lua_State *L, const char *key) override; void parseParam(lua_State *L, const char *key) override;
}; };
@@ -562,7 +586,7 @@ class LvglWidgetImage : public LvglWidgetObject
void clearRefs(lua_State *L) override; void clearRefs(lua_State *L) override;
protected: protected:
LvglParamFuncOrValue filename = { .function = LUA_REFNIL, .txt = ""}; LvglParamFuncOrString filename = { .function = LUA_REFNIL, .txt = ""};
bool fillFrame = false; bool fillFrame = false;
void build(lua_State *L) override; void build(lua_State *L) override;
@@ -612,7 +636,7 @@ class LvglWidgetTextButtonBase : public LvglWidgetObject, public LvglTextParams
void parseParam(lua_State *L, const char *key) override; void parseParam(lua_State *L, const char *key) override;
void refresh() override void refresh() override
{ {
setText(txt.txt); setText(txt.chars());
setFont(font.flags); setFont(font.flags);
setTextColor(textColor.flags); setTextColor(textColor.flags);
LvglWidgetObject::refresh(); LvglWidgetObject::refresh();
@@ -751,11 +775,12 @@ class LvglWidgetVerticalSlider : public LvglWidgetSliderBase
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
class LvglWidgetPage : public LvglWidgetObject, public LvglTitleParam class LvglWidgetPage : public LvglWidgetObject, public LvglTitleParam, public LvglScrollableParams
{ {
public: public:
LvglWidgetPage() : LvglWidgetObject() {} LvglWidgetPage() : LvglWidgetObject() {}
bool callRefs(lua_State *L) override;
void clearRefs(lua_State *L) override; void clearRefs(lua_State *L) override;
coord_t getScrollX() override; coord_t getScrollX() override;
@@ -764,7 +789,6 @@ class LvglWidgetPage : public LvglWidgetObject, public LvglTitleParam
protected: protected:
std::string subtitle; std::string subtitle;
std::string iconFile; std::string iconFile;
lv_dir_t scrollDir = LV_DIR_ALL;
int backActionFunction = LUA_REFNIL; int backActionFunction = LUA_REFNIL;
@@ -816,7 +840,7 @@ class LvglWidgetMessageDialog : public LvglWidgetObject, public LvglTitleParam,
LvglWidgetMessageDialog() : LvglWidgetObject(LVGL_SIMPLEMETATABLE) {} LvglWidgetMessageDialog() : LvglWidgetObject(LVGL_SIMPLEMETATABLE) {}
protected: protected:
const char *details = nullptr; std::string details;
void build(lua_State *L) override; void build(lua_State *L) override;
void parseParam(lua_State *L, const char *key) override; void parseParam(lua_State *L, const char *key) override;
@@ -846,6 +870,7 @@ class LvglWidgetChoice : public LvglWidgetPicker, public LvglTitleParam, public
protected: protected:
int filterFunction = LUA_REFNIL; int filterFunction = LUA_REFNIL;
coord_t popupWidth = 0;
void build(lua_State *L) override; void build(lua_State *L) override;
void parseParam(lua_State *L, const char *key) override; void parseParam(lua_State *L, const char *key) override;
@@ -943,8 +968,8 @@ class LvglWidgetFilePicker : public LvglWidgetPicker, public LvglTitleParam
LvglWidgetFilePicker() : LvglWidgetPicker() {} LvglWidgetFilePicker() : LvglWidgetPicker() {}
protected: protected:
const char* folder = nullptr; std::string folder;
const char* extension = nullptr; std::string extension;
int maxLen = 255; int maxLen = 255;
bool hideExtension = false; bool hideExtension = false;
+4 -3
View File
@@ -49,9 +49,8 @@ LuaWidgetFactory::LuaWidgetFactory(const char* name, ZoneOption* widgetOptions,
LuaWidgetFactory::~LuaWidgetFactory() { LuaWidgetFactory::~LuaWidgetFactory() {
unregisterWidget(this); unregisterWidget(this);
if (displayName) { if (name) delete name;
delete displayName; if (displayName) delete displayName;
}
auto option = getDefaultOptions(); auto option = getDefaultOptions();
while (option && option->name != nullptr) { while (option && option->name != nullptr) {
@@ -277,6 +276,7 @@ ZoneOption* LuaWidgetFactory::parseOptionDefinitions(int reference)
for (lua_pushnil(lsWidgets); lua_next(lsWidgets, -2); lua_pop(lsWidgets, 1)) { for (lua_pushnil(lsWidgets); lua_next(lsWidgets, -2); lua_pop(lsWidgets, 1)) {
count++; count++;
} }
lua_pop(lsWidgets, 1);
// TRACE("we have %d options", count); // TRACE("we have %d options", count);
if (count > MAX_WIDGET_OPTIONS) { if (count > MAX_WIDGET_OPTIONS) {
@@ -337,6 +337,7 @@ ZoneOption* LuaWidgetFactory::parseOptionDefinitions(int reference)
} }
option++; option++;
} }
lua_pop(lsWidgets, 1);
option->name = NULL; // sentinel option->name = NULL; // sentinel
} }
else else
+2 -1
View File
@@ -146,7 +146,7 @@ void luaLoadWidgetCallback(const char* filename)
if (name && createFunction) { if (name && createFunction) {
ZoneOption * options = LuaWidgetFactory::parseOptionDefinitions(optionDefinitionsReference); ZoneOption * options = LuaWidgetFactory::parseOptionDefinitions(optionDefinitionsReference);
if (options) { if (options) {
new LuaWidgetFactory(name, options, optionDefinitionsReference, new LuaWidgetFactory(strdup(name), options, optionDefinitionsReference,
createFunction, updateFunction, refreshFunction, backgroundFunction, createFunction, updateFunction, refreshFunction, backgroundFunction,
translateFunction, lvglLayout, filename); translateFunction, lvglLayout, filename);
TRACE("Loaded Lua widget %s", name); TRACE("Loaded Lua widget %s", name);
@@ -167,6 +167,7 @@ static void luaLoadFile(const char * filename, std::function<void()> callback)
if (luaLoadScriptFileToState(lsWidgets, filename, LUA_SCRIPT_LOAD_MODE) == SCRIPT_OK) { if (luaLoadScriptFileToState(lsWidgets, filename, LUA_SCRIPT_LOAD_MODE) == SCRIPT_OK) {
if (lua_pcall(lsWidgets, 0, 1, 0) == LUA_OK && lua_istable(lsWidgets, -1)) { if (lua_pcall(lsWidgets, 0, 1, 0) == LUA_OK && lua_istable(lsWidgets, -1)) {
callback(); callback();
lua_pop(lsWidgets, 1);
} }
else { else {
TRACE("luaLoadFile(%s): Error parsing script: %s", filename, lua_tostring(lsWidgets, -1)); TRACE("luaLoadFile(%s): Error parsing script: %s", filename, lua_tostring(lsWidgets, -1));