From 97f29f83365194e870c9c53edc3617f4adecc4c6 Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Mon, 14 Dec 2015 10:22:42 -0800 Subject: [PATCH] Clean up UI navigation push constructor scheme. --- firmware/application/ui_debug.cpp | 26 +++++++++++++------------- firmware/application/ui_navigation.cpp | 18 +++++++++--------- firmware/application/ui_navigation.hpp | 11 ++++++++++- firmware/application/ui_receiver.cpp | 3 +-- firmware/application/ui_setup.cpp | 6 +++--- 5 files changed, 36 insertions(+), 28 deletions(-) diff --git a/firmware/application/ui_debug.cpp b/firmware/application/ui_debug.cpp index a07bea91c..154208541 100644 --- a/firmware/application/ui_debug.cpp +++ b/firmware/application/ui_debug.cpp @@ -108,22 +108,22 @@ void RegistersWidget::draw_values( DebugMenuView::DebugMenuView(NavigationView& nav) { add_items<7>({ { - { "Memory", [&nav](){ nav.push(new DebugMemoryView { nav }); } }, - { "Radio State", [&nav](){ nav.push(new NotImplementedView { nav }); } }, - { "SD Card", [&nav](){ nav.push(new NotImplementedView { nav }); } }, - { "RFFC5072", [&nav](){ nav.push(new RegistersView { - nav, "RFFC5072", { 31, 2, 4, 4 }, + { "Memory", [&nav](){ nav.push(nav); } }, + { "Radio State", [&nav](){ nav.push(nav); } }, + { "SD Card", [&nav](){ nav.push(nav); } }, + { "RFFC5072", [&nav](){ nav.push( + nav, "RFFC5072", RegistersWidgetConfig { 31, 2, 4, 4 }, [](const size_t register_number) { return radio::first_if.read(register_number); } - }); } }, - { "MAX2837", [&nav](){ nav.push(new RegistersView { - nav, "MAX2837", { 32, 2, 3, 4 }, + ); } }, + { "MAX2837", [&nav](){ nav.push( + nav, "MAX2837", RegistersWidgetConfig { 32, 2, 3, 4 }, [](const size_t register_number) { return radio::second_if.read(register_number); } - }); } }, - { "Si5351C", [&nav](){ nav.push(new RegistersView { - nav, "Si5351C", { 96, 2, 2, 8 }, + ); } }, + { "Si5351C", [&nav](){ nav.push( + nav, "Si5351C", RegistersWidgetConfig { 96, 2, 2, 8 }, [](const size_t register_number) { return portapack::clock_generator.read_register(register_number); } - }); } }, - { "WM8731", [&nav](){ nav.push(new NotImplementedView { nav }); } }, + ); } }, + { "WM8731", [&nav](){ nav.push(nav); } }, } }); on_left = [&nav](){ nav.pop(); }; } diff --git a/firmware/application/ui_navigation.cpp b/firmware/application/ui_navigation.cpp index f603c3cc0..afa1bc731 100644 --- a/firmware/application/ui_navigation.cpp +++ b/firmware/application/ui_navigation.cpp @@ -48,7 +48,7 @@ NavigationView::NavigationView() { } -void NavigationView::push(View* new_view) { +void NavigationView::push_view(View* new_view) { // TODO: Trap nullptr? // TODO: Trap push of object already on stack? view_stack.push_back(new_view); @@ -96,13 +96,13 @@ void NavigationView::focus() { SystemMenuView::SystemMenuView(NavigationView& nav) { add_items<7>({ { - { "Receiver", [&nav](){ nav.push(new ReceiverView { nav, portapack::receiver_model }); } }, - { "Capture", [&nav](){ nav.push(new NotImplementedView { nav }); } }, - { "Analyze", [&nav](){ nav.push(new NotImplementedView { nav }); } }, - { "Setup", [&nav](){ nav.push(new SetupMenuView { nav }); } }, - { "About", [&nav](){ nav.push(new AboutView { nav }); } }, - { "Debug", [&nav](){ nav.push(new DebugMenuView { nav }); } }, - { "HackRF", [&nav](){ nav.push(new HackRFFirmwareView { nav }); } }, + { "Receiver", [&nav](){ nav.push(nav, portapack::receiver_model); } }, + { "Capture", [&nav](){ nav.push(nav); } }, + { "Analyze", [&nav](){ nav.push(nav); } }, + { "Setup", [&nav](){ nav.push(nav); } }, + { "About", [&nav](){ nav.push(nav); } }, + { "Debug", [&nav](){ nav.push(nav); } }, + { "HackRF", [&nav](){ nav.push(nav); } }, } }); } @@ -138,7 +138,7 @@ SystemView::SystemView( // Initial view. // TODO: Restore from non-volatile memory? - navigation_view.push(new SystemMenuView { navigation_view }); + navigation_view.push(navigation_view); } Context& SystemView::context() const { diff --git a/firmware/application/ui_navigation.hpp b/firmware/application/ui_navigation.hpp index 7307aed92..0e12438e5 100644 --- a/firmware/application/ui_navigation.hpp +++ b/firmware/application/ui_navigation.hpp @@ -33,6 +33,7 @@ #include "ui_sd_card_status_view.hpp" #include +#include namespace ui { @@ -56,7 +57,13 @@ public: NavigationView(const NavigationView&) = delete; NavigationView(NavigationView&&) = delete; - void push(View* new_view); + template + T* push(Args&&... args) { + const auto new_view = new T(std::forward(args)...); + push_view(new_view); + return reinterpret_cast(new_view); + } + void pop(); void focus() override; @@ -66,6 +73,8 @@ private: Widget* view() const; void set_view(Widget* const new_view); + + void push_view(View* new_view); }; class SystemMenuView : public MenuView { diff --git a/firmware/application/ui_receiver.cpp b/firmware/application/ui_receiver.cpp index d540b2103..176dee47b 100644 --- a/firmware/application/ui_receiver.cpp +++ b/firmware/application/ui_receiver.cpp @@ -423,12 +423,11 @@ ReceiverView::ReceiverView( }; field_frequency.on_edit = [this, &nav]() { // TODO: Provide separate modal method/scheme? - auto new_view = new FrequencyKeypadView { nav, this->receiver_model.tuning_frequency() }; + auto new_view = nav.push(nav, this->receiver_model.tuning_frequency()); new_view->on_changed = [this](rf::Frequency f) { this->on_tuning_frequency_changed(f); this->field_frequency.set_value(f); }; - nav.push(new_view); }; field_frequency.on_show_options = [this]() { this->on_show_options_frequency(); diff --git a/firmware/application/ui_setup.cpp b/firmware/application/ui_setup.cpp index 178bac1a5..e7a93f982 100644 --- a/firmware/application/ui_setup.cpp +++ b/firmware/application/ui_setup.cpp @@ -159,9 +159,9 @@ void AboutView::focus() { SetupMenuView::SetupMenuView(NavigationView& nav) { add_items<3>({ { - { "Date/Time", [&nav](){ nav.push(new SetDateTimeView { nav }); } }, - { "Frequency Correction", [&nav](){ nav.push(new SetFrequencyCorrectionView { nav }); } }, - { "Touch", [&nav](){ nav.push(new NotImplementedView { nav }); } }, + { "Date/Time", [&nav](){ nav.push(nav); } }, + { "Frequency Correction", [&nav](){ nav.push(nav); } }, + { "Touch", [&nav](){ nav.push(nav); } }, } }); on_left = [&nav](){ nav.pop(); }; }