From 79eee634fda5ae8d6680a834673ec0075aeb6927 Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Sun, 27 Jun 2021 08:48:55 +0200 Subject: [PATCH] [color] set focus on current model or close to the current operation Resolves #345 --- radio/src/gui/colorlcd/model_select.cpp | 22 +++++++++++++++---- radio/src/storage/modelslist.cpp | 29 +++++++++++++++++++++++++ radio/src/storage/modelslist.h | 3 +++ 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/radio/src/gui/colorlcd/model_select.cpp b/radio/src/gui/colorlcd/model_select.cpp index a9cdd89fb..909b2cc5c 100644 --- a/radio/src/gui/colorlcd/model_select.cpp +++ b/radio/src/gui/colorlcd/model_select.cpp @@ -210,14 +210,21 @@ class ModelCategoryPageBody : public FormWindow update(); } - void update(int selected = 0) + void update(int selected = -1) { clear(); + if (selected < 0) { + auto model = modelslist.getCurrentModel(); + selected = category->getModelIndex(model); + if (selected < 0) selected = 0; + } + int index = 0; coord_t y = MODEL_CELL_PADDING; coord_t x = MODEL_CELL_PADDING; + ModelButton* selectButton = nullptr; for (auto &model : *category) { auto button = new ModelButton( this, {x, y, MODEL_SELECT_CELL_WIDTH, MODEL_SELECT_CELL_HEIGHT}, @@ -237,7 +244,7 @@ class ModelCategoryPageBody : public FormWindow storageCheck(true); modelslist.setCurrentModel(model); - update(); // modelslist.getModelIndex(modelCell)); + update(); }); } menu->addLine(STR_CREATE_MODEL, getCreateModelAction()); @@ -264,7 +271,7 @@ class ModelCategoryPageBody : public FormWindow .c_str(), [=] { modelslist.removeModel(category, model); - update(index > 0 ? index - 1 : 0); + update(index < category->size() - 1 ? index : index - 1); }); }); } @@ -275,7 +282,7 @@ class ModelCategoryPageBody : public FormWindow }); if (selected == index) { - button->setFocus(SET_FOCUS_DEFAULT); + selectButton = button; } index++; @@ -299,6 +306,8 @@ class ModelCategoryPageBody : public FormWindow if (category->empty()) { setFocus(); + } else if (selectButton) { + selectButton->setFocus(); } } @@ -368,4 +377,9 @@ ModelSelectMenu::ModelSelectMenu(): for (auto category: modelslist.getCategories()) { addTab(new ModelCategoryPage(category)); } + + int idx = modelslist.getCurrentCategoryIdx(); + if (idx >= 0) { + setCurrentTab(idx); + } } diff --git a/radio/src/storage/modelslist.cpp b/radio/src/storage/modelslist.cpp index 1ba806284..f6cf61609 100644 --- a/radio/src/storage/modelslist.cpp +++ b/radio/src/storage/modelslist.cpp @@ -240,6 +240,19 @@ void ModelsCategory::moveModel(ModelCell * model, int8_t step) erase(current); } +int ModelsCategory::getModelIndex(const ModelCell* model) +{ + int idx = 0; + for (auto m : *this) { + if (model == m) + return idx; + + ++idx; + } + + return -1; +} + void ModelsCategory::save(FIL * file) { #if !defined(SDCARD_YAML) @@ -389,6 +402,22 @@ void ModelsList::setCurrentCategory(ModelsCategory * cat) currentCategory = cat; } +int ModelsList::getCurrentCategoryIdx() const +{ + if (!currentCategory) + return -1; + + int idx = 0; + for (auto cat : categories) { + if (currentCategory == cat) + return idx; + + ++idx; + } + + return -1; +} + void ModelsList::setCurrentModel(ModelCell * cell) { currentModel = cell; diff --git a/radio/src/storage/modelslist.h b/radio/src/storage/modelslist.h index f9bde8302..0991fc78e 100644 --- a/radio/src/storage/modelslist.h +++ b/radio/src/storage/modelslist.h @@ -80,6 +80,8 @@ public: void removeModel(ModelCell * model); void moveModel(ModelCell * model, int8_t step); + int getModelIndex(const ModelCell* model); + void save(FIL * file); }; @@ -118,6 +120,7 @@ public: { return currentCategory; } + int getCurrentCategoryIdx() const; void setCurrentModel(ModelCell * cell);