|
| 1 | +/// |
| 2 | +/// ContainerContext.cpp |
| 3 | +/// Container context management for layout tracking |
| 4 | +/// |
| 5 | + |
| 6 | +#include "ContainerContext.hpp" |
| 7 | + |
| 8 | +namespace margelo::nitro::cssnitro { |
| 9 | + |
| 10 | + // Static member initialization |
| 11 | + std::unordered_map<std::string, LayoutBounds> ContainerContext::_layoutMap; |
| 12 | + std::unordered_map<std::string, ScopeHierarchy> ContainerContext::_scopeMap; |
| 13 | + |
| 14 | + std::optional<std::string> ContainerContext::findInScope(const std::string &containerScope, |
| 15 | + const std::optional<std::string> &name) { |
| 16 | + // If no name provided, return the container scope directly |
| 17 | + if (!name.has_value()) { |
| 18 | + return containerScope; |
| 19 | + } |
| 20 | + |
| 21 | + // Check if the scope exists |
| 22 | + auto scopeIt = _scopeMap.find(containerScope); |
| 23 | + if (scopeIt == _scopeMap.end()) { |
| 24 | + return std::nullopt; |
| 25 | + } |
| 26 | + |
| 27 | + const ScopeHierarchy &hierarchy = scopeIt->second; |
| 28 | + |
| 29 | + // Check if name exists in current scope |
| 30 | + if (hierarchy.names.find(name.value()) != hierarchy.names.end()) { |
| 31 | + return containerScope; |
| 32 | + } |
| 33 | + |
| 34 | + // If not found and parent is not "root", check parent scope |
| 35 | + if (!hierarchy.parent.empty() && hierarchy.parent != "root") { |
| 36 | + return findInScope(hierarchy.parent, name); |
| 37 | + } |
| 38 | + |
| 39 | + // Not found and we've reached root |
| 40 | + return std::nullopt; |
| 41 | + } |
| 42 | + |
| 43 | + void ContainerContext::setScope(const std::string &containerScope, |
| 44 | + const std::string &parent, |
| 45 | + const std::unordered_set<std::string> &names) { |
| 46 | + _scopeMap[containerScope] = ScopeHierarchy(parent, names); |
| 47 | + } |
| 48 | + |
| 49 | + std::optional<double> ContainerContext::getX(const std::string &containerScope, |
| 50 | + const std::optional<std::string> &name, |
| 51 | + reactnativecss::Effect::GetProxy &get) { |
| 52 | + auto foundKey = findInScope(containerScope, name); |
| 53 | + if (!foundKey.has_value()) { |
| 54 | + return std::nullopt; |
| 55 | + } |
| 56 | + |
| 57 | + // Get layout bounds |
| 58 | + auto it = _layoutMap.find(foundKey.value()); |
| 59 | + if (it == _layoutMap.end()) { |
| 60 | + return std::nullopt; |
| 61 | + } |
| 62 | + |
| 63 | + // Track the observable through the proxy |
| 64 | + return get(*it->second.x); |
| 65 | + } |
| 66 | + |
| 67 | + std::optional<double> ContainerContext::getY(const std::string &containerScope, |
| 68 | + const std::optional<std::string> &name, |
| 69 | + reactnativecss::Effect::GetProxy &get) { |
| 70 | + auto foundKey = findInScope(containerScope, name); |
| 71 | + if (!foundKey.has_value()) { |
| 72 | + return std::nullopt; |
| 73 | + } |
| 74 | + |
| 75 | + auto it = _layoutMap.find(foundKey.value()); |
| 76 | + if (it == _layoutMap.end()) { |
| 77 | + return std::nullopt; |
| 78 | + } |
| 79 | + |
| 80 | + return get(*it->second.y); |
| 81 | + } |
| 82 | + |
| 83 | + std::optional<double> ContainerContext::getWidth(const std::string &containerScope, |
| 84 | + const std::optional<std::string> &name, |
| 85 | + reactnativecss::Effect::GetProxy &get) { |
| 86 | + auto foundKey = findInScope(containerScope, name); |
| 87 | + if (!foundKey.has_value()) { |
| 88 | + return std::nullopt; |
| 89 | + } |
| 90 | + |
| 91 | + auto it = _layoutMap.find(foundKey.value()); |
| 92 | + if (it == _layoutMap.end()) { |
| 93 | + return std::nullopt; |
| 94 | + } |
| 95 | + |
| 96 | + return get(*it->second.width); |
| 97 | + } |
| 98 | + |
| 99 | + std::optional<double> ContainerContext::getHeight(const std::string &containerScope, |
| 100 | + const std::optional<std::string> &name, |
| 101 | + reactnativecss::Effect::GetProxy &get) { |
| 102 | + auto foundKey = findInScope(containerScope, name); |
| 103 | + if (!foundKey.has_value()) { |
| 104 | + return std::nullopt; |
| 105 | + } |
| 106 | + |
| 107 | + auto it = _layoutMap.find(foundKey.value()); |
| 108 | + if (it == _layoutMap.end()) { |
| 109 | + return std::nullopt; |
| 110 | + } |
| 111 | + |
| 112 | + return get(*it->second.height); |
| 113 | + } |
| 114 | + |
| 115 | + void ContainerContext::setLayout(const std::string &key, |
| 116 | + double x, double y, |
| 117 | + double width, double height) { |
| 118 | + LayoutBounds &bounds = _layoutMap[key]; |
| 119 | + bounds.x->set(x); |
| 120 | + bounds.y->set(y); |
| 121 | + bounds.width->set(width); |
| 122 | + bounds.height->set(height); |
| 123 | + } |
| 124 | + |
| 125 | +} // namespace margelo::nitro::cssnitro |
0 commit comments