22#include < math.h>
33#include < stack>
44
5+ std::shared_ptr<IScreen> CurrentScreen;
56static std::stack<std::shared_ptr<IScreen>> screenHistory;
67
78static inline void IScreen_leave ()
89{
910 // only leave if we have something to go back to
10- if (screenHistory.size () > 1 )
11+ if (screenHistory.size () > 0 )
1112 {
1213 // clear the current screen of lvgl
1314 lv_obj_clean (lv_scr_act ());
1415
15- // remove current screen
16- screenHistory.pop ();
1716 // draw previous screen
1817 screenHistory.top ()->draw ();
19- // remove it once again as the draw function has added it again
18+ // remove current screen
2019 screenHistory.pop ();
2120 }
2221 else
2322 {
24- LV_LOG_INFO (" I won't leave the first screen!" );
23+ LV_LOG_INFO (" No previous screen!" );
2524 }
2625}
2726
@@ -40,10 +39,12 @@ static void ScreenMenu_submenu_cb(lv_event_t *e)
4039
4140 if ((code == LV_EVENT_SHORT_CLICKED) && (item != nullptr ))
4241 {
42+ // save current screen in history
43+ screenHistory.push (CurrentScreen);
4344 // if we were clicked shortly, draw the next menu screen
4445 lv_obj_clean (lv_scr_act ());
45- auto ptrSubMenuScreen = new ScreenMenu{ *item->getSubMenuList ()} ;
46- ptrSubMenuScreen ->draw ();
46+ CurrentScreen = std::make_shared< ScreenMenu>( *item->getSubMenuList ()) ;
47+ CurrentScreen ->draw ();
4748 }
4849 else if (code == LV_EVENT_KEY)
4950 {
@@ -104,9 +105,11 @@ static void ScreenMenu_value_cb(lv_event_t *e)
104105
105106 if ((code == LV_EVENT_SHORT_CLICKED) && (item != nullptr ))
106107 {
108+ // save current screen in history
109+ screenHistory.push (CurrentScreen);
107110 // if we were clicked shortly, draw the value modification screen
108- auto ValModifyScreen = new ScreenValueModifier{ item} ;
109- ValModifyScreen ->draw ();
111+ CurrentScreen = std::make_shared< ScreenValueModifier>( item) ;
112+ CurrentScreen ->draw ();
110113 }
111114 else if (code == LV_EVENT_KEY)
112115 {
@@ -253,9 +256,6 @@ void ScreenMenu::draw()
253256
254257 /* actually draw the screen with lvgl */
255258 lv_scr_load (screen);
256-
257- /* add the screen to the screens history list */
258- screenHistory.push (shared_from_this ());
259259}
260260
261261/* *
@@ -464,7 +464,4 @@ void ScreenValueModifier::draw()
464464
465465 /* actually draw the screen with lvgl */
466466 lv_scr_load (screen);
467-
468- /* add the screen to the screens history list */
469- screenHistory.push (shared_from_this ());
470467}
0 commit comments