44import mpos .wifi
55from mpos .ui .anim import WidgetAnimator
66import mpos .ui .topmenu
7+ import mpos .util
78
89th = None
910
@@ -172,30 +173,44 @@ def close_top_layer_msgboxes():
172173 print (f"Top layer still has { child_count } children" )
173174
174175
175- screen_stack = [] # Stack of (activity, screen) tuples
176+ screen_stack = [] # Stack of (activity, screen, focusgroup ) tuples
176177
177178def empty_screen_stack ():
178179 global screen_stack
179180 screen_stack .clear ()
180181
182+ def move_focusgroup_objects (fromgroup , togroup ):
183+ print (f"Moving { fromgroup .get_obj_count ()} focused objects" )
184+ for objnr in range (fromgroup .get_obj_count ()):
185+ #print(f"saving object {objnr} from default focusgroup to current_focusgroup")
186+ next = fromgroup .get_obj_by_index (0 )
187+ mpos .util .print_lvgl_widget (next )
188+ if next :
189+ togroup .add_obj (next )
190+ print ("Done moving focused objects" )
191+
192+
193+ # Saves all objects from the default focus group in the activity's focus group
194+ def save_and_clear_current_focusgroup ():
195+ global screen_stack
196+ if len (screen_stack ) > 0 :
197+ current_activity , current_screen , current_focusgroup = screen_stack [- 1 ]
198+ move_focusgroup_objects (lv .group_get_default (), current_focusgroup )
199+
181200# new_activity might be None for compatibility, can be removed if compatibility is no longer needed
182201def setContentView (new_activity , new_screen ):
183202 global screen_stack
184203
185204 # Get current activity and screen
186- current_activity , current_screen = None , None
187205 if len (screen_stack ) > 0 :
188- current_activity , current_screen = screen_stack [- 1 ]
189-
190- if current_activity and current_screen :
206+ current_activity , current_screen , current_focusgroup = screen_stack [- 1 ]
191207 # Notify current activity that it's being backgrounded:
192208 current_activity .onPause (current_screen )
193209 current_activity .onStop (current_screen )
194210 # don't destroy because the user might go back to it
195211
196- # Start the new one:
197- print ("Appending screen to screen_stack" )
198- screen_stack .append ((new_activity , new_screen ))
212+ print ("Appending new activity, new screen and new focusgroup to screen_stack" )
213+ screen_stack .append ((new_activity , new_screen , lv .group_create ()))
199214 close_top_layer_msgboxes () # otherwise they remain
200215 if new_activity :
201216 #start_time = utime.ticks_ms()
@@ -215,11 +230,13 @@ def setContentView(new_activity, new_screen):
215230 #print(f"ui.py setContentView: new_activity.onResume took {end_time}ms")
216231
217232def remove_and_stop_current_activity ():
218- current_activity , current_screen = screen_stack .pop () # Get current activity and its screen
233+ current_activity , current_screen , current_focusgroup = screen_stack .pop () # Get current activity and its screen
219234 if current_activity :
220235 current_activity .onPause (current_screen )
221236 current_activity .onStop (current_screen )
222237 current_activity .onDestroy (current_screen )
238+ if current_screen :
239+ current_screen .clean () # should free up memory
223240
224241def back_screen ():
225242 print ("back_screen() running" )
@@ -229,11 +246,14 @@ def back_screen():
229246 return False # No previous screen
230247 #close_top_layer_msgboxes() # would be nicer to "cancel" all input events
231248 remove_and_stop_current_activity ()
232- prev_activity , prev_screen = screen_stack [- 1 ] # load previous screen
249+ prev_activity , prev_screen , prev_focusgroup = screen_stack [- 1 ] # load previous screen
233250 print ("loading prev_screen with animation" )
234251 lv .screen_load_anim (prev_screen , lv .SCR_LOAD_ANIM .OVER_RIGHT , 500 , 0 , True ) # True means delete the old screen, which is fine as we're going back and current_activity.onDestroy() was called
252+ # Restore the focused objects
253+ move_focusgroup_objects (prev_focusgroup , lv .group_get_default ())
235254 if prev_activity :
236255 prev_activity .onResume (prev_screen )
256+ print (f"5 default focus group has { lv .group_get_default ().get_obj_count ()} items" )
237257 if len (screen_stack ) == 1 :
238258 mpos .ui .topmenu .open_bar ()
239259
0 commit comments