@@ -173,7 +173,7 @@ def close_top_layer_msgboxes():
173173 print (f"Top layer still has { child_count } children" )
174174
175175
176- screen_stack = [] # Stack of (activity, screen, focusgroup) tuples
176+ screen_stack = [] # Stack of (activity, screen, focusgroup, focused_object ) tuples
177177
178178def empty_screen_stack ():
179179 global screen_stack
@@ -193,24 +193,30 @@ def move_focusgroup_objects(fromgroup, togroup):
193193# Saves all objects from the default focus group in the activity's focus group
194194def save_and_clear_current_focusgroup ():
195195 global screen_stack
196- if len (screen_stack ) > 0 :
197- current_activity , current_screen , current_focusgroup = screen_stack [- 1 ]
196+ default_focusgroup = lv .group_get_default ()
197+ if default_focusgroup and len (screen_stack ) > 0 :
198+ current_activity , current_screen , current_focusgroup , _ = screen_stack .pop ()
199+ current_focused_object = default_focusgroup .get_focused ()
200+ if current_focused_object :
201+ print ("current_focused_object: " )
202+ mpos .util .print_lvgl_widget (current_focused_object )
198203 move_focusgroup_objects (lv .group_get_default (), current_focusgroup )
204+ screen_stack .append ((current_activity , current_screen , current_focusgroup , current_focused_object ))
199205
200206# new_activity might be None for compatibility, can be removed if compatibility is no longer needed
201207def setContentView (new_activity , new_screen ):
202208 global screen_stack
203209
204210 # Get current activity and screen
205211 if len (screen_stack ) > 0 :
206- current_activity , current_screen , current_focusgroup = screen_stack [- 1 ]
212+ current_activity , current_screen , current_focusgroup , current_focused_object = screen_stack [- 1 ]
207213 # Notify current activity that it's being backgrounded:
208214 current_activity .onPause (current_screen )
209215 current_activity .onStop (current_screen )
210216 # don't destroy because the user might go back to it
211217
212218 print ("Appending new activity, new screen and new focusgroup to screen_stack" )
213- screen_stack .append ((new_activity , new_screen , lv .group_create ()))
219+ screen_stack .append ((new_activity , new_screen , lv .group_create (), None ))
214220 close_top_layer_msgboxes () # otherwise they remain
215221 if new_activity :
216222 #start_time = utime.ticks_ms()
@@ -230,14 +236,26 @@ def setContentView(new_activity, new_screen):
230236 #print(f"ui.py setContentView: new_activity.onResume took {end_time}ms")
231237
232238def remove_and_stop_current_activity ():
233- current_activity , current_screen , current_focusgroup = screen_stack .pop () # Get current activity and its screen
239+ current_activity , current_screen , current_focusgroup , current_focused_object = screen_stack .pop () # Get current activity and its screen
234240 if current_activity :
235241 current_activity .onPause (current_screen )
236242 current_activity .onStop (current_screen )
237243 current_activity .onDestroy (current_screen )
238244 if current_screen :
239245 current_screen .clean () # should free up memory
240246
247+ # This function is missing so emulate it using focus_next():
248+ def emulate_focus_obj (focusgroup , to_focus ):
249+ for objnr in range (focusgroup .get_obj_count ()):
250+ obj = focusgroup .get_obj_by_index (objnr )
251+ #print ("checking obj for equality...")
252+ mpos .util .print_lvgl_widget (obj )
253+ if obj is to_focus :
254+ #print("found it!")
255+ break
256+ else :
257+ focusgroup .focus_next ()
258+
241259def back_screen ():
242260 print ("back_screen() running" )
243261 global screen_stack
@@ -246,11 +264,15 @@ def back_screen():
246264 return False # No previous screen
247265 #close_top_layer_msgboxes() # would be nicer to "cancel" all input events
248266 remove_and_stop_current_activity ()
249- prev_activity , prev_screen , prev_focusgroup = screen_stack [- 1 ] # load previous screen
267+ prev_activity , prev_screen , prev_focusgroup , prev_focused_object = screen_stack [- 1 ] # load previous screen
250268 print ("loading prev_screen with animation" )
251269 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
252270 # Restore the focused objects
253- move_focusgroup_objects (prev_focusgroup , lv .group_get_default ())
271+ default_focusgroup = lv .group_get_default ()
272+ move_focusgroup_objects (prev_focusgroup , default_focusgroup )
273+ print ("restoring prev_focused_object: " )
274+ mpos .util .print_lvgl_widget (prev_focused_object )
275+ emulate_focus_obj (default_focusgroup , prev_focused_object ) # LVGL 9.3 should have: default_focusgroup.focus_obj(prev_focused_object)
254276 if prev_activity :
255277 prev_activity .onResume (prev_screen )
256278 print (f"5 default focus group has { lv .group_get_default ().get_obj_count ()} items" )
0 commit comments