diff --git a/radio/src/gui/colorlcd/model_select.cpp b/radio/src/gui/colorlcd/model_select.cpp index c7de043b9..de19bfe3b 100644 --- a/radio/src/gui/colorlcd/model_select.cpp +++ b/radio/src/gui/colorlcd/model_select.cpp @@ -707,21 +707,44 @@ void ModelLabelsWindow::newModel() storageFlushCurrentModel(); storageCheck(true); - // Create a new blank ModelCell and activate it first, createmodel() will modify - // the model in memory. - auto newCell = modelslist.addModel("", false); - modelslist.setCurrentModel(newCell); + new SelectTemplateFolder([=](std::string folder, std::string name) { + // Create a new blank ModelCell and activate it first, createmodel() will modify + // the model in memory. + auto newCell = modelslist.addModel("", false); + modelslist.setCurrentModel(newCell); - // Make the new model - createModel(); - - new SelectTemplateFolder([=]() { - // On complete update the current cell's data - modelslist.updateCurrentModelCell(); + // Make the new model + createModel(); // Close Window auto w = Layer::back(); if (w) w->onCancel(); + + // Check for not 'Blank Model' + if (name.size() > 0) + { + static constexpr size_t LEN_BUFFER = sizeof(TEMPLATES_PATH) + 2 * TEXT_FILENAME_MAXLEN + 1; + + char path[LEN_BUFFER + 1]; + snprintf(path, LEN_BUFFER, "%s/%s", TEMPLATES_PATH, folder.c_str()); + + // Read model template + loadModelTemplate((name + YAML_EXT).c_str(), path); + storageFlushCurrentModel(); + storageCheck(true); + + // Update the current cell's data + modelslist.updateCurrentModelCell(); + +#if defined(LUA) + // If there is a wizard Lua script, fire it up + snprintf(path, LEN_BUFFER, "%s/%s%s", path, name.c_str(), SCRIPT_EXT); + if (f_stat(path, 0) == FR_OK) { + luaExec(path); + StandaloneLuaWindow::instance()->attach(); + } +#endif + } }); } diff --git a/radio/src/gui/colorlcd/model_templates.cpp b/radio/src/gui/colorlcd/model_templates.cpp index 8ceaab339..b3a8f749e 100644 --- a/radio/src/gui/colorlcd/model_templates.cpp +++ b/radio/src/gui/colorlcd/model_templates.cpp @@ -24,8 +24,6 @@ #include "standalone_lua.h" #include "str_functions.h" -std::function 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}; @@ -96,7 +94,7 @@ void TemplatePage::onEvent(event_t event) class SelectTemplate : public TemplatePage { public: - SelectTemplate(TemplatePage* tp, std::string folder) : templateFolderPage(tp) + SelectTemplate(SelectTemplateFolder* tp, std::string folder) : templateFolderPage(tp) { header.setTitle(STR_SELECT_TEMPLATE); @@ -138,26 +136,8 @@ class SelectTemplate : public TemplatePage auto tb = new TextButton( listWindow, rect_t{0, 0, lv_pct(100), PAGE_LINE_HEIGHT * 2}, name, [=]() -> uint8_t { - // Read model template - loadModelTemplate((name + YAML_EXT).c_str(), path); - storageDirty(EE_MODEL); - storageCheck(true); - // Dismiss template pages deleteLater(); - templateFolderPage->deleteLater(); -#if defined(LUA) - // If there is a wizard Lua script, fire it up - snprintf(buffer, LEN_BUFFER, "%s/%s%s", path, name.c_str(), - SCRIPT_EXT); - if (f_stat(buffer, 0) == FR_OK) { - luaExec(buffer); - // Need to update() the ModelCategoryPageBody before attaching - // StandaloneLuaWindow to not mess up focus - update(); - update = nullptr; - StandaloneLuaWindow::instance()->attach(); - } -#endif + templateFolderPage->doUpdate(folder, name); return 0; }); tb->setFocusHandler([=](bool active) { @@ -183,19 +163,19 @@ class SelectTemplate : public TemplatePage } protected: - TemplatePage* templateFolderPage; + SelectTemplateFolder* templateFolderPage; }; -SelectTemplateFolder::SelectTemplateFolder(std::function update) +SelectTemplateFolder::SelectTemplateFolder(std::function update) { - TemplatePage::update = update; + this->update = update; header.setTitle(STR_SELECT_TEMPLATE_FOLDER); auto tfb = new TextButton(listWindow, rect_t{0, 0, lv_pct(100), PAGE_LINE_HEIGHT * 2}, STR_BLANK_MODEL, [=]() -> uint8_t { - deleteLater(); + doUpdate("", ""); return 0; }); tfb->setFocusHandler([=](bool active) { @@ -259,8 +239,3 @@ SelectTemplateFolder::SelectTemplateFolder(std::function update) lv_group_focus_obj(tfb->getLvObj()); } - -SelectTemplateFolder::~SelectTemplateFolder() -{ - if (update) update(); -} diff --git a/radio/src/gui/colorlcd/model_templates.h b/radio/src/gui/colorlcd/model_templates.h index c545aed56..c91677cf2 100644 --- a/radio/src/gui/colorlcd/model_templates.h +++ b/radio/src/gui/colorlcd/model_templates.h @@ -27,37 +27,42 @@ class TemplatePage : public Page { - public: - TemplatePage(); + public: + TemplatePage(); - void updateInfo(); + void updateInfo(); #if defined(HARDWARE_KEYS) - void onEvent(event_t event) override; + void onEvent(event_t event) override; #endif #if defined(DEBUG_WINDOWS) - std::string getName() const { return "TemplatePage"; } + std::string getName() const { return "TemplatePage"; } #endif - protected: - FormWindow* listWindow = nullptr; - lv_obj_t* infoLabel = nullptr; + protected: + FormWindow* listWindow = nullptr; + lv_obj_t* infoLabel = nullptr; - static constexpr size_t LEN_INFO_TEXT = 300; - static constexpr size_t LEN_PATH = - sizeof(TEMPLATES_PATH) + TEXT_FILENAME_MAXLEN; - static constexpr size_t LEN_BUFFER = - sizeof(TEMPLATES_PATH) + 2 * TEXT_FILENAME_MAXLEN + 1; + static constexpr size_t LEN_INFO_TEXT = 300; + static constexpr size_t LEN_PATH = sizeof(TEMPLATES_PATH) + TEXT_FILENAME_MAXLEN; + 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 update; + char buffer[LEN_BUFFER + 1] = ""; + char infoText[LEN_INFO_TEXT + 1] = ""; }; class SelectTemplateFolder : public TemplatePage { public: - SelectTemplateFolder(std::function update); - ~SelectTemplateFolder(); + SelectTemplateFolder(std::function update); + + void doUpdate(std::string folder, std::string name) { + deleteLater(); + if (update) + update(folder, name); + } + + protected: + std::function update; };