Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 61 additions & 11 deletions NeuralAmpModeler/NeuralAmpModelerControls.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ enum class NAMBrowserState
Loaded // when file loaded, show "Clear" button
};

enum class NAMFileLoadSource
{
ExistingBrowserSelection,
FilePickerSelection
};

// Where the corner button on the plugin (settings, close settings) goes
// :param rect: Rect for the whole plugin's UI
IRECT CornerButtonArea(const IRECT& rect)
Expand Down Expand Up @@ -297,7 +303,7 @@ class NAMFileBrowserControl : public IDirBrowseControlBase
if (pItem)
{
mSelectedItemIndex = mItems.Find(pItem);
LoadFileAtCurrentIndex();
LoadFileAtCurrentIndex(NAMFileLoadSource::ExistingBrowserSelection);
}
}
}
Expand All @@ -313,7 +319,7 @@ class NAMFileBrowserControl : public IDirBrowseControlBase
if (mSelectedItemIndex < 0)
mSelectedItemIndex = nItems - 1;

LoadFileAtCurrentIndex();
LoadFileAtCurrentIndex(NAMFileLoadSource::ExistingBrowserSelection);
};

auto nextFileFunc = [&](IControl* pCaller) {
Expand All @@ -325,7 +331,7 @@ class NAMFileBrowserControl : public IDirBrowseControlBase
if (mSelectedItemIndex >= nItems)
mSelectedItemIndex = 0;

LoadFileAtCurrentIndex();
LoadFileAtCurrentIndex(NAMFileLoadSource::ExistingBrowserSelection);
};

auto loadFileFunc = [&](IControl* pCaller) {
Expand All @@ -340,7 +346,7 @@ class NAMFileBrowserControl : public IDirBrowseControlBase
AddPath(path.Get(), "");
SetupMenu();
SelectFirstFile();
LoadFileAtCurrentIndex();
LoadFileAtCurrentIndex(NAMFileLoadSource::ExistingBrowserSelection);
}
});
#else
Expand All @@ -352,7 +358,7 @@ class NAMFileBrowserControl : public IDirBrowseControlBase
AddPath(path.Get(), "");
SetupMenu();
SetSelectedFile(fileName.Get());
LoadFileAtCurrentIndex();
LoadFileAtCurrentIndex(NAMFileLoadSource::FilePickerSelection, &fileName, &path);
}
});
#endif
Expand Down Expand Up @@ -412,15 +418,26 @@ class NAMFileBrowserControl : public IDirBrowseControlBase
SetBrowserState(NAMBrowserState::Empty);
}

void LoadFileAtCurrentIndex()
void LoadFileAtCurrentIndex(NAMFileLoadSource source, const WDL_String* filePickerFileName = nullptr,
const WDL_String* filePickerPath = nullptr)
{
if (mSelectedItemIndex > -1 && mSelectedItemIndex < NItems())
if (source == NAMFileLoadSource::FilePickerSelection && mSelectedItemIndex == -1 && filePickerFileName != nullptr
&& filePickerPath != nullptr)
{
ReportDirectoryScanFailure(*filePickerFileName, *filePickerPath);
return;
}

if (mSelectedItemIndex < 0 || mSelectedItemIndex >= NItems())
{
WDL_String fileName, path;
GetSelectedFile(fileName);
mFileNameControl->SetLabelAndTooltipEllipsizing(fileName);
mCompletionHandlerFunc(fileName, path);
ReportUnexpectedLoadFailure(source, filePickerFileName);
return;
}

WDL_String fileName, path;
GetSelectedFile(fileName);
mFileNameControl->SetLabelAndTooltipEllipsizing(fileName);
mCompletionHandlerFunc(fileName, path);
}

void OnMsgFromDelegate(int msgTag, int dataSize, const void* pData) override
Expand Down Expand Up @@ -456,6 +473,39 @@ class NAMFileBrowserControl : public IDirBrowseControlBase
}

private:
void ReportDirectoryScanFailure(const WDL_String& fileName, const WDL_String& path)
{
mFileNameControl->SetLabelAndTooltipEllipsizing(fileName);
const std::string label = std::string("(FAILED) ") + mFileNameControl->GetLabelStr();

std::stringstream message;
message << "The selected file '" << fileName.Get() << "' was not found after scanning directory '" << path.Get()
<< "'. The host may not have granted permission to enumerate that directory.";

mFileNameControl->SetLabelStr(label.c_str());
mFileNameControl->SetTooltip(message.str().c_str());
SetBrowserState(NAMBrowserState::Empty);
std::fprintf(stderr, "NAM: File picker selection produced index -1. %s\n", message.str().c_str());
}

void ReportUnexpectedLoadFailure(NAMFileLoadSource source, const WDL_String* filePickerFileName)
{
if (filePickerFileName != nullptr)
mFileNameControl->SetLabelAndTooltipEllipsizing(*filePickerFileName);

const std::string label = std::string("(FAILED) ") + mFileNameControl->GetLabelStr();
const std::string message =
"The selected file could not be loaded because the file browser encountered an "
"unexpected selection state. Please select the file again.";

mFileNameControl->SetLabelStr(label.c_str());
mFileNameControl->SetTooltip(message.c_str());
SetBrowserState(NAMBrowserState::Empty);
std::fprintf(stderr, "NAM: %s Source: %s; selected index: %d; item count: %d.\n", message.c_str(),
source == NAMFileLoadSource::FilePickerSelection ? "file picker" : "existing browser selection",
mSelectedItemIndex, NItems());
}

void SelectFirstFile() { mSelectedItemIndex = mFiles.GetSize() ? 0 : -1; }

void GetSelectedFileDirectory(WDL_String& path)
Expand Down
Loading