-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathLayout.cpp
More file actions
54 lines (47 loc) · 1.72 KB
/
Copy pathLayout.cpp
File metadata and controls
54 lines (47 loc) · 1.72 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
#include "Layout.h"
#include "Abyss/Common/JSON.h"
#include "Abyss/Singletons.h"
#include <absl/container/flat_hash_map.h>
#include <absl/strings/str_cat.h>
namespace OD2::Layouts {
namespace {
void mergeFromParent(nlohmann::json &object, nlohmann::json &parent) {
if (parent.contains("fields")) {
nlohmann::json fields = std::move(parent["fields"]);
if (object.contains("fields")) {
fields.merge_patch(object["fields"]);
}
object["fields"] = std::move(fields);
}
if (object.contains("children") && parent.contains("children")) {
absl::flat_hash_map<std::string, nlohmann::json *> brothers;
for (auto &c : parent["children"]) {
if (c.contains("name")) {
brothers[c["name"].get<std::string_view>()] = &c;
}
}
for (auto &c : object["children"]) {
if (c.contains("name")) {
auto it = brothers.find(c["name"].get<std::string_view>());
if (it != brothers.end()) {
nlohmann::json &brother = *it->second;
mergeFromParent(c, brother);
}
}
}
}
}
nlohmann::json readMergedLayout(std::string_view name) {
nlohmann::json me = Abyss::Common::parseJson(Abyss::Singletons::getFileProvider().loadString(absl::StrCat("/data/global/ui/layouts/", name)));
if (me.contains("basedOn")) {
nlohmann::json parent = readMergedLayout(me["basedOn"].get<std::string_view>());
mergeFromParent(me, parent);
}
return me;
}
} // namespace
Layout::Layout(std::string_view name, const Profile &profile) {
_data = readMergedLayout(name);
profile.resolveReferences(_data);
}
} // namespace OD2::Layouts