Skip to content
Permalink
Browse files

gui: remember last path in file browser

  • Loading branch information
JaCzekanski committed Sep 7, 2019
1 parent 291fe57 commit 5283d088eefcbe47b538c43e21c128de5ace7922
Showing with 40 additions and 11 deletions.
  1. +4 −1 src/config.cpp
  2. +36 −10 src/platform/windows/gui/file.cpp
@@ -177,7 +177,10 @@ const json defaultConfig = {
{"memoryCard", {
{"1", "data/memory/card1.mcr"},
{"2", ""}
}}
}},
{"gui", {
{"lastPath", ""},
}},
};
// clang-format on

@@ -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;
}

@@ -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);
@@ -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();
@@ -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() {}

0 comments on commit 5283d08

Please sign in to comment.
You can’t perform that action at this time.