AirGuanZ / imgui-filebrowser

File browser implementation for dear-imgui. C++17 is required.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

crashes when selecting ok when no directory is selected

claudeha opened this issue · comments

In a directory browser, if you press ok without selecting anything then ok_ is still set, so HasSelected() is true despite selectedFilenames_ being empty, which means user code that calls GetSelected() will crash (begin() points nowhere). Here is a small fix that returns the current directory in that case, preventing the crash, and providing reasonably intuitive behaviour.

diff --git a/imgui-filebrowser/imfilebrowser.h b/imgui-filebrowser/imfilebrowser.h
index 8b92c598..18909c8f 100644
--- a/imgui-filebrowser/imfilebrowser.h
+++ b/imgui-filebrowser/imfilebrowser.h
@@ -587,6 +587,10 @@ inline const class std::filesystem::path &ImGui::FileBrowser::GetPwd() const noe
 
 inline std::filesystem::path ImGui::FileBrowser::GetSelected() const
 {
+    if ((flags_ & ImGuiFileBrowserFlags_SelectDirectory) && selectedFilenames_.empty())
+    {
+        return pwd_;
+    }
     return pwd_ / *selectedFilenames_.begin();
 }
 

If absolutely necessary I could go through the pull request dance...

Hi,
Thank you for pointing out this. I also note that in this case, FileBrowser::GetMultiSelected doesn't return current directory as promised in README/Usage. So I modified both functions according to your suggestion. Tks again!