chore(color): LVGLify model template selection pages (#3076)

* LVGL flex conversion for model template selection pages.
* chore: Formatting
This commit is contained in:
philmoz
2023-01-27 21:44:25 +10:00
committed by GitHub
parent d53b94d14c
commit 1dc422592d
2 changed files with 182 additions and 189 deletions
+97 -86
View File
@@ -20,15 +20,46 @@
*/ */
#include "model_templates.h" #include "model_templates.h"
#include "standalone_lua.h" #include "standalone_lua.h"
#include "str_functions.h" #include "str_functions.h"
char TemplatePage::path[LEN_PATH + 1];
std::function<void(void)> TemplatePage::update = nullptr; std::function<void(void)> TemplatePage::update = nullptr;
static const lv_coord_t col_dsc[] = {LV_GRID_FR(1), LV_GRID_FR(1),
LV_GRID_TEMPLATE_LAST};
static const lv_coord_t row_dsc[] = {LV_GRID_CONTENT, LV_GRID_TEMPLATE_LAST};
TemplatePage::TemplatePage() : Page(ICON_MODEL_SELECT)
{
body.padAll(0);
auto form = new FormWindow(&body, rect_t{});
form->setFlexLayout();
form->padAll(4);
FlexGridLayout grid(col_dsc, row_dsc, 4);
auto line = form->newLine(&grid);
listWindow = new FormWindow(line, rect_t{});
listWindow->setFlexLayout(LV_FLEX_FLOW_COLUMN, 8);
listWindow->setHeight(body.height() - 16);
lv_obj_set_flex_align(listWindow->getLvObj(), LV_FLEX_ALIGN_START,
LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_SPACE_BETWEEN);
lv_obj_set_grid_cell(listWindow->getLvObj(), LV_GRID_ALIGN_STRETCH, 0, 1,
LV_GRID_ALIGN_START, 0, 1);
infoLabel = lv_label_create(line->getLvObj());
lv_obj_set_height(infoLabel, body.height() - 16);
lv_obj_set_style_text_align(infoLabel, LV_TEXT_ALIGN_LEFT, 0);
lv_obj_set_grid_cell(infoLabel, LV_GRID_ALIGN_STRETCH, 1, 1,
LV_GRID_ALIGN_CENTER, 0, 1);
}
void TemplatePage::updateInfo() void TemplatePage::updateInfo()
{ {
if (buffer[0]) {
FIL fp; FIL fp;
FRESULT res = f_open(&fp, buffer, FA_READ); FRESULT res = f_open(&fp, buffer, FA_READ);
unsigned int bytesRead = 0; unsigned int bytesRead = 0;
@@ -37,7 +68,17 @@ void TemplatePage::updateInfo()
f_close(&fp); f_close(&fp);
} }
infoText[bytesRead] = '\0'; infoText[bytesRead] = '\0';
invalidate(); }
if (infoText[0] == 0) {
lv_label_set_text(infoLabel, STR_NO_INFORMATION);
lv_obj_set_style_text_color(infoLabel, makeLvColor(COLOR_THEME_DISABLED),
0);
} else {
lv_label_set_text(infoLabel, infoText);
lv_obj_set_style_text_color(infoLabel, makeLvColor(COLOR_THEME_PRIMARY1),
0);
}
} }
#if defined(HARDWARE_KEYS) #if defined(HARDWARE_KEYS)
@@ -52,45 +93,32 @@ void TemplatePage::onEvent(event_t event)
} }
#endif #endif
void TemplatePage::paint(BitmapBuffer *dc) class SelectTemplate : public TemplatePage
{ {
Page::paint(dc); public:
rect_t rect = body.getRect(); SelectTemplate(TemplatePage* tp, std::string folder) : templateFolderPage(tp)
coord_t x = LCD_W / 2 + PAGE_PADDING;
coord_t y = rect.y + PAGE_PADDING;
coord_t w = LCD_W / 2 - 2 * PAGE_PADDING;
coord_t h = rect.h - 2 * PAGE_PADDING;
if (count > 0) {
if (infoText[0] == '\0')
drawTextLines(dc, x, y, w, h, STR_NO_INFORMATION, COLOR_THEME_DISABLED);
else
drawTextLines(dc, x, y, w, h, infoText, COLOR_THEME_PRIMARY1);
}
}
SelectTemplate::SelectTemplate(TemplatePage* tp)
: templateFolderPage(tp)
{ {
rect_t rect = {PAGE_TITLE_LEFT, PAGE_TITLE_TOP + 10, LCD_W - PAGE_TITLE_LEFT, PAGE_LINE_HEIGHT}; header.setTitle(STR_SELECT_TEMPLATE);
new StaticText(&header, rect, STR_SELECT_TEMPLATE, 0, COLOR_THEME_PRIMARY2);
char path[LEN_PATH + 1];
snprintf(path, LEN_PATH, "%s/%s", TEMPLATES_PATH, folder.c_str());
FormGridLayout grid;
grid.spacer(PAGE_PADDING);
std::list<std::string> files; std::list<std::string> files;
FILINFO fno; FILINFO fno;
DIR dir; DIR dir;
FRESULT res = f_opendir(&dir, path); FRESULT res = f_opendir(&dir, path);
Button* firstButton = nullptr;
if (res == FR_OK) { if (res == FR_OK) {
// read all entries // read all entries
for (;;) { for (;;) {
res = f_readdir(&dir, &fno); res = f_readdir(&dir, &fno);
if (res != FR_OK || fno.fname[0] == 0) if (res != FR_OK || fno.fname[0] == 0)
break; // Break on error or end of dir break; // Break on error or end of dir
if (strlen((const char*)fno.fname) > SD_SCREEN_FILE_LENGTH) if (strlen((const char*)fno.fname) > SD_SCREEN_FILE_LENGTH) continue;
continue; if (fno.fattrib & (AM_DIR | AM_HID | AM_SYS))
if (fno.fattrib & (AM_DIR|AM_HID|AM_SYS)) continue; /* Ignore folders, hidden and system files */ continue; /* Ignore folders, hidden and system files */
if (fno.fname[0] == '.') continue; /* Ignore UNIX hidden files */ if (fno.fname[0] == '.') continue; /* Ignore UNIX hidden files */
const char* ext = getFileExtension(fno.fname); const char* ext = getFileExtension(fno.fname);
@@ -107,7 +135,9 @@ SelectTemplate::SelectTemplate(TemplatePage* tp)
files.sort(compare_nocase); files.sort(compare_nocase);
for (auto name : files) { for (auto name : files) {
auto tb = new TemplateButton(&body, grid.getLabelSlot(), name, [=]() -> uint8_t { auto tb = new TextButton(
listWindow, rect_t{0, 0, lv_pct(100), PAGE_LINE_HEIGHT * 2}, name,
[=]() -> uint8_t {
// Read model template // Read model template
loadModelTemplate((name + YAML_EXT).c_str(), path); loadModelTemplate((name + YAML_EXT).c_str(), path);
storageDirty(EE_MODEL); storageDirty(EE_MODEL);
@@ -117,10 +147,12 @@ SelectTemplate::SelectTemplate(TemplatePage* tp)
templateFolderPage->deleteLater(); templateFolderPage->deleteLater();
#if defined(LUA) #if defined(LUA)
// If there is a wizard Lua script, fire it up // If there is a wizard Lua script, fire it up
snprintf(buffer, LEN_BUFFER, "%s%c%s%s", path, '/', name.c_str(), SCRIPT_EXT); snprintf(buffer, LEN_BUFFER, "%s/%s%s", path, name.c_str(),
SCRIPT_EXT);
if (f_stat(buffer, 0) == FR_OK) { if (f_stat(buffer, 0) == FR_OK) {
luaExec(buffer); luaExec(buffer);
// Need to update() the ModelCategoryPageBody before attaching StandaloneLuaWindow to not mess up focus // Need to update() the ModelCategoryPageBody before attaching
// StandaloneLuaWindow to not mess up focus
update(); update();
update = nullptr; update = nullptr;
StandaloneLuaWindow::instance()->attach(); StandaloneLuaWindow::instance()->attach();
@@ -130,68 +162,49 @@ SelectTemplate::SelectTemplate(TemplatePage* tp)
}); });
tb->setFocusHandler([=](bool active) { tb->setFocusHandler([=](bool active) {
if (active) { if (active) {
snprintf(buffer, LEN_BUFFER, "%s%c%s%s", path, '/', name.c_str(), TEXT_EXT); snprintf(buffer, LEN_BUFFER, "%s/%s%s", path, name.c_str(),
TEXT_EXT);
updateInfo(); updateInfo();
} }
}); });
tb->setHeight(PAGE_LINE_HEIGHT * 2);
grid.spacer(tb->height() + 5); if (!firstButton) firstButton = tb;
} }
} }
f_closedir(&dir); f_closedir(&dir);
count = files.size();
if (count == 0) {
rect_t rect = body.getRect();
rect.x = PAGE_PADDING;
rect.y = PAGE_PADDING;
rect.w = rect.w - (2 * PAGE_PADDING);
#if LCD_W > LCD_H if (files.size() == 0) {
rect.h = PAGE_LINE_HEIGHT; new StaticText(listWindow, rect_t{0, 0, lv_pct(100), lv_pct(50)},
int charBreak = 60; STR_NO_TEMPLATES, 0, COLOR_THEME_PRIMARY1);
#else
rect.h = PAGE_LINE_HEIGHT * 2;
int charBreak = 40;
#endif
new StaticText(&body, rect, wrap(STR_NO_TEMPLATES, charBreak), 0, COLOR_THEME_PRIMARY1);
// The following button is needed because the EXIT key does not work without...
rect = body.getRect();
rect.x = rect.w - PAGE_PADDING - 100;
rect.y = rect.h - PAGE_PADDING - (PAGE_LINE_HEIGHT * 2);
rect.w = 100;
rect.h = PAGE_LINE_HEIGHT * 2;
new TextButton(&body, rect, STR_EXIT, [=]() -> uint8_t { deleteLater(); return 0; });
} else { } else {
snprintf(buffer, LEN_BUFFER, "%s%c%s%s", path, '/', files.front().c_str(), TEXT_EXT); lv_group_focus_obj(firstButton->getLvObj());
updateInfo();
} }
} }
protected:
TemplatePage* templateFolderPage;
};
SelectTemplateFolder::SelectTemplateFolder(std::function<void(void)> update) SelectTemplateFolder::SelectTemplateFolder(std::function<void(void)> update)
{ {
TemplatePage::update = update; TemplatePage::update = update;
rect_t rect = {PAGE_TITLE_LEFT, PAGE_TITLE_TOP + 10, LCD_W - PAGE_TITLE_LEFT, PAGE_LINE_HEIGHT};
new StaticText(&header, rect, STR_SELECT_TEMPLATE_FOLDER, 0, COLOR_THEME_PRIMARY2);
FormGridLayout grid; header.setTitle(STR_SELECT_TEMPLATE_FOLDER);
grid.spacer(PAGE_PADDING);
auto tfb = new TemplateButton(&body, grid.getLabelSlot(), STR_BLANK_MODEL, [=]() -> uint8_t { auto tfb = new TextButton(listWindow,
rect_t{0, 0, lv_pct(100), PAGE_LINE_HEIGHT * 2},
STR_BLANK_MODEL, [=]() -> uint8_t {
deleteLater(); deleteLater();
return 0; return 0;
}); });
snprintf(infoText, LEN_INFO_TEXT, "%s", STR_BLANK_MODEL_INFO);
tfb->setFocusHandler([=](bool active) { tfb->setFocusHandler([=](bool active) {
if (active) { if (active) {
snprintf(infoText, LEN_INFO_TEXT, "%s", STR_BLANK_MODEL_INFO); buffer[0] = 0;
invalidate(); strcpy(infoText, STR_BLANK_MODEL_INFO);
updateInfo();
} }
}); });
tfb->setHeight(PAGE_LINE_HEIGHT * 2);
grid.spacer(tfb->height() + 5);
std::list<std::string> directories; std::list<std::string> directories;
FILINFO fno; FILINFO fno;
@@ -204,12 +217,11 @@ SelectTemplateFolder::SelectTemplateFolder(std::function<void(void)> update)
res = f_readdir(&dir, &fno); res = f_readdir(&dir, &fno);
if (res != FR_OK || fno.fname[0] == 0) if (res != FR_OK || fno.fname[0] == 0)
break; // Break on error or end of dir break; // Break on error or end of dir
if (strlen((const char*)fno.fname) > SD_SCREEN_FILE_LENGTH) if (strlen((const char*)fno.fname) > SD_SCREEN_FILE_LENGTH) continue;
continue; if (fno.fattrib & (AM_HID | AM_SYS))
if (fno.fattrib & (AM_HID|AM_SYS)) continue; /* Ignore hidden and system files */ continue; /* Ignore hidden and system files */
if (fno.fname[0] == '.') continue; /* Ignore UNIX hidden files */ if (fno.fname[0] == '.') continue; /* Ignore UNIX hidden files */
if (fno.fattrib & AM_DIR) if (fno.fattrib & AM_DIR) directories.push_back((char*)fno.fname);
directories.push_back((char*)fno.fname);
} }
directories.sort(compare_nocase); directories.sort(compare_nocase);
@@ -219,37 +231,36 @@ SelectTemplateFolder::SelectTemplateFolder(std::function<void(void)> update)
// Don't show wizards dir if no lua // Don't show wizards dir if no lua
if (!strcasecmp(name.c_str(), "WIZARD") == 0) { if (!strcasecmp(name.c_str(), "WIZARD") == 0) {
#endif #endif
auto tfb = new TemplateButton(&body, grid.getLabelSlot(), name, auto tfb = new TextButton(
listWindow, rect_t{0, 0, lv_pct(100), PAGE_LINE_HEIGHT * 2}, name,
[=]() -> uint8_t { [=]() -> uint8_t {
snprintf(path, LEN_PATH, "%s%c%s", TEMPLATES_PATH, '/', name.c_str()); new SelectTemplate(this, name);
new SelectTemplate(this);
return 0; return 0;
}); });
tfb->setFocusHandler([=](bool active) { tfb->setFocusHandler([=](bool active) {
if (active) { if (active) {
snprintf(buffer, LEN_BUFFER, "%s%c%s%c%s%s", TEMPLATES_PATH, '/', name.c_str(), '/', "about", TEXT_EXT); snprintf(buffer, LEN_BUFFER, "%s/%s/about%s", TEMPLATES_PATH,
name.c_str(), TEXT_EXT);
updateInfo(); updateInfo();
} }
}); });
tfb->setHeight(PAGE_LINE_HEIGHT * 2);
grid.spacer(tfb->height() + 5);
}
#if not defined(LUA) #if not defined(LUA)
} }
#endif #endif
} }
}
f_closedir(&dir); f_closedir(&dir);
count = directories.size();
if (count == 0) { if (directories.size() == 0) {
rect_t rect = grid.getLabelSlot(); new StaticText(listWindow, rect_t{0, 0, lv_pct(100), lv_pct(50)},
rect.w = body.getRect().w - 2 * PAGE_PADDING; STR_NO_TEMPLATES, 0, COLOR_THEME_PRIMARY1);
new StaticText(&body, rect, STR_NO_TEMPLATES, 0, COLOR_THEME_PRIMARY1);
} }
lv_group_focus_obj(tfb->getLvObj());
} }
SelectTemplateFolder::~SelectTemplateFolder() SelectTemplateFolder::~SelectTemplateFolder()
{ {
if (update) if (update) update();
update();
} }
+12 -30
View File
@@ -22,18 +22,13 @@
#pragma once #pragma once
#include <functional> #include <functional>
#include "opentx.h"
constexpr size_t LEN_INFO_TEXT = 300; #include "opentx.h"
constexpr size_t LEN_PATH = sizeof(TEMPLATES_PATH) + TEXT_FILENAME_MAXLEN;
constexpr size_t LEN_BUFFER = sizeof(TEMPLATES_PATH) + 2 * TEXT_FILENAME_MAXLEN + 1;
class TemplatePage : public Page class TemplatePage : public Page
{ {
public: public:
TemplatePage();
TemplatePage() : Page(ICON_MODEL_SELECT)
{ }
void updateInfo(); void updateInfo();
@@ -46,31 +41,18 @@ class TemplatePage : public Page
#endif #endif
protected: protected:
FormWindow* listWindow = nullptr;
lv_obj_t* infoLabel = nullptr;
static char path[LEN_PATH + 1]; static constexpr size_t LEN_INFO_TEXT = 300;
char buffer[LEN_BUFFER + 1]; static constexpr size_t LEN_PATH =
char infoText[LEN_INFO_TEXT + 1] = { 0 }; sizeof(TEMPLATES_PATH) + TEXT_FILENAME_MAXLEN;
unsigned int count = 0; static constexpr size_t LEN_BUFFER =
sizeof(TEMPLATES_PATH) + 2 * TEXT_FILENAME_MAXLEN + 1;
char buffer[LEN_BUFFER + 1] = "";
char infoText[LEN_INFO_TEXT + 1] = "";
static std::function<void(void)> update; static std::function<void(void)> update;
void paint(BitmapBuffer *dc) override;
};
class TemplateButton : public TextButton
{
public:
TemplateButton(FormGroup* parent, const rect_t& rect, std::string name, std::function<uint8_t(void)> pressHandler = nullptr)
: TextButton(parent, rect, name, pressHandler)
{ }
};
class SelectTemplate : public TemplatePage
{
public:
SelectTemplate(TemplatePage* tp);
protected:
TemplatePage* templateFolderPage;
}; };
class SelectTemplateFolder : public TemplatePage class SelectTemplateFolder : public TemplatePage