-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdisplay_manager.cpp
More file actions
76 lines (64 loc) · 2.02 KB
/
Copy pathdisplay_manager.cpp
File metadata and controls
76 lines (64 loc) · 2.02 KB
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
#include "display_manager.h"
#include <utility>
namespace nativeapi {
DisplayManager& DisplayManager::GetInstance() {
static DisplayManager instance;
return instance;
}
std::vector<std::shared_ptr<Display>> DisplayManager::GetAll() {
return Reconcile(EnumerateNativeDisplays(), nullptr, nullptr);
}
std::shared_ptr<Display> DisplayManager::GetPrimary() {
auto natives = EnumerateNativeDisplays();
auto displays = Reconcile(natives, nullptr, nullptr);
for (size_t i = 0; i < natives.size(); ++i) {
if (natives[i].is_primary) {
return displays[i];
}
}
return displays.empty() ? nullptr : displays.front();
}
std::vector<std::shared_ptr<Display>> DisplayManager::Reconcile(
const std::vector<NativeDisplayInfo>& natives,
std::vector<std::shared_ptr<Display>>* added,
std::vector<std::shared_ptr<Display>>* removed) {
std::vector<std::shared_ptr<Display>> current;
current.reserve(natives.size());
std::unordered_map<std::string, std::shared_ptr<Display>> next;
next.reserve(natives.size());
for (const auto& native : natives) {
auto it = displays_.find(native.key);
std::shared_ptr<Display> display;
if (it != displays_.end()) {
display = it->second;
} else {
display = std::make_shared<Display>(native.native);
if (added) {
added->push_back(display);
}
}
next.emplace(native.key, display);
current.push_back(std::move(display));
}
if (removed) {
for (const auto& entry : displays_) {
if (next.find(entry.first) == next.end()) {
removed->push_back(entry.second);
}
}
}
displays_ = std::move(next);
return current;
}
void DisplayManager::HandleDisplaysChanged() {
std::vector<std::shared_ptr<Display>> added;
std::vector<std::shared_ptr<Display>> removed;
Reconcile(EnumerateNativeDisplays(), &added, &removed);
for (const auto& display : added) {
Emit<DisplayAddedEvent>(display);
}
for (const auto& display : removed) {
Emit<DisplayRemovedEvent>(display);
}
}
} // namespace nativeapi