Can you lazy load submenus for file-browsing popupmenus (plugin preset system)?

I’m trying to make a popupmenu for my plugin’s preset system. I’ve been trying to mimic fabfilter’s popupmenu system which allows you to browse the file system via popupmenus/submenus. From some testing it seems fabfilter (or similar) tries to load the entire folder structure from the desired root preset folder and then mirror this with popupmenus and submenus. However, it seems to stops loading items if a certain amount of folders/items is met. For example, you get lots of blank submenus if you set the root preset folder to any folder with too much content e.g. C:.

But, I’m unsure what criteria it uses to decide which folders/presets to include, and I was thinking that a more lazy way of loading folders could be simpler and prevent the issue of loading blank submenus and not explaining why.

For example, one could populate a submenu’s items at the point that they are actually shown and then empty them when they disappear. Or to prevent running so many file system operations, one could keep a queue of submenus such that whenever a new submenu (one not in the queue) is shown, you add it to the front of the queue and remove menus from the back of the queue and empty them until all qeued menus are currently being shown or the queue contains n items. The only problem with this is that I cannot find an easy way to hook into when a popupmenu is actually about to be shown or about to disappear. I also thought perhaps I could achieve a similar result by creating the actual submenu when it’s about to be shown and removing it when no longer needed. But looks like you cannot remove submenus and I cannot find a way of hooking into when a submenu would need to be constructed. Does anyone know if this is possible?

So I thought for a second that I found a solution but turned out I hadn’t quite. I thought I could simply use a PopupMenu::CustomComponent for folders and then when these become highlighted, either build the respective submenu and add it or populate an existing empty submenu. But I cannot find a simple way to do this. You could poll isItemHighlighted() using a timer or try and work out what makes isItemHighlighted() become true and hook into that somehow (e.g. when the mouse is over the item or when it has err keyboard focus?). Any ideas?

In the end I gave up and instead have the entire popupmenu hierarchy built all at once with a hard limit to the number of items the popupmenu (including submenus) can hold. There is a refresh button the user can use to refresh it.