Skip to content

Commit

Permalink
feat(color): enhancements and fixes for Lua LVGL API (#5558)
Browse files Browse the repository at this point in the history
New:
line and triangle primitives
message popup dialog
expose buttons for for font, alignment, color, timer, switch and source pickers
generic popup dialog that can be filled with other things
lvgl.isAppMode() function to let widgets know when app mode is active
additional containers for creating layouts (box and setting)
flex layouts

Fixes:
colors are updated when theme changes
fix memory leaks when stand alone scripts close
run stand alone scripts in a separate Lua state and use lua_pcallk() to prevent cross boundary errors (color only)
  • Loading branch information
philmoz authored Sep 29, 2024
1 parent 471f87f commit c1c600c
Show file tree
Hide file tree
Showing 63 changed files with 2,123 additions and 1,180 deletions.
6 changes: 3 additions & 3 deletions radio/src/gui/colorlcd/controls/color_picker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ class ColorEditorPopup : public BaseDialog
}

public:
ColorEditorPopup(Window* parent, uint32_t color,
ColorEditorPopup(uint32_t color,
std::function<void(uint32_t)> _setValue) :
BaseDialog(parent, STR_COLOR_PICKER, false, COLOR_EDIT_WIDTH,
BaseDialog(STR_COLOR_PICKER, false, COLOR_EDIT_WIDTH,
LV_SIZE_CONTENT)
{
FlexGridLayout grid(col_dsc, row_dsc);
Expand Down Expand Up @@ -170,7 +170,7 @@ ColorPicker::ColorPicker(Window* parent, const rect_t& rect,

void ColorPicker::onClicked()
{
new ColorEditorPopup(this, getColor(), [=](uint32_t c) { setColor(c); });
new ColorEditorPopup(getColor(), [=](uint32_t c) { setColor(c); });
}

void ColorPicker::setColor(uint32_t c)
Expand Down
2 changes: 1 addition & 1 deletion radio/src/gui/colorlcd/controls/sourcechoice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ void SourceChoice::openMenu()
inverted = getIntValue() < 0;
inMenu = true;

auto menu = new Menu(this);
auto menu = new Menu();
if (menuTitle) menu->setTitle(menuTitle);

auto tb = new SourceChoiceMenuToolbar(this, menu);
Expand Down
2 changes: 1 addition & 1 deletion radio/src/gui/colorlcd/controls/switchchoice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void SwitchChoice::openMenu()
{
setEditMode(true); // this needs to be done first before menu is created.

auto menu = new Menu(this);
auto menu = new Menu();
if (menuTitle) menu->setTitle(menuTitle);

inverted = _getValue() < 0;
Expand Down
2 changes: 1 addition & 1 deletion radio/src/gui/colorlcd/libui/choice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ void Choice::openMenu()
{
setEditMode(true); // this needs to be done first before menu is created.

auto menu = new Menu(this);
auto menu = new Menu();
if (menuTitle) menu->setTitle(menuTitle);

fillMenu(menu);
Expand Down
35 changes: 18 additions & 17 deletions radio/src/gui/colorlcd/libui/dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,22 @@
class BaseDialogForm : public Window
{
public:
BaseDialogForm(Window* parent, lv_coord_t width) : Window(parent, rect_t{})
BaseDialogForm(Window* parent, lv_coord_t width, bool flexLayout) : Window(parent, rect_t{})
{
etx_scrollbar(lvobj);
padAll(PAD_MEDIUM);
setFlexLayout(LV_FLEX_FLOW_COLUMN, PAD_MEDIUM, width, LV_SIZE_CONTENT);
padAll(PAD_TINY);
if (flexLayout)
setFlexLayout(LV_FLEX_FLOW_COLUMN, PAD_ZERO, width, LV_SIZE_CONTENT);
}

protected:
void onClicked() override { Keyboard::hide(false); }
};

BaseDialog::BaseDialog(Window* parent, const char* title,
BaseDialog::BaseDialog(const char* title,
bool closeIfClickedOutside, lv_coord_t width,
lv_coord_t maxHeight) :
ModalWindow(parent, closeIfClickedOutside)
lv_coord_t maxHeight, bool flexLayout) :
ModalWindow(closeIfClickedOutside)
{
auto content = new Window(this, rect_t{});
content->setWindowFlag(OPAQUE);
Expand All @@ -56,7 +57,7 @@ BaseDialog::BaseDialog(Window* parent, const char* title,
header->padAll(PAD_SMALL);
header->show(title != nullptr);

form = new BaseDialogForm(content, width);
form = new BaseDialogForm(content, width, flexLayout);
if (maxHeight != LV_SIZE_CONTENT)
lv_obj_set_style_max_height(form->getLvObj(), maxHeight - EdgeTxStyles::UI_ELEMENT_HEIGHT, LV_PART_MAIN);
}
Expand All @@ -68,9 +69,9 @@ void BaseDialog::setTitle(const char* title)

//-----------------------------------------------------------------------------

ProgressDialog::ProgressDialog(Window* parent, const char* title,
ProgressDialog::ProgressDialog(const char* title,
std::function<void()> onClose) :
BaseDialog(parent, title, false), onClose(std::move(onClose))
BaseDialog(title, false), onClose(std::move(onClose))
{
progress = new Progress(form, rect_t{0, 0, LV_PCT(100), 32});
updateProgress(0);
Expand All @@ -96,10 +97,10 @@ void ProgressDialog::closeDialog()

//-----------------------------------------------------------------------------

MessageDialog::MessageDialog(Window* parent, const char* title,
MessageDialog::MessageDialog(const char* title,
const char* message, const char* info,
LcdFlags messageFlags, LcdFlags infoFlags) :
BaseDialog(parent, title, true)
BaseDialog(title, true)
{
messageWidget = new StaticText(form, {0, 0, LV_PCT(100), LV_SIZE_CONTENT},
message, COLOR_THEME_PRIMARY1_INDEX, messageFlags);
Expand All @@ -115,9 +116,9 @@ void MessageDialog::onClicked() { deleteLater(); }
//-----------------------------------------------------------------------------

DynamicMessageDialog::DynamicMessageDialog(
Window* parent, const char* title, std::function<std::string()> textHandler,
const char* title, std::function<std::string()> textHandler,
const char* message, const int lineHeight, LcdColorIndex color, LcdFlags textFlags) :
BaseDialog(parent, title, true)
BaseDialog(title, true)
{
messageWidget = new StaticText(form, {0, 0, LV_PCT(100), LV_SIZE_CONTENT},
message, COLOR_THEME_PRIMARY1_INDEX, CENTERED);
Expand All @@ -130,11 +131,11 @@ void DynamicMessageDialog::onClicked() { deleteLater(); }

//-----------------------------------------------------------------------------

ConfirmDialog::ConfirmDialog(Window* parent, const char* title,
ConfirmDialog::ConfirmDialog(const char* title,
const char* message,
std::function<void(void)> confirmHandler,
std::function<void(void)> cancelHandler) :
BaseDialog(parent, title, false),
BaseDialog(title, false),
confirmHandler(std::move(confirmHandler)),
cancelHandler(std::move(cancelHandler))
{
Expand Down Expand Up @@ -168,9 +169,9 @@ void ConfirmDialog::onCancel()

//-----------------------------------------------------------------------------

LabelDialog::LabelDialog(Window *parent, const char *label, int length, const char* title,
LabelDialog::LabelDialog(const char *label, int length, const char* title,
std::function<void(std::string)> _saveHandler) :
ModalWindow(parent, false), saveHandler(std::move(_saveHandler))
ModalWindow(false), saveHandler(std::move(_saveHandler))
{
assert(length <= MAX_LABEL_LENGTH);

Expand Down
16 changes: 8 additions & 8 deletions radio/src/gui/colorlcd/libui/dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ class Progress;
class BaseDialog : public ModalWindow
{
public:
BaseDialog(Window* parent, const char* title, bool closeIfClickedOutside,
BaseDialog(const char* title, bool closeIfClickedOutside,
lv_coord_t width = DIALOG_DEFAULT_WIDTH,
lv_coord_t maxHeight = DIALOG_DEFAULT_HEIGHT);
lv_coord_t maxHeight = DIALOG_DEFAULT_HEIGHT,
bool flexLayout = true);

void setTitle(const char* title);

Expand All @@ -52,8 +53,7 @@ class BaseDialog : public ModalWindow
class ProgressDialog : public BaseDialog
{
public:
ProgressDialog(Window* parent, const char* title,
std::function<void()> onClose);
ProgressDialog(const char* title, std::function<void()> onClose);

void updateProgress(int percentage);
void setTitle(std::string title);
Expand All @@ -74,7 +74,7 @@ class ProgressDialog : public BaseDialog
class MessageDialog : public BaseDialog
{
public:
MessageDialog(Window* parent, const char* title, const char* message,
MessageDialog(const char* title, const char* message,
const char* info = nullptr, LcdFlags messageFlags = CENTERED,
LcdFlags infoFlags = CENTERED);

Expand All @@ -94,7 +94,7 @@ class MessageDialog : public BaseDialog
class DynamicMessageDialog : public BaseDialog
{
public:
DynamicMessageDialog(Window* parent, const char* title,
DynamicMessageDialog(const char* title,
std::function<std::string()> textHandler,
const char* message = "",
const int lineHeight = EdgeTxStyles::PAGE_LINE_HEIGHT,
Expand All @@ -117,7 +117,7 @@ class DynamicMessageDialog : public BaseDialog
class ConfirmDialog : public BaseDialog
{
public:
ConfirmDialog(Window* parent, const char* title, const char* message,
ConfirmDialog(const char* title, const char* message,
std::function<void(void)> confirmHandler,
std::function<void(void)> cancelHandler = nullptr);

Expand All @@ -133,7 +133,7 @@ class ConfirmDialog : public BaseDialog
class LabelDialog : public ModalWindow
{
public:
LabelDialog(Window *parent, const char *label, int length, const char* title,
LabelDialog(const char *label, int length, const char* title,
std::function<void(std::string)> _saveHandler = nullptr);

static constexpr int MAX_LABEL_LENGTH = 255;
Expand Down
27 changes: 25 additions & 2 deletions radio/src/gui/colorlcd/libui/etx_lv_theme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ EdgeTxStyles::EdgeTxStyles()
lv_style_set_img_recolor_opa(&img_color[i], LV_OPA_COVER);
lv_style_init(&border_color[i]);
lv_style_init(&arc_color[i]);
lv_style_init(&line_color[i]);
}
lv_style_init(&outline_color_light);
lv_style_init(&outline_color_normal);
Expand Down Expand Up @@ -310,6 +311,7 @@ void EdgeTxStyles::applyColors()
lv_style_set_img_recolor(&img_color[i], c);
lv_style_set_border_color(&border_color[i], c);
lv_style_set_arc_color(&arc_color[i], c);
lv_style_set_line_color(&line_color[i], c);
}

lv_style_set_line_color(&graph_border, makeLvColor(COLOR_THEME_SECONDARY2));
Expand Down Expand Up @@ -466,12 +468,33 @@ void etx_arc_color(lv_obj_t* obj, LcdColorIndex colorIdx,
etx_obj_add_style(obj, styles->arc_color[colorIdx], selector);
}

void etx_img_color(lv_obj_t* obj, LcdColorIndex colorIdx,
lv_style_selector_t selector)
void etx_remove_line_color(lv_obj_t* obj, lv_style_selector_t selector)
{
// Remove styles
for (int i = 0; i < TOTAL_COLOR_COUNT; i += 1)
lv_obj_remove_style(obj, &styles->line_color[i], selector);
}

void etx_line_color(lv_obj_t* obj, LcdColorIndex colorIdx,
lv_style_selector_t selector)
{
// Remove old style first
etx_remove_line_color(obj, selector);
etx_obj_add_style(obj, styles->line_color[colorIdx], selector);
}

void etx_remove_img_color(lv_obj_t* obj, lv_style_selector_t selector)
{
// Remove styles
for (int i = 0; i < TOTAL_COLOR_COUNT; i += 1)
lv_obj_remove_style(obj, &styles->img_color[i], selector);
}

void etx_img_color(lv_obj_t* obj, LcdColorIndex colorIdx,
lv_style_selector_t selector)
{
// Remove old style first
etx_remove_img_color(obj, selector);
etx_obj_add_style(obj, styles->img_color[colorIdx], selector);
}

Expand Down
6 changes: 6 additions & 0 deletions radio/src/gui/colorlcd/libui/etx_lv_theme.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ void etx_remove_arc_color(lv_obj_t* obj, lv_style_selector_t selector = LV_PART_
void etx_arc_color(lv_obj_t* obj, LcdColorIndex colorIdx,
lv_style_selector_t selector = LV_PART_MAIN);

void etx_remove_line_color(lv_obj_t* obj, lv_style_selector_t selector = LV_PART_MAIN);
void etx_line_color(lv_obj_t* obj, LcdColorIndex colorIdx,
lv_style_selector_t selector = LV_PART_MAIN);

void etx_remove_img_color(lv_obj_t* obj, lv_style_selector_t selector = LV_PART_MAIN);
void etx_img_color(lv_obj_t* obj, LcdColorIndex colorIdx,
lv_style_selector_t selector = LV_PART_MAIN);

Expand Down Expand Up @@ -145,6 +150,7 @@ class EdgeTxStyles
lv_style_t img_color[TOTAL_COLOR_COUNT];
lv_style_t border_color[TOTAL_COLOR_COUNT];
lv_style_t arc_color[TOTAL_COLOR_COUNT];
lv_style_t line_color[TOTAL_COLOR_COUNT];
lv_style_t outline_color_light;
lv_style_t outline_color_normal;
lv_style_t outline_color_focus;
Expand Down
4 changes: 2 additions & 2 deletions radio/src/gui/colorlcd/libui/filechoice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ void FileChoice::openMenu()
if (fileCount > 0) {
setEditMode(true); // this needs to be done first before menu is created.

auto menu = new Menu(this);
auto menu = new Menu();
if (menuTitle) menu->setTitle(menuTitle);

auto tb = new FileChoiceMenuToolbar(this, menu);
Expand All @@ -192,6 +192,6 @@ void FileChoice::openMenu()

menu->setCloseHandler([=]() { setEditMode(false); });
} else {
new MessageDialog(this, STR_SDCARD, STR_NO_FILES_ON_SD);
new MessageDialog(STR_SDCARD, STR_NO_FILES_ON_SD);
}
}
4 changes: 2 additions & 2 deletions radio/src/gui/colorlcd/libui/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@ class MenuWindowContent : public Window

//-----------------------------------------------------------------------------

Menu::Menu(Window* parent, bool multiple) :
ModalWindow(parent, true),
Menu::Menu(bool multiple) :
ModalWindow(true),
multiple(multiple),
content(new MenuWindowContent(this))
{
Expand Down
2 changes: 1 addition & 1 deletion radio/src/gui/colorlcd/libui/menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Menu : public ModalWindow
friend class MenuBody;

public:
explicit Menu(Window *parent, bool multiple = false);
explicit Menu(bool multiple = false);

#if defined(DEBUG_WINDOWS)
std::string getName() const override { return "Menu"; }
Expand Down
5 changes: 3 additions & 2 deletions radio/src/gui/colorlcd/libui/modal_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "layer.h"
#include "etx_lv_theme.h"
#include "mainwindow.h"

// Modal overlay style (for dimming background)
void modal_window_constructor(const lv_obj_class_t* class_p, lv_obj_t* obj)
Expand All @@ -45,8 +46,8 @@ static lv_obj_t* modal_create(lv_obj_t* parent)
return etx_create(&modal_window_class, parent);
}

ModalWindow::ModalWindow(Window* parent, bool closeWhenClickOutside) :
Window(parent->getFullScreenWindow(), {0, 0, LCD_W, LCD_H}, modal_create),
ModalWindow::ModalWindow(bool closeWhenClickOutside) :
Window(MainWindow::instance(), {0, 0, LCD_W, LCD_H}, modal_create),
closeWhenClickOutside(closeWhenClickOutside)
{
Layer::push(this);
Expand Down
2 changes: 1 addition & 1 deletion radio/src/gui/colorlcd/libui/modal_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
class ModalWindow : public Window
{
public:
explicit ModalWindow(Window* parent, bool closeWhenClickOutside = false);
explicit ModalWindow(bool closeWhenClickOutside = false);

#if defined(DEBUG_WINDOWS)
std::string getName() const override { return "ModalWindow"; }
Expand Down
2 changes: 1 addition & 1 deletion radio/src/gui/colorlcd/libui/popups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static void _run_popup_dialog(const char* title, const char* msg,
// RELEASED/CLICKED to be called in a loop
lv_indev_reset(nullptr, nullptr);

auto md = new MessageDialog(MainWindow::instance(), title, msg, info);
auto md = new MessageDialog(title, msg, info);
md->setCloseHandler([&]() { running = false; });
while (running) {
// Allow power off while showing popup
Expand Down
2 changes: 1 addition & 1 deletion radio/src/gui/colorlcd/mainview/screen_setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class LayoutChoice : public Button

void onPress() override
{
auto menu = new Menu(parent);
auto menu = new Menu();
for (auto layout : LayoutFactory::getRegisteredLayouts()) {
menu->addLine(layout->getBitmap(), layout->getName(),
[=]() { setValue(layout); });
Expand Down
2 changes: 1 addition & 1 deletion radio/src/gui/colorlcd/mainview/view_about.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const std::string copyright_str = "Copyright (C) " BUILD_YEAR " EdgeTX";
const std::string edgetx_url = "https://edgetx.org";

AboutUs::AboutUs() :
BaseDialog(MainWindow::instance(), STR_ABOUT_US, true, 220, LV_SIZE_CONTENT)
BaseDialog(STR_ABOUT_US, true, 220, LV_SIZE_CONTENT)
{
new StaticText(form, {0, 0, LV_PCT(100), LV_SIZE_CONTENT},
about_str + "\n" + copyright_str,
Expand Down
2 changes: 1 addition & 1 deletion radio/src/gui/colorlcd/mainview/view_main_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ ViewMainMenu::ViewMainMenu(Window* parent, std::function<void()> closeHandler) :
carousel->addButton(
ICON_MODEL_TELEMETRY, STR_MAIN_MENU_RESET_TELEMETRY, [=]() -> uint8_t {
deleteLater();
Menu* resetMenu = new Menu(parent);
Menu* resetMenu = new Menu();
resetMenu->addLine(STR_RESET_FLIGHT, []() { flightReset(); });
resetMenu->addLine(STR_RESET_TIMER1, []() { timerReset(0); });
resetMenu->addLine(STR_RESET_TIMER2, []() { timerReset(1); });
Expand Down
6 changes: 1 addition & 5 deletions radio/src/gui/colorlcd/mainview/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,7 @@ void Widget::openMenu()
}

if (getOptions() || fsAllowed) {
// Widgets are placed on a full screen window which is underneath the main
// view menu bar Find the parent of this so that when the popup loads it
// covers the main view menu
Window* w = parent->getFullScreenWindow()->getParent();
Menu* menu = new Menu(w ? w : this);
Menu* menu = new Menu();
menu->setTitle(getFactory()->getDisplayName());
if (fsAllowed) {
menu->addLine(STR_WIDGET_FULLSCREEN, [&]() { setFullscreen(true); });
Expand Down
Loading

0 comments on commit c1c600c

Please sign in to comment.