Skip to content

Commit 6cb0564

Browse files
committed
input path improvment
1 parent 87a89c2 commit 6cb0564

6 files changed

+95
-38
lines changed

CustomImGuiFileDialogConfig.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,6 @@ Comment theses line if you not want to have customization, like icon font here
6262
#define removeBookmarkButtonString ICON_IGFD_REMOVE
6363

6464
// Enter key for input path edition
65-
#define IGFD_KEY_ENTER GLFW_KEY_ENTER
65+
//#define IGFD_INPUT_PATH_VALIDATION GLFW_KEY_ENTER
66+
// excape key for quit input path editio
67+
//#define IGFD_INPUT_PATH_ESCAPE GLFW_KEY_ESCAPE

ImGuiFileDialog/ImGuiFileDialog.cpp

+63-30
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ namespace igfd
8181
// for lets you define your button widget
8282
// if you have like me a special bi-color button
8383
#ifndef IMGUI_PATH_BUTTON
84-
#define IMGUI_PATH_BUTTON ImGui::Button
84+
#define IMGUI_PATH_BUTTON ImGui::Button
8585
#endif
8686
#ifndef IMGUI_BUTTON
8787
#define IMGUI_BUTTON ImGui::Button
@@ -149,9 +149,25 @@ namespace igfd
149149
#define tableHeaderFileDateString "Date"
150150
#endif
151151

152-
#ifndef IGFD_KEY_ENTER
153-
#define IGFD_KEY_ENTER GLFW_KEY_ENTER
152+
#ifndef IGFD_INPUT_PATH_VALIDATION
153+
#define IGFD_INPUT_PATH_VALIDATION GLFW_KEY_ENTER
154154
#endif
155+
#ifndef IGFD_INPUT_PATH_ESCAPE
156+
#define IGFD_INPUT_PATH_ESCAPE GLFW_KEY_ESCAPE
157+
#endif
158+
159+
#ifndef IGFD_KEY_UP
160+
#define IGFD_KEY_UP GLFW_KEY_UP
161+
#endif
162+
#ifndef IGFD_KEY_DOWN
163+
#define IGFD_KEY_DOWN GLFW_KEY_DOWN
164+
#endif
165+
#ifndef IGFD_KEY_ENTER
166+
#define IGFD_KEY_ENTER GLFW_KEY_ENTER
167+
#endif
168+
#ifndef IGFD_KEY_BACKSPACE
169+
#define IGFD_KEY_BACKSPACE GLFW_KEY_BACKSPACE
170+
#endif
155171

156172
#ifdef USE_BOOKMARK
157173
#ifndef bookmarkPaneWith
@@ -389,6 +405,13 @@ namespace igfd
389405
vBuffer[0] = '\0';
390406
}
391407

408+
inline void SetBuffer(char* vBuffer, size_t vBufferLen, const std::string& vStr)
409+
{
410+
ResetBuffer(vBuffer);
411+
AppendToBuffer(vBuffer, vBufferLen, vStr);
412+
}
413+
414+
char ImGuiFileDialog::InputPathBuffer[MAX_PATH_BUFFER_SIZE] = "";
392415
char ImGuiFileDialog::FileNameBuffer[MAX_FILE_DIALOG_NAME_BUFFER] = "";
393416
char ImGuiFileDialog::DirectoryNameBuffer[MAX_FILE_DIALOG_NAME_BUFFER] = "";
394417
char ImGuiFileDialog::SearchBuffer[MAX_FILE_DIALOG_NAME_BUFFER] = "";
@@ -564,7 +587,7 @@ namespace igfd
564587
dlg_filters = vFilters;
565588
ParseFilters(dlg_filters);
566589
dlg_path = vPath;
567-
dlg_defaultFileName = vDefaultFileName;
590+
SetDefaultFileName(vDefaultFileName);
568591
dlg_optionsPane = std::move(vOptionsPane);
569592
dlg_userDatas = vUserDatas;
570593
dlg_optionsPaneWidth = vOptionsPaneWidth;
@@ -598,13 +621,13 @@ namespace igfd
598621
if (ps.isOk)
599622
{
600623
dlg_path = ps.path;
601-
dlg_defaultFileName = vFilePathName;
624+
SetDefaultFileName(vFilePathName);
602625
dlg_defaultExt = "." + ps.ext;
603626
}
604627
else
605628
{
606-
dlg_path = ".";
607-
dlg_defaultFileName.clear();
629+
dlg_path = ".";
630+
SetDefaultFileName("");
608631
dlg_defaultExt.clear();
609632
}
610633

@@ -638,13 +661,13 @@ namespace igfd
638661
if (ps.isOk)
639662
{
640663
dlg_path = ps.path;
641-
dlg_defaultFileName = vFilePathName;
664+
SetDefaultFileName(vFilePathName);
642665
dlg_defaultExt = "." + ps.ext;
643666
}
644667
else
645668
{
646669
dlg_path = ".";
647-
dlg_defaultFileName.clear();
670+
SetDefaultFileName("");
648671
dlg_defaultExt.clear();
649672
}
650673

@@ -674,7 +697,7 @@ namespace igfd
674697
dlg_filters = vFilters;
675698
ParseFilters(dlg_filters);
676699
dlg_path = vPath;
677-
dlg_defaultFileName = vDefaultFileName;
700+
SetDefaultFileName(vDefaultFileName);
678701
dlg_optionsPane = nullptr;
679702
dlg_userDatas = vUserDatas;
680703
dlg_optionsPaneWidth = 0;
@@ -802,8 +825,7 @@ namespace igfd
802825

803826
if (!dlg_defaultFileName.empty())
804827
{
805-
ResetBuffer(FileNameBuffer);
806-
AppendToBuffer(FileNameBuffer, MAX_FILE_DIALOG_NAME_BUFFER, dlg_defaultFileName);
828+
SetDefaultFileName(dlg_defaultFileName);
807829
SetSelectedFilterWithExt(dlg_defaultExt);
808830
}
809831

@@ -898,13 +920,13 @@ namespace igfd
898920
if (m_InputPathActivated)
899921
{
900922
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
901-
ImGui::InputText("##path", m_InputPathBuffer, 1023);
923+
ImGui::InputText("##pathedition", InputPathBuffer, MAX_PATH_BUFFER_SIZE);
902924
ImGui::PopItemWidth();
903925
}
904926
else
905927
{
906-
int _id = 0;
907-
for (auto itPathDecomp = m_CurrentPath_Decomposition.begin();
928+
int _id = 0;
929+
for (auto itPathDecomp = m_CurrentPath_Decomposition.begin();
908930
itPathDecomp != m_CurrentPath_Decomposition.end(); ++itPathDecomp)
909931
{
910932
if (itPathDecomp != m_CurrentPath_Decomposition.begin())
@@ -914,15 +936,16 @@ namespace igfd
914936
ImGui::PopID();
915937
if (click)
916938
{
917-
ComposeNewPath(itPathDecomp);
939+
m_CurrentPath = ComposeNewPath(itPathDecomp);
918940
pathClick = true;
919941
break;
920942
}
921943
// activate input for path
922944
if (ImGui::IsItemClicked(ImGuiMouseButton_Right))
923945
{
924-
m_InputPathActivated = true;
925-
break;
946+
SetBuffer(InputPathBuffer, MAX_PATH_BUFFER_SIZE, ComposeNewPath(itPathDecomp));
947+
m_InputPathActivated = true;
948+
break;
926949
}
927950
}
928951
}
@@ -940,8 +963,7 @@ namespace igfd
940963
ImGui::SameLine();
941964
ImGui::Text(searchString);
942965
ImGui::SameLine();
943-
float aw = ImGui::GetContentRegionAvail().x;
944-
ImGui::PushItemWidth(aw);
966+
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
945967
bool edited = ImGui::InputText("##ImGuiFileDialogSearchFiled", SearchBuffer, MAX_FILE_DIALOG_NAME_BUFFER);
946968
ImGui::PopItemWidth();
947969
if (edited)
@@ -1105,11 +1127,15 @@ namespace igfd
11051127

11061128
if (m_InputPathActivated)
11071129
{
1108-
if (ImGui::IsKeyReleased(IGFD_KEY_ENTER))
1130+
if (ImGui::IsKeyReleased(IGFD_INPUT_PATH_VALIDATION))
11091131
{
1110-
SetPath(std::string(m_InputPathBuffer));
1132+
SetPath(std::string(InputPathBuffer));
11111133
m_InputPathActivated = false;
11121134
}
1135+
if (ImGui::IsKeyReleased(IGFD_INPUT_PATH_ESCAPE))
1136+
{
1137+
m_InputPathActivated = false;
1138+
}
11131139
}
11141140
#ifdef USE_EXPLORATION_BY_KEYS
11151141
else
@@ -1345,6 +1371,12 @@ namespace igfd
13451371
m_FileExtentionInfos.clear();
13461372
}
13471373

1374+
void ImGuiFileDialog::SetDefaultFileName(const std::string& vFileName)
1375+
{
1376+
dlg_defaultFileName = vFileName;
1377+
SetBuffer(FileNameBuffer, MAX_FILE_DIALOG_NAME_BUFFER, vFileName);
1378+
}
1379+
13481380
bool ImGuiFileDialog::SelectDirectory(const FileInfoStruct& vInfos)
13491381
{
13501382
bool pathClick = false;
@@ -1353,7 +1385,7 @@ namespace igfd
13531385
{
13541386
if (m_CurrentPath_Decomposition.size() > 1)
13551387
{
1356-
ComposeNewPath(m_CurrentPath_Decomposition.end() - 2);
1388+
m_CurrentPath = ComposeNewPath(m_CurrentPath_Decomposition.end() - 2);
13571389
pathClick = true;
13581390
}
13591391
}
@@ -1846,8 +1878,7 @@ namespace igfd
18461878
{
18471879
m_CurrentPath = m_CurrentPath.substr(0, m_CurrentPath.size() - 1);
18481880
}
1849-
ResetBuffer(m_InputPathBuffer);
1850-
AppendToBuffer(m_InputPathBuffer, 1023, m_CurrentPath);
1881+
SetBuffer(InputPathBuffer, MAX_PATH_BUFFER_SIZE, m_CurrentPath);
18511882
m_CurrentPath_Decomposition = splitStringToVector(m_CurrentPath, PATH_SEP, false);
18521883
#if defined(UNIX) // UNIX is LINUX or APPLE
18531884
m_CurrentPath_Decomposition.insert(m_CurrentPath_Decomposition.begin(), std::string(1u, PATH_SEP));
@@ -1878,16 +1909,16 @@ namespace igfd
18781909
return res;
18791910
}
18801911

1881-
void ImGuiFileDialog::ComposeNewPath(std::vector<std::string>::iterator vIter)
1912+
std::string ImGuiFileDialog::ComposeNewPath(std::vector<std::string>::iterator vIter)
18821913
{
1883-
m_CurrentPath.clear();
1914+
std::string res;
18841915

18851916
while (true)
18861917
{
1887-
if (!m_CurrentPath.empty())
1918+
if (!res.empty())
18881919
{
18891920
#ifdef WIN32
1890-
m_CurrentPath = *vIter + PATH_SEP + m_CurrentPath;
1921+
res = *vIter + PATH_SEP + res;
18911922
#elif defined(UNIX) // UNIX is LINUX or APPLE
18921923
if (*vIter == s_fs_root)
18931924
{
@@ -1901,7 +1932,7 @@ namespace igfd
19011932
}
19021933
else
19031934
{
1904-
m_CurrentPath = *vIter;
1935+
res = *vIter;
19051936
}
19061937

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

19161947
--vIter;
19171948
}
1949+
1950+
return res;
19181951
}
19191952

19201953
void ImGuiFileDialog::GetDrives()

ImGuiFileDialog/ImGuiFileDialog.h

+12-4
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ you can use your own and define the path of your custom config file realtive to
7676
*/
7777
#pragma once
7878

79-
#define IMGUIFILEDIALOG_VERSION "v0.4"
79+
#define IMGUIFILEDIALOG_VERSION "v0.5"
8080

8181
#include <imgui.h>
8282

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

102102
namespace igfd
103103
{
104+
#ifndef MAX_FILE_DIALOG_NAME_BUFFER
104105
#define MAX_FILE_DIALOG_NAME_BUFFER 1024
106+
#endif
107+
108+
#ifndef MAX_PATH_BUFFER_SIZE
109+
#define MAX_PATH_BUFFER_SIZE 1024
110+
#endif
105111

106112
typedef void* UserDatas;
107113

@@ -182,7 +188,7 @@ namespace igfd
182188
std::vector<FilterInfosStruct> m_Filters;
183189
FilterInfosStruct m_SelectedFilter;
184190
bool m_InputPathActivated = false; // show input for path edition
185-
char m_InputPathBuffer[1024] = "";
191+
186192
#ifdef USE_BOOKMARK
187193
std::vector<BookmarkStruct> m_Bookmarks;
188194
bool m_BookmarkPaneShown = false;
@@ -194,6 +200,7 @@ namespace igfd
194200
float m_FlashAlphaAttenInSecs = 1.0f; // fps display dependant
195201

196202
public:
203+
static char InputPathBuffer[MAX_PATH_BUFFER_SIZE];
197204
static char FileNameBuffer[MAX_FILE_DIALOG_NAME_BUFFER];
198205
static char DirectoryNameBuffer[MAX_FILE_DIALOG_NAME_BUFFER];
199206
static char SearchBuffer[MAX_FILE_DIALOG_NAME_BUFFER];
@@ -218,7 +225,7 @@ namespace igfd
218225
size_t dlg_countSelectionMax = 1; // 0 for infinite
219226
bool dlg_modal = false;
220227

221-
private:
228+
private: // detail view
222229
std::string m_HeaderFileName;
223230
std::string m_HeaderFileSize;
224231
std::string m_HeaderFileDate;
@@ -288,6 +295,7 @@ namespace igfd
288295
void ClearExtentionInfos();
289296

290297
private:
298+
void SetDefaultFileName(const std::string& vFileName);
291299
bool SelectDirectory(const FileInfoStruct& vInfos);
292300
void SelectFileName(const FileInfoStruct& vInfos);
293301
void RemoveFileNameInSelection(const std::string& vFileName);
@@ -298,7 +306,7 @@ namespace igfd
298306
void ScanDir(const std::string& vPath);
299307
void SetCurrentDir(const std::string& vPath);
300308
bool CreateDir(const std::string& vPath);
301-
void ComposeNewPath(std::vector<std::string>::iterator vIter);
309+
std::string ComposeNewPath(std::vector<std::string>::iterator vIter);
302310
void GetDrives();
303311
void ParseFilters(const char *vFilters);
304312
void SetSelectedFilterWithExt(const std::string& vFilter);

ImGuiFileDialog/ImGuiFileDialogConfig.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
// uncomment and modify defines under for customize ImGuiFileDialog
44

5+
//#define MAX_FILE_DIALOG_NAME_BUFFER 1024
6+
//#define MAX_PATH_BUFFER_SIZE 1024
7+
58
//#define USE_IMGUI_TABLES
69

710
//#define USE_EXPLORATION_BY_KEYS
@@ -57,4 +60,6 @@
5760
//#define removeBookmarkButtonString "-"
5861

5962
// Enter key for input path edition
60-
//#define IGFD_KEY_ENTER GLFW_KEY_ENTER
63+
//#define IGFD_INPUT_PATH_VALIDATION GLFW_KEY_ENTER
64+
// excape key for quit input path editio
65+
//#define IGFD_INPUT_PATH_ESCAPE GLFW_KEY_ESCAPE

README.md

+11-2
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,21 @@ Save => std::string bookmarkString = igfd::ImGuiFileDialog::Instance()->Serializ
241241

242242
## Path Edition :
243243

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

247-
you can modify the Validation key with the define : IGFD_KEY_ENTER
248+
you can use your own Validation and Escape keys with the defines :
249+
IGFD_INPUT_PATH_VALIDATION
250+
IGFD_INPUT_PATH_ESCAPE
248251
in you custom config file (CustomImGuiFileDialogConfig.h in this app example)
249252

253+
see in this gif :
254+
1) button edition with mouse button right and escape key for quit the edition
255+
2) focus the input and press validation for set path
256+
257+
![inputPathEdition.gif](doc/inputPathEdition.gif)
258+
250259
## How to Integrate ImGuiFileDialog in your porject
251260

252261
### ImGuiFileDialog require :

doc/inputPathEdition.gif

125 KB
Loading

0 commit comments

Comments
 (0)