From 12e7cb0625389075fe02c5cb4961d59c942157d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=A9meryc?= Date: Wed, 12 Jul 2023 21:41:09 -0400 Subject: [PATCH 1/2] Open Folder in File Explorer --- Editor/Editor.cpp | 4 ++-- Editor/src/NewEditor.cpp | 3 +-- Editor/src/Windows/EditorInterface.cpp | 2 +- Editor/src/Windows/FileSystemUI.cpp | 12 ++++++++++-- Editor/src/Windows/WelcomeWindow.cpp | 2 +- Nuake/Engine.cpp | 2 +- Nuake/src/Core/FileSystem.cpp | 6 ++++++ Nuake/src/Core/FileSystem.h | 1 + Nuake/src/Core/OS.cpp | 17 +++++++++++++++++ Nuake/src/Core/OS.h | 11 ++++++----- 10 files changed, 46 insertions(+), 14 deletions(-) create mode 100644 Nuake/src/Core/OS.cpp diff --git a/Editor/Editor.cpp b/Editor/Editor.cpp index c1d33b85..0c96cbe1 100644 --- a/Editor/Editor.cpp +++ b/Editor/Editor.cpp @@ -92,7 +92,7 @@ int main(int argc, char* argv[]) editor.BuildFonts(); Ref project = Nuake::Project::New(); - FileSystem::SetRootDirectory(projectPath + "/../"); + FileSystem::SetRootDirectory(FileSystem::RemoveFileFromPath(projectPath)); project->FullPath = projectPath; project->Deserialize(FileSystem::ReadFile(projectPath, true)); @@ -143,7 +143,7 @@ int main(int argc, char* argv[]) if (shouldLoadProject) { - FileSystem::SetRootDirectory(projectPath + "/../"); + FileSystem::SetRootDirectory(FileSystem::RemoveFileFromPath(projectPath)); auto project = Project::New(); auto projectFileData = FileSystem::ReadFile(projectPath, true); diff --git a/Editor/src/NewEditor.cpp b/Editor/src/NewEditor.cpp index d7c57c13..31765cba 100644 --- a/Editor/src/NewEditor.cpp +++ b/Editor/src/NewEditor.cpp @@ -41,8 +41,7 @@ namespace Nuake { // Parse the project and load it. std::string projectPath = FileDialog::OpenFile(".project"); - - FileSystem::SetRootDirectory(projectPath + "/../"); + FileSystem::SetRootDirectory(FileSystem::RemoveFileFromPath(projectPath)); Ref project = Project::New(); if (!project->Deserialize(FileSystem::ReadFile(projectPath, true))) { diff --git a/Editor/src/Windows/EditorInterface.cpp b/Editor/src/Windows/EditorInterface.cpp index 292dbe6b..fda63778 100644 --- a/Editor/src/Windows/EditorInterface.cpp +++ b/Editor/src/Windows/EditorInterface.cpp @@ -1291,7 +1291,7 @@ namespace Nuake { if (projectPath == "") // Hit cancel. return; - FileSystem::SetRootDirectory(projectPath + "/../"); + FileSystem::SetRootDirectory(FileSystem::RemoveFileFromPath(projectPath)); Ref project = Project::New(); if (!project->Deserialize(FileSystem::ReadFile(projectPath, true))) { diff --git a/Editor/src/Windows/FileSystemUI.cpp b/Editor/src/Windows/FileSystemUI.cpp index 1d7a25af..13440a6e 100644 --- a/Editor/src/Windows/FileSystemUI.cpp +++ b/Editor/src/Windows/FileSystemUI.cpp @@ -176,15 +176,23 @@ namespace Nuake if (ImGui::BeginPopup(hoverMenuId.c_str())) { + if (ImGui::MenuItem("Show in File Explorer")) + { + OS::OpenInFileExplorer(file->GetAbsolutePath()); + } + + ImGui::Separator(); + if (ImGui::MenuItem("Delete")) { if(FileSystem::RemoveFile(file->GetAbsolutePath()) != 0) { - Logger::Log("Failed to remove file: " + file->GetAbsolutePath(), CRITICAL); + Logger::Log("Failed to remove file: " + file->GetRelativePath(), CRITICAL); } + RefreshFileBrowser(); } + ImGui::EndPopup(); - RefreshFileBrowser(); } ImGui::Text(file->GetName().c_str()); diff --git a/Editor/src/Windows/WelcomeWindow.cpp b/Editor/src/Windows/WelcomeWindow.cpp index 0c2040f0..9ebe5687 100644 --- a/Editor/src/Windows/WelcomeWindow.cpp +++ b/Editor/src/Windows/WelcomeWindow.cpp @@ -254,7 +254,7 @@ namespace Nuake SaveRecentFile(); std::string projectPath = _Projects[SelectedProject].Path; - FileSystem::SetRootDirectory(projectPath + "/../"); + FileSystem::SetRootDirectory(FileSystem::RemoveFileFromPath(projectPath)); auto project = Project::New(); auto projectFileData = FileSystem::ReadFile(projectPath, true); diff --git a/Nuake/Engine.cpp b/Nuake/Engine.cpp index 05d72b37..93f13926 100644 --- a/Nuake/Engine.cpp +++ b/Nuake/Engine.cpp @@ -163,7 +163,7 @@ namespace Nuake return false; } - FileSystem::SetRootDirectory(project->FullPath + "/../"); + FileSystem::SetRootDirectory(FileSystem::RemoveFileFromPath(project->FullPath)); return true; } diff --git a/Nuake/src/Core/FileSystem.cpp b/Nuake/src/Core/FileSystem.cpp index 5c744d1d..34eb2164 100644 --- a/Nuake/src/Core/FileSystem.cpp +++ b/Nuake/src/Core/FileSystem.cpp @@ -124,6 +124,12 @@ namespace Nuake return fs::relative(absolutePath, rootPath).generic_string(); } + std::string FileSystem::RemoveFileFromPath(const std::string& fullPath) + { + std::filesystem::path pathObj(fullPath); + return pathObj.parent_path().string(); + } + std::string FileSystem::ReadFile(const std::string& path, bool absolute) { std::string finalPath = path; diff --git a/Nuake/src/Core/FileSystem.h b/Nuake/src/Core/FileSystem.h index 18e96c80..9f43464e 100644 --- a/Nuake/src/Core/FileSystem.h +++ b/Nuake/src/Core/FileSystem.h @@ -30,6 +30,7 @@ namespace Nuake static void Scan(); static std::string AbsoluteToRelative(const std::string& path); + static std::string RemoveFileFromPath(const std::string& fullPath); static Ref GetFileTree(); static Ref GetFile(const std::string& path); static std::string GetFileNameFromPath(const std::string& path); diff --git a/Nuake/src/Core/OS.cpp b/Nuake/src/Core/OS.cpp new file mode 100644 index 00000000..db06ed01 --- /dev/null +++ b/Nuake/src/Core/OS.cpp @@ -0,0 +1,17 @@ +#include "OS.h" + +#include +#include "String.h" +#include + +using namespace Nuake; + +int OS::GetTime() +{ + return static_cast(std::chrono::system_clock::now().time_since_epoch().count()); +} + +void OS::OpenInFileExplorer(const std::string& filePath) +{ + ShellExecuteA(nullptr, "open", "explorer.exe", ("/select," + std::string(filePath)).c_str(), nullptr, SW_SHOWDEFAULT); +} \ No newline at end of file diff --git a/Nuake/src/Core/OS.h b/Nuake/src/Core/OS.h index 15430935..0d9be97e 100644 --- a/Nuake/src/Core/OS.h +++ b/Nuake/src/Core/OS.h @@ -1,13 +1,14 @@ -#pragma once +#pragma once + +#include + namespace Nuake { class OS { public: - static int GetTime() - { - return static_cast(std::chrono::system_clock::now().time_since_epoch().count()); - } + static int GetTime(); + static void OpenInFileExplorer(const std::string& filePath); }; } From 43d1e07aba3d71212c11494d97d54d546961be20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=A9meryc?= Date: Wed, 12 Jul 2023 22:10:23 -0400 Subject: [PATCH 2/2] PR Review comments --- Editor/Editor.cpp | 4 ++-- Editor/src/NewEditor.cpp | 2 +- Editor/src/Windows/EditorInterface.cpp | 2 +- Editor/src/Windows/FileSystemUI.cpp | 2 +- Editor/src/Windows/WelcomeWindow.cpp | 2 +- Nuake/Engine.cpp | 2 +- Nuake/src/Core/FileSystem.cpp | 2 +- Nuake/src/Core/FileSystem.h | 2 +- Nuake/src/Core/OS.cpp | 3 +-- Nuake/src/Core/OS.h | 3 +-- 10 files changed, 11 insertions(+), 13 deletions(-) diff --git a/Editor/Editor.cpp b/Editor/Editor.cpp index 0c96cbe1..db9a8176 100644 --- a/Editor/Editor.cpp +++ b/Editor/Editor.cpp @@ -92,7 +92,7 @@ int main(int argc, char* argv[]) editor.BuildFonts(); Ref project = Nuake::Project::New(); - FileSystem::SetRootDirectory(FileSystem::RemoveFileFromPath(projectPath)); + FileSystem::SetRootDirectory(FileSystem::GetParentPath(projectPath)); project->FullPath = projectPath; project->Deserialize(FileSystem::ReadFile(projectPath, true)); @@ -143,7 +143,7 @@ int main(int argc, char* argv[]) if (shouldLoadProject) { - FileSystem::SetRootDirectory(FileSystem::RemoveFileFromPath(projectPath)); + FileSystem::SetRootDirectory(FileSystem::GetParentPath(projectPath)); auto project = Project::New(); auto projectFileData = FileSystem::ReadFile(projectPath, true); diff --git a/Editor/src/NewEditor.cpp b/Editor/src/NewEditor.cpp index 31765cba..857aba47 100644 --- a/Editor/src/NewEditor.cpp +++ b/Editor/src/NewEditor.cpp @@ -41,7 +41,7 @@ namespace Nuake { // Parse the project and load it. std::string projectPath = FileDialog::OpenFile(".project"); - FileSystem::SetRootDirectory(FileSystem::RemoveFileFromPath(projectPath)); + FileSystem::SetRootDirectory(FileSystem::GetParentPath(projectPath)); Ref project = Project::New(); if (!project->Deserialize(FileSystem::ReadFile(projectPath, true))) { diff --git a/Editor/src/Windows/EditorInterface.cpp b/Editor/src/Windows/EditorInterface.cpp index fda63778..7e2b9412 100644 --- a/Editor/src/Windows/EditorInterface.cpp +++ b/Editor/src/Windows/EditorInterface.cpp @@ -1291,7 +1291,7 @@ namespace Nuake { if (projectPath == "") // Hit cancel. return; - FileSystem::SetRootDirectory(FileSystem::RemoveFileFromPath(projectPath)); + FileSystem::SetRootDirectory(FileSystem::GetParentPath(projectPath)); Ref project = Project::New(); if (!project->Deserialize(FileSystem::ReadFile(projectPath, true))) { diff --git a/Editor/src/Windows/FileSystemUI.cpp b/Editor/src/Windows/FileSystemUI.cpp index 13440a6e..3eb90511 100644 --- a/Editor/src/Windows/FileSystemUI.cpp +++ b/Editor/src/Windows/FileSystemUI.cpp @@ -178,7 +178,7 @@ namespace Nuake { if (ImGui::MenuItem("Show in File Explorer")) { - OS::OpenInFileExplorer(file->GetAbsolutePath()); + OS::ShowInFileExplorer(file->GetAbsolutePath()); } ImGui::Separator(); diff --git a/Editor/src/Windows/WelcomeWindow.cpp b/Editor/src/Windows/WelcomeWindow.cpp index 9ebe5687..3cd1a42d 100644 --- a/Editor/src/Windows/WelcomeWindow.cpp +++ b/Editor/src/Windows/WelcomeWindow.cpp @@ -254,7 +254,7 @@ namespace Nuake SaveRecentFile(); std::string projectPath = _Projects[SelectedProject].Path; - FileSystem::SetRootDirectory(FileSystem::RemoveFileFromPath(projectPath)); + FileSystem::SetRootDirectory(FileSystem::GetParentPath(projectPath)); auto project = Project::New(); auto projectFileData = FileSystem::ReadFile(projectPath, true); diff --git a/Nuake/Engine.cpp b/Nuake/Engine.cpp index 93f13926..9fed2815 100644 --- a/Nuake/Engine.cpp +++ b/Nuake/Engine.cpp @@ -163,7 +163,7 @@ namespace Nuake return false; } - FileSystem::SetRootDirectory(FileSystem::RemoveFileFromPath(project->FullPath)); + FileSystem::SetRootDirectory(FileSystem::GetParentPath(project->FullPath)); return true; } diff --git a/Nuake/src/Core/FileSystem.cpp b/Nuake/src/Core/FileSystem.cpp index 34eb2164..cc2b525a 100644 --- a/Nuake/src/Core/FileSystem.cpp +++ b/Nuake/src/Core/FileSystem.cpp @@ -124,7 +124,7 @@ namespace Nuake return fs::relative(absolutePath, rootPath).generic_string(); } - std::string FileSystem::RemoveFileFromPath(const std::string& fullPath) + std::string FileSystem::GetParentPath(const std::string& fullPath) { std::filesystem::path pathObj(fullPath); return pathObj.parent_path().string(); diff --git a/Nuake/src/Core/FileSystem.h b/Nuake/src/Core/FileSystem.h index 9f43464e..460a9966 100644 --- a/Nuake/src/Core/FileSystem.h +++ b/Nuake/src/Core/FileSystem.h @@ -30,7 +30,7 @@ namespace Nuake static void Scan(); static std::string AbsoluteToRelative(const std::string& path); - static std::string RemoveFileFromPath(const std::string& fullPath); + static std::string GetParentPath(const std::string& fullPath); static Ref GetFileTree(); static Ref GetFile(const std::string& path); static std::string GetFileNameFromPath(const std::string& path); diff --git a/Nuake/src/Core/OS.cpp b/Nuake/src/Core/OS.cpp index db06ed01..40721cda 100644 --- a/Nuake/src/Core/OS.cpp +++ b/Nuake/src/Core/OS.cpp @@ -1,7 +1,6 @@ #include "OS.h" #include -#include "String.h" #include using namespace Nuake; @@ -11,7 +10,7 @@ int OS::GetTime() return static_cast(std::chrono::system_clock::now().time_since_epoch().count()); } -void OS::OpenInFileExplorer(const std::string& filePath) +void OS::ShowInFileExplorer(const std::string& filePath) { ShellExecuteA(nullptr, "open", "explorer.exe", ("/select," + std::string(filePath)).c_str(), nullptr, SW_SHOWDEFAULT); } \ No newline at end of file diff --git a/Nuake/src/Core/OS.h b/Nuake/src/Core/OS.h index 0d9be97e..c2fdbaf3 100644 --- a/Nuake/src/Core/OS.h +++ b/Nuake/src/Core/OS.h @@ -2,13 +2,12 @@ #include - namespace Nuake { class OS { public: static int GetTime(); - static void OpenInFileExplorer(const std::string& filePath); + static void ShowInFileExplorer(const std::string& filePath); }; }