Skip to content

Commit b6660e7

Browse files
committed
Prevents Dear imgui reentry when a modal dialog is display on Windows
1 parent 8199563 commit b6660e7

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

include/cinder/app/msw/PlatformMsw.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,19 @@ class CI_API PlatformMsw : public Platform {
7171
//! Set the windows specific HWND, needed for some things like file dialogs.
7272
void setHwnd( HWND hwnd ) { mHwnd = hwnd; }
7373

74+
//! Returns whether the application is inside a modal dialog loop
75+
bool isInsideModalLoop() const { return mInsideModalLoop; }
76+
//! Flags whether the application is currently inside the event loop of modal dialog
77+
void setInsideModalLoop( bool inside = true ) { mInsideModalLoop = inside; }
78+
7479
private:
7580
std::unique_ptr<std::ostream> mOutputStream;
7681
bool mDirectConsoleToCout;
7782

7883
bool mDisplaysInitialized;
7984
std::vector<DisplayRef> mDisplays;
8085
HWND mHwnd = nullptr;
86+
bool mInsideModalLoop = false;
8187
};
8288

8389
//! MSW-specific Exception for failed resource loading, reports windows resource id and type

src/cinder/CinderImGui.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
#include "cinder/gl/Context.h"
1111
#include "cinder/Clipboard.h"
1212

13+
#if defined( CINDER_MSW )
14+
#include "cinder/app/msw/PlatformMsw.h"
15+
#endif
16+
1317
#include <unordered_map>
1418

1519
static bool sInitialized = false;
@@ -653,6 +657,14 @@ static void ImGui_ImplCinder_NewFrameGuard( const ci::app::WindowRef& window )
653657

654658
static void ImGui_ImplCinder_PostDraw()
655659
{
660+
#if defined( CINDER_MSW )
661+
// Skip ImGui rendering when inside modal dialog loop, but reset trigger flag
662+
if( static_cast<ci::app::PlatformMsw*>( ci::app::Platform::get() )->isInsideModalLoop() ) {
663+
sTriggerNewFrame = true;
664+
return;
665+
}
666+
#endif
667+
656668
ImGui::Render();
657669
ImDrawData* drawData = ImGui::GetDrawData();
658670

src/cinder/app/msw/PlatformMsw.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,11 @@ fs::path PlatformMsw::getOpenFilePath( const fs::path &initialPath, const std::v
178178
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
179179

180180
// Display the Open dialog box.
181-
if( ::GetOpenFileNameW( &ofn ) == TRUE ) {
181+
setInsideModalLoop( true );
182+
BOOL result = ::GetOpenFileNameW( &ofn );
183+
setInsideModalLoop( false );
184+
185+
if( result == TRUE ) {
182186
return fs::path( ofn.lpstrFile );
183187
}
184188
else
@@ -214,7 +218,9 @@ fs::path PlatformMsw::getFolderPath( const fs::path &initialPath )
214218
bi.lParam = reinterpret_cast<LPARAM>( initialPathWide.c_str() );
215219
bi.lpfn = getFolderPathBrowseCallbackProc;
216220
bi.lpszTitle = L"Pick a Directory";
221+
setInsideModalLoop( true );
217222
::LPITEMIDLIST pidl = ::SHBrowseForFolder( &bi );
223+
setInsideModalLoop( false );
218224
if( pidl ) {
219225
// get the name of the folder
220226
TCHAR path[MAX_PATH];
@@ -307,8 +313,12 @@ fs::path PlatformMsw::getSaveFilePath( const fs::path &initialPath, const std::v
307313
}
308314
ofn.Flags = OFN_SHOWHELP | OFN_OVERWRITEPROMPT;
309315

310-
// Display the Open dialog box.
311-
if( ::GetSaveFileName( &ofn ) == TRUE )
316+
// Display the Save dialog box.
317+
setInsideModalLoop( true );
318+
BOOL result = ::GetSaveFileName( &ofn );
319+
setInsideModalLoop( false );
320+
321+
if( result == TRUE )
312322
return fs::path( ofn.lpstrFile );
313323
else
314324
return fs::path();

0 commit comments

Comments
 (0)