Skip to content

Commit

Permalink
#5231: Convert call to askForSave() to a veto-able ApplicationShutdow…
Browse files Browse the repository at this point in the history
…nRequest sent over the message bus
  • Loading branch information
codereader committed Jul 11, 2020
1 parent 6ec1bd3 commit f394ef1
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 19 deletions.
1 change: 1 addition & 0 deletions include/imessagebus.h
Expand Up @@ -39,6 +39,7 @@ class IMessage
// Plugin code can define their own IDs in the range of 1000+
enum Type : std::size_t
{
ApplicationShutdownRequest,
AutomaticMapSaveRequest,
CommandExecutionFailed,
GameConfigNeeded,
Expand Down
41 changes: 41 additions & 0 deletions libs/messages/ApplicationShutdownRequest.h
@@ -0,0 +1,41 @@
#pragma once

#include "imessagebus.h"

namespace radiant
{

/**
* Message that is broadcast when the user requests to close
* the application. Subscribes can deny() the request to prevent
* the loss of unsaved data or similar.
*/
class ApplicationShutdownRequest :
public radiant::IMessage
{
private:
bool _denied;

public:
ApplicationShutdownRequest()
{}

std::size_t getId() const override
{
return Type::ApplicationShutdownRequest;
}

// Deny this request to keep the app running
void deny()
{
_denied = true;
}

// TRUE whether this request has been denied and the app should stay alive
bool isDenied() const
{
return _denied;
}
};

}
44 changes: 25 additions & 19 deletions radiant/ui/mainframe/MainFrame.cpp
Expand Up @@ -22,6 +22,7 @@
#include "ui/mainframe/TopLevelFrame.h"

#include "module/StaticModule.h"
#include "messages/ApplicationShutdownRequest.h"
#include <functional>
#include <fmt/format.h>

Expand Down Expand Up @@ -274,31 +275,36 @@ void MainFrame::preDestructionCleanup()

void MainFrame::onTopLevelFrameClose(wxCloseEvent& ev)
{
// If the event is vetoable, first check for unsaved data before closing
if (ev.CanVeto() && false /*!GlobalMap().askForSave(_("Exit DarkRadiant"))*/) // TODO
// If the event is vetoable, issue the shutdown message and get the green light
if (ev.CanVeto())
{
// Do nothing
ev.Veto();
radiant::ApplicationShutdownRequest request;
GlobalRadiantCore().getMessageBus().sendMessage(request);

if (request.isDenied())
{
// Keep running
ev.Veto();
return;
}
}
else
{
wxASSERT(wxTheApp->GetTopWindow() == _topLevelWindow);

wxASSERT(wxTheApp->GetTopWindow() == _topLevelWindow);

_topLevelWindow->Hide();
_topLevelWindow->Hide();

// Invoke cleanup code which still needs the GUI hierarchy to be
// present
preDestructionCleanup();
// Invoke cleanup code which still needs the GUI hierarchy to be
// present
preDestructionCleanup();

// Destroy the actual window
_topLevelWindow->Destroy();
// Destroy the actual window
_topLevelWindow->Destroy();

// wxWidgets is supposed to quit when the main window is destroyed, but
// it doesn't so we need to exit the main loop manually. Probably we
// are keeping some other window around internally which makes wx think
// that the application is still needed.
wxTheApp->ExitMainLoop();
}
// wxWidgets is supposed to quit when the main window is destroyed, but
// it doesn't so we need to exit the main loop manually. Probably we
// are keeping some other window around internally which makes wx think
// that the application is still needed.
wxTheApp->ExitMainLoop();
}

wxFrame* MainFrame::getWxTopLevelWindow()
Expand Down
1 change: 1 addition & 0 deletions tools/msvc/libs.vcxproj
Expand Up @@ -158,6 +158,7 @@
<ClInclude Include="..\..\libs\generic\callback.h" />
<ClInclude Include="..\..\libs\KeyValueStore.h" />
<ClInclude Include="..\..\libs\maplib.h" />
<ClInclude Include="..\..\libs\messages\ApplicationShutdownRequest.h" />
<ClInclude Include="..\..\libs\messages\AutomaticMapSaveRequest.h" />
<ClInclude Include="..\..\libs\messages\CommandExecutionFailed.h" />
<ClInclude Include="..\..\libs\messages\GameConfigNeededMessage.h" />
Expand Down
3 changes: 3 additions & 0 deletions tools/msvc/libs.vcxproj.filters
Expand Up @@ -234,6 +234,9 @@
<ClInclude Include="..\..\libs\selection\BestPoint.h">
<Filter>selection</Filter>
</ClInclude>
<ClInclude Include="..\..\libs\messages\ApplicationShutdownRequest.h">
<Filter>messages</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="util">
Expand Down

0 comments on commit f394ef1

Please sign in to comment.