|
| 1 | +#include "PseudoClasses.hpp" |
| 2 | + |
| 3 | +namespace margelo::nitro::cssnitro { |
| 4 | + |
| 5 | + // Initialize the static map |
| 6 | + std::unordered_map<std::string, PseudoClassState> PseudoClasses::states; |
| 7 | + |
| 8 | + bool PseudoClasses::get(const std::string &key, PseudoClassType type, |
| 9 | + reactnativecss::Effect::GetProxy &get) { |
| 10 | + // Find or create the state for this key |
| 11 | + auto stateIt = states.find(key); |
| 12 | + if (stateIt == states.end()) { |
| 13 | + // Key doesn't exist, create it |
| 14 | + PseudoClassState newState; |
| 15 | + states[key] = newState; |
| 16 | + stateIt = states.find(key); |
| 17 | + } |
| 18 | + |
| 19 | + auto &state = stateIt->second; |
| 20 | + |
| 21 | + // Get the appropriate observable based on type |
| 22 | + std::optional<std::shared_ptr<reactnativecss::Observable<bool>>> *observablePtr = nullptr; |
| 23 | + |
| 24 | + // Select the correct observable based on the PseudoClassType |
| 25 | + switch (type) { |
| 26 | + case PseudoClassType::ACTIVE: |
| 27 | + observablePtr = &state.active; |
| 28 | + break; |
| 29 | + case PseudoClassType::HOVER: |
| 30 | + observablePtr = &state.hover; |
| 31 | + break; |
| 32 | + case PseudoClassType::FOCUS: |
| 33 | + observablePtr = &state.focus; |
| 34 | + break; |
| 35 | + } |
| 36 | + |
| 37 | + // Safety check - should never happen but guards against warnings |
| 38 | + if (observablePtr == nullptr) { |
| 39 | + return false; |
| 40 | + } |
| 41 | + |
| 42 | + // If the observable doesn't exist, create it with default value of false |
| 43 | + if (!observablePtr->has_value()) { |
| 44 | + *observablePtr = reactnativecss::Observable<bool>::create(false); |
| 45 | + } |
| 46 | + |
| 47 | + // Subscribe to the observable and return its value |
| 48 | + return get(*observablePtr->value()); |
| 49 | + } |
| 50 | + |
| 51 | + void PseudoClasses::set(const std::string &key, PseudoClassType type, bool value) { |
| 52 | + // Find or create the state for this key |
| 53 | + auto stateIt = states.find(key); |
| 54 | + if (stateIt == states.end()) { |
| 55 | + // Key doesn't exist, create it |
| 56 | + PseudoClassState newState; |
| 57 | + states[key] = newState; |
| 58 | + stateIt = states.find(key); |
| 59 | + } |
| 60 | + |
| 61 | + auto &state = stateIt->second; |
| 62 | + |
| 63 | + // Get the appropriate observable based on type |
| 64 | + std::optional<std::shared_ptr<reactnativecss::Observable<bool>>> *observablePtr = nullptr; |
| 65 | + |
| 66 | + switch (type) { |
| 67 | + case PseudoClassType::ACTIVE: |
| 68 | + observablePtr = &state.active; |
| 69 | + break; |
| 70 | + case PseudoClassType::HOVER: |
| 71 | + observablePtr = &state.hover; |
| 72 | + break; |
| 73 | + case PseudoClassType::FOCUS: |
| 74 | + observablePtr = &state.focus; |
| 75 | + break; |
| 76 | + } |
| 77 | + |
| 78 | + // Safety check - should never happen but guards against warnings |
| 79 | + if (observablePtr == nullptr) { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + // If the observable doesn't exist, create it |
| 84 | + if (!observablePtr->has_value()) { |
| 85 | + *observablePtr = reactnativecss::Observable<bool>::create(value); |
| 86 | + } else { |
| 87 | + // Update the existing observable |
| 88 | + observablePtr->value()->set(value); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + void PseudoClasses::remove(const std::string &key) { |
| 93 | + // Remove the key from the map |
| 94 | + states.erase(key); |
| 95 | + } |
| 96 | + |
| 97 | +} // namespace margelo::nitro::cssnitro |
0 commit comments