Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
#5662: More regular status updates
  • Loading branch information
codereader committed Jul 18, 2021
1 parent 5ac648b commit 201fdbf
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 30 deletions.
79 changes: 51 additions & 28 deletions plugins/vcs/ui/VcsStatus.cpp
Expand Up @@ -28,7 +28,8 @@ namespace ui

VcsStatus::VcsStatus(wxWindow* parent) :
_panel(loadNamedPanel(parent, "VcsStatusBar")),
_timer(this),
_fetchTimer(this),
_statusTimer(this),
_fetchInProgress(false),
_popupMenu(new wxutil::PopupMenu)
{
Expand All @@ -46,10 +47,10 @@ VcsStatus::VcsStatus(wxWindow* parent) :
_panel->Bind(wxEVT_IDLE, &VcsStatus::onIdle, this);

GlobalRegistry().signalForKey(RKEY_AUTO_FETCH_ENABLED).connect(
sigc::mem_fun(this, &VcsStatus::restartTimer)
sigc::mem_fun(this, &VcsStatus::restartFetchTimer)
);
GlobalRegistry().signalForKey(RKEY_AUTO_FETCH_INTERVAL).connect(
sigc::mem_fun(this, &VcsStatus::restartTimer)
sigc::mem_fun(this, &VcsStatus::restartFetchTimer)
);

GlobalMapModule().signal_modifiedChanged().connect(
Expand All @@ -61,6 +62,8 @@ VcsStatus::VcsStatus(wxWindow* parent) :
);

createPopupMenu();

_statusTimer.Start(500);
}

VcsStatus::~VcsStatus()
Expand Down Expand Up @@ -107,12 +110,12 @@ void VcsStatus::setRepository(const std::shared_ptr<git::Repository>& repository
if (!_repository)
{
_remoteStatus->SetLabel(_("Not under version control"));
_timer.Stop();
_fetchTimer.Stop();
return;
}

_remoteStatus->SetLabel(_repository->getCurrentBranchName());
restartTimer();
restartFetchTimer();

// Run a fetch update right after connecting to the repo, if auto-fetch is enabled
if (registry::getValue<bool>(RKEY_AUTO_FETCH_ENABLED))
Expand All @@ -121,26 +124,31 @@ void VcsStatus::setRepository(const std::shared_ptr<git::Repository>& repository
}
}

void VcsStatus::restartTimer()
void VcsStatus::restartFetchTimer()
{
_timer.Stop();
_fetchTimer.Stop();

if (registry::getValue<bool>(RKEY_AUTO_FETCH_ENABLED))
{
int interval = static_cast<int>(registry::getValue<float>(RKEY_AUTO_FETCH_INTERVAL) * 60 * 1000);

if (interval > 0)
{
_timer.Start(interval);
_fetchTimer.Start(interval);
}
}
}

void VcsStatus::onMapEvent(IMap::MapEvent ev)
{
if (ev == IMap::MapSaved)
if (ev == IMap::MapSaved || ev == IMap::MapLoaded)
{
updateMapFileStatus();

if (_repository)
{
analyseRemoteStatus(_repository);
}
}
}

Expand All @@ -166,24 +174,42 @@ void VcsStatus::startFetchTask()

void VcsStatus::onIntervalReached(wxTimerEvent& ev)
{
startFetchTask();
if (ev.GetTimer().GetId() == _fetchTimer.GetId())
{
startFetchTask();
}
else if (ev.GetTimer().GetId() == _statusTimer.GetId())
{
updateMapFileStatus();
}
}

void VcsStatus::updateMapFileStatus()
{
if (GlobalMapModule().isUnnamed())
{
setMapFileStatus(_("Map not saved yet"));
return;
}

if (GlobalMapModule().getActiveMergeOperation())
{
setMapFileStatus(_("Merging"));
return;
}

if (GlobalMapModule().isModified())
{
_mapStatus->SetLabel(_("Map is modified"));
return;
}
else
{
_mapStatus->SetLabel(_("Map is saved"));

if (_repository)
{
auto repository = _repository->clone();
_mapFileTask = std::async(std::launch::async, std::bind(&VcsStatus::performMapFileStatusCheck, this, repository));
}
_mapStatus->SetLabel(_("Map is saved"));

if (_repository)
{
auto repository = _repository->clone();
_mapFileTask = std::async(std::launch::async, std::bind(&VcsStatus::performMapFileStatusCheck, this, repository));
}
}

Expand Down Expand Up @@ -218,17 +244,22 @@ void VcsStatus::performFetch(std::shared_ptr<git::Repository> repository)
setRemoteStatus(git::RemoteStatus{ 0, 0, _("Fetching...") });

repository->fetchFromTrackedRemote();

setRemoteStatus(git::analyseRemoteStatus(repository));
}
catch (const git::GitException& ex)
{
setRemoteStatus(git::RemoteStatus{ 0, 0, ex.what() });
}

analyseRemoteStatus(repository);

_fetchInProgress = false;
}

void VcsStatus::analyseRemoteStatus(std::shared_ptr<git::Repository> repository)
{
setRemoteStatus(git::analyseRemoteStatus(repository));
}

void VcsStatus::performSync(std::shared_ptr<git::Repository> repository)
{
try
Expand Down Expand Up @@ -273,16 +304,8 @@ void VcsStatus::setRemoteStatus(const git::RemoteStatus& status)

void VcsStatus::performMapFileStatusCheck(std::shared_ptr<git::Repository> repository)
{
setMapFileStatus(_("Checking map status..."));

try
{
if (GlobalMapModule().isUnnamed())
{
setMapFileStatus(_("Map not saved yet"));
return;
}

auto relativePath = repository->getRepositoryRelativePath(GlobalMapModule().getMapName());

if (relativePath.empty())
Expand Down
6 changes: 4 additions & 2 deletions plugins/vcs/ui/VcsStatus.h
Expand Up @@ -27,7 +27,8 @@ class VcsStatus final :
private:
wxPanel* _panel;

wxTimer _timer;
wxTimer _fetchTimer;
wxTimer _statusTimer;
std::mutex _taskLock;
bool _fetchInProgress;
std::future<void> _fetchTask;
Expand All @@ -53,7 +54,7 @@ class VcsStatus final :
private:
void createPopupMenu();
void startFetchTask();
void restartTimer();
void restartFetchTimer();
void onIntervalReached(wxTimerEvent& ev);
void onIdle(wxIdleEvent& ev);
void performFetch(std::shared_ptr<git::Repository> repository);
Expand All @@ -64,6 +65,7 @@ class VcsStatus final :
void onMapEvent(IMap::MapEvent ev);
void setMapFileStatus(const std::string& status);
void setRemoteStatus(const git::RemoteStatus& status);
void analyseRemoteStatus(std::shared_ptr<git::Repository> repository);

std::string getRepositoryRelativePath(const std::string& path, const std::shared_ptr<git::Repository>& repository);
};
Expand Down

0 comments on commit 201fdbf

Please sign in to comment.