Properly refresh openness state with FileTreeComponent

Has anyone figured out a way to maintain the openness state of multiple levels of a tree structure using FileTreeComponent? Every other topic I’ve seen on the matter is 4+ years old and unresolved. This seems like something that should be pretty simple, but nothing I’ve come across in the documentation seems to work the way I want it to. Basically I would like this pattern:

  • user opens multiple levels of a file tree
  • user drags file into tree
  • store openness state of tree
  • refresh contents of tree
  • restore openness state of tree

The closest I’ve gotten is using get/restoreOpennessState() from the TreeView class. However, even if I wait for the refresh to finish (as determined by looking at isStillLoading() on the DirectoryContentsList object), the best I’ve gotten out of this method is a single level of the file tree being opened back up.

Previous threads on the topic :

Commenting in case someone stumbles across this later - I left this alone for a while and came back to it, but still couldn’t find a robust way around it. So I hacked it and added a timer to spam the reloadCollection() method until the tree has opened back up.
It’s a bit weird to me that this is still an issue given how simple it would be to fix, but it’s not really worth modifying the JUCE library code unless you’ve already done so elsewhere

I think you don’t need to wait for finishing. You can apply the opennessState every time new items arrive from the DirectoryContentsList. Just be careful, when you read the state, since then you discard the information about items, that were not read.

  • when the user opens or closes a leaf, and isStillLoading() is false, store the opennessState
  • whenever DirectoryContentsList reads new entries (ChangeListener), apply the opennessState

Hope that helps