diff --git a/SerialPrograms/Source/CommonFramework/Panels/PanelList.cpp b/SerialPrograms/Source/CommonFramework/Panels/PanelList.cpp index ae16768c86..0ebce8d7e7 100644 --- a/SerialPrograms/Source/CommonFramework/Panels/PanelList.cpp +++ b/SerialPrograms/Source/CommonFramework/Panels/PanelList.cpp @@ -6,6 +6,7 @@ #include "Common/Cpp/Exceptions.h" #include "CommonFramework/Logging/Logger.h" +#include "CommonFramework/Panels/PanelSession.h" #include "PanelList.h" //#include @@ -33,6 +34,19 @@ PanelListDescriptor::PanelListDescriptor( } } +bool load_panel_session( + const std::shared_ptr& descriptor, + const JsonValue& panel_json, + PanelHolder& holder +){ + if (!holder.report_new_panel_intent(*descriptor)){ + return false; + } + std::unique_ptr panel = descriptor->make_panel(); + panel->load_json(panel_json); + holder.load_panel(descriptor, std::move(panel)); + return true; +} diff --git a/SerialPrograms/Source/CommonFramework/Panels/PanelList.h b/SerialPrograms/Source/CommonFramework/Panels/PanelList.h index b6da3ab2d5..0ab5f36dca 100644 --- a/SerialPrograms/Source/CommonFramework/Panels/PanelList.h +++ b/SerialPrograms/Source/CommonFramework/Panels/PanelList.h @@ -7,6 +7,7 @@ #ifndef PokemonAutomation_PanelList_H #define PokemonAutomation_PanelList_H +#include #include #include "CommonFramework/ImageTypes/ImageRGB32.h" #include "CommonFramework/Panels/PanelTools.h" @@ -48,6 +49,16 @@ class PanelListDescriptor{ }; +// Instantiate `descriptor` (non-null), load `panel_json` into it, and install +// it in `holder`. Returns false, loading nothing, if +// `holder.report_new_panel_intent()` declines. +bool load_panel_session( + const std::shared_ptr& descriptor, + const JsonValue& panel_json, + PanelHolder& holder +); + + } #endif diff --git a/SerialPrograms/Source/CommonFramework/Panels/UI/PanelListWidget.cpp b/SerialPrograms/Source/CommonFramework/Panels/UI/PanelListWidget.cpp index 387ca75331..277bdfebba 100644 --- a/SerialPrograms/Source/CommonFramework/Panels/UI/PanelListWidget.cpp +++ b/SerialPrograms/Source/CommonFramework/Panels/UI/PanelListWidget.cpp @@ -93,18 +93,16 @@ void PanelListWidget::handle_panel_clicked(const std::string& text){ PERSISTENT_SETTINGS().panels[JSON_PROGRAM_PANEL] = ""; return; } - std::shared_ptr& descriptor = iter->second; + const std::shared_ptr& descriptor = iter->second; // cout << descriptor->display_name() << endl; - if (!m_panel_holder.report_new_panel_intent(*descriptor)){ - return; - } try{ - std::unique_ptr panel = descriptor->make_panel(); -// try{ - panel->load_json(PERSISTENT_SETTINGS().panels[descriptor->identifier()]); -// }catch (ParseException&){} - m_panel_holder.load_panel(descriptor, std::move(panel)); - + if (!load_panel_session( + descriptor, + PERSISTENT_SETTINGS().panels[descriptor->identifier()], + m_panel_holder + )){ + return; + } PERSISTENT_SETTINGS().panels[JSON_PROGRAM_PANEL] = iter->first; }catch (const Exception& error){ QMessageBox box; diff --git a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp index 0cda95e530..058586d5bd 100644 --- a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp +++ b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp @@ -23,6 +23,7 @@ #include "CommonFramework/Tools/GlobalThreadPools.h" #include "CommonFramework/Windows/LabChannel_Tests.h" #include "CommonFramework/Startup/StartupArguments_Tests.h" +#include "PanelCatalog_Tests.h" #include "CommonFramework/Startup/LabStartupPolicy_Tests.h" #include "CommonFramework/Devices/LabDeviceAccessQt_Tests.h" #include "CommonTools/VisualDetectors/BlackBorderDetector.h" @@ -86,6 +87,7 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ add_tests_AudioFileLoader(ret); add_tests_LabChannel(ret); add_tests_StartupArguments(ret); + add_tests_PanelCatalog(ret); add_tests_LabStartupPolicy(ret); add_tests_LabDeviceAccess(ret); OCR::add_tests(ret); diff --git a/SerialPrograms/Source/PanelCatalog.cpp b/SerialPrograms/Source/PanelCatalog.cpp new file mode 100644 index 0000000000..545a036e92 --- /dev/null +++ b/SerialPrograms/Source/PanelCatalog.cpp @@ -0,0 +1,66 @@ +/* Panel Catalog + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/StaticGlobals.h" +#include "ML/ML_Panels.h" +#include "NintendoSwitch/NintendoSwitch_Panels.h" +#include "VideoGames/PokemonSwSh/PokemonSwSh_Panels.h" +#include "VideoGames/PokemonHome/PokemonHome_Panels.h" +#include "PokemonBDSP/PokemonBDSP_Panels.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Panels.h" +#include "VideoGames/PokemonLA/PokemonLA_Panels.h" +#include "VideoGames/PokemonLGPE/PokemonLGPE_Panels.h" +#include "VideoGames/PokemonRSE/PokemonRSE_Panels.h" +#include "VideoGames/PokemonSV/PokemonSV_Panels.h" +#include "VideoGames/PokemonLZA/PokemonLZA_Panels.h" +#include "VideoGames/PokemonPokopia/PokemonPokopia_Panels.h" +#include "VideoGames/ZeldaTotK/ZeldaTotK_Panels.h" +#include "PanelCatalog.h" + +namespace PokemonAutomation{ + + +std::vector> make_all_panel_lists(){ + std::vector> ret; + ret.emplace_back(std::make_unique()); + ret.emplace_back(std::make_unique()); + ret.emplace_back(std::make_unique()); + ret.emplace_back(std::make_unique()); + ret.emplace_back(std::make_unique()); + ret.emplace_back(std::make_unique()); + ret.emplace_back(std::make_unique()); + ret.emplace_back(std::make_unique()); + ret.emplace_back(std::make_unique()); + ret.emplace_back(std::make_unique()); + if (STATIC_GLOBALS.DEVELOPER_MODE){ + ret.emplace_back(std::make_unique()); + } + + ret.emplace_back(std::make_unique()); + + if (STATIC_GLOBALS.DEVELOPER_MODE){ + ret.emplace_back(std::make_unique()); + } + return ret; +} + +std::optional find_panel( + const std::vector>& lists, + std::string_view identifier +){ + for (size_t c = 0; c < lists.size(); c++){ + for (PanelEntry& entry : lists[c]->panels()){ + if (entry.descriptor == nullptr || entry.descriptor->identifier() != identifier){ + continue; + } + return PanelMatch{.category_index = c, .entry = std::move(entry)}; + } + } + return std::nullopt; +} + + +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PanelCatalog.h b/SerialPrograms/Source/PanelCatalog.h new file mode 100644 index 0000000000..6d2eb49ad2 --- /dev/null +++ b/SerialPrograms/Source/PanelCatalog.h @@ -0,0 +1,38 @@ +/* Panel Catalog + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PanelCatalog_H +#define PokemonAutomation_PanelCatalog_H + +#include +#include +#include +#include +#include +#include "CommonFramework/Panels/PanelList.h" + +namespace PokemonAutomation{ + + +// Every program category, in dropdown order. +std::vector> make_all_panel_lists(); + + +// A panel found by find_panel(), with the index of its list in `lists`. +struct PanelMatch{ + size_t category_index; + PanelEntry entry; +}; + +// Scans each list's panels() for the panel with `identifier`. +std::optional find_panel( + const std::vector>& lists, + std::string_view identifier +); + + +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PanelCatalog_Tests.cpp b/SerialPrograms/Source/PanelCatalog_Tests.cpp new file mode 100644 index 0000000000..dccf8ba173 --- /dev/null +++ b/SerialPrograms/Source/PanelCatalog_Tests.cpp @@ -0,0 +1,125 @@ +/* Panel Catalog Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Common/Cpp/Json/JsonValue.h" +#include "CommonFramework/Panels/PanelDescriptor.h" +#include "CommonFramework/Panels/PanelList.h" +#include "CommonFramework/Panels/PanelSession.h" +#include "PanelCatalog.h" +#include "PanelCatalog_Tests.h" + +namespace PokemonAutomation{ +namespace{ + + +class FakePanelDescriptor final : public PanelDescriptor{ +public: + explicit FakePanelDescriptor(std::string identifier) + : PanelDescriptor( + identifier, + /*category=*/"FakeCategory", + /*display_name=*/identifier, + /*doc_link=*/"", + /*description=*/"", + PanelQualityStanding::DEVELOPER, + /*restore_defaults_button=*/false + ) + {} + + virtual std::unique_ptr make_panel() const override{ + return std::make_unique(*this); + } +}; + +// One category: a divider, then the panel "Fake:Alpha". +class FakePanelList final : public PanelListDescriptor{ +public: + FakePanelList() + : PanelListDescriptor("FakeList") + {} + +protected: + virtual std::vector make_panels() const override{ + std::vector ret; + ret.emplace_back("Divider"); + ret.emplace_back(std::make_unique("Fake:Alpha")); + return ret; + } +}; + +class DecliningPanelHolder final : public PanelHolder{ +public: + virtual bool report_new_panel_intent(const PanelDescriptor&) override{ + return false; + } + virtual void load_panel( + std::shared_ptr, + std::unique_ptr + ) override{ + loaded = true; + } + virtual void on_busy() override{} + virtual void on_idle() override{} + + bool loaded = false; +}; + + +class FindPanelPastDividerTest final : public UnitTest{ +public: + FindPanelPastDividerTest() + : UnitTest("PanelCatalog/FindPanel/past a divider") + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + std::vector> lists; + lists.emplace_back(std::make_unique()); + + const std::optional match = find_panel(lists, "Fake:Alpha"); + if (!match){ + return "find_panel() did not find Fake:Alpha."; + } + if (match->category_index != 0){ + return "find_panel() returned the wrong category index."; + } + if (match->entry.descriptor == nullptr || match->entry.descriptor->identifier() != "Fake:Alpha"){ + return "find_panel() returned the wrong panel."; + } + return true; + } +}; + +class LoadPanelSessionDeclinedTest final : public UnitTest{ +public: + LoadPanelSessionDeclinedTest() + : UnitTest("PanelCatalog/LoadPanelSession/declined intent skips load") + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + DecliningPanelHolder holder; + const std::shared_ptr descriptor = + std::make_shared("Fake:Alpha"); + + if (load_panel_session(descriptor, JsonValue(), holder)){ + return "load_panel_session() returned true after the holder declined."; + } + if (holder.loaded){ + return "load_panel_session() loaded a panel after the holder declined."; + } + return true; + } +}; + + +} // namespace + +void add_tests_PanelCatalog(UnitTestDatabase& database){ + database.add(); + database.add(); +} + + +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PanelCatalog_Tests.h b/SerialPrograms/Source/PanelCatalog_Tests.h new file mode 100644 index 0000000000..c2883043c9 --- /dev/null +++ b/SerialPrograms/Source/PanelCatalog_Tests.h @@ -0,0 +1,18 @@ +/* Panel Catalog Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PanelCatalog_Tests_H +#define PokemonAutomation_PanelCatalog_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ + +// Register PanelCatalog and load_panel_session() unit tests. +void add_tests_PanelCatalog(UnitTestDatabase& database); + +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PanelLists.cpp b/SerialPrograms/Source/PanelLists.cpp index cf857b7c9c..054dd61006 100644 --- a/SerialPrograms/Source/PanelLists.cpp +++ b/SerialPrograms/Source/PanelLists.cpp @@ -7,24 +7,11 @@ #include #include #include "Common/Qt/NoWheelComboBox.h" -#include "CommonFramework/StaticGlobals.h" #include "CommonFramework/PersistentSettings.h" #include "CommonFramework/ImageTypes/ImageRGB32_Qt.h" #include "CommonFramework/Windows/DpiScaler.h" #include "CommonFramework/Panels/UI/PanelListWidget.h" -#include "ML/ML_Panels.h" -#include "NintendoSwitch/NintendoSwitch_Panels.h" -#include "VideoGames/PokemonSwSh/PokemonSwSh_Panels.h" -#include "VideoGames/PokemonHome/PokemonHome_Panels.h" -#include "PokemonBDSP/PokemonBDSP_Panels.h" -#include "VideoGames/PokemonFRLG/PokemonFRLG_Panels.h" -#include "VideoGames/PokemonLA/PokemonLA_Panels.h" -#include "VideoGames/PokemonLGPE/PokemonLGPE_Panels.h" -#include "VideoGames/PokemonRSE/PokemonRSE_Panels.h" -#include "VideoGames/PokemonSV/PokemonSV_Panels.h" -#include "VideoGames/PokemonLZA/PokemonLZA_Panels.h" -#include "VideoGames/PokemonPokopia/PokemonPokopia_Panels.h" -#include "VideoGames/ZeldaTotK/ZeldaTotK_Panels.h" +#include "PanelCatalog.h" #include "PanelLists.h" //#include @@ -59,24 +46,8 @@ ProgramSelect::ProgramSelect(QWidget& parent, PanelHolder& holder) layout->addWidget(m_dropdown); - add(std::make_unique()); - add(std::make_unique()); - add(std::make_unique()); - add(std::make_unique()); - add(std::make_unique()); - add(std::make_unique()); - add(std::make_unique()); - add(std::make_unique()); - add(std::make_unique()); - add(std::make_unique()); - if (STATIC_GLOBALS.DEVELOPER_MODE){ - add(std::make_unique()); - } - - add(std::make_unique()); - - if (STATIC_GLOBALS.DEVELOPER_MODE){ - add(std::make_unique()); + for (std::unique_ptr& list : make_all_panel_lists()){ + add(std::move(list)); } @@ -118,8 +89,9 @@ void ProgramSelect::add(std::unique_ptr list){ m_lists.emplace_back(std::move(list)); const PanelListDescriptor& back = *m_lists.back(); if (!m_tab_map.emplace(back.name(), index).second){ + const std::string name = back.name(); m_lists.pop_back(); - throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Duplicate Category Name: " + back.name()); + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Duplicate Category Name: " + name); } if (!back.enabled()){ auto* model = qobject_cast(m_dropdown->model()); @@ -152,20 +124,16 @@ void ProgramSelect::load_persistent_panel(){ } std::string ProgramSelect::select_program(const std::string& identifier){ - for (size_t c = 0; c < m_lists.size(); c++){ - for (const PanelEntry& entry : m_lists[c]->panels()){ - if (entry.descriptor == nullptr || entry.descriptor->identifier() != identifier){ - continue; - } - if (m_locked){ - return "A program is running."; - } - change_list((int)c); - m_active_list->set_panel(entry.display_name); - return ""; - } + const std::optional match = find_panel(m_lists, identifier); + if (!match){ + return "No program with identifier \"" + identifier + "\"."; + } + if (m_locked){ + return "A program is running."; } - return "No program with identifier \"" + identifier + "\"."; + change_list((int)match->category_index); + m_active_list->set_panel(match->entry.display_name); + return ""; } void ProgramSelect::change_list(int index){ diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 5e5ced94bf..41df561e6d 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1456,6 +1456,10 @@ file(GLOB LIBRARY_SOURCES Source/NintendoSwitch/Programs/Typewriter/NintendoSwitch_Typewriter_Tests.h Source/NintendoSwitch/Programs/Typewriter/NintendoSwitch_TypewriterTextMapping.cpp Source/NintendoSwitch/Programs/Typewriter/NintendoSwitch_TypewriterTextMapping.h + Source/PanelCatalog.cpp + Source/PanelCatalog.h + Source/PanelCatalog_Tests.cpp + Source/PanelCatalog_Tests.h Source/PanelLists.cpp Source/PanelLists.h Source/Pokemon/Inference/Pokemon_AbilityReader.cpp diff --git a/docs/design/Headless-CC.md b/docs/design/Headless-CC.md index b89d132086..2d85ebb1fe 100644 --- a/docs/design/Headless-CC.md +++ b/docs/design/Headless-CC.md @@ -40,15 +40,15 @@ Extend `StartupMode` (`HEADLESS`) and `StartupPlan` on `lab/main`: Currently `ProgramSelect` (`SerialPrograms/Source/PanelLists.h`, `PanelLists.cpp`) owns `std::vector> m_lists` and `select_program(identifier)`, while `PanelListWidget::handle_panel_clicked()` (`CommonFramework/Panels/UI/PanelListWidget.cpp:90`) stores `std::shared_ptr`, calls `make_panel()`, loads JSON, calls `load_panel()`, and writes `PERSISTENT_SETTINGS().panels[JSON_PROGRAM_PANEL]`. -Extract a widget-free `PanelCatalog` (in PR 1, no behavior change): -- **Ownership (no caching in PR 1):** `PanelListDescriptor::panels()` creates a fresh `std::vector` per call (`PanelList.h`). `PanelCatalog::find_panel(identifier)` scans `panels()` per lookup (matching `ProgramSelect::select_program()` today) and converts the matching `PanelEntry`'s `std::unique_ptr` into an owning `std::shared_ptr`. `HeadlessHost` keeps that `shared_ptr` alongside `m_current_panel` so it outlives the `PanelSession`. +Extract widget-free free functions `make_all_panel_lists()` and `find_panel(lists, identifier)` into `Source/PanelCatalog.{h,cpp}` (in PR 1, no behavior change): +- **Ownership (no caching in PR 1):** `PanelListDescriptor::panels()` creates a fresh `std::vector` per call (`PanelList.h`). `find_panel(lists, identifier)` scans `panels()` per lookup (matching `ProgramSelect::select_program()` today) and returns the matching `PanelEntry` with its category index; the caller converts its `std::unique_ptr` into an owning `std::shared_ptr`. `HeadlessHost` keeps that `shared_ptr` alongside `m_current_panel` so it outlives the `PanelSession`. - **Shared load helper:** Extract the `report_new_panel_intent()` -> `make_panel()` -> `load_json()` -> `load_panel()` sequence out of `PanelListWidget::handle_panel_clicked()` into a shared helper with a `bool persist_selection` flag (`true` in GUI, `false` in headless) that reports errors via return/exception instead of opening `QMessageBox` directly. ### 3.3 `HeadlessHost` (Widgetless `PanelHolder`) Add `HeadlessHost : public PanelHolder`: - **Owns:** - - `PanelCatalog&` + - the panel lists from `make_all_panel_lists()` - `std::shared_ptr m_current_panel_descriptor` - `std::unique_ptr m_current_panel` - (`ProgramSession::run_program()` already holds a `SleepSuppressScope` at `ProgramSession.cpp:323`, so `HeadlessHost` needs no `m_sleep_scope` and does not rely on `on_busy()`/`on_idle()`.) @@ -69,7 +69,7 @@ Add `HeadlessHost : public PanelHolder`: ### 3.5 One-Shot Execution, Readiness, Exit Codes, and Concurrency -1. **Select & verify runnable:** `HeadlessHost` looks up `` via `PanelCatalog::find_panel()`, instantiates `PanelSession`, applies `PERSISTENT_SETTINGS().panels[id]`, validates/applies `--options` (rejecting `ConsoleSetup` or parse errors with exit `4`), and casts with `dynamic_cast` (`LabChannel.cpp:58`). If null (e.g. a settings panel), exit `4`. +1. **Select & verify runnable:** `HeadlessHost` looks up `` via `find_panel()`, instantiates `PanelSession`, applies `PERSISTENT_SETTINGS().panels[id]`, validates/applies `--options` (rejecting `ConsoleSetup` or parse errors with exit `4`), and casts with `dynamic_cast` (`LabChannel.cpp:58`). If null (e.g. a settings panel), exit `4`. 2. **Per-device leases & readiness:** - Per-device leases (`acquire_lab_serial`, `acquire_lab_capture` from `lab-devices.json`) throw `UserSetupError` on the connect path; in headless mode, log the error and fail fast with exit `5` (no wait, no dialog). - Otherwise, if the panel has a console (`UnitTestRunner` has no console and skips this wait), wait up to a bounded timeout (e.g. 10 s) for the controller status (`"ready"` / `"none"` / `""`, factored from `LabChannel.h:88`, `LabRequest.h:70` without requiring `LabChannel`) to become `"ready"`, exiting `5` on timeout. @@ -94,7 +94,7 @@ Add `HeadlessHost : public PanelHolder`: ## 4. Rollout Plan 1. **PR 1 — `PanelCatalog` extraction (no behavior change):** - - Extract `PanelCatalog` from `ProgramSelect` (`PanelLists.{h,cpp}`) and share the panel-load helper with `PanelListWidget::handle_panel_clicked()`. + - Move the panel-list factories and the lookup out of `ProgramSelect` (`PanelLists.{h,cpp}`) into `make_all_panel_lists()` / `find_panel()` and share the panel-load helper with `PanelListWidget::handle_panel_clicked()`. - Acceptance: Existing GUI behavior and `StartupArguments_Tests` / `LabStartupPolicy_Tests` unchanged. 2. **PR 2 — `--headless --run` + `HeadlessHost` (macOS-only):** - Add `StartupMode::HEADLESS`, `--headless`, `--run`, `--options` to `StartupArguments` + `LabStartupPolicy` (macOS-only, requires `--profile`) with unit tests.