[colorlcd] Sort Lua scripts before building UI in radio tools (#1150)

* sort lua scripts before building ui in radio tools

* Formatting

* Case insensitive sorting

Co-authored-by: kkoenig1@optonline.com <kkoenig1@optonline.com>
Co-authored-by: Peter Feerick <peter.feerick@gmail.com>
This commit is contained in:
Kevin Koenig
2021-12-05 23:50:52 +11:00
committed by GitHub
co-authored by kkoenig1@optonline.com <kkoenig1@optonline.com> Peter Feerick
parent 71dd6bca89
commit 9694689067
2 changed files with 66 additions and 41 deletions
+56 -32
View File
@@ -72,6 +72,56 @@ void RadioToolsPage::checkEvents()
PageTab::checkEvents();
}
struct LuaScript
{
std::string path;
std::string label;
bool operator < (const LuaScript &a) { return label < a.label; }
};
inline bool LuaScript_compare_nocase(LuaScript first, LuaScript second)
{
return strcasecmp(first.label.c_str(), second.label.c_str()) < 0;
}
void buildLuaUi(std::vector<LuaScript> luaScripts, FormWindow *window, FormGridLayout &grid)
{
for (auto luaScript : luaScripts) {
auto txt = new StaticText(window, grid.getLabelSlot(), "lua",
BUTTON_BACKGROUND, COLOR_THEME_PRIMARY1 | CENTERED);
auto b = new TextButton(
window, grid.getFieldSlot(1), luaScript.label,
[window, luaScript]() -> uint8_t {
char toolPath[FF_MAX_LFN + 1];
strncpy(toolPath, luaScript.path.c_str(), sizeof(toolPath)-1);
*((char *)getBasename(toolPath)-1) = '\0';
f_chdir(toolPath);
luaExec(luaScript.path.c_str());
StandaloneLuaWindow::instance()->attach(window);
return 0;
},
OPAQUE);
b->setBgColorHandler([=]() {
return COLOR_THEME_PRIMARY2;
});
b->setFocusHandler([=](bool focus) {
if (focus) {
txt->setBackgroundColor(COLOR_THEME_FOCUS);
txt->setTextFlags(COLOR_THEME_PRIMARY2 | CENTERED);
} else {
txt->setBackgroundColor(COLOR_THEME_SECONDARY2);
txt->setTextFlags(COLOR_THEME_PRIMARY1 | CENTERED);
}
txt->invalidate();
});
grid.nextLine();
}
}
void RadioToolsPage::rebuild(FormWindow * window)
{
FormGridLayout grid;
@@ -93,6 +143,8 @@ void RadioToolsPage::rebuild(FormWindow * window)
FRESULT res = f_opendir(&dir, SCRIPTS_TOOLS_PATH);
if (res == FR_OK) {
std::vector<LuaScript> luaScripts; // gather them all and then create UI after sort
for (;;) {
TCHAR path[FF_MAX_LFN+1] = SCRIPTS_TOOLS_PATH "/";
res = f_readdir(&dir, &fno); /* Read a directory item */
@@ -113,41 +165,13 @@ void RadioToolsPage::rebuild(FormWindow * window)
*ext = '\0';
label = getBasename(path);
}
auto txt = new StaticText(window, grid.getLabelSlot(), "lua",
BUTTON_BACKGROUND, COLOR_THEME_PRIMARY1 | CENTERED);
std::string path_str(path);
auto b = new TextButton(
window, grid.getFieldSlot(1), label,
[window, path_str]() -> uint8_t {
char toolPath[FF_MAX_LFN + 1];
strncpy(toolPath, path_str.c_str(), sizeof(toolPath)-1);
*((char *)getBasename(toolPath)-1) = '\0';
f_chdir(toolPath);
luaExec(path_str.c_str());
StandaloneLuaWindow::instance()->attach(window);
return 0;
},
OPAQUE);
b->setBgColorHandler([=]() {
return COLOR_THEME_PRIMARY2;
});
b->setFocusHandler([=](bool focus) {
if (focus) {
txt->setBackgroundColor(COLOR_THEME_FOCUS);
txt->setTextFlags(COLOR_THEME_PRIMARY2 | CENTERED);
} else {
txt->setBackgroundColor(COLOR_THEME_SECONDARY2);
txt->setTextFlags(COLOR_THEME_PRIMARY1 | CENTERED);
}
txt->invalidate();
});
grid.nextLine();
luaScripts.emplace_back(LuaScript{ path, label });
}
}
std::sort(luaScripts.begin(), luaScripts.end(), LuaScript_compare_nocase);
buildLuaUi(luaScripts, window, grid);
}
#endif
+10 -9
View File
@@ -21,16 +21,17 @@
#include "tabsgroup.h"
class RadioToolsPage: public PageTab {
public:
RadioToolsPage();
class RadioToolsPage : public PageTab
{
public:
RadioToolsPage();
void build(FormWindow * window) override;
void build(FormWindow* window) override;
void checkEvents() override;
void checkEvents() override;
protected:
static void rebuild(FormWindow * window);
FormWindow * window = nullptr;
uint8_t waiting = 0;
protected:
static void rebuild(FormWindow* window);
FormWindow* window = nullptr;
uint8_t waiting = 0;
};