Skip to content

Commit 5237093

Browse files
committed
[ADD] : add the feature #157, for display a custom tooltip for a file in a column
1 parent 8ba5dd9 commit 5237093

3 files changed

Lines changed: 95 additions & 33 deletions

File tree

ImGuiFileDialog.cpp

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,33 @@ std::string IGFD::Utils::GetPathSeparator() {
909909
return std::string(1U, PATH_SEP);
910910
}
911911

912+
std::string IGFD::Utils::RoundNumber(double vvalue, int n) {
913+
std::stringstream tmp;
914+
tmp << std::setprecision(n) << std::fixed << vvalue;
915+
return tmp.str();
916+
}
917+
918+
std::string IGFD::Utils::FormatFileSize(size_t vByteSize) {
919+
if (vByteSize != 0) {
920+
static double lo = 1024.0;
921+
static double ko = 1024.0 * 1024.0;
922+
static double mo = 1024.0 * 1024.0 * 1024.0;
923+
924+
auto v = (double)vByteSize;
925+
926+
if (v < lo)
927+
return RoundNumber(v, 0) + " " + fileSizeBytes; // octet
928+
else if (v < ko)
929+
return RoundNumber(v / lo, 2) + " " + fileSizeKiloBytes; // ko
930+
else if (v < mo)
931+
return RoundNumber(v / ko, 2) + " " + fileSizeMegaBytes; // Mo
932+
else
933+
return RoundNumber(v / mo, 2) + " " + fileSizeGigaBytes; // Go
934+
}
935+
936+
return "0 " fileSizeBytes;
937+
}
938+
912939
#pragma endregion
913940

914941
#pragma region FileStyle
@@ -1806,7 +1833,7 @@ bool IGFD::FileManager::m_CompleteFileInfosWithUserFileAttirbutes(const FileDial
18061833
return false; // the file will be ignored, so not added to the file list, so not displayed
18071834
} else {
18081835
if (!vInfos->fileType.isDir()) {
1809-
vInfos->formatedFileSize = m_FormatFileSize(vInfos->fileSize);
1836+
vInfos->formatedFileSize = IGFD::Utils::FormatFileSize(vInfos->fileSize);
18101837
}
18111838
}
18121839
}
@@ -2046,33 +2073,6 @@ void IGFD::FileManager::m_ApplyFilteringOnFileList(const FileDialogInternal& vFi
20462073
}
20472074
}
20482075

2049-
std::string IGFD::FileManager::m_RoundNumber(double vvalue, int n) {
2050-
std::stringstream tmp;
2051-
tmp << std::setprecision(n) << std::fixed << vvalue;
2052-
return tmp.str();
2053-
}
2054-
2055-
std::string IGFD::FileManager::m_FormatFileSize(size_t vByteSize) {
2056-
if (vByteSize != 0) {
2057-
static double lo = 1024.0;
2058-
static double ko = 1024.0 * 1024.0;
2059-
static double mo = 1024.0 * 1024.0 * 1024.0;
2060-
2061-
auto v = (double)vByteSize;
2062-
2063-
if (v < lo)
2064-
return m_RoundNumber(v, 0) + " " + fileSizeBytes; // octet
2065-
else if (v < ko)
2066-
return m_RoundNumber(v / lo, 2) + " " + fileSizeKiloBytes; // ko
2067-
else if (v < mo)
2068-
return m_RoundNumber(v / ko, 2) + " " + fileSizeMegaBytes; // Mo
2069-
else
2070-
return m_RoundNumber(v / mo, 2) + " " + fileSizeGigaBytes; // Go
2071-
}
2072-
2073-
return "0 " fileSizeBytes;
2074-
}
2075-
20762076
void IGFD::FileManager::m_CompleteFileInfos(const std::shared_ptr<FileInfos>& vInfos) {
20772077
if (!vInfos.use_count()) return;
20782078

@@ -2105,7 +2105,7 @@ void IGFD::FileManager::m_CompleteFileInfos(const std::shared_ptr<FileInfos>& vI
21052105
if (!result) {
21062106
if (!vInfos->fileType.isDir()) {
21072107
vInfos->fileSize = (size_t)statInfos.st_size;
2108-
vInfos->formatedFileSize = m_FormatFileSize(vInfos->fileSize);
2108+
vInfos->formatedFileSize = IGFD::Utils::FormatFileSize(vInfos->fileSize);
21092109
}
21102110

21112111
size_t len = 0;
@@ -3911,6 +3911,16 @@ void IGFD::FileDialog::m_SelectableItem(int vidx, std::shared_ptr<FileInfos> vIn
39113911
}
39123912
}
39133913

3914+
void IGFD::FileDialog::m_DisplayFileInfosTooltip(const int32_t& vRowIdx, const int32_t& vColumnIdx, std::shared_ptr<FileInfos> vFileInfos) {
3915+
if (ImGui::IsItemHovered()) {
3916+
if (vFileInfos != nullptr && vFileInfos->tooltipColumn == vColumnIdx) {
3917+
if (!vFileInfos->tooltipMessage.empty()) {
3918+
ImGui::SetTooltip("%s", vFileInfos->tooltipMessage.c_str());
3919+
}
3920+
}
3921+
}
3922+
}
3923+
39143924
void IGFD::FileDialog::m_BeginFileColorIconStyle(std::shared_ptr<FileInfos> vFileInfos, bool& vOutShowColor, std::string& vOutStr, ImFont** vOutFont) {
39153925
vOutStr.clear();
39163926
vOutShowColor = false;
@@ -4049,6 +4059,7 @@ void IGFD::FileDialog::m_DrawFileListView(ImVec2 vSize) {
40494059
ImFont* _font = nullptr;
40504060
bool _showColor = false;
40514061

4062+
int column_id = 0;
40524063
m_FileListClipper.Begin((int)fdi.GetFilteredListSize(), ImGui::GetTextLineHeightWithSpacing());
40534064
while (m_FileListClipper.Step()) {
40544065
for (int i = m_FileListClipper.DisplayStart; i < m_FileListClipper.DisplayEnd; i++) {
@@ -4063,13 +4074,16 @@ void IGFD::FileDialog::m_DrawFileListView(ImVec2 vSize) {
40634074

40644075
ImGui::TableNextRow();
40654076

4077+
column_id = 0;
40664078
if (ImGui::TableNextColumn()) // file name
40674079
{
40684080
m_SelectableItem(i, infos, selected, _str.c_str());
4081+
m_DisplayFileInfosTooltip(i, column_id++, infos);
40694082
}
40704083
if (ImGui::TableNextColumn()) // file type
40714084
{
40724085
ImGui::Text("%s", infos->fileExtLevels[0].c_str());
4086+
m_DisplayFileInfosTooltip(i, column_id++, infos);
40734087
}
40744088
if (ImGui::TableNextColumn()) // file size
40754089
{
@@ -4078,10 +4092,12 @@ void IGFD::FileDialog::m_DrawFileListView(ImVec2 vSize) {
40784092
} else {
40794093
ImGui::TextUnformatted("");
40804094
}
4095+
m_DisplayFileInfosTooltip(i, column_id++, infos);
40814096
}
40824097
if (ImGui::TableNextColumn()) // file date + time
40834098
{
40844099
ImGui::Text("%s", infos->fileModifDate.c_str());
4100+
m_DisplayFileInfosTooltip(i, column_id++, infos);
40854101
}
40864102

40874103
m_EndFileColorIconStyle(_showColor, _font);
@@ -4231,6 +4247,7 @@ void IGFD::FileDialog::m_DrawThumbnailsListView(ImVec2 vSize) {
42314247
ImGuiContext& g = *GImGui;
42324248
const float itemHeight = ImMax(g.FontSize, DisplayMode_ThumbailsList_ImageHeight) + g.Style.ItemSpacing.y;
42334249

4250+
int column_id = 0;
42344251
m_FileListClipper.Begin((int)fdi.GetFilteredListSize(), itemHeight);
42354252
while (m_FileListClipper.Step()) {
42364253
for (int i = m_FileListClipper.DisplayStart; i < m_FileListClipper.DisplayEnd; i++) {
@@ -4245,13 +4262,16 @@ void IGFD::FileDialog::m_DrawThumbnailsListView(ImVec2 vSize) {
42454262

42464263
ImGui::TableNextRow();
42474264

4265+
column_id = 0;
42484266
if (ImGui::TableNextColumn()) // file name
42494267
{
42504268
m_SelectableItem(i, infos, selected, _str.c_str());
4269+
m_DisplayFileInfosTooltip(i, column_id++, infos);
42514270
}
42524271
if (ImGui::TableNextColumn()) // file type
42534272
{
42544273
ImGui::Text("%s", infos->fileExtLevels[0].c_str());
4274+
m_DisplayFileInfosTooltip(i, column_id++, infos);
42554275
}
42564276
if (ImGui::TableNextColumn()) // file size
42574277
{
@@ -4260,10 +4280,12 @@ void IGFD::FileDialog::m_DrawThumbnailsListView(ImVec2 vSize) {
42604280
} else {
42614281
ImGui::TextUnformatted("");
42624282
}
4283+
m_DisplayFileInfosTooltip(i, column_id++, infos);
42634284
}
42644285
if (ImGui::TableNextColumn()) // file date + time
42654286
{
42664287
ImGui::Text("%s", infos->fileModifDate.c_str());
4288+
m_DisplayFileInfosTooltip(i, column_id++, infos);
42674289
}
42684290
if (ImGui::TableNextColumn()) // file thumbnails
42694291
{
@@ -4275,6 +4297,7 @@ void IGFD::FileDialog::m_DrawThumbnailsListView(ImVec2 vSize) {
42754297
if (th->isReadyToDisplay && th->textureID) {
42764298
ImGui::Image((ImTextureID)th->textureID, ImVec2((float)th->textureWidth, (float)th->textureHeight));
42774299
}
4300+
m_DisplayFileInfosTooltip(i, column_id++, infos);
42784301
}
42794302

42804303
m_EndFileColorIconStyle(_showColor, _font);

ImGuiFileDialog.h

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,23 @@ config.userFileAttributes = [](IGFD::FileInfos* vFileInfosPtr, IGFD::UserDatas v
10741074
};
10751075
```
10761076
1077+
you can also display a tootlip for a file displayed when the mouse is over a dedicated column
1078+
1079+
you juste need to set your message for the FileDialogConfig.tooltipMessage
1080+
and specify the column in FileDialogConfig.tooltipColumn
1081+
1082+
ex code from the DemoApp branch for display the decomposition of gltf total size
1083+
1084+
syntax :
1085+
```cpp
1086+
vFileInfosPtr->tooltipMessage = toStr("%s : %s\n%s : %s", //
1087+
(vFileInfosPtr->fileNameLevels[0] + ".gltf").c_str(), //
1088+
IGFD::Utils::FormatFileSize(vFileInfosPtr->fileSize).c_str(), //
1089+
(vFileInfosPtr->fileNameLevels[0] + ".bin").c_str(), //
1090+
IGFD::Utils::FormatFileSize((size_t)statInfos.st_size).c_str()); //
1091+
vFileInfosPtr->tooltipColumn = 1;
1092+
```
1093+
10771094
################################################################
10781095
## How to Integrate ImGuiFileDialog in your project
10791096
################################################################
@@ -1501,6 +1518,8 @@ class IGFD_API Utils {
15011518
static size_t GetCharCountInString(const std::string& vString, const char& vChar);
15021519
static size_t GetLastCharPosWithMinCharCount(const std::string& vString, const char& vChar, const size_t& vMinCharCount);
15031520
static std::string GetPathSeparator(); // return the slash for any OS ( \\ win, / unix)
1521+
static std::string RoundNumber(double vvalue, int n); // custom rounding number
1522+
static std::string FormatFileSize(size_t vByteSize); // format file size field
15041523
};
15051524

15061525
#pragma endregion
@@ -1675,6 +1694,8 @@ class IGFD_API FileInfos {
16751694
std::string fileName; // file name only
16761695
std::string fileNameExt; // filename of the file (file name + extention) (but no path)
16771696
std::string fileNameExt_optimized; // optimized for search => insensitivecase
1697+
std::string tooltipMessage; // message to display on the tooltip, is not empty
1698+
int32_t tooltipColumn = -1; // the tooltip will appears only when the mouse is over the tooltipColumn if > -1
16781699
size_t fileSize = 0U; // for sorting operations
16791700
std::string formatedFileSize; // file size formated (10 o, 10 ko, 10 mo, 10 go)
16801701
std::string fileModifDate; // file user defined format of the date (data + time by default)
@@ -1787,8 +1808,6 @@ class IGFD_API FileManager {
17871808
#else
17881809
private:
17891810
#endif
1790-
static std::string m_RoundNumber(double vvalue, int n); // custom rounding number
1791-
static std::string m_FormatFileSize(size_t vByteSize); // format file size field
17921811
static void m_CompleteFileInfos(const std::shared_ptr<FileInfos>& vInfos); // set time and date infos of a file (detail view mode)
17931812
void m_RemoveFileNameInSelection(const std::string& vFileName); // selection : remove a file name
17941813
void m_m_AddFileNameInSelection(const std::string& vFileName, bool vSetLastSelectionFileName); // selection : add a file name
@@ -2217,6 +2236,8 @@ class IGFD_API FileDialog : public BookMarkFeature, public KeyExplorerFeature, p
22172236
std::string& vOutStr,
22182237
ImFont** vOutFont); // begin style apply of filter with color an icon if any
22192238
void m_EndFileColorIconStyle(const bool& vShowColor, ImFont* vFont); // end style apply of filter
2239+
2240+
void m_DisplayFileInfosTooltip(const int32_t& vRowIdx, const int32_t& vColumnIdx, std::shared_ptr<FileInfos> vFileInfos);
22202241
};
22212242

22222243
#pragma endregion

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,11 +1040,29 @@ config.userFileAttributes = [](IGFD::FileInfos* vFileInfosPtr, IGFD::UserDatas v
10401040

10411041
Before the User of the userAttribute callback :
10421042

1043-
![user_files_attributes_before.gif](https://github.com/aiekick/ImGuiFileDialog/blob/DemoApp/doc/user_files_attributes_before.png)
1043+
![user_files_attributes_before.png](https://github.com/aiekick/ImGuiFileDialog/blob/DemoApp/doc/user_files_attributes_before.png)
10441044

10451045
After :
10461046

1047-
![user_files_attributes_after.gif](https://github.com/aiekick/ImGuiFileDialog/blob/DemoApp/doc/user_files_attributes_after.png)
1047+
![user_files_attributes_after.png](https://github.com/aiekick/ImGuiFileDialog/blob/DemoApp/doc/user_files_attributes_after.png)
1048+
1049+
You can also display a tootlip for a file displayed when the mouse is over a dedicated column
1050+
1051+
you juste need to set your message for the FileDialogConfig.tooltipMessage
1052+
and specify the column in FileDialogConfig.tooltipColumn
1053+
1054+
ex code from the DemoApp branch for display the decomposition of gltf total size
1055+
1056+
syntax :
1057+
```cpp
1058+
vFileInfosPtr->tooltipMessage = toStr("%s : %s\n%s : %s", //
1059+
(vFileInfosPtr->fileNameLevels[0] + ".gltf").c_str(), //
1060+
IGFD::Utils::FormatFileSize(vFileInfosPtr->fileSize).c_str(), //
1061+
(vFileInfosPtr->fileNameLevels[0] + ".bin").c_str(), //
1062+
IGFD::Utils::FormatFileSize((size_t)statInfos.st_size).c_str()); //
1063+
vFileInfosPtr->tooltipColumn = 1; // column of file size
1064+
```
1065+
![file_tooltip_message.png](https://github.com/aiekick/ImGuiFileDialog/blob/DemoApp/doc/file_tooltip_message.png)
10481066
10491067
</blockquote></details>
10501068

0 commit comments

Comments
 (0)