From 8b92b3be982c3e9556a81e404b5390890e7d35b6 Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Tue, 15 Jun 2021 17:37:13 +0200 Subject: [PATCH] [Color] Some SD manager fixes --- radio/src/gui/colorlcd/radio_sdmanager.cpp | 38 +++++++++++++++------- radio/src/gui/colorlcd/radio_tools.cpp | 10 ++++-- radio/src/gui/colorlcd/tabsgroup.cpp | 5 +-- radio/src/gui/colorlcd/tabsgroup.h | 14 ++------ 4 files changed, 40 insertions(+), 27 deletions(-) diff --git a/radio/src/gui/colorlcd/radio_sdmanager.cpp b/radio/src/gui/colorlcd/radio_sdmanager.cpp index de720ed17..cd442c3c1 100644 --- a/radio/src/gui/colorlcd/radio_sdmanager.cpp +++ b/radio/src/gui/colorlcd/radio_sdmanager.cpp @@ -92,6 +92,10 @@ class FileNameEditWindow : public Page RadioSdManagerPage::RadioSdManagerPage() : PageTab(SD_IS_HC() ? STR_SDHC_CARD : STR_SD_CARD, ICON_RADIO_SD_MANAGER) { + setOnSetVisibleHandler([]() { + TRACE("f_chdir(ROOT_PATH)"); + f_chdir(ROOT_PATH); + }); } void RadioSdManagerPage::rebuild(FormWindow * window) @@ -115,6 +119,13 @@ char * getFullPath(const std::string &filename) return full_path; } +char * getCurrentPath() +{ + static char path[FF_MAX_LFN + 1]; // TODO optimize that! + f_getcwd((TCHAR*)path, FF_MAX_LFN); + return path; +} + class FilePreview : public Window { public: @@ -209,6 +220,7 @@ void RadioSdManagerPage::build(FormWindow * window) std::list files; std::list directories; + std::string currentPath(getCurrentPath()); auto preview = new FilePreview(window, {LCD_W / 2 + 6, 0, LCD_W / 2 - 16, window->height()}); FRESULT res = f_opendir(&dir, "."); // Open the directory @@ -238,7 +250,8 @@ void RadioSdManagerPage::build(FormWindow * window) for (auto name: directories) { auto b = new TextButton(window, grid.getLabelSlot(), name, [=]() -> uint8_t { - f_chdir(name.data()); + std::string fullpath = currentPath + "/" + name; + f_chdir(fullpath.c_str()); window->clear(); build(window); return 0; @@ -250,6 +263,7 @@ void RadioSdManagerPage::build(FormWindow * window) for (auto name: files) { auto button = new TextButton(window, grid.getLabelSlot(), name, [=]() -> uint8_t { auto menu = new Menu(window); + f_chdir(currentPath.c_str()); const char *ext = getFileExtension(name.data()); if (ext) { if (!strcasecmp(ext, SOUNDS_EXT)) { @@ -286,14 +300,14 @@ void RadioSdManagerPage::build(FormWindow * window) dialog->flash(getFullPath(name)); }); } - else if (isExtensionMatching(ext, BITMAPS_EXT)) { - // TODO - } - else if (!strcasecmp(ext, TEXT_EXT)) { - menu->addLine(STR_VIEW_TEXT, [=]() { - // TODO - }); - } + // else if (isExtensionMatching(ext, BITMAPS_EXT)) { + // // TODO + // } + // else if (!strcasecmp(ext, TEXT_EXT)) { + // menu->addLine(STR_VIEW_TEXT, [=]() { + // // TODO + // }); + // } if (!READ_ONLY() && !strcasecmp(ext, FIRMWARE_EXT)) { if (isBootloader(name.data())) { menu->addLine(STR_FLASH_BOOTLOADER, [=]() { @@ -328,8 +342,9 @@ void RadioSdManagerPage::build(FormWindow * window) } #if defined(LUA) else if (isExtensionMatching(ext, SCRIPTS_EXT)) { + std::string fullpath = currentPath + "/" + name; menu->addLine(STR_EXECUTE_FILE, [=]() { - luaExec(getFullPath(name)); + luaExec(fullpath.c_str()); //TODO: check 'luaState' StandaloneLuaWindow::instance()->attach(window); }); @@ -374,7 +389,8 @@ void RadioSdManagerPage::build(FormWindow * window) }); } return 0; - }, 0); + }, OPAQUE); + button->setBgColorHandler([=]() -> LcdFlags { return FIELD_BGCOLOR; }); button->setFocusHandler([=](bool active) { if (active) { preview->setFile(getFullPath(name)); diff --git a/radio/src/gui/colorlcd/radio_tools.cpp b/radio/src/gui/colorlcd/radio_tools.cpp index 3484f2495..88c71033e 100644 --- a/radio/src/gui/colorlcd/radio_tools.cpp +++ b/radio/src/gui/colorlcd/radio_tools.cpp @@ -119,10 +119,14 @@ void RadioToolsPage::rebuild(FormWindow * window) auto b = new TextButton( window, grid.getFieldSlot(1), label, [window, path_str]() -> uint8_t { - f_chdir("/SCRIPTS/TOOLS/"); - luaExec(path_str.c_str()); - // TODO: check 'luaState' + + char toolPath[FF_MAX_LFN + 1]; + strncpy(toolPath, path_str.c_str(), sizeof(toolPath)-1); + *((char *)getBasename(toolPath)-1) = '\0'; + f_chdir(toolPath); + StandaloneLuaWindow::instance()->attach(window); + luaExec(path_str.c_str()); return 0; }, OPAQUE); diff --git a/radio/src/gui/colorlcd/tabsgroup.cpp b/radio/src/gui/colorlcd/tabsgroup.cpp index a0b7c32a8..f3ea3163b 100644 --- a/radio/src/gui/colorlcd/tabsgroup.cpp +++ b/radio/src/gui/colorlcd/tabsgroup.cpp @@ -146,9 +146,10 @@ void TabsGroup::setVisibleTab(PageTab * tab) Keyboard::hide(); #endif currentTab = tab; + if (tab->onSetVisible) tab->onSetVisible(); tab->build(&body); - if (!focusWindow) - setFocus(SET_FOCUS_DEFAULT); + if (!focusWindow) setFocus(SET_FOCUS_DEFAULT); + header.setTitle(tab->title.c_str()); invalidate(); } diff --git a/radio/src/gui/colorlcd/tabsgroup.h b/radio/src/gui/colorlcd/tabsgroup.h index d3af8c15d..1618bf7a0 100644 --- a/radio/src/gui/colorlcd/tabsgroup.h +++ b/radio/src/gui/colorlcd/tabsgroup.h @@ -50,17 +50,9 @@ class PageTab { { } - void destroy() + void setOnSetVisibleHandler(std::function handler) { - if (onPageDestroyed) { - onPageDestroyed(); - } - delete this; - } - - void setOnPageDestroyedHandler(std::function handler) - { - onPageDestroyed = std::move(handler); + onSetVisible = std::move(handler); } void setTitle(std::string value) @@ -81,7 +73,7 @@ class PageTab { protected: std::string title; unsigned icon = 0; - std::function onPageDestroyed; + std::function onSetVisible; }; class TabsCarousel: public Window {