Skip to content

Commit

Permalink
#5662: Extract UserInterfaceModule to allow plugins to dispatch code …
Browse files Browse the repository at this point in the history
…to the UI thread
  • Loading branch information
codereader committed Jul 5, 2021
1 parent 5c746b5 commit ef045b7
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 8 deletions.
29 changes: 29 additions & 0 deletions include/iuserinterface.h
@@ -0,0 +1,29 @@
#pragma once

#include "imodule.h"

namespace ui
{

class IUserInterfaceModule :
public RegisterableModule
{
public:
virtual ~IUserInterfaceModule() {}

// Runs the specified action in the UI thread
// this happens when the application has a chance to, usually during event processing
// This method is safe to be called from any thread.
virtual void dispatch(const std::function<void()>& action) = 0;
};

}

constexpr const char* const MODULE_USERINTERFACE = "UserInterfaceModule";

// The accessor function
inline ui::IUserInterfaceModule& GlobalUserInterface()
{
static module::InstanceReference<ui::IUserInterfaceModule> _reference(MODULE_USERINTERFACE);
return _reference;
}
6 changes: 6 additions & 0 deletions plugins/vcs/GitModule.cpp
Expand Up @@ -75,6 +75,12 @@ void GitModule::shutdownModule()
{
rMessage() << getName() << "::shutdownModule called." << std::endl;

if (_statusBarWidget)
{
_statusBarWidget->Destroy();
_statusBarWidget = nullptr;
}

_repository.reset();

git_libgit2_shutdown();
Expand Down
18 changes: 14 additions & 4 deletions plugins/vcs/ui/VcsStatus.h
@@ -1,5 +1,6 @@
#pragma once

#include "iuserinterface.h"
#include <wx/panel.h>
#include <wx/stattext.h>
#include <wx/sizer.h>
Expand All @@ -14,7 +15,7 @@ namespace vcs
namespace ui
{

class VcsStatus :
class VcsStatus final :
public wxPanel
{
private:
Expand Down Expand Up @@ -43,6 +44,14 @@ class VcsStatus :
Bind(wxEVT_TIMER, &VcsStatus::onIntervalReached, this);
}

~VcsStatus()
{
if (_fetchTask.valid())
{
_fetchTask.get(); // Wait for the thread to complete
}
}

void setRepository(const std::shared_ptr<git::Repository>& repository)
{
_repository = repository;
Expand Down Expand Up @@ -72,17 +81,18 @@ class VcsStatus :
_fetchInProgress = true;
auto repository = _repository->clone();
_fetchTask = std::async(std::launch::async, std::bind(&VcsStatus::performFetch, this, repository));

_text->SetLabel("Fetching...");
}

void performFetch(std::shared_ptr<git::Repository> repository)
{
rMessage() << "Async fetch from remote..." << std::endl;
GlobalUserInterface().dispatch([this]() { _text->SetLabel("Fetching..."); });

repository->fetchFromTrackedRemote();

std::lock_guard<std::mutex> guard(_fetchLock);
_fetchInProgress = false;

GlobalUserInterface().dispatch([this]() { _text->SetLabel("Up to date"); });
}
};

Expand Down
2 changes: 1 addition & 1 deletion radiant/ui/UserInterfaceModule.cpp
Expand Up @@ -84,7 +84,7 @@ namespace

const std::string& UserInterfaceModule::getName() const
{
static std::string _name("UserInterfaceModule");
static std::string _name(MODULE_USERINTERFACE);
return _name;
}

Expand Down
6 changes: 3 additions & 3 deletions radiant/ui/UserInterfaceModule.h
Expand Up @@ -3,7 +3,7 @@
#include <sigc++/connection.h>
#include <wx/event.h>

#include "imodule.h"
#include "iuserinterface.h"
#include "iorthocontextmenu.h"
#include "icommandsystem.h"

Expand Down Expand Up @@ -35,7 +35,7 @@ namespace ui
*/
class UserInterfaceModule :
public wxEvtHandler,
public RegisterableModule
public IUserInterfaceModule
{
private:
std::unique_ptr<LongRunningOperationHandler> _longOperationHandler;
Expand Down Expand Up @@ -68,7 +68,7 @@ class UserInterfaceModule :
// Runs the specified action in the UI thread
// this happens when the application has a chance to, usually during event processing
// This method is safe to be called from any thread.
void dispatch(const std::function<void()>& action);
void dispatch(const std::function<void()>& action) override;

private:
void registerUICommands();
Expand Down
1 change: 1 addition & 0 deletions tools/msvc/include.vcxproj
Expand Up @@ -201,6 +201,7 @@
<ClInclude Include="..\..\include\itransformable.h" />
<ClInclude Include="..\..\include\itransformnode.h" />
<ClInclude Include="..\..\include\iundo.h" />
<ClInclude Include="..\..\include\iuserinterface.h" />
<ClInclude Include="..\..\include\ivolumetest.h" />
<ClInclude Include="..\..\include\iwxgl.h" />
<ClInclude Include="..\..\include\mapfile.h" />
Expand Down

0 comments on commit ef045b7

Please sign in to comment.