+ // keep track of the current screen, as only that needs a call to Screen::leave()
+ Screen* current_screen = m_screen_stack.empty() ? nullptr : m_screen_stack.back().get();
+
+ // Screen::setup() might push more screens, so loop till everything is done
+ while (!m_actions.empty())
+ {
+ // move actions to a new vector since setup() might modify it
+ auto actions = std::move(m_actions);
+
+ for(auto it = actions.begin(); it != actions.end(); ++it)
+ {
+ auto& action = *it;
+
+ switch (action.type)
+ {
+ case Action::POP_ACTION:
+ assert(!m_screen_stack.empty());
+ if (current_screen == m_screen_stack.back().get())
+ {
+ m_screen_stack.back()->leave();
+ }
+ m_screen_stack.pop_back();
+ break;
+
+ case Action::PUSH_ACTION:
+ assert(action.screen);
+
+ if (!m_screen_stack.empty())
+ {
+ if (current_screen == m_screen_stack.back().get())
+ {
+ m_screen_stack.back()->leave();
+ }
+ }
+ m_screen_stack.push_back(std::move(action.screen));
+ break;
+
+ case Action::QUIT_ACTION:
+ m_screen_stack.clear();
+ break;
+ }