ocornut / imgui

Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
MIT License
58.36k stars 10k forks source link

Folder/File renaming in file browser #3325

Open dgregorius opened 4 years ago

dgregorius commented 4 years ago

Version: 177 Branch: docking Back-ends: imgui_impl_opengl3.cpp + imgui_impl_glfw.cpp Compiler: VS2019 (Community) Operating System: Windows 10 (version 2004)

My Issue/Question:

I have a basic file browser based on the std::filesystem. It looks currently something like this:

FileBrowser

I would like to add folder/file renaming, but I am not sure what the best way would be to add this. Basically the user can select a folder/file and then either press F2 or single click. The code looks like this:

// Folders for ( const auto& Entry : fs::directory_iterator( Selection ) ) { if ( Entry.is_directory() ) { char Buffer[ 256 ] = { 0 }; sprintf( Buffer, " %s %s", RN_ICON_FOLDER_CLOSED, Entry.path().filename().string().c_str() ); ImGui::Selectable( Buffer );

    if ( ImGui::IsItemDoubleClicked() )
        {
        mEditor->SetActiveDirectory( Entry.path() );
        }
    }
}

How do I edit the selectable if the user wants to rename the file? It seems I need to switch to a text input, but then it is not clear how I decide this. I feel this should be simple, but I am missing something right now.

Thanks, -Dirk

rokups commented 4 years ago

What you do is you render ImGui::InputText() widget instead of ImGui::Selectable() when rename is in progress. This is how i implemented it: https://github.com/rokups/rbfx/blob/f96fbeaea3a7c7a62d4bc430dc3dd4eff19ec086/Source/Tools/Editor/Tabs/ResourceTab.cpp#L137-L143

dgregorius commented 4 years ago

Nice work! This fixed also my alignment issues and giving focus to the text input :)

Thanks rokups!