From 4e29e07eee34918afdd43b9680c26dcbee6dc171 Mon Sep 17 00:00:00 2001 From: David Capello Date: Fri, 16 Nov 2018 17:46:54 -0300 Subject: [PATCH] Sort items in Scripts menu by file name and move Open Script Folder item at the bottom (with a separator) --- src/app/app_menus.cpp | 21 +++++++++++++++------ src/app/app_menus.h | 4 +++- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/app/app_menus.cpp b/src/app/app_menus.cpp index 9481c8a7d..0c00f97ae 100644 --- a/src/app/app_menus.cpp +++ b/src/app/app_menus.cpp @@ -358,7 +358,7 @@ void AppMenus::reload() std::string scriptsDir = rf.getFirstOrCreateDefault(); scriptsDir = base::get_file_path(scriptsDir); if (base::is_directory(scriptsDir)) { - loadScriptsSubmenu(scriptsMenu->getSubmenu(), scriptsDir); + loadScriptsSubmenu(scriptsMenu->getSubmenu(), scriptsDir, true); } #else // Scripting is not available @@ -396,13 +396,19 @@ void AppMenus::reload() } #ifdef ENABLE_SCRIPTING -void AppMenus::loadScriptsSubmenu(ui::Menu* menu, const std::string& dir) +void AppMenus::loadScriptsSubmenu(ui::Menu* menu, + const std::string& dir, + const bool rootLevel) { Command* cmd_run_script = Commands::instance()->byId(CommandId::RunScript()); auto files = base::list_files(dir); - std::sort(files.begin(), files.end()); + std::sort(files.begin(), files.end(), + [](const std::string& a, const std::string& b) { + return base::compare_filenames(a, b) < 0; + }); + int insertPos = 0; for (auto fn : files) { std::string fullFn = base::join_path(dir, fn); AppMenuItem* menuitem = nullptr; @@ -422,15 +428,18 @@ void AppMenus::loadScriptsSubmenu(ui::Menu* menu, const std::string& dir) } else if (base::is_directory(fullFn)) { Menu* submenu = new Menu(); - loadScriptsSubmenu(submenu, fullFn); + loadScriptsSubmenu(submenu, fullFn, false); menuitem = new AppMenuItem( base::get_file_title(fn).c_str()); menuitem->setSubmenu(submenu); } - if (menuitem) - menu->addChild(menuitem); + if (menuitem) { + menu->insertChild(insertPos++, menuitem); + } } + if (rootLevel && insertPos > 0) + menu->insertChild(insertPos, new MenuSeparator()); } #endif diff --git a/src/app/app_menus.h b/src/app/app_menus.h index 82c46db30..91e2869b4 100644 --- a/src/app/app_menus.h +++ b/src/app/app_menus.h @@ -78,7 +78,9 @@ namespace app { void createNativeSubmenus(os::Menu* osMenu, const ui::Menu* uiMenu); #ifdef ENABLE_SCRIPTING - void loadScriptsSubmenu(ui::Menu* menu, const std::string& dir); + void loadScriptsSubmenu(ui::Menu* menu, + const std::string& dir, + const bool rootLevel); #endif std::unique_ptr m_rootMenu;