From cf7fa3faead8edd5813a27d13d3b4feef3527cf0 Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Tue, 31 May 2022 12:26:44 +0200 Subject: [PATCH] theme editor: various fixes --- radio/src/gui/colorlcd/file_carosell.cpp | 4 +- radio/src/gui/colorlcd/fullscreen_dialog.cpp | 7 ++- radio/src/gui/colorlcd/listbox.cpp | 7 ++- radio/src/gui/colorlcd/listbox.h | 1 + radio/src/gui/colorlcd/radio_theme.cpp | 59 ++++++++------------ radio/src/gui/colorlcd/theme_manager.cpp | 2 +- radio/src/gui/colorlcd/theme_manager.h | 15 +++-- 7 files changed, 47 insertions(+), 48 deletions(-) diff --git a/radio/src/gui/colorlcd/file_carosell.cpp b/radio/src/gui/colorlcd/file_carosell.cpp index 8122036fd..4a527817f 100644 --- a/radio/src/gui/colorlcd/file_carosell.cpp +++ b/radio/src/gui/colorlcd/file_carosell.cpp @@ -64,7 +64,9 @@ void FileCarosell::setSelected(int n) void FileCarosell::paint(BitmapBuffer *dc) { if (selected == -1 || _fileNames.size() == 0) { - const char *message = selected == -1 && _fileNames.size() > 0 ? "Loading..." : "No theme image"; + const char *message = selected == -1 && _fileNames.size() > 0 + ? "Loading..." + : "No theme image"; dc->drawText(width() / 2, height() / 2, message, FONT(L) + CENTERED); } } diff --git a/radio/src/gui/colorlcd/fullscreen_dialog.cpp b/radio/src/gui/colorlcd/fullscreen_dialog.cpp index 051b5aa87..a6568f698 100644 --- a/radio/src/gui/colorlcd/fullscreen_dialog.cpp +++ b/radio/src/gui/colorlcd/fullscreen_dialog.cpp @@ -100,7 +100,7 @@ void FullScreenDialog::paint(BitmapBuffer * dc) } else if (type == WARNING_TYPE_CONFIRM) { LcdFlags flags = FONT(BOLD); - int w = getTextWidth(STR_OK, flags); + int w = getTextWidth(STR_OK, 0, flags); dc->drawText(ALERT_MESSAGE_LEFT, ALERT_ACTION_TOP, STR_OK, flags); @@ -158,6 +158,11 @@ void FullScreenDialog::runForever() #if defined(HARDWARE_TOUCH) mainWin->setTouchEnabled(true); #endif + + // reset input devices to avoid + // RELEASED/CLICKED to be called in a loop + lv_indev_reset(nullptr, nullptr); + while (running) { resetBacklightTimeout(); diff --git a/radio/src/gui/colorlcd/listbox.cpp b/radio/src/gui/colorlcd/listbox.cpp index 9ad3beed8..8fc1c506d 100644 --- a/radio/src/gui/colorlcd/listbox.cpp +++ b/radio/src/gui/colorlcd/listbox.cpp @@ -81,13 +81,18 @@ void ListBase::setAutoEdit(bool enable) } } +void ListBase::setName(uint16_t idx, const std::string& name) +{ + lv_table_set_cell_value(lvobj, idx, 0, name.c_str()); +} + void ListBase::setNames(const std::vector& names) { setRowCount(names.size()); uint16_t row = 0; for (const auto& name: names) { - lv_table_set_cell_value(lvobj, row, 0, name.c_str()); + setName(row, name); row++; } } diff --git a/radio/src/gui/colorlcd/listbox.h b/radio/src/gui/colorlcd/listbox.h index 11ffcbbc9..c53698705 100644 --- a/radio/src/gui/colorlcd/listbox.h +++ b/radio/src/gui/colorlcd/listbox.h @@ -44,6 +44,7 @@ class ListBase : public TableField bool getAutoEdit() const { return autoEdit; } void setAutoEdit(bool enable); + void setName(uint16_t idx, const std::string& name); void setNames(const std::vector& names); void setLineHeight(uint8_t height); diff --git a/radio/src/gui/colorlcd/radio_theme.cpp b/radio/src/gui/colorlcd/radio_theme.cpp index 16978afaa..bc0a753c9 100644 --- a/radio/src/gui/colorlcd/radio_theme.cpp +++ b/radio/src/gui/colorlcd/radio_theme.cpp @@ -457,45 +457,32 @@ void ThemeSetupPage::displayThemeMenu(Window *window, ThemePersistance *tp) // you cant edit the default theme if (listBox->getSelected() != 0) { - menu->addLine(STR_EDIT, - [=] () { - auto theme = tp->getThemeByIndex(currentTheme); - if (theme == nullptr) return; + menu->addLine(STR_EDIT, [=]() { + auto themeIdx = listBox->getSelected(); + if (themeIdx < 0) return; + + auto theme = tp->getThemeByIndex(themeIdx); + if (theme == nullptr) return; + + new ThemeEditPage(*theme, [=](ThemeFile &theme) { + theme.serialize(); - new ThemeEditPage(*theme, - [=](ThemeFile &theme) { - auto curTheme = tp->getThemeByIndex(currentTheme); - if (curTheme != nullptr) { - curTheme->setName(theme.getName()); - curTheme->setAuthor(theme.getAuthor()); - curTheme->setInfo(theme.getInfo()); + // if the theme info currently displayed + // were changed, update the UI + if (themeIdx == currentTheme) { + setAuthor(&theme); + nameText->setText(theme.getName()); + listBox->setName(currentTheme, theme.getName()); + } - // update the colors that were edited - int n = 0; - for (auto color : theme.getColorList()) { - curTheme->setColorByIndex(n, color.colorValue); - n++; - } + // if the active theme changed, re-apply it + if (themeIdx == tp->getThemeIndex()) theme.applyTheme(); - themeColorPreview->setColorList(theme.getColorList()); - - // save it to disk so it will come back the next time. - curTheme->serialize(); - - // if the active theme was edited then activate and update - // the UI - if (listBox->getSelected() == tp->getThemeIndex()) { - tp->applyTheme(listBox->getSelected()); - setAuthor(&theme); - nameText->setText(theme.getName()); - nameText->setTextFlags(COLOR_THEME_PRIMARY1); - authorText->setTextFlags(COLOR_THEME_PRIMARY1); - } - - // the list of theme names might have changed - listBox->setNames(tp->getNames()); - } - }); + // update cached theme data + auto tp_theme = tp->getThemeByIndex(themeIdx); + if (!tp_theme) return; + *tp_theme = theme; + }); }); } diff --git a/radio/src/gui/colorlcd/theme_manager.cpp b/radio/src/gui/colorlcd/theme_manager.cpp index 1f4231716..89cbbb14b 100644 --- a/radio/src/gui/colorlcd/theme_manager.cpp +++ b/radio/src/gui/colorlcd/theme_manager.cpp @@ -254,7 +254,7 @@ bool ThemeFile::readNextLine(FIL &file, char *line, int maxlen) if (f_gets(line, maxlen, &file) != NULL) { int curlen = strlen(line) - 1; if (line[curlen] == '\n') { // remove unwanted chars if file was edited using windows - if (line[curlen - 1] == '\r') { + if (curlen > 0 && line[curlen - 1] == '\r') { line[curlen - 1] = 0; } else { line[curlen] = 0; diff --git a/radio/src/gui/colorlcd/theme_manager.h b/radio/src/gui/colorlcd/theme_manager.h index 660443c4a..f5b5ed7f4 100644 --- a/radio/src/gui/colorlcd/theme_manager.h +++ b/radio/src/gui/colorlcd/theme_manager.h @@ -56,7 +56,9 @@ class ThemeFile } ThemeFile(std::string themePath); - ThemeFile(const ThemeFile &theme) + ThemeFile(const ThemeFile &theme) : + colorList(theme.colorList), + _imageFileNames(theme._imageFileNames) { path = theme.path; strncpy(name, theme.name, NAME_LENGTH); @@ -65,13 +67,10 @@ class ThemeFile author[AUTHOR_LENGTH] = '\0'; strncpy(info, theme.info, INFO_LENGTH); info[INFO_LENGTH] = '\0'; - colorList.assign(theme.colorList.begin(), theme.colorList.end()); - } - virtual ~ThemeFile() - { } + virtual ~ThemeFile() {} - ThemeFile& operator= (const ThemeFile& theme); + ThemeFile& operator= (const ThemeFile& theme); void serialize(); @@ -168,8 +167,8 @@ class ThemePersistance void applyTheme(int index) { - if (index >= 0 && index < (int) themes.size()) { - auto theme = themes[index]; + if (index >= 0 && index < (int)themes.size()) { + auto theme = themes[index]; theme->applyTheme(); } }