BR: FileListComponent::setSelectedFile()

There is a bug where FileListComponent::setSelectedFile() won’t work when the directoryContentsList has just been given a new directory and is still loading.

The issue is that FileListComponent::lastDirectory isn’t updated immediately when directoryContentsList.setDirectory() is called, and so fileWaitingToBeSelected is cleared as soon as FileListComponent::changeListenerCallback() is called here:

Here is a PIP to reproduce (a FileListComponent loading the /Applications folder and trying to select /Applications/Utilities)

PIP
/*******************************************************************************
 The block below describes the properties of this PIP. A PIP is a short snippet
 of code that can be read by the Projucer and used to generate a JUCE project.

 BEGIN_JUCE_PIP_METADATA

  name:             TestPip

  dependencies:     juce_core, juce_data_structures, juce_events, juce_graphics, juce_gui_basics
  exporters:        XCODE_MAC

  moduleFlags:      JUCE_STRICT_REFCOUNTEDPOINTER=1

  type:             Component
  mainClass:        MyComponent

 END_JUCE_PIP_METADATA

*******************************************************************************/

#pragma once


//==============================================================================
class MyComponent  : public juce::Component
{
public:
    //==============================================================================
    MyComponent()
    {
        addAndMakeVisible (fileListComponent);
        timeSliceThread.startThread();

        auto applicationsDir = File::getSpecialLocation (File::globalApplicationsDirectory);
        directoryContentsList.setDirectory (applicationsDir, File::findFilesAndDirectories, true);
        fileListComponent.setSelectedFile (applicationsDir.getChildFile ("Utilities"));
        setSize (600, 400);
    }

    //==============================================================================
    void paint (juce::Graphics& g) override { g.fillAll (Colours::white); }
    void resized() override { fileListComponent.setBounds (getLocalBounds()); }

private:
    //==============================================================================
    TimeSliceThread timeSliceThread { "timeSliceThread" };
    DirectoryContentsList directoryContentsList { nullptr, timeSliceThread };
    FileListComponent fileListComponent { directoryContentsList };
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MyComponent)
};
1 Like

let me know if you can’t reproduce with the provided PIP.
a fix would be welcome! :slight_smile:
it doesn’t seem possible to have a reliable FileListComponent without dirty workarounds atm