Skip to content

Commit

Permalink
gui: remember last path in file browser
Browse files Browse the repository at this point in the history
  • Loading branch information
JaCzekanski committed Sep 7, 2019
1 parent 291fe57 commit 5283d08
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
5 changes: 4 additions & 1 deletion src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,10 @@ const json defaultConfig = {
{"memoryCard", {
{"1", "data/memory/card1.mcr"},
{"2", ""}
}}
}},
{"gui", {
{"lastPath", ""},
}},
};
// clang-format on

Expand Down
46 changes: 36 additions & 10 deletions src/platform/windows/gui/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,21 @@ std::string pathInput = "";
fs::path path;
std::vector<fs::directory_entry> files;

std::array<const char*, 9> supportedFiles = {
".iso", //
".cue", //
".bin", //
".img", //
".chd", //
".exe", //
".psexe", //
".psf", //
".minipsf", //
};

void openFile() {
if (!openFileWindow) {
path = fs::current_path();
path = fs::path(config["gui"]["lastPath"]);
readDirectory = true;
}

Expand Down Expand Up @@ -55,6 +67,11 @@ void openFile() {
openFileWindow = true;
ImGui::Begin("Open file", &openFileWindow, ImVec2(400.f, 300.f), -1.f, ImGuiWindowFlags_NoCollapse);

if (ImGui::Button("Home")) {
readDirectory = true;
path = fs::current_path();
}
ImGui::SameLine();
if (ImGui::InputText("Directory", &pathInput, ImGuiInputTextFlags_EnterReturnsTrue)) {
readDirectory = true; // Load new directory
path = fs::path(pathInput);
Expand Down Expand Up @@ -86,26 +103,30 @@ void openFile() {
for (auto& f : files) {
auto filename = f.path().filename().string();
ImVec4 color = ImVec4(1.0, 1.0, 1.0, 1.0);
bool isSupported = false;

if (fs::is_directory(f)) {
color = ImVec4(0.34, 0.54, 0.56, 1.0);
}
if (filename[0] == '.' && filename != "..") {
} else if (filename[0] == '.' && filename != "..") {
color = ImVec4(color.x - 0.3, color.y - 0.3, color.z - 0.3, 1.0);
} else {
std::string ext = f.path().extension().string();
std::transform(ext.begin(), ext.end(), ext.begin(), tolower);
isSupported = std::find(supportedFiles.begin(), supportedFiles.end(), ext) != supportedFiles.end();

if (!isSupported) {
color = ImVec4(0.3, 0.3, 0.3, 1.0);
}
}

ImGui::PushStyleColor(ImGuiCol_Text, color);
if (ImGui::Selectable(filename.c_str(), false, ImGuiSelectableFlags_SpanAllColumns)) {
if (fs::is_directory(f)) {
path = f.path();
readDirectory = true;
} else if (fs::exists(f)) {
std::string ext = f.path().extension().string();
std::transform(ext.begin(), ext.end(), ext.begin(), tolower);
if (ext == ".iso" || ext == ".cue" || ext == ".bin" || ext == ".img" || ext == ".chd" || ext == ".exe" || ext == ".psexe") {
bus.notify(Event::File::Load{f.path().string(), true});
openFileWindow = false;
}
} else if (fs::exists(f) && isSupported) {
bus.notify(Event::File::Load{f.path().string(), true});
openFileWindow = false;
}
}
ImGui::PopStyleColor();
Expand Down Expand Up @@ -133,6 +154,11 @@ void openFile() {
ImGui::PopStyleVar();

ImGui::End();

// Save last path on window close
if (!openFileWindow) {
config["gui"]["lastPath"] = path.string();
}
}

void close() {}
Expand Down

0 comments on commit 5283d08

Please sign in to comment.