Merge pull request #48 from antopilo/NK-141-rename-file&folder

NK-141 - Rename File/Folder
This commit is contained in:
émeryc
2023-07-23 00:01:42 -04:00
committed by GitHub
5 changed files with 131 additions and 13 deletions

View File

@@ -1,15 +1,15 @@
#include "PopupHelper.h"
#include "imgui/imgui.h"
#include "regex"
void PopupHelper::Confirmation(const std::string& id)
void PopupHelper::OpenPopup(const std::string& id)
{
ImGui::TextWrapped(id.c_str());
ImGui::OpenPopup(id.c_str());
}
bool PopupHelper::DefineDialog(const std::string& id, const std::string& text)
bool PopupHelper::DefineConfirmationDialog(const std::string& id, const std::string& text)
{
bool result = false;
@@ -30,3 +30,56 @@ bool PopupHelper::DefineDialog(const std::string& id, const std::string& text)
return result;
}
bool PopupHelper::DefineTextDialog(const std::string& id, std::string& currentText)
{
bool result = false;
ImVec2 center = ImGui::GetMainViewport()->GetCenter();
ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
if (ImGui::BeginPopupModal(id.c_str(), NULL, ImGuiWindowFlags_AlwaysAutoResize))
{
bool isSanitized = true;
char buffer[256];
memset(buffer, 0, sizeof(buffer));
std::strncpy(buffer, currentText.c_str(), sizeof(buffer));
if (ImGui::InputText("", buffer, sizeof(buffer)))
{
currentText = std::string(buffer);
}
// returns false when there is a match, quirky
if (!regex_match(buffer, std::regex("[^ \\ / :*? \"<>|]+")))
{
isSanitized = false;
}
if (currentText.empty())
{
ImGui::TextColored(ImVec4(0.76, 0.45, 0.47, 1.0), "A file name can't be empty.");
}
else if (!isSanitized)
{
ImGui::TextColored(ImVec4(0.76, 0.45, 0.47, 1.0), "A file name can't contain any of the following characters: \\/:*?\"<>|");
}
ImGui::Separator();
if (ImGui::Button("OK", ImVec2(120, 0)))
{
if (isSanitized)
{
result = true;
ImGui::CloseCurrentPopup();
}
}
ImGui::SetItemDefaultFocus();
ImGui::SameLine();
if (ImGui::Button("Cancel", ImVec2(120, 0))) { ImGui::CloseCurrentPopup(); }
ImGui::EndPopup();
}
return result;
}

View File

@@ -4,6 +4,7 @@
class PopupHelper
{
public:
static void Confirmation(const std::string& id);
static bool DefineDialog(const std::string& id, const std::string& text);
static void OpenPopup(const std::string& id);
static bool DefineConfirmationDialog(const std::string& id, const std::string& text);
static bool PopupHelper::DefineTextDialog(const std::string& id, std::string& currentText);
};

View File

@@ -31,6 +31,9 @@ namespace Nuake
{
}
std::string renameTempValue = "";
void FileSystemUI::EditorInterfaceDrawFiletree(Ref<Directory> dir)
{
ImGuiTreeNodeFlags base_flags = ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick | ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_FramePadding;
@@ -73,6 +76,9 @@ namespace Nuake
m_hasClickedOnFile = true;
}
const std::string rename = "Rename" + std::string("##") + hoverMenuId;
bool shouldRename = false;
if (ImGui::BeginPopup(hoverMenuId.c_str()))
{
if (ImGui::MenuItem("Open"))
@@ -106,12 +112,10 @@ namespace Nuake
RefreshFileBrowser();
}
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1, 1, 1, 0.2f));
if (ImGui::MenuItem("Rename"))
{
shouldRename = true;
}
ImGui::PopStyleColor();
ImGui::Separator();
@@ -123,6 +127,22 @@ namespace Nuake
ImGui::EndPopup();
}
if (shouldRename)
{
renameTempValue = directory->name;
PopupHelper::OpenPopup(rename);
}
if (PopupHelper::DefineTextDialog(rename, renameTempValue))
{
if (OS::RenameDirectory(directory, renameTempValue) != 0)
{
Logger::Log("Cannot rename directory: " + renameTempValue, "editor", CRITICAL);
}
RefreshFileBrowser();
renameTempValue = "";
}
ImGui::Text(directory->name.c_str());
ImGui::PopFont();
}
@@ -206,6 +226,8 @@ namespace Nuake
const std::string openScene = "Open Scene" + std::string("##") + hoverMenuId;
bool shouldOpenScene = false;
const std::string rename = "Rename" + std::string("##") + hoverMenuId;
bool shouldRename = false;
if (ImGui::BeginPopup(hoverMenuId.c_str()))
{
@@ -268,12 +290,10 @@ namespace Nuake
}
}
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1, 1, 1, 0.2f));
if (ImGui::MenuItem("Rename"))
{
shouldRename = true;
}
ImGui::PopStyleColor();
ImGui::Separator();
@@ -290,12 +310,14 @@ namespace Nuake
shouldOpenScene = file->GetExtension() == ".scene";
}
// Open Scene Popup
if (shouldOpenScene)
{
PopupHelper::Confirmation(openScene);
PopupHelper::OpenPopup(openScene);
}
if (PopupHelper::DefineDialog(openScene, " Open the scene? \n Changes will not be saved."))
if (PopupHelper::DefineConfirmationDialog(openScene, " Open the scene? \n Changes will not be saved."))
{
Ref<Scene> scene = Scene::New();
const std::string projectPath = file->GetAbsolutePath();
@@ -309,6 +331,25 @@ namespace Nuake
Engine::LoadScene(scene);
}
// Rename Popup
if (shouldRename)
{
renameTempValue = file->GetName();
PopupHelper::OpenPopup(rename);
}
if (PopupHelper::DefineTextDialog(rename, renameTempValue))
{
if(OS::RenameFile(file, renameTempValue) != 0)
{
Logger::Log("Cannot rename file: " + renameTempValue, "editor", CRITICAL);
}
RefreshFileBrowser();
renameTempValue = "";
}
ImGui::Text(file->GetName().c_str());
ImGui::PopFont();
}

View File

@@ -7,6 +7,7 @@
#include <Windows.h>
#include <chrono>
#include <imgui/imgui.h>
using namespace Nuake;
@@ -44,6 +45,25 @@ void OS::OpenIn(const std::string& filePath)
ShellExecuteA(nullptr, "open", filePath.c_str(), nullptr, nullptr, SW_SHOWDEFAULT);
}
int OS::RenameFile(const Ref<File>& file, const std::string& newName)
{
std::string extension = !String::EndsWith(newName, file->GetExtension().c_str()) ? file->GetExtension() : "";
std::string newFilePath = file->GetParent()->fullPath + newName + extension;
std::error_code resultError;
std::filesystem::rename(file->GetAbsolutePath().c_str(), newFilePath.c_str(), resultError);
return resultError.value() == 0;
}
int OS::RenameDirectory(const Ref<Directory>& dir, const std::string& newName)
{
std::string newDirPath = dir->Parent->fullPath + newName;
std::error_code resultError;
std::filesystem::rename(dir->fullPath.c_str(), newDirPath.c_str(), resultError);
return resultError.value() == 0;
}
void OS::ShowInFileExplorer(const std::string& filePath)
{
ShellExecuteA(nullptr, "open", "explorer.exe", ("/select," + std::string(filePath)).c_str(), nullptr, SW_SHOWDEFAULT);

View File

@@ -1,6 +1,7 @@
#pragma once
#include <string>
#include "FileSystem.h"
namespace Nuake
{
@@ -11,6 +12,8 @@ namespace Nuake
static std::string GetFromClipboard();
static int GetTime();
static void OpenIn(const std::string& filePath);
static int OS::RenameFile(const Ref<File>& file, const std::string& newName);
static int OS::RenameDirectory(const Ref<Directory>& dir, const std::string& newName);
static void ShowInFileExplorer(const std::string& filePath);
};
}