-
Notifications
You must be signed in to change notification settings - Fork 2
/
WindowManager.cpp
185 lines (162 loc) · 4.83 KB
/
WindowManager.cpp
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "WindowManager.h"
#include "WindowHost.h"
#include "SystemCommon/ThreadEx.h"
#include "SystemCommon/Exceptions.h"
#include "common/ContainerEx.hpp"
#include <mutex>
#include <future>
namespace xziar::gui
{
namespace detail
{
common::mlog::MiniLogger<false>& wdLog()
{
static common::mlog::MiniLogger<false> wdlog(u"WindowHost", { common::mlog::GetConsoleBackend() });
return wdlog;
}
}
auto& AllBackends() noexcept
{
static std::vector<std::unique_ptr<WindowBackend>> Backends;
return Backends;
}
void detail::RegisterBackend(std::unique_ptr<WindowBackend> backend) noexcept
{
AllBackends().emplace_back(std::move(backend));
}
common::span<WindowBackend* const> WindowBackend::GetBackends() noexcept
{
static_assert(sizeof(WindowBackend*) == sizeof(std::unique_ptr<WindowBackend>));
const auto& backends = AllBackends();
const auto ptr = reinterpret_cast<WindowBackend* const*>(backends.data());
return { ptr, backends.size() };
}
enum class BackendStatus : uint8_t
{
None = 0, Inited = 1, Running = 2
};
struct WindowBackend::Pimpl
{
std::thread MainThread;
std::promise<void> RunningPromise;
std::atomic<uint8_t> Status;
bool SupportNewThread : 1;
Pimpl(bool supportNewThread) noexcept : Status(0), SupportNewThread(supportNewThread) {}
};
WindowBackend::WindowBackend(bool supportNewThread) noexcept : Impl(std::make_unique<Pimpl>(supportNewThread))
{
}
WindowBackend::~WindowBackend()
{}
void WindowBackend::OnInitialize(const void*)
{
[[maybe_unused]] const auto ret = common::TransitAtomicEnum(Impl->Status, BackendStatus::None, BackendStatus::Inited);
Ensures(ret);
}
void WindowBackend::OnDeInitialize() noexcept
{
[[maybe_unused]] const auto ret = common::TransitAtomicEnum(Impl->Status, BackendStatus::Inited, BackendStatus::None);
Ensures(ret);
}
void WindowBackend::OnPrepare() noexcept { }
void WindowBackend::OnTerminate() noexcept { }
void WindowBackend::CheckIfInited()
{
if (Impl->Status >= common::enum_cast(BackendStatus::Inited))
COMMON_THROW(common::BaseException, u"WindowHostBackend already initialized!").Attach("status", Impl->Status.load());
}
bool WindowBackend::Run(bool isNewThread, common::BasicPromise<void>* pms)
{
if (isNewThread && !Impl->SupportNewThread)
COMMON_THROW(common::BaseException, u"WindowHostBackend does not support run in new thread!");
if (common::TransitAtomicEnum(Impl->Status, BackendStatus::Inited, BackendStatus::Running))
{
Impl->RunningPromise = {};
if (isNewThread)
{ // new thread
std::promise<void> pms2;
Impl->MainThread = std::thread([&]()
{
common::ThreadObject::GetCurrentThreadObject().SetName(u"WindowManager");
OnPrepare();
if (pms)
pms->SetData();
pms2.set_value();
OnMessageLoop();
OnTerminate();
common::TransitAtomicEnum(Impl->Status, BackendStatus::Running, BackendStatus::Inited);
Impl->RunningPromise.set_value();
});
pms2.get_future().get();
}
else
{ // in place
OnPrepare();
if (pms)
pms->SetData();
OnMessageLoop();
OnTerminate();
common::TransitAtomicEnum(Impl->Status, BackendStatus::Running, BackendStatus::Inited);
Impl->RunningPromise.set_value();
}
return true;
}
return false;
}
bool WindowBackend::Stop()
{
if (Impl->Status == common::enum_cast(BackendStatus::Running))
{
if (RequestStop())
{
Impl->RunningPromise.get_future().get();
if (Impl->MainThread.joinable()) // ensure MainThread invalid
{
Impl->MainThread.join();
}
return true;
}
}
return false;
}
namespace detail
{
using common::loop::LoopBase;
WindowManager::WindowManager() : Logger(u"WindowManager", { common::mlog::GetConsoleBackend() })
{ }
WindowManager::~WindowManager()
{
//StopNewThread();
}
const void* WindowManager::GetWindowData(const WindowHost_*, std::string_view) const noexcept
{
return nullptr;
}
bool WindowManager::UnregisterHost(WindowHost_* host)
{
for (auto it = WindowList.begin(); it != WindowList.end(); ++it)
{
if (it->second.get() == host)
{
WindowList.erase(it);
return true;
}
}
return false;
}
void WindowManager::HandleTask()
{
while (!InvokeList.IsEmpty())
{
auto task = InvokeList.Begin();
task->Task();
InvokeList.PopNode(task);
}
}
void WindowManager::AddInvoke(std::function<void(void)>&& task)
{
InvokeList.AppendNode(new InvokeNode(std::move(task)));
NotifyTask();
}
}
}