| 1 | /* |
| 2 | * Copyright (C) 2016 The Qt Company Ltd. |
| 3 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 4 | * |
| 5 | * SPDX-License-Identifier: MIT |
| 6 | */ |
| 7 | |
| 8 | #pragma once |
| 9 | |
| 10 | #include <functional> |
| 11 | #include <vector> |
| 12 | #include <array> |
| 13 | #include <yoga/YGEnums.h> |
| 14 | #include <stdint.h> |
| 15 | |
| 16 | QT_YOGA_NAMESPACE_BEGIN |
| 17 | |
| 18 | struct YGConfig; |
| 19 | struct YGNode; |
| 20 | namespace facebook { |
| 21 | namespace yoga { |
| 22 | |
| 23 | enum struct LayoutType : int { |
| 24 | kLayout = 0, |
| 25 | kMeasure = 1, |
| 26 | kCachedLayout = 2, |
| 27 | kCachedMeasure = 3 |
| 28 | }; |
| 29 | |
| 30 | enum struct LayoutPassReason : int { |
| 31 | kInitial = 0, |
| 32 | kAbsLayout = 1, |
| 33 | kStretch = 2, |
| 34 | kMultilineStretch = 3, |
| 35 | kFlexLayout = 4, |
| 36 | kMeasureChild = 5, |
| 37 | kAbsMeasureChild = 6, |
| 38 | kFlexMeasure = 7, |
| 39 | COUNT |
| 40 | }; |
| 41 | |
| 42 | struct LayoutData { |
| 43 | int layouts; |
| 44 | int measures; |
| 45 | int maxMeasureCache; |
| 46 | int cachedLayouts; |
| 47 | int cachedMeasures; |
| 48 | int measureCallbacks; |
| 49 | std::array<int, static_cast<uint8_t>(LayoutPassReason::COUNT)> |
| 50 | measureCallbackReasonsCount; |
| 51 | }; |
| 52 | |
| 53 | const char* LayoutPassReasonToString(const LayoutPassReason value); |
| 54 | |
| 55 | struct YOGA_EXPORT Event { |
| 56 | enum Type { |
| 57 | NodeAllocation, |
| 58 | NodeDeallocation, |
| 59 | NodeLayout, |
| 60 | LayoutPassStart, |
| 61 | LayoutPassEnd, |
| 62 | MeasureCallbackStart, |
| 63 | MeasureCallbackEnd, |
| 64 | NodeBaselineStart, |
| 65 | NodeBaselineEnd, |
| 66 | }; |
| 67 | class Data; |
| 68 | using Subscriber = void(const YGNode&, Type, Data); |
| 69 | using Subscribers = std::vector<std::function<Subscriber>>; |
| 70 | |
| 71 | template <Type E> |
| 72 | struct TypedData {}; |
| 73 | |
| 74 | class Data { |
| 75 | const void* data_; |
| 76 | |
| 77 | public: |
| 78 | template <Type E> |
| 79 | Data(const TypedData<E>& data) : data_{&data} {} |
| 80 | |
| 81 | template <Type E> |
| 82 | const TypedData<E>& get() const { |
| 83 | return *static_cast<const TypedData<E>*>(data_); |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | static void reset(); |
| 88 | |
| 89 | static void subscribe(std::function<Subscriber>&& subscriber); |
| 90 | |
| 91 | template <Type E> |
| 92 | static void publish(const YGNode& node, const TypedData<E>& eventData = {}) { |
| 93 | publish(node, E, Data{eventData}); |
| 94 | } |
| 95 | |
| 96 | template <Type E> |
| 97 | static void publish(const YGNode* node, const TypedData<E>& eventData = {}) { |
| 98 | publish<E>(*node, eventData); |
| 99 | } |
| 100 | |
| 101 | private: |
| 102 | static void publish(const YGNode&, Type, const Data&); |
| 103 | }; |
| 104 | |
| 105 | template <> |
| 106 | struct Event::TypedData<Event::NodeAllocation> { |
| 107 | YGConfig* config; |
| 108 | }; |
| 109 | |
| 110 | template <> |
| 111 | struct Event::TypedData<Event::NodeDeallocation> { |
| 112 | YGConfig* config; |
| 113 | }; |
| 114 | |
| 115 | template <> |
| 116 | struct Event::TypedData<Event::LayoutPassStart> { |
| 117 | void* layoutContext; |
| 118 | }; |
| 119 | |
| 120 | template <> |
| 121 | struct Event::TypedData<Event::LayoutPassEnd> { |
| 122 | void* layoutContext; |
| 123 | LayoutData* layoutData; |
| 124 | }; |
| 125 | |
| 126 | template <> |
| 127 | struct Event::TypedData<Event::MeasureCallbackEnd> { |
| 128 | void* layoutContext; |
| 129 | float width; |
| 130 | YGMeasureMode widthMeasureMode; |
| 131 | float height; |
| 132 | YGMeasureMode heightMeasureMode; |
| 133 | float measuredWidth; |
| 134 | float measuredHeight; |
| 135 | const LayoutPassReason reason; |
| 136 | }; |
| 137 | |
| 138 | template <> |
| 139 | struct Event::TypedData<Event::NodeLayout> { |
| 140 | LayoutType layoutType; |
| 141 | void* layoutContext; |
| 142 | }; |
| 143 | |
| 144 | } // namespace yoga |
| 145 | } // namespace facebook |
| 146 | |
| 147 | QT_YOGA_NAMESPACE_END |
| 148 | |