From 18c4672ba26b38c94a04027283fabfbed2c0ebeb Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Mon, 14 Dec 2015 10:52:08 -0800 Subject: [PATCH] Clean up navigation stack alloc/delete sequence. --- firmware/application/ui_navigation.cpp | 44 ++++++++++++-------------- firmware/application/ui_navigation.hpp | 3 +- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/firmware/application/ui_navigation.cpp b/firmware/application/ui_navigation.cpp index afa1bc73..3f6a0a38 100644 --- a/firmware/application/ui_navigation.cpp +++ b/firmware/application/ui_navigation.cpp @@ -49,43 +49,41 @@ NavigationView::NavigationView() } void NavigationView::push_view(View* new_view) { - // TODO: Trap nullptr? - // TODO: Trap push of object already on stack? + free_view(); + view_stack.push_back(new_view); - set_view(new_view); + + update_view(); } void NavigationView::pop() { // Can't pop last item from stack. if( view_stack.size() > 1 ) { - const auto old_view = view_stack.back(); + free_view(); + + delete view_stack.back(); view_stack.pop_back(); - const auto new_view = view_stack.back(); - set_view(new_view); - delete old_view; + + update_view(); } } +void NavigationView::free_view() { + remove_child(view()); +} + +void NavigationView::update_view() { + const auto new_view = view_stack.back(); + add_child(new_view); + new_view->set_parent_rect({ {0, 0}, size() }); + focus(); + set_dirty(); +} + Widget* NavigationView::view() const { return children_.empty() ? nullptr : children_[0]; } -void NavigationView::set_view(Widget* const new_view) { - const auto old_view = view(); - if( old_view ) { - remove_child(old_view); - } - - // TODO: Allow new_view == nullptr?! - if( new_view ) { - add_child(new_view); - new_view->set_parent_rect({ {0, 0}, size() }); - focus(); - } - - set_dirty(); -} - void NavigationView::focus() { if( view() ) { view()->focus(); diff --git a/firmware/application/ui_navigation.hpp b/firmware/application/ui_navigation.hpp index 0e12438e..4d4cd2ad 100644 --- a/firmware/application/ui_navigation.hpp +++ b/firmware/application/ui_navigation.hpp @@ -72,8 +72,9 @@ private: std::vector view_stack; Widget* view() const; - void set_view(Widget* const new_view); + void free_view(); + void update_view(); void push_view(View* new_view); };