Skip to content

Commit 48f3b10

Browse files
committed
[ADD] : add a "select all files" shortcut. by default "ctrl+A". can be modified in config file
1 parent bac553f commit 48f3b10

File tree

3 files changed

+94
-58
lines changed

3 files changed

+94
-58
lines changed

ImGuiFileDialog.cpp

Lines changed: 83 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,14 @@ SOFTWARE.
269269
#define DateTimeFormat "%Y/%m/%d %H:%M"
270270
#endif // DateTimeFormat
271271

272+
///////////////////////////////
273+
//// SHORTCUTS => ctrl + KEY
274+
///////////////////////////////
275+
276+
#ifndef SelectAllFilesKey
277+
#define SelectAllFilesKey ImGuiKey_A
278+
#endif // SelectAllFilesKey
279+
272280
///////////////////////////////
273281
// THUMBNAILS
274282
///////////////////////////////
@@ -1321,7 +1329,7 @@ void IGFD::FilterManager::SetFileStyle(const IGFD_FileStyleFlags& vFlags, const
13211329

13221330
// will be called internally
13231331
// will not been exposed to IGFD API
1324-
bool IGFD::FilterManager::m_FillFileStyle(std::shared_ptr<FileInfos> vFileInfos) const {
1332+
bool IGFD::FilterManager::FillFileStyle(std::shared_ptr<FileInfos> vFileInfos) const {
13251333
// todo : better system to found regarding what style to priorize regarding other
13261334
// maybe with a lambda fucntion for let the user use his style
13271335
// according to his use case
@@ -1919,7 +1927,7 @@ void IGFD::FileManager::m_AddFile(const FileDialogInternal& vFileDialogInternal,
19191927
}
19201928
}
19211929

1922-
vFileDialogInternal.filterManager.m_FillFileStyle(infos_ptr);
1930+
vFileDialogInternal.filterManager.FillFileStyle(infos_ptr);
19231931

19241932
m_CompleteFileInfos(infos_ptr);
19251933

@@ -1948,7 +1956,7 @@ void IGFD::FileManager::m_AddPath(const FileDialogInternal& vFileDialogInternal,
19481956
}
19491957
}
19501958

1951-
vFileDialogInternal.filterManager.m_FillFileStyle(infos_ptr);
1959+
vFileDialogInternal.filterManager.FillFileStyle(infos_ptr);
19521960

19531961
m_CompleteFileInfos(infos_ptr);
19541962

@@ -2180,7 +2188,10 @@ void IGFD::FileManager::m_RemoveFileNameInSelection(const std::string& vFileName
21802188
}
21812189
}
21822190

2183-
void IGFD::FileManager::m_m_AddFileNameInSelection(const std::string& vFileName, bool vSetLastSelectionFileName) {
2191+
void IGFD::FileManager::m_AddFileNameInSelection(const std::string& vFileName, bool vSetLastSelectionFileName) {
2192+
if (vFileName == "." || vFileName == "..") {
2193+
return;
2194+
}
21842195
m_SelectedFileNames.emplace(vFileName);
21852196

21862197
if (m_SelectedFileNames.size() == 1) {
@@ -2189,7 +2200,9 @@ void IGFD::FileManager::m_m_AddFileNameInSelection(const std::string& vFileName,
21892200
snprintf(fileNameBuffer, MAX_FILE_DIALOG_NAME_BUFFER, "%zu files Selected", m_SelectedFileNames.size());
21902201
}
21912202

2192-
if (vSetLastSelectionFileName) m_LastSelectedFileName = vFileName;
2203+
if (vSetLastSelectionFileName) {
2204+
m_LastSelectedFileName = vFileName;
2205+
}
21932206
}
21942207

21952208
void IGFD::FileManager::SetCurrentDir(const std::string& vPath) {
@@ -2337,24 +2350,38 @@ bool IGFD::FileManager::SelectDirectory(const std::shared_ptr<FileInfos>& vInfos
23372350
return pathClick;
23382351
}
23392352

2353+
void IGFD::FileManager::SelectAllFileNames() {
2354+
m_SelectedFileNames.clear();
2355+
for (const auto& infos_ptr : m_FilteredFileList) {
2356+
if (infos_ptr != nullptr) {
2357+
m_AddFileNameInSelection(infos_ptr->fileNameExt, true);
2358+
}
2359+
}
2360+
}
2361+
23402362
void IGFD::FileManager::SelectFileName(const FileDialogInternal& vFileDialogInternal, const std::shared_ptr<FileInfos>& vInfos) {
2341-
if (!vInfos.use_count()) return;
2363+
if (!vInfos.use_count()) {
2364+
return;
2365+
}
2366+
m_AddFileNameInSelection(vInfos->fileNameExt, true);
2367+
}
2368+
2369+
void IGFD::FileManager::SelectOrDeselectFileName(const FileDialogInternal& vFileDialogInternal, const std::shared_ptr<FileInfos>& vInfos) {
2370+
if (!vInfos.use_count()) {
2371+
return;
2372+
}
23422373

23432374
if (ImGui::IsKeyDown(ImGuiMod_Ctrl)) {
2344-
if (dLGcountSelectionMax == 0) // infinite selection
2345-
{
2346-
if (m_SelectedFileNames.find(vInfos->fileNameExt) == m_SelectedFileNames.end()) // not found +> add
2347-
{
2348-
m_m_AddFileNameInSelection(vInfos->fileNameExt, true);
2375+
if (dLGcountSelectionMax == 0) { // infinite selection
2376+
if (m_SelectedFileNames.find(vInfos->fileNameExt) == m_SelectedFileNames.end()) { // not found +> add
2377+
m_AddFileNameInSelection(vInfos->fileNameExt, true);
23492378
} else { // found +> remove
23502379
m_RemoveFileNameInSelection(vInfos->fileNameExt);
23512380
}
2352-
} else // selection limited by size
2353-
{
2381+
} else { // selection limited by size
23542382
if (m_SelectedFileNames.size() < dLGcountSelectionMax) {
2355-
if (m_SelectedFileNames.find(vInfos->fileNameExt) == m_SelectedFileNames.end()) // not found +> add
2356-
{
2357-
m_m_AddFileNameInSelection(vInfos->fileNameExt, true);
2383+
if (m_SelectedFileNames.find(vInfos->fileNameExt) == m_SelectedFileNames.end()) { // not found +> add
2384+
m_AddFileNameInSelection(vInfos->fileNameExt, true);
23582385
} else { // found +> remove
23592386
m_RemoveFileNameInSelection(vInfos->fileNameExt);
23602387
}
@@ -2364,45 +2391,46 @@ void IGFD::FileManager::SelectFileName(const FileDialogInternal& vFileDialogInte
23642391
if (dLGcountSelectionMax != 1) {
23652392
m_SelectedFileNames.clear();
23662393
// we will iterate filelist and get the last selection after the start selection
2367-
bool startMultiSelection = false;
2394+
bool startMultiSelection = false;
23682395
std::string fileNameToSelect = vInfos->fileNameExt;
23692396
std::string savedLastSelectedFileName; // for invert selection mode
23702397
for (const auto& file : m_FileList) {
2371-
if (!file.use_count()) continue;
2372-
2398+
if (!file.use_count()) {
2399+
continue;
2400+
}
23732401
bool canTake = true;
2374-
if (!file->SearchForTag(vFileDialogInternal.searchManager.searchTag)) canTake = false;
2375-
if (canTake) // if not filtered, we will take files who are filtered by the dialog
2376-
{
2402+
if (!file->SearchForTag(vFileDialogInternal.searchManager.searchTag))
2403+
canTake = false;
2404+
if (canTake) { // if not filtered, we will take files who are filtered by the dialog
23772405
if (file->fileNameExt == m_LastSelectedFileName) {
23782406
startMultiSelection = true;
2379-
m_m_AddFileNameInSelection(m_LastSelectedFileName, false);
2407+
m_AddFileNameInSelection(m_LastSelectedFileName, false);
23802408
} else if (startMultiSelection) {
2381-
if (dLGcountSelectionMax == 0) // infinite selection
2382-
{
2383-
m_m_AddFileNameInSelection(file->fileNameExt, false);
2409+
if (dLGcountSelectionMax == 0) { // infinite selection
2410+
m_AddFileNameInSelection(file->fileNameExt, false);
23842411
} else { // selection limited by size
23852412
if (m_SelectedFileNames.size() < dLGcountSelectionMax) {
2386-
m_m_AddFileNameInSelection(file->fileNameExt, false);
2413+
m_AddFileNameInSelection(file->fileNameExt, false);
23872414
} else {
23882415
startMultiSelection = false;
2389-
if (!savedLastSelectedFileName.empty()) m_LastSelectedFileName = savedLastSelectedFileName;
2416+
if (!savedLastSelectedFileName.empty())
2417+
m_LastSelectedFileName = savedLastSelectedFileName;
23902418
break;
23912419
}
23922420
}
23932421
}
23942422

23952423
if (file->fileNameExt == fileNameToSelect) {
2396-
if (!startMultiSelection) // we are before the last Selected FileName, so we must inverse
2397-
{
2424+
if (!startMultiSelection) { // we are before the last Selected FileName, so we must inverse
23982425
savedLastSelectedFileName = m_LastSelectedFileName;
2399-
m_LastSelectedFileName = fileNameToSelect;
2400-
fileNameToSelect = savedLastSelectedFileName;
2401-
startMultiSelection = true;
2402-
m_m_AddFileNameInSelection(m_LastSelectedFileName, false);
2426+
m_LastSelectedFileName = fileNameToSelect;
2427+
fileNameToSelect = savedLastSelectedFileName;
2428+
startMultiSelection = true;
2429+
m_AddFileNameInSelection(m_LastSelectedFileName, false);
24032430
} else {
24042431
startMultiSelection = false;
2405-
if (!savedLastSelectedFileName.empty()) m_LastSelectedFileName = savedLastSelectedFileName;
2432+
if (!savedLastSelectedFileName.empty())
2433+
m_LastSelectedFileName = savedLastSelectedFileName;
24062434
break;
24072435
}
24082436
}
@@ -2412,7 +2440,7 @@ void IGFD::FileManager::SelectFileName(const FileDialogInternal& vFileDialogInte
24122440
} else {
24132441
m_SelectedFileNames.clear();
24142442
IGFD::Utils::ResetBuffer(fileNameBuffer);
2415-
m_m_AddFileNameInSelection(vInfos->fileNameExt, true);
2443+
m_AddFileNameInSelection(vInfos->fileNameExt, true);
24162444
}
24172445
}
24182446

@@ -2668,6 +2696,12 @@ void IGFD::FileDialogInternal::EndFrame() {
26682696
fileManager.inputPathActivated = false;
26692697
}
26702698
}
2699+
2700+
if (ImGui::IsKeyDown(ImGuiMod_Ctrl)) {
2701+
if (ImGui::IsKeyDown(SelectAllFilesKey)) {
2702+
fileManager.SelectAllFileNames();
2703+
}
2704+
}
26712705
}
26722706

26732707
void IGFD::FileDialogInternal::ResetForNewDialog() {
@@ -3971,7 +4005,8 @@ bool IGFD::FileDialog::m_DrawFooter() {
39714005
}
39724006

39734007
void IGFD::FileDialog::m_SelectableItem(int vidx, std::shared_ptr<FileInfos> vInfos, bool vSelected, const char* vFmt, ...) {
3974-
if (!vInfos.use_count()) return;
4008+
if (!vInfos.use_count())
4009+
return;
39754010

39764011
auto& fdi = m_FileDialogInternal.fileManager;
39774012

@@ -3990,8 +4025,9 @@ void IGFD::FileDialog::m_SelectableItem(int vidx, std::shared_ptr<FileInfos> vIn
39904025
#endif // USE_THUMBNAILS
39914026
#ifdef USE_EXPLORATION_BY_KEYS
39924027
bool flashed = m_BeginFlashItem((size_t)vidx);
3993-
bool res = m_FlashableSelectable(fdi.variadicBuffer, vSelected, selectableFlags, flashed, ImVec2(-1.0f, h));
3994-
if (flashed) m_EndFlashItem();
4028+
bool res = m_FlashableSelectable(fdi.variadicBuffer, vSelected, selectableFlags, flashed, ImVec2(-1.0f, h));
4029+
if (flashed)
4030+
m_EndFlashItem();
39954031
#else // USE_EXPLORATION_BY_KEYS
39964032
(void)vidx; // remove a warnings ofr unused var
39974033

@@ -4002,28 +4038,22 @@ void IGFD::FileDialog::m_SelectableItem(int vidx, std::shared_ptr<FileInfos> vIn
40024038
// nav system, selectable cause open directory or select directory
40034039
if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) {
40044040
// little fix for get back the mouse behavior in nav system
4005-
if (ImGui::IsMouseDoubleClicked(0)) // 0 -> left mouse button double click
4006-
{
4041+
if (ImGui::IsMouseDoubleClicked(0)) { // 0 -> left mouse button double click
40074042
fdi.pathClicked = fdi.SelectDirectory(vInfos);
4008-
} else if (fdi.dLGDirectoryMode) // directory chooser
4009-
{
4010-
fdi.SelectFileName(m_FileDialogInternal, vInfos);
4043+
} else if (fdi.dLGDirectoryMode) { // directory chooser
4044+
fdi.SelectOrDeselectFileName(m_FileDialogInternal, vInfos);
40114045
} else {
40124046
fdi.pathClicked = fdi.SelectDirectory(vInfos);
40134047
}
4014-
} else // no nav system => classic behavior
4015-
{
4016-
if (ImGui::IsMouseDoubleClicked(0)) // 0 -> left mouse button double click
4017-
{
4048+
} else { // no nav system => classic behavior
4049+
if (ImGui::IsMouseDoubleClicked(0)) { // 0 -> left mouse button double click
40184050
fdi.pathClicked = fdi.SelectDirectory(vInfos);
4019-
} else if (fdi.dLGDirectoryMode) // directory chooser
4020-
{
4021-
fdi.SelectFileName(m_FileDialogInternal, vInfos);
4051+
} else if (fdi.dLGDirectoryMode) { // directory chooser
4052+
fdi.SelectOrDeselectFileName(m_FileDialogInternal, vInfos);
40224053
}
40234054
}
40244055
} else {
4025-
fdi.SelectFileName(m_FileDialogInternal, vInfos);
4026-
4056+
fdi.SelectOrDeselectFileName(m_FileDialogInternal, vInfos);
40274057
if (ImGui::IsMouseDoubleClicked(0)) {
40284058
m_FileDialogInternal.isOk = true;
40294059
}

ImGuiFileDialog.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ class IGFD_API FilterManager {
411411
const FilterInfos& GetSelectedFilter() const;
412412
void ParseFilters(const char* vFilters); // Parse filter syntax, detect and parse filter collection
413413
void SetSelectedFilterWithExt(const std::string& vFilter); // Select filter
414-
bool m_FillFileStyle(std::shared_ptr<FileInfos> vFileInfos) const; // fill with the good style
414+
bool FillFileStyle(std::shared_ptr<FileInfos> vFileInfos) const; // fill with the good style
415415
void SetFileStyle(const IGFD_FileStyleFlags& vFlags, const char* vCriteria, const FileStyle& vInfos); // Set FileStyle
416416
void SetFileStyle(const IGFD_FileStyleFlags& vFlags,
417417
const char* vCriteria,
@@ -597,7 +597,7 @@ class IGFD_API FileManager {
597597
#endif
598598
static void m_CompleteFileInfos(const std::shared_ptr<FileInfos>& vInfos); // set time and date infos of a file (detail view mode)
599599
void m_RemoveFileNameInSelection(const std::string& vFileName); // selection : remove a file name
600-
void m_m_AddFileNameInSelection(const std::string& vFileName, bool vSetLastSelectionFileName); // selection : add a file name
600+
void m_AddFileNameInSelection(const std::string& vFileName, bool vSetLastSelectionFileName); // selection : add a file name
601601
void m_AddFile(const FileDialogInternal& vFileDialogInternal,
602602
const std::string& vPath,
603603
const std::string& vFileName,
@@ -651,12 +651,12 @@ class IGFD_API FileManager {
651651
void SetCurrentPath(const std::string& vCurrentPath); // set the current path
652652
void SetDefaultFileName(const std::string& vFileName);
653653
bool SelectDirectory(const std::shared_ptr<FileInfos>& vInfos); // enter directory
654-
void SelectFileName(const FileDialogInternal& vFileDialogInternal,
655-
const std::shared_ptr<FileInfos>& vInfos); // select filename
654+
void SelectAllFileNames();
655+
void SelectFileName(const FileDialogInternal& vFileDialogInternal, const std::shared_ptr<FileInfos>& vInfos); // add a filename in selection
656+
void SelectOrDeselectFileName(const FileDialogInternal& vFileDialogInternal,const std::shared_ptr<FileInfos>& vInfos); // add/remove a filename in selection
656657
void SetCurrentDir(const std::string& vPath); // define current directory for scan
657658
void ScanDir(const FileDialogInternal& vFileDialogInternal,
658659
const std::string& vPath); // scan the directory for retrieve the file list
659-
660660
std::string GetResultingPath();
661661
std::string GetResultingFileName(FileDialogInternal& vFileDialogInternal, IGFD_ResultMode vFlag);
662662
std::string GetResultingFilePathName(FileDialogInternal& vFileDialogInternal, IGFD_ResultMode vFlag);

ImGuiFileDialogConfig.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@
6868
// BackSpace for comming back to the last directory
6969
// #define IGFD_KEY_BACKSPACE ImGuiKey_Backspace
7070

71+
/////////////////////////////////
72+
//// SHORTCUTS => ctrl + KEY ////
73+
/////////////////////////////////
74+
75+
// #define SelectAllFilesKey ImGuiKey_A
76+
7177
/////////////////////////////////
7278
//// DIALOG EXIT ////////////////
7379
/////////////////////////////////

0 commit comments

Comments
 (0)