Create ModulePresetPathItems for each patch in a directory.
| 971 | |
| 972 | // Create ModulePresetPathItems for each patch in a directory. |
| 973 | static void appendPresetItems(ui::Menu* menu, WeakPtr<ModuleWidget> moduleWidget, std::string presetDir) { |
| 974 | bool hasPresets = false; |
| 975 | if (system::isDirectory(presetDir)) { |
| 976 | // Note: This is not cached, so opening this menu each time might have a bit of latency. |
| 977 | std::vector<std::string> entries = system::getEntries(presetDir); |
| 978 | std::sort(entries.begin(), entries.end()); |
| 979 | for (std::string path : entries) { |
| 980 | std::string name = system::getStem(path); |
| 981 | // Remove "1_", "42_", "001_", etc at the beginning of preset filenames |
| 982 | std::regex r("^\\d+_"); |
| 983 | name = std::regex_replace(name, r, ""); |
| 984 | |
| 985 | if (system::isDirectory(path)) { |
| 986 | hasPresets = true; |
| 987 | |
| 988 | menu->addChild(createSubmenuItem(name, "", [=](ui::Menu* menu) { |
| 989 | if (!moduleWidget) |
| 990 | return; |
| 991 | appendPresetItems(menu, moduleWidget, path); |
| 992 | })); |
| 993 | } |
| 994 | else if (system::getExtension(path) == ".vcvm" && name != "template") { |
| 995 | hasPresets = true; |
| 996 | |
| 997 | menu->addChild(createMenuItem(name, "", [=]() { |
| 998 | if (!moduleWidget) |
| 999 | return; |
| 1000 | try { |
| 1001 | moduleWidget->loadAction(path); |
| 1002 | } |
| 1003 | catch (Exception& e) { |
| 1004 | async_dialog_message(e.what()); |
| 1005 | } |
| 1006 | })); |
| 1007 | } |
| 1008 | } |
| 1009 | } |
| 1010 | if (!hasPresets) { |
| 1011 | menu->addChild(createMenuLabel("(None)")); |
| 1012 | } |
| 1013 | }; |
| 1014 | |
| 1015 | |
| 1016 | void ModuleWidget::createContextMenu() { |
no test coverage detected