Skip to content

Commit

Permalink
gui: fixed Open Avocado directory for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
JaCzekanski committed Jun 9, 2020
1 parent 5b108cd commit 9b602d0
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 12 deletions.
2 changes: 2 additions & 0 deletions premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ if os.istarget("windows") then
include "premake/sdl2.lua"
end

group "avocado"
project "core"
uuid "176665c5-37ff-4a42-bef8-02edaeb1b426"
kind "StaticLib"
Expand Down Expand Up @@ -302,6 +303,7 @@ project "avocado"
"SDL2",
}

group "tests"
project "avocado_test"
uuid "07e62c76-7617-4add-bfb5-a5dba4ef41ce"
kind "ConsoleApp"
Expand Down
4 changes: 2 additions & 2 deletions src/platform/windows/gui/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ void GUI::mainMenu(std::unique_ptr<System>& sys) {
}
if (ImGui::BeginMenu("File")) {
ImGui::MenuItem("Open", nullptr, &openFile.openWindowOpen);
#if defined(__APPLE__) || defined(__WIN32__) || defined(__WIN64__) || defined(__linux__)
#if defined(__APPLE__) || defined(_WIN32) || defined(__linux__)
ImGui::Separator();
if (ImGui::MenuItem("Open Avocado directory")) {
openFileBrowser(avocado::PATH_USER.c_str());
openFileBrowser(avocado::PATH_USER);
}
#endif
ImGui::Separator();
Expand Down
8 changes: 4 additions & 4 deletions src/platform/windows/gui/helper/file_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,15 @@ void FileDialog::display(bool& windowOpen) {

#if defined(__APPLE__)
if (ImGui::Button("Reveal in Finder")) {
openFileBrowser(path.string().c_str());
openFileBrowser(path.string());
}
#elif defined(__WIN32__) || defined(__WIN64__)
#elif defined(_WIN32)
if (ImGui::Button("Open in Explorer")) {
openFileBrowser(path.string().c_str());
openFileBrowser(path.string());
}
#elif defined(__linux__)
if (ImGui::Button("Open in file explorer")) {
openFileBrowser(path.string().c_str());
openFileBrowser(path.string());
}
#endif

Expand Down
3 changes: 2 additions & 1 deletion src/platform/windows/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ void changeWorkingDirectory() {
}

workingDirectory /= "data";

avocado::PATH_DATA = workingDirectory.string();
#endif

Expand Down Expand Up @@ -138,6 +137,8 @@ void changeWorkingDirectory() {
SDL_free(prefPath);
}
#endif
avocado::PATH_DATA = replaceAll(avocado::PATH_DATA, "\\", "/");
avocado::PATH_USER = replaceAll(avocado::PATH_USER, "\\", "/");

if (!endsWith(avocado::PATH_DATA, "/")) {
avocado::PATH_DATA += "/";
Expand Down
8 changes: 5 additions & 3 deletions src/platform/windows/utils/platform_tools.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "platform_tools.h"
#include <fmt/core.h>
#include <utils/string.h>

#if defined(ANDROID)
#include <jni.h>
#include <SDL.h>
Expand Down Expand Up @@ -30,11 +32,11 @@ bool hasExternalStoragePermission() {
}
#endif

void openFileBrowser(const char* path) {
void openFileBrowser(const std::string& path) {
#if defined(__APPLE__)
system(fmt::format("open \"{}\"", path).c_str());
#elif defined(__WIN32__) || defined(__WIN64__)
system(fmt::format("explorer \"{}\"", path).c_str());
#elif defined(_WIN32)
system(fmt::format("explorer \"{}\"", replaceAll(path, "/", "\\")).c_str());
#elif defined(__linux__)
system(fmt::format("xdg-open \"{}\"", path).c_str());
#endif
Expand Down
3 changes: 2 additions & 1 deletion src/platform/windows/utils/platform_tools.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#pragma once
#include <string>

#if defined(ANDROID)
bool hasExternalStoragePermission();
#endif

void openFileBrowser(const char* path);
void openFileBrowser(const std::string& path);
11 changes: 11 additions & 0 deletions src/utils/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,21 @@ std::string_view trim(std::string_view str) {

return trimmed;
}

bool endsWith(const std::string& a, const std::string& b) {
if (a.length() >= b.length()) {
return a.compare(a.length() - b.length(), b.length(), b) == 0;
} else {
return false;
}
}

std::string replaceAll(const std::string& str, const std::string& find, const std::string& replace) {
std::string s = str;
size_t pos = 0;
while ((pos = s.find(find, pos)) != std::string::npos) {
s.replace(pos, find.length(), replace);
pos += replace.length();
}
return s;
}
3 changes: 2 additions & 1 deletion src/utils/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

std::vector<std::string_view> split(std::string_view str, std::string_view delim);
std::string_view trim(std::string_view str);
bool endsWith(const std::string& a, const std::string& b);
bool endsWith(const std::string& a, const std::string& b);
std::string replaceAll(const std::string& str, const std::string& find, const std::string& replace);

0 comments on commit 9b602d0

Please sign in to comment.