Has anyone figured out a robust way to get the root View of whatever is currently being presented?
I'm working on a little tool which resizes the current view to show what it looks like on various devices (using a scrollview for displaying a larger screen on a small device).
One approach (probably the one I should take) is that the user has to know the root view variable name, and pass it into my method. But what I am trying to do is to programmatically run a script, let it present the view, then step in and hijack the root.
This can be done by using the gc module to discover all Views in memory.
My original plan was to filter by on_screen, and filter for Views which have no superview or navigation_view.
What I didn't realize is that some on screen views might not have a superview.
Examples I have encountered are mostly Buttons and Labels (which I think are part of TableCellViews).
My current approach finds the largest frame of the candidates. This is sort of robust, but technically one could create a view that is larger than whatever contains it.
Alternatively, I could filter out Buttons/Labels, since these are rarely root views, although I have used Buttons as roots on many occasions as a poor man's touch_ended to avoid making a custom class.
Any other thoughts? Or undocumented ui.get_current_panel() that I've missed?
It would be great if a view knew whether it was being presented (and what type) and/or if the ui system could report the currently presented panel, fullscreen, popover, sheet, etc.
import gc
def findRootView():
'''Find the current root view... the hard and slow way!'''
gc.collect() #do a collect before listing remaining objects
candidate_root_views=[v for v in gc.get_objects()
if isinstance(type(v), ui.View)
and not v.superview
and not v.navigation_view]
#could also filter for v.on_screen if we've just executed a script that presents a view
#find view with largest frame
root_view = max(candidate_root_views,key=lambda v:v.width*v.height)
return root_view