forked from floft/sprouts
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgui.h
85 lines (79 loc) · 2.02 KB
/
gui.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#ifndef GUI_H_INCLUDED
#define GUI_H_INCLUDED
#include <memory>
#include <functional>
#include <vector>
#include <unordered_map>
#include "guielement.h"
#include "guicontainer.h"
#include "guibutton.h"
#include "guilabel.h"
#include "guicanvas.h"
#include "guicircle_arrangement.h"
/**************
*Description:
*Input:
*Output:
**************/
using namespace std;
class GUIRunner final : public enable_shared_from_this<GUIRunner>
{
shared_ptr<GUIContainer> gui;
vector<function<void()>> functionList;
static unordered_map<shared_ptr<GUIContainer>, weak_ptr<GUIRunner>> * runners;
static void makeRunners();
GUIRunner(shared_ptr<GUIContainer> gui)
: gui(gui)
{
makeRunners();
}
GUIRunner(const GUIRunner &) = delete;
const GUIRunner & operator =(const GUIRunner &) = delete;
bool runRetval = false;
bool needQuit = false;
public:
~GUIRunner()
{
if(gui != nullptr && runners != nullptr)
{
runners->erase(gui);
}
}
static shared_ptr<GUIRunner> make(shared_ptr<GUIContainer> gui)
{
makeRunners();
gui = gui->getTopLevelParent();
shared_ptr<GUIRunner> retval = shared_ptr<GUIRunner>(new GUIRunner(gui));
(*runners)[gui] = retval;
return retval;
}
static shared_ptr<GUIRunner> get(shared_ptr<GUIContainer> gui)
{
makeRunners();
gui = gui->getTopLevelParent();
auto iter = runners->find(gui);
if(iter == runners->end())
return shared_ptr<GUIRunner>(nullptr);
return std::get<1>(*iter).lock();
}
bool run();
void scheduleFunction(function<void()> fn)
{
functionList.push_back(fn);
}
void quit(bool retval = false)
{
needQuit = true;
runRetval = retval;
}
shared_ptr<GUIContainer> getGUI()
{
return gui;
}
};
inline bool runAsDialog(shared_ptr<GUIContainer> gui)
{
shared_ptr<GUIRunner> runner = GUIRunner::make(gui);
return runner->run();
}
#endif // GUI_H_INCLUDED