Skip to content
Open
Show file tree
Hide file tree
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
23 changes: 17 additions & 6 deletions Source/FilePicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ FilePicker::FilePicker(
setOpaque (true);

// setDisplayedFilePath(fluidSynthModel.getCurrentSoundFontAbsPath());
setDisplayedFilePath(valueTreeState.state.getChildWithName("soundFont").getProperty("path", ""));
String soundFontPath = valueTreeState.state.getChildWithName("soundFont").getProperty("path", "");
DBG("######## valueTreePropertyChanged getAbsolutePath("+soundFontPath+")="+Util::getAbsolutePath(soundFontPath));
soundFontPath = Util::getAbsolutePath(soundFontPath);
setDisplayedFilePath(soundFontPath);

addAndMakeVisible (fileChooser);
fileChooser.addListener (this);
Expand Down Expand Up @@ -91,8 +94,11 @@ void FilePicker::filenameComponentChanged (FilenameComponent*) {
#endif
// currentPath = fileChooser.getCurrentFile().getFullPathName();
// fluidSynthModel.onFileNameChanged(fileChooser.getCurrentFile().getFullPathName(), -1, -1);
String path = fileChooser.getCurrentFile().getFullPathName();
DBG("######## filenameComponentChanged getRelativeToHomePath("+path+")="+ Util::getRelativeToHomePath(path));
path = Util::getRelativeToHomePath(path);
Value value{valueTreeState.state.getChildWithName("soundFont").getPropertyAsValue("path", nullptr)};
value.setValue(fileChooser.getCurrentFile().getFullPathName());
value.setValue(path);
// value = fileChooser.getCurrentFile().getFullPathName();
}

Expand All @@ -102,8 +108,10 @@ void FilePicker::valueTreePropertyChanged(ValueTree& treeWhosePropertyHasChanged
// if (&treeWhosePropertyHasChanged == &valueTree) {
if (property == StringRef("path")) {
String soundFontPath = treeWhosePropertyHasChanged.getProperty("path", "");
DBG("######## valueTreePropertyChanged getAbsolutePath("+soundFontPath+")="+Util::getAbsolutePath(soundFontPath));
soundFontPath = Util::getAbsolutePath(soundFontPath);
DEBUG_PRINT(soundFontPath);
setDisplayedFilePath(soundFontPath);
// setDisplayedFilePath(soundFontPath);
// if (soundFontPath.isNotEmpty()) {
// loadFont(soundFontPath);
// }
Expand All @@ -112,17 +120,20 @@ void FilePicker::valueTreePropertyChanged(ValueTree& treeWhosePropertyHasChanged
}

void FilePicker::setDisplayedFilePath(const String& path) {
if (!shouldChangeDisplayedFilePath(path)) {
return;
}
if (!shouldChangeDisplayedFilePath(path)) {
return;
}
// currentPath = path;

DBG("######## setDisplayedFilePath "+path);
fileChooser.setCurrentFile(File(path), true, dontSendNotification);
}

bool FilePicker::shouldChangeDisplayedFilePath(const String &path) {
if (path.isEmpty()) {
return false;
}
DBG("######## shouldChangeDisplayedFilePath "+path+" VS "+ currentPath);
if (path == currentPath) {
return false;
}
Expand Down
6 changes: 4 additions & 2 deletions Source/FluidSynthModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,17 @@ void FluidSynthModel::valueTreePropertyChanged(ValueTree& treeWhosePropertyHasCh
StringRef path {String::fromCFString(cfPath.get())};
if (path.isNotEmpty()) {
CFURLStartAccessingSecurityScopedResource(cfURL.get());
unloadAndLoadFont(path);
DBG("######## getAbsolutePath("+path+")="+ Util::getAbsolutePath(path));
unloadAndLoadFont(Util::getAbsolutePath(path));
CFURLStopAccessingSecurityScopedResource(cfURL.get());
}
}
#else
if (property == StringRef("path")) {
String soundFontPath = treeWhosePropertyHasChanged.getProperty("path", "");
if (soundFontPath.isNotEmpty()) {
unloadAndLoadFont(soundFontPath);
DBG("######## getAbsolutePath("+soundFontPath+")="+ Util::getAbsolutePath(soundFontPath));
unloadAndLoadFont(Util::getAbsolutePath(soundFontPath));
}
}
#endif
Expand Down
42 changes: 41 additions & 1 deletion Source/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,49 @@
#endif

namespace Util {
inline String getHomeDirectory() {
static String homeDirectory = []() -> String {
#if JUCE_WINDOWS
const char* homeDir = std::getenv("USERPROFILE");
#else
const char* homeDir = std::getenv("HOME");
#endif
return homeDir != nullptr ? String(homeDir) : String();
}();

return homeDirectory;
}

inline String getAbsolutePath(const String& relativePath) {
String homeDir = getHomeDirectory();
String normalizedRelativePath = relativePath.replace("~", homeDir);
#if JUCE_WINDOWS
#else
normalizedRelativePath = normalizedRelativePath.replace("\\", "/");
#endif
File absoluteFile(normalizedRelativePath);
return absoluteFile.getFullPathName();
}

inline String getRelativeToHomePath(const String& absolutePath) {
String homeDir = getHomeDirectory();

if (absolutePath.startsWith(homeDir)) {
String resultPath = "~/" + absolutePath.substring(homeDir.length() + 1);
#if JUCE_WINDOWS
resultPath = resultPath.replace("\\", "/");
#else
#endif

return resultPath;
}

return absolutePath;
}

inline int compare(int a, int b) {
if (a > b) return 1;
if (a == b) return 0;
return -1;
}
}
}