From 1a2289c396768fee005d16437befed2296b5f7c3 Mon Sep 17 00:00:00 2001 From: Pablo Curiel Date: Sun, 20 Jun 2021 16:37:31 -0400 Subject: [PATCH] Simplified LayeredErrorFrame by letting extended classes handle task events. --- include/gamecard_tab.hpp | 8 ++-- include/layered_error_frame.hpp | 82 ++------------------------------- source/gamecard_tab.cpp | 10 +++- source/layered_error_frame.cpp | 78 +++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 85 deletions(-) create mode 100644 source/layered_error_frame.cpp diff --git a/include/gamecard_tab.hpp b/include/gamecard_tab.hpp index 707a899..8602ab1 100644 --- a/include/gamecard_tab.hpp +++ b/include/gamecard_tab.hpp @@ -30,14 +30,13 @@ namespace nxdt::views { - /* Instantiate the template for our class. */ - typedef LayeredErrorFrame GameCardLayeredErrorFrame; - - class GameCardTab: public GameCardLayeredErrorFrame + class GameCardTab: public LayeredErrorFrame { typedef bool (*GameCardSizeFunc)(u64 *size); private: + nxdt::tasks::GameCardTask *gc_status_task = nullptr; + nxdt::tasks::GameCardStatusEvent::Subscription gc_status_task_sub; GameCardStatus gc_status = GameCardStatus_NotInserted; FocusableTable *properties_table = nullptr; @@ -60,6 +59,7 @@ namespace nxdt::views public: GameCardTab(nxdt::tasks::GameCardTask *gc_status_task); + ~GameCardTab(void); }; } diff --git a/include/layered_error_frame.hpp b/include/layered_error_frame.hpp index 695e7e7..bc480dc 100644 --- a/include/layered_error_frame.hpp +++ b/include/layered_error_frame.hpp @@ -28,102 +28,26 @@ namespace nxdt::views { - /* Extended class to switch between ErrorFrame and List views whenever an event is triggered. */ - template + /* Extended class to switch between ErrorFrame and List views on demand. */ class LayeredErrorFrame: public brls::LayerView { private: - TaskType *task = nullptr; - std::vector task_subs; + int layer_view_index = 0; ErrorFrame *error_frame = nullptr; brls::List *list = nullptr; std::vector list_views; - - int layer_view_index = 0; protected: void SwitchLayerView(bool use_error_frame); void SetErrorFrameMessage(std::string msg = ""); void AddListView(brls::View* view); - void RegisterListener(typename EventType::Callback cb); public: - LayeredErrorFrame(TaskType *task); + LayeredErrorFrame(void); ~LayeredErrorFrame(void); }; - - template - LayeredErrorFrame::LayeredErrorFrame(TaskType *task) : brls::LayerView(), task(task) - { - /* Error frame. */ - this->error_frame = new ErrorFrame(); - this->addLayer(this->error_frame); - - /* List. */ - this->list = new brls::List(); - this->list->setSpacing(this->list->getSpacing() / 2); - this->list->setMarginBottom(20); - this->addLayer(this->list); - } - - template - LayeredErrorFrame::~LayeredErrorFrame(void) - { - /* Unregister task listeners. */ - for(typename EventType::Subscription task_sub : this->task_subs) this->task->UnregisterListener(task_sub); - - /* Clear task subscriptions vector. */ - if (this->task_subs.size()) this->task_subs.clear(); - - /* Clear list views vector. */ - if (this->list_views.size()) this->list_views.clear(); - } - - template - void LayeredErrorFrame::SwitchLayerView(bool use_error_frame) - { - if ((use_error_frame && this->layer_view_index == 0) || (!use_error_frame && this->layer_view_index == 1)) return; - - int index = (this->layer_view_index ^ 1); - brls::View *current_focus = brls::Application::getCurrentFocus(); - - /* Focus the sidebar if we're currently focusing an element from our List. */ - for(brls::View* list_view : this->list_views) - { - if (current_focus == list_view) - { - brls::Application::onGamepadButtonPressed(GLFW_GAMEPAD_BUTTON_DPAD_LEFT, false); - break; - } - } - - /* Change layer view. */ - this->changeLayer(index); - this->invalidate(true); - this->layer_view_index = index; - } - - template - void LayeredErrorFrame::SetErrorFrameMessage(std::string msg) - { - this->error_frame->SetMessage(msg); - } - - template - void LayeredErrorFrame::AddListView(brls::View* view) - { - this->list->addView(view); - this->list_views.push_back(view); - } - - template - void LayeredErrorFrame::RegisterListener(typename EventType::Callback cb) - { - typename EventType::Subscription task_sub = this->task->RegisterListener(cb); - this->task_subs.push_back(task_sub); - } } #endif /* __LAYERED_ERROR_FRAME_HPP__ */ diff --git a/source/gamecard_tab.cpp b/source/gamecard_tab.cpp index e7d6b1d..ebdd03e 100644 --- a/source/gamecard_tab.cpp +++ b/source/gamecard_tab.cpp @@ -41,7 +41,7 @@ namespace nxdt::views [GameCardCompatibilityType_Terra] = "Terra" }; - GameCardTab::GameCardTab(nxdt::tasks::GameCardTask *gc_status_task) : GameCardLayeredErrorFrame(gc_status_task) + GameCardTab::GameCardTab(nxdt::tasks::GameCardTask *gc_status_task) : LayeredErrorFrame(), gc_status_task(gc_status_task) { /* Error frame. */ this->SetErrorFrameMessage("gamecard_tab/error_frame/not_inserted"_i18n); @@ -81,7 +81,7 @@ namespace nxdt::views this->AddListView(this->dump_hfs_partitions); /* Subscribe to gamecard status event. */ - this->RegisterListener([this](GameCardStatus gc_status) { + this->gc_status_task_sub = this->gc_status_task->RegisterListener([this](GameCardStatus gc_status) { if (gc_status < GameCardStatus_InsertedAndInfoLoaded) this->SwitchLayerView(true); switch(gc_status) @@ -137,6 +137,12 @@ namespace nxdt::views }); } + GameCardTab::~GameCardTab(void) + { + /* Unregister task listener. */ + this->gc_status_task->UnregisterListener(this->gc_status_task_sub); + } + std::string GameCardTab::GetFormattedSizeString(GameCardSizeFunc func) { u64 size = 0; diff --git a/source/layered_error_frame.cpp b/source/layered_error_frame.cpp new file mode 100644 index 0000000..018f641 --- /dev/null +++ b/source/layered_error_frame.cpp @@ -0,0 +1,78 @@ +/* + * layered_error_frame.cpp + * + * Copyright (c) 2020-2021, DarkMatterCore . + * + * This file is part of nxdumptool (https://github.com/DarkMatterCore/nxdumptool). + * + * nxdumptool is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * nxdumptool is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +namespace nxdt::views +{ + LayeredErrorFrame::LayeredErrorFrame(void) : brls::LayerView() + { + /* Error frame. */ + this->error_frame = new ErrorFrame(); + this->addLayer(this->error_frame); + + /* List. */ + this->list = new brls::List(); + this->list->setSpacing(this->list->getSpacing() / 2); + this->list->setMarginBottom(20); + this->addLayer(this->list); + } + + LayeredErrorFrame::~LayeredErrorFrame(void) + { + /* Clear list views vector. */ + if (this->list_views.size()) this->list_views.clear(); + } + + void LayeredErrorFrame::SwitchLayerView(bool use_error_frame) + { + if ((use_error_frame && this->layer_view_index == 0) || (!use_error_frame && this->layer_view_index == 1)) return; + + int index = (this->layer_view_index ^ 1); + brls::View *current_focus = brls::Application::getCurrentFocus(); + + /* Focus the sidebar if we're currently focusing an element from our List. */ + for(brls::View* list_view : this->list_views) + { + if (current_focus == list_view) + { + brls::Application::onGamepadButtonPressed(GLFW_GAMEPAD_BUTTON_DPAD_LEFT, false); + break; + } + } + + /* Change layer view. */ + this->changeLayer(index); + this->invalidate(true); + this->layer_view_index = index; + } + + void LayeredErrorFrame::SetErrorFrameMessage(std::string msg) + { + this->error_frame->SetMessage(msg); + } + + void LayeredErrorFrame::AddListView(brls::View* view) + { + this->list->addView(view); + this->list_views.push_back(view); + } +}