Skip to content

Commit

Permalink
input path improvment
Browse files Browse the repository at this point in the history
  • Loading branch information
aiekick committed Oct 21, 2020
1 parent 87a89c2 commit 6cb0564
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 38 deletions.
4 changes: 3 additions & 1 deletion CustomImGuiFileDialogConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,6 @@ Comment theses line if you not want to have customization, like icon font here
#define removeBookmarkButtonString ICON_IGFD_REMOVE

// Enter key for input path edition
#define IGFD_KEY_ENTER GLFW_KEY_ENTER
//#define IGFD_INPUT_PATH_VALIDATION GLFW_KEY_ENTER
// excape key for quit input path editio
//#define IGFD_INPUT_PATH_ESCAPE GLFW_KEY_ESCAPE
93 changes: 63 additions & 30 deletions ImGuiFileDialog/ImGuiFileDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ namespace igfd
// for lets you define your button widget
// if you have like me a special bi-color button
#ifndef IMGUI_PATH_BUTTON
#define IMGUI_PATH_BUTTON ImGui::Button
#define IMGUI_PATH_BUTTON ImGui::Button
#endif
#ifndef IMGUI_BUTTON
#define IMGUI_BUTTON ImGui::Button
Expand Down Expand Up @@ -149,9 +149,25 @@ namespace igfd
#define tableHeaderFileDateString "Date"
#endif

#ifndef IGFD_KEY_ENTER
#define IGFD_KEY_ENTER GLFW_KEY_ENTER
#ifndef IGFD_INPUT_PATH_VALIDATION
#define IGFD_INPUT_PATH_VALIDATION GLFW_KEY_ENTER
#endif
#ifndef IGFD_INPUT_PATH_ESCAPE
#define IGFD_INPUT_PATH_ESCAPE GLFW_KEY_ESCAPE
#endif

#ifndef IGFD_KEY_UP
#define IGFD_KEY_UP GLFW_KEY_UP
#endif
#ifndef IGFD_KEY_DOWN
#define IGFD_KEY_DOWN GLFW_KEY_DOWN
#endif
#ifndef IGFD_KEY_ENTER
#define IGFD_KEY_ENTER GLFW_KEY_ENTER
#endif
#ifndef IGFD_KEY_BACKSPACE
#define IGFD_KEY_BACKSPACE GLFW_KEY_BACKSPACE
#endif

#ifdef USE_BOOKMARK
#ifndef bookmarkPaneWith
Expand Down Expand Up @@ -389,6 +405,13 @@ namespace igfd
vBuffer[0] = '\0';
}

inline void SetBuffer(char* vBuffer, size_t vBufferLen, const std::string& vStr)
{
ResetBuffer(vBuffer);
AppendToBuffer(vBuffer, vBufferLen, vStr);
}

char ImGuiFileDialog::InputPathBuffer[MAX_PATH_BUFFER_SIZE] = "";
char ImGuiFileDialog::FileNameBuffer[MAX_FILE_DIALOG_NAME_BUFFER] = "";
char ImGuiFileDialog::DirectoryNameBuffer[MAX_FILE_DIALOG_NAME_BUFFER] = "";
char ImGuiFileDialog::SearchBuffer[MAX_FILE_DIALOG_NAME_BUFFER] = "";
Expand Down Expand Up @@ -564,7 +587,7 @@ namespace igfd
dlg_filters = vFilters;
ParseFilters(dlg_filters);
dlg_path = vPath;
dlg_defaultFileName = vDefaultFileName;
SetDefaultFileName(vDefaultFileName);
dlg_optionsPane = std::move(vOptionsPane);
dlg_userDatas = vUserDatas;
dlg_optionsPaneWidth = vOptionsPaneWidth;
Expand Down Expand Up @@ -598,13 +621,13 @@ namespace igfd
if (ps.isOk)
{
dlg_path = ps.path;
dlg_defaultFileName = vFilePathName;
SetDefaultFileName(vFilePathName);
dlg_defaultExt = "." + ps.ext;
}
else
{
dlg_path = ".";
dlg_defaultFileName.clear();
dlg_path = ".";
SetDefaultFileName("");
dlg_defaultExt.clear();
}

Expand Down Expand Up @@ -638,13 +661,13 @@ namespace igfd
if (ps.isOk)
{
dlg_path = ps.path;
dlg_defaultFileName = vFilePathName;
SetDefaultFileName(vFilePathName);
dlg_defaultExt = "." + ps.ext;
}
else
{
dlg_path = ".";
dlg_defaultFileName.clear();
SetDefaultFileName("");
dlg_defaultExt.clear();
}

Expand Down Expand Up @@ -674,7 +697,7 @@ namespace igfd
dlg_filters = vFilters;
ParseFilters(dlg_filters);
dlg_path = vPath;
dlg_defaultFileName = vDefaultFileName;
SetDefaultFileName(vDefaultFileName);
dlg_optionsPane = nullptr;
dlg_userDatas = vUserDatas;
dlg_optionsPaneWidth = 0;
Expand Down Expand Up @@ -802,8 +825,7 @@ namespace igfd

if (!dlg_defaultFileName.empty())
{
ResetBuffer(FileNameBuffer);
AppendToBuffer(FileNameBuffer, MAX_FILE_DIALOG_NAME_BUFFER, dlg_defaultFileName);
SetDefaultFileName(dlg_defaultFileName);
SetSelectedFilterWithExt(dlg_defaultExt);
}

Expand Down Expand Up @@ -898,13 +920,13 @@ namespace igfd
if (m_InputPathActivated)
{
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
ImGui::InputText("##path", m_InputPathBuffer, 1023);
ImGui::InputText("##pathedition", InputPathBuffer, MAX_PATH_BUFFER_SIZE);
ImGui::PopItemWidth();
}
else
{
int _id = 0;
for (auto itPathDecomp = m_CurrentPath_Decomposition.begin();
int _id = 0;
for (auto itPathDecomp = m_CurrentPath_Decomposition.begin();
itPathDecomp != m_CurrentPath_Decomposition.end(); ++itPathDecomp)
{
if (itPathDecomp != m_CurrentPath_Decomposition.begin())
Expand All @@ -914,15 +936,16 @@ namespace igfd
ImGui::PopID();
if (click)
{
ComposeNewPath(itPathDecomp);
m_CurrentPath = ComposeNewPath(itPathDecomp);
pathClick = true;
break;
}
// activate input for path
if (ImGui::IsItemClicked(ImGuiMouseButton_Right))
{
m_InputPathActivated = true;
break;
SetBuffer(InputPathBuffer, MAX_PATH_BUFFER_SIZE, ComposeNewPath(itPathDecomp));
m_InputPathActivated = true;
break;
}
}
}
Expand All @@ -940,8 +963,7 @@ namespace igfd
ImGui::SameLine();
ImGui::Text(searchString);
ImGui::SameLine();
float aw = ImGui::GetContentRegionAvail().x;
ImGui::PushItemWidth(aw);
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
bool edited = ImGui::InputText("##ImGuiFileDialogSearchFiled", SearchBuffer, MAX_FILE_DIALOG_NAME_BUFFER);
ImGui::PopItemWidth();
if (edited)
Expand Down Expand Up @@ -1105,11 +1127,15 @@ namespace igfd

if (m_InputPathActivated)
{
if (ImGui::IsKeyReleased(IGFD_KEY_ENTER))
if (ImGui::IsKeyReleased(IGFD_INPUT_PATH_VALIDATION))
{
SetPath(std::string(m_InputPathBuffer));
SetPath(std::string(InputPathBuffer));
m_InputPathActivated = false;
}
if (ImGui::IsKeyReleased(IGFD_INPUT_PATH_ESCAPE))
{
m_InputPathActivated = false;
}
}
#ifdef USE_EXPLORATION_BY_KEYS
else
Expand Down Expand Up @@ -1345,6 +1371,12 @@ namespace igfd
m_FileExtentionInfos.clear();
}

void ImGuiFileDialog::SetDefaultFileName(const std::string& vFileName)
{
dlg_defaultFileName = vFileName;
SetBuffer(FileNameBuffer, MAX_FILE_DIALOG_NAME_BUFFER, vFileName);
}

bool ImGuiFileDialog::SelectDirectory(const FileInfoStruct& vInfos)
{
bool pathClick = false;
Expand All @@ -1353,7 +1385,7 @@ namespace igfd
{
if (m_CurrentPath_Decomposition.size() > 1)
{
ComposeNewPath(m_CurrentPath_Decomposition.end() - 2);
m_CurrentPath = ComposeNewPath(m_CurrentPath_Decomposition.end() - 2);
pathClick = true;
}
}
Expand Down Expand Up @@ -1846,8 +1878,7 @@ namespace igfd
{
m_CurrentPath = m_CurrentPath.substr(0, m_CurrentPath.size() - 1);
}
ResetBuffer(m_InputPathBuffer);
AppendToBuffer(m_InputPathBuffer, 1023, m_CurrentPath);
SetBuffer(InputPathBuffer, MAX_PATH_BUFFER_SIZE, m_CurrentPath);
m_CurrentPath_Decomposition = splitStringToVector(m_CurrentPath, PATH_SEP, false);
#if defined(UNIX) // UNIX is LINUX or APPLE
m_CurrentPath_Decomposition.insert(m_CurrentPath_Decomposition.begin(), std::string(1u, PATH_SEP));
Expand Down Expand Up @@ -1878,16 +1909,16 @@ namespace igfd
return res;
}

void ImGuiFileDialog::ComposeNewPath(std::vector<std::string>::iterator vIter)
std::string ImGuiFileDialog::ComposeNewPath(std::vector<std::string>::iterator vIter)
{
m_CurrentPath.clear();
std::string res;

while (true)
{
if (!m_CurrentPath.empty())
if (!res.empty())
{
#ifdef WIN32
m_CurrentPath = *vIter + PATH_SEP + m_CurrentPath;
res = *vIter + PATH_SEP + res;
#elif defined(UNIX) // UNIX is LINUX or APPLE
if (*vIter == s_fs_root)
{
Expand All @@ -1901,7 +1932,7 @@ namespace igfd
}
else
{
m_CurrentPath = *vIter;
res = *vIter;
}

if (vIter == m_CurrentPath_Decomposition.begin())
Expand All @@ -1915,6 +1946,8 @@ namespace igfd

--vIter;
}

return res;
}

void ImGuiFileDialog::GetDrives()
Expand Down
16 changes: 12 additions & 4 deletions ImGuiFileDialog/ImGuiFileDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ you can use your own and define the path of your custom config file realtive to
*/
#pragma once

#define IMGUIFILEDIALOG_VERSION "v0.4"
#define IMGUIFILEDIALOG_VERSION "v0.5"

#include <imgui.h>

Expand All @@ -101,7 +101,13 @@ you can use your own and define the path of your custom config file realtive to

namespace igfd
{
#ifndef MAX_FILE_DIALOG_NAME_BUFFER
#define MAX_FILE_DIALOG_NAME_BUFFER 1024
#endif

#ifndef MAX_PATH_BUFFER_SIZE
#define MAX_PATH_BUFFER_SIZE 1024
#endif

typedef void* UserDatas;

Expand Down Expand Up @@ -182,7 +188,7 @@ namespace igfd
std::vector<FilterInfosStruct> m_Filters;
FilterInfosStruct m_SelectedFilter;
bool m_InputPathActivated = false; // show input for path edition
char m_InputPathBuffer[1024] = "";

#ifdef USE_BOOKMARK
std::vector<BookmarkStruct> m_Bookmarks;
bool m_BookmarkPaneShown = false;
Expand All @@ -194,6 +200,7 @@ namespace igfd
float m_FlashAlphaAttenInSecs = 1.0f; // fps display dependant

public:
static char InputPathBuffer[MAX_PATH_BUFFER_SIZE];
static char FileNameBuffer[MAX_FILE_DIALOG_NAME_BUFFER];
static char DirectoryNameBuffer[MAX_FILE_DIALOG_NAME_BUFFER];
static char SearchBuffer[MAX_FILE_DIALOG_NAME_BUFFER];
Expand All @@ -218,7 +225,7 @@ namespace igfd
size_t dlg_countSelectionMax = 1; // 0 for infinite
bool dlg_modal = false;

private:
private: // detail view
std::string m_HeaderFileName;
std::string m_HeaderFileSize;
std::string m_HeaderFileDate;
Expand Down Expand Up @@ -288,6 +295,7 @@ namespace igfd
void ClearExtentionInfos();

private:
void SetDefaultFileName(const std::string& vFileName);
bool SelectDirectory(const FileInfoStruct& vInfos);
void SelectFileName(const FileInfoStruct& vInfos);
void RemoveFileNameInSelection(const std::string& vFileName);
Expand All @@ -298,7 +306,7 @@ namespace igfd
void ScanDir(const std::string& vPath);
void SetCurrentDir(const std::string& vPath);
bool CreateDir(const std::string& vPath);
void ComposeNewPath(std::vector<std::string>::iterator vIter);
std::string ComposeNewPath(std::vector<std::string>::iterator vIter);
void GetDrives();
void ParseFilters(const char *vFilters);
void SetSelectedFilterWithExt(const std::string& vFilter);
Expand Down
7 changes: 6 additions & 1 deletion ImGuiFileDialog/ImGuiFileDialogConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

// uncomment and modify defines under for customize ImGuiFileDialog

//#define MAX_FILE_DIALOG_NAME_BUFFER 1024
//#define MAX_PATH_BUFFER_SIZE 1024

//#define USE_IMGUI_TABLES

//#define USE_EXPLORATION_BY_KEYS
Expand Down Expand Up @@ -57,4 +60,6 @@
//#define removeBookmarkButtonString "-"

// Enter key for input path edition
//#define IGFD_KEY_ENTER GLFW_KEY_ENTER
//#define IGFD_INPUT_PATH_VALIDATION GLFW_KEY_ENTER
// excape key for quit input path editio
//#define IGFD_INPUT_PATH_ESCAPE GLFW_KEY_ESCAPE
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,21 @@ Save => std::string bookmarkString = igfd::ImGuiFileDialog::Instance()->Serializ

## Path Edition :

if you click right on one of any path button, you can input or modify a path.
if you click right on one of any path button, you can input or modify the path pointed by this button.
then press the validate key (Enter by default with GLFW) for validate the new path
or press the escape key (Escape by default with GLFW) for quit the input path edition

you can modify the Validation key with the define : IGFD_KEY_ENTER
you can use your own Validation and Escape keys with the defines :
IGFD_INPUT_PATH_VALIDATION
IGFD_INPUT_PATH_ESCAPE
in you custom config file (CustomImGuiFileDialogConfig.h in this app example)

see in this gif :
1) button edition with mouse button right and escape key for quit the edition
2) focus the input and press validation for set path

![inputPathEdition.gif](doc/inputPathEdition.gif)

## How to Integrate ImGuiFileDialog in your porject

### ImGuiFileDialog require :
Expand Down
Binary file added doc/inputPathEdition.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6cb0564

Please sign in to comment.