/
LongRunningOperationHandler.h
64 lines (54 loc) · 1.29 KB
/
LongRunningOperationHandler.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#pragma once
#include <functional>
#include "iradiant.h"
#include "imainframe.h"
#include "messages/LongRunningOperationMessage.h"
namespace ui
{
/**
* Listener class responding to LongRunningOperationMessages
* sent from the core routines. It will show a blocking dialog
* when any operation is started, and will hide that window
* once the last running operation is finished.
*/
class LongRunningOperationHandler
{
private:
std::size_t _level;
IScopedScreenUpdateBlockerPtr _blocker;
public:
LongRunningOperationHandler() :
_level(0)
{
GlobalRadiantCore().getMessageBus().addListener(
radiant::TypeListener<radiant::LongRunningOperationMessage>(
std::bind(&LongRunningOperationHandler::onMessage, this, std::placeholders::_1)
)
);
}
private:
void onMessage(radiant::LongRunningOperationMessage& message)
{
if (message.getType() == radiant::OperationEvent::Started)
{
std::string title = message.getTitle();
if (title.empty())
{
title = _("Operation in progress");
}
if (++_level == 1)
{
_blocker = GlobalMainFrame().getScopedScreenUpdateBlocker(title, std::string());
}
}
else if (message.getType() == radiant::OperationEvent::Finished)
{
assert(_level > 0);
if (_level > 0 && --_level == 0)
{
_blocker.reset();
}
}
}
};
}