From 96946890673ad638dcd5d70e7458c2ce4a5a095f Mon Sep 17 00:00:00 2001 From: Kevin Koenig Date: Sun, 5 Dec 2021 07:50:52 -0500 Subject: [PATCH] [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 Co-authored-by: Peter Feerick --- radio/src/gui/colorlcd/radio_tools.cpp | 88 ++++++++++++++++---------- radio/src/gui/colorlcd/radio_tools.h | 19 +++--- 2 files changed, 66 insertions(+), 41 deletions(-) diff --git a/radio/src/gui/colorlcd/radio_tools.cpp b/radio/src/gui/colorlcd/radio_tools.cpp index b815eeba0..db1d4d486 100644 --- a/radio/src/gui/colorlcd/radio_tools.cpp +++ b/radio/src/gui/colorlcd/radio_tools.cpp @@ -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 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 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 diff --git a/radio/src/gui/colorlcd/radio_tools.h b/radio/src/gui/colorlcd/radio_tools.h index 804df18c0..bef8cf5b5 100644 --- a/radio/src/gui/colorlcd/radio_tools.h +++ b/radio/src/gui/colorlcd/radio_tools.h @@ -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; };