-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: Generalize FileChooser logic, fix Win internet-shortcut crash
- Loading branch information
1 parent
bdb6640
commit 1c898d2
Showing
5 changed files
with
125 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#include "FileChooserManager.h" | ||
|
||
FileChooserManager::FileChooserManager(AudioPluginAudioProcessor& processor) | ||
: processorRef(processor) | ||
{ | ||
lastOpenedFolder = juce::File::getSpecialLocation(juce::File::SpecialLocationType::userHomeDirectory); | ||
} | ||
|
||
// Generalized function, atm for single files only | ||
void FileChooserManager::openFileChooser(const juce::String& dialogTitle, | ||
const juce::File& initialDirectory, | ||
const juce::String& filePatterns, | ||
std::function<void(const juce::File&)> onValidFileChosenCallback) | ||
{ | ||
if (lastOpenedFolder.exists()) { | ||
dirToOpen = lastOpenedFolder; | ||
} else { | ||
dirToOpen = initialDirectory;; | ||
} | ||
|
||
fileChooser = std::make_unique<juce::FileChooser>(dialogTitle, dirToOpen, filePatterns, true); | ||
|
||
fileChooser->launchAsync(juce::FileBrowserComponent::openMode | juce::FileBrowserComponent::canSelectFiles, | ||
[this, filePatterns, onValidFileChosenCallback](const juce::FileChooser& chooser) | ||
{ | ||
juce::File chosen; | ||
auto results = chooser.getURLResults(); | ||
|
||
for (const auto& result : results) | ||
{ | ||
if (result.isLocalFile()) | ||
{ | ||
chosen = result.getLocalFile(); | ||
lastOpenedFolder = chosen.getParentDirectory(); | ||
break; // We only need one valid file | ||
} | ||
else { | ||
return; | ||
} | ||
} | ||
|
||
// Check if the file is valid (non-empty, correct extension based on filePatterns) | ||
if (chosen.getSize() != 0 && fileHasValidExtension(chosen, filePatterns)) | ||
onValidFileChosenCallback(chosen); | ||
else | ||
warningWindow.showWarningWindow(UnsupportedFileType); | ||
}); | ||
} | ||
|
||
bool FileChooserManager::fileHasValidExtension(const juce::File& file, const juce::String& filePatterns) | ||
{ | ||
juce::StringArray validExtensions; | ||
validExtensions.addTokens(filePatterns, ";,", "*"); | ||
|
||
for (auto& ext : validExtensions) | ||
{ | ||
ext = ext.trimCharactersAtStart("*."); // Remove wildcard characters | ||
if (file.hasFileExtension(ext)) | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
// Use case: Network-specific file chooser | ||
void FileChooserManager::openFileChooserForNetwork(int networkID) | ||
{ | ||
if (networkID != 1 && networkID != 2) | ||
{ | ||
jassertfalse; | ||
return; | ||
} | ||
|
||
auto onFileChosen = [this, networkID](const juce::File& file) | ||
{ | ||
processorRef.loadExternalModel(file.getFullPathName(), networkID); | ||
}; | ||
|
||
openFileChooser("Choose a model file...", | ||
juce::File::getSpecialLocation(juce::File::SpecialLocationType::userHomeDirectory), | ||
"*.ort", | ||
onFileChosen); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#pragma once | ||
#include <JuceHeader.h> | ||
#include "FileChooserManager.h" | ||
#include "PluginProcessor.h" | ||
|
||
class FileChooserManager | ||
{ | ||
public: | ||
FileChooserManager(AudioPluginAudioProcessor& processor); | ||
|
||
// Generalized file chooser function, atm for single files only | ||
void openFileChooser(const juce::String& dialogTitle, | ||
const juce::File& initialDirectory, | ||
const juce::String& filePatterns, | ||
std::function<void(const juce::File&)> onValidFileChosenCallback); | ||
|
||
bool fileHasValidExtension(const juce::File& file, const juce::String& filePatterns); | ||
|
||
// Network-specific file chooser function, using openFileChooser | ||
void openFileChooserForNetwork(int networkID); | ||
|
||
private: | ||
AudioPluginAudioProcessor& processorRef; | ||
|
||
std::unique_ptr<juce::FileChooser> fileChooser; | ||
juce::File dirToOpen; | ||
juce::File lastOpenedFolder; | ||
|
||
WarningWindow warningWindow; | ||
}; |