Skip to content

Commit

Permalink
#5675: Block auto saves when a modal dialog is open
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Aug 6, 2021
1 parent c895b4f commit 4cbcf68
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
38 changes: 38 additions & 0 deletions libs/wxutil/dialog/AutoSaveRequestBlocker.h
@@ -0,0 +1,38 @@
#pragma once

#include "imessagebus.h"
#include "iradiant.h"
#include "messages/AutomaticMapSaveRequest.h"

namespace wxutil
{

class AutoSaveRequestBlocker
{
private:
std::size_t _msgSubscription;
std::string _reason;

public:
AutoSaveRequestBlocker(const std::string& reason) :
_reason(reason)
{
_msgSubscription = GlobalRadiantCore().getMessageBus().addListener(
radiant::IMessage::Type::AutomaticMapSaveRequest,
radiant::TypeListener<map::AutomaticMapSaveRequest>(
sigc::mem_fun(this, &AutoSaveRequestBlocker::handleRequest)));
}

~AutoSaveRequestBlocker()
{
GlobalRadiantCore().getMessageBus().removeListener(_msgSubscription);
}

private:
void handleRequest(map::AutomaticMapSaveRequest& msg)
{
msg.denyWithReason(_reason);
}
};

}
10 changes: 10 additions & 0 deletions libs/wxutil/dialog/DialogBase.cpp
@@ -1,5 +1,7 @@
#include "DialogBase.h"

#include "AutoSaveRequestBlocker.h"

namespace wxutil
{

Expand Down Expand Up @@ -57,4 +59,12 @@ void DialogBase::FitToScreen(float xProp, float yProp)
CenterOnScreen();
}

int DialogBase::ShowModal()
{
// While this dialog is active, block any auto save requests
AutoSaveRequestBlocker blocker("Modal Dialog is active");

return wxDialog::ShowModal();
}

}
2 changes: 2 additions & 0 deletions libs/wxutil/dialog/DialogBase.h
Expand Up @@ -32,6 +32,8 @@ class DialogBase :
*/
void FitToScreen(float xProp, float yProp);

virtual int ShowModal() override;

protected:
// Overrideable: return true to prevent the window from being deleted
virtual bool _onDeleteEvent()
Expand Down
1 change: 1 addition & 0 deletions tools/msvc/wxutillib.vcxproj
Expand Up @@ -151,6 +151,7 @@
<ClInclude Include="..\..\libs\wxutil\dataview\TreeViewItemStyle.h" />
<ClInclude Include="..\..\libs\wxutil\dataview\VFSTreePopulator.h" />
<ClInclude Include="..\..\libs\wxutil\DeferredMotionDelta.h" />
<ClInclude Include="..\..\libs\wxutil\dialog\AutoSaveRequestBlocker.h" />
<ClInclude Include="..\..\libs\wxutil\dialog\Dialog.h" />
<ClInclude Include="..\..\libs\wxutil\dialog\DialogBase.h" />
<ClInclude Include="..\..\libs\wxutil\dialog\DialogElements.h" />
Expand Down
3 changes: 3 additions & 0 deletions tools/msvc/wxutillib.vcxproj.filters
Expand Up @@ -150,6 +150,9 @@
</ClInclude>
<ClInclude Include="..\..\libs\wxutil\Bitmap.h" />
<ClInclude Include="..\..\libs\wxutil\LocalBitmapArtProvider.h" />
<ClInclude Include="..\..\libs\wxutil\dialog\AutoSaveRequestBlocker.h">
<Filter>dialog</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\libs\wxutil\dialog\MessageBox.cpp">
Expand Down

0 comments on commit 4cbcf68

Please sign in to comment.