1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

FileTreeComponent: Avoid blocking the main thread when selecting items while scanning is in progress

This commit is contained in:
reuk 2022-08-31 17:06:25 +01:00
parent 21c214aec8
commit 86ad2a77a0
No known key found for this signature in database
GPG key ID: FCB43929F012EE5C

View file

@ -117,39 +117,30 @@ public:
newList->addChangeListener (this);
}
bool selectFile (const File& target)
void selectFile (const File& target)
{
if (file == target)
{
setSelected (true, true);
return true;
return;
}
if (target.isAChildOf (file))
if (subContentsList != nullptr && subContentsList->isStillLoading())
{
setOpen (true);
for (int maxRetries = 500; --maxRetries > 0;)
{
for (int i = 0; i < getNumSubItems(); ++i)
if (auto* f = dynamic_cast<FileListTreeItem*> (getSubItem (i)))
if (f->selectFile (target))
return true;
// if we've just opened and the contents are still loading, wait for it..
if (subContentsList != nullptr && subContentsList->isStillLoading())
{
Thread::sleep (10);
rebuildItemsFromContentList();
}
else
{
break;
}
}
pendingFileSelection.emplace (*this, target);
return;
}
return false;
pendingFileSelection.reset();
if (! target.isAChildOf (file))
return;
setOpen (true);
for (int i = 0; i < getNumSubItems(); ++i)
if (auto* f = dynamic_cast<FileListTreeItem*> (getSubItem (i)))
f->selectFile (target);
}
void changeListenerCallback (ChangeBroadcaster*) override
@ -224,6 +215,33 @@ public:
const File file;
private:
class PendingFileSelection : private Timer
{
public:
PendingFileSelection (FileListTreeItem& item, const File& f)
: owner (item), fileToSelect (f)
{
startTimer (10);
}
~PendingFileSelection() override
{
stopTimer();
}
private:
void timerCallback() override
{
// Take a copy of the file here, in case this PendingFileSelection
// object is destroyed during the call to selectFile.
owner.selectFile (File { fileToSelect });
}
FileListTreeItem& owner;
File fileToSelect;
};
Optional<PendingFileSelection> pendingFileSelection;
FileTreeComponent& owner;
DirectoryContentsList* parentContentsList;
int indexInContentsList;
@ -316,8 +334,7 @@ void FileTreeComponent::setDragAndDropDescription (const String& description)
void FileTreeComponent::setSelectedFile (const File& target)
{
if (auto* t = dynamic_cast<FileListTreeItem*> (getRootItem()))
if (! t->selectFile (target))
clearSelectedItems();
t->selectFile (target);
}
void FileTreeComponent::setItemHeight (int newHeight)