feat(color): additions/fixes for Lua LVGL API (#6294)

This commit is contained in:
philmoz
2025-05-26 10:35:53 +10:00
committed by GitHub
parent 936a66bcd8
commit 7977e8e694
7 changed files with 431 additions and 275 deletions
+21 -10
View File
@@ -71,12 +71,18 @@ ChoiceBase::ChoiceBase(Window* parent, const rect_t& rect,
etx_font(label, FONT_XS_INDEX, LV_STATE_USER_1);
}
void ChoiceBase::checkEvents()
{
update();
Window::checkEvents();
}
std::string Choice::getLabelText()
{
std::string text;
if (_getValue) {
int val = _getValue();
if (currentValue != INT_MAX) {
int val = currentValue;
if (textHandler) {
text = textHandler(val);
} else {
@@ -94,15 +100,20 @@ std::string Choice::getLabelText()
void ChoiceBase::update()
{
if (!deleted()) {
if (width() > 0) {
int w = width() - (type == CHOICE_TYPE_DROPOWN ? ICON_W - 2 : ICON_W) - PAD_TINY * 3;
if (getTextWidth(getLabelText().c_str(), 0, FONT(STD)) > w)
lv_obj_add_state(label, LV_STATE_USER_1);
else
lv_obj_clear_state(label, LV_STATE_USER_1);
if (!deleted() && _getValue) {
int v = _getValue();
if (v != currentValue) {
currentValue = v;
std::string s = getLabelText();
if (width() > 0) {
int w = width() - (type == CHOICE_TYPE_DROPOWN ? ICON_W - 2 : ICON_W) - PAD_TINY * 3;
if (getTextWidth(s.c_str(), 0, FONT(STD)) > w)
lv_obj_add_state(label, LV_STATE_USER_1);
else
lv_obj_clear_state(label, LV_STATE_USER_1);
}
lv_label_set_text(label, s.c_str());
}
lv_label_set_text(label, getLabelText().c_str());
}
}
+4
View File
@@ -19,6 +19,7 @@
#pragma once
#include <vector>
#include <climits>
#include "form.h"
@@ -60,11 +61,14 @@ class ChoiceBase : public FormField
int vmax = 0;
const char *menuTitle = nullptr;
ChoiceType type;
int currentValue = INT_MAX;
std::function<int()> _getValue;
std::function<void(int)> _setValue;
std::function<std::string(int)> textHandler;
virtual std::string getLabelText() = 0;
void checkEvents() override;
};
class Choice : public ChoiceBase
+1 -2
View File
@@ -99,12 +99,11 @@ FileChoice::FileChoice(Window *parent, const rect_t &rect, std::string folder,
bool stripExtension, const char *title) :
Choice(
parent, rect, 0, 0, [=]() { return selectedIdx; },
[=](int val) { setValue(getString(val)); }, title, CHOICE_TYPE_FOLDER),
[=](int val) { setValue(getString(val)); selectedIdx = val; }, title, CHOICE_TYPE_FOLDER),
folder(std::move(folder)),
extension(extension),
maxlen(maxlen),
getValue(std::move(getValue)),
setValue(std::move(setValue)),
stripExtension(stripExtension)
{
update();
@@ -45,7 +45,6 @@ protected:
const char* extension;
int maxlen;
std::function<std::string()> getValue;
std::function<void(std::string)> setValue;
bool stripExtension;
void loadFiles();